Skip to content

Commit 758d56f

Browse files
authored
feat(useList): support clearData (#534)
* fix(useList): support clearData * test(useList): add clearData test
1 parent 3ba39f5 commit 758d56f

File tree

4 files changed

+175
-7
lines changed

4 files changed

+175
-7
lines changed

src/useList/__tests__/useList.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,42 @@ describe('Test useList hook', () => {
154154
expect(result.current.params.total).toBe(0);
155155
expect(result.current.error).toBe(undefined);
156156
});
157+
158+
it('Should ONLY clear data while not reset loading', async () => {
159+
const fetcher = jest.fn().mockImplementation(
160+
() =>
161+
new Promise((resolve) => {
162+
setTimeout(() => {
163+
resolve({
164+
total: 1,
165+
data: [{ uuid: 1 }],
166+
});
167+
}, 1000);
168+
})
169+
);
170+
171+
const { result } = renderHook(() =>
172+
useList(fetcher, { current: 1, pageSize: 20, search: '' }, { immediate: false })
173+
);
174+
act(() => {
175+
result.current.mutate({ current: 2 });
176+
});
177+
178+
expect(result.current.loading).toBe(true);
179+
expect(result.current.params.current).toBe(2);
180+
181+
act(() => {
182+
result.current.clearData();
183+
});
184+
// clearData won't reset params and loading status
185+
expect(result.current.loading).toBe(true);
186+
expect(result.current.params.current).toBe(2);
187+
188+
act(() => {
189+
result.current.clear();
190+
});
191+
// clear method will clear all data including params and loading status
192+
expect(result.current.loading).toBe(false);
193+
expect(result.current.params.current).toBe(1);
194+
});
157195
});

src/useList/demos/query.tsx

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import React from 'react';
2+
import { Input, Result, Table } from 'antd';
3+
import { Empty, useList } from 'dt-react-component';
4+
5+
import getMockData, { type MockData } from './data';
6+
7+
export default () => {
8+
const { error, params, loading, data, mutate, clearData } = useList<
9+
MockData,
10+
{ current: number; pageSize: number; search?: string }
11+
>(
12+
(params) => {
13+
return new Promise<{
14+
data: MockData[];
15+
total: number;
16+
}>((resolve) => {
17+
setTimeout(() => {
18+
resolve(getMockData(params));
19+
}, 1000);
20+
});
21+
},
22+
() => ({
23+
current: 1,
24+
pageSize: 20,
25+
})
26+
);
27+
28+
if (error) return <Result status={500} />;
29+
30+
return (
31+
<>
32+
<Input.Search
33+
value={params.search}
34+
onChange={(e) => {
35+
mutate({ search: e.target.value }, { revalidate: false });
36+
clearData();
37+
}}
38+
onSearch={() => mutate()}
39+
style={{ marginBottom: 12 }}
40+
/>
41+
<Table
42+
columns={[
43+
{
44+
key: 'name',
45+
title: 'name',
46+
dataIndex: 'name',
47+
},
48+
{
49+
key: 'address',
50+
title: 'address',
51+
dataIndex: 'address',
52+
},
53+
{
54+
key: 'company',
55+
title: 'company',
56+
dataIndex: 'company',
57+
},
58+
{
59+
key: 'gender',
60+
title: 'gender',
61+
dataIndex: 'gender',
62+
},
63+
{
64+
key: 'weight',
65+
title: 'weight',
66+
dataIndex: 'weight',
67+
},
68+
]}
69+
onChange={(pagination) =>
70+
mutate({ current: pagination.current, pageSize: pagination.pageSize })
71+
}
72+
size="small"
73+
scroll={{ y: 200 }}
74+
dataSource={data}
75+
pagination={{
76+
current: params.current,
77+
pageSize: params.pageSize,
78+
total: params.total,
79+
}}
80+
locale={{
81+
emptyText: () =>
82+
loading ? (
83+
<Empty type="search" active description="搜索中" />
84+
) : (
85+
<Empty type="default" />
86+
),
87+
}}
88+
rowKey="uuid"
89+
bordered
90+
/>
91+
</>
92+
);
93+
};

src/useList/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ toc: content
1717
<code src="./demos/options.tsx" title="相关配置" description="设置 immediate 值防止初始化的时候进行请求"></code>
1818
<code src="./demos/mutate.tsx" title="相关配置" description="用 undefined 覆盖 prevPrams 时,需采用 functional 的写法 "></code>
1919
<code src="./demos/mutateOptions" title="mutate相关配置" description="revalidate 修改后请求数据,clearData 请求前清除数据"></code>
20+
<code src="./demos/query" title="搜索" description="支持修改后重置搜索"></code>
2021

2122
## API
2223

src/useList/index.ts

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,45 @@ export interface IUseListOptions {
2121
immediate?: boolean;
2222
}
2323

24+
/**
25+
* 返回值
26+
*/
27+
export interface UseListResponseState<
28+
T extends Record<string, any>,
29+
P extends Record<string, any>
30+
> {
31+
/**
32+
* 请求是否执行中
33+
*/
34+
loading: boolean;
35+
/**
36+
* 请求的相关参数以及结果数据的总数
37+
*/
38+
params: P & { total: number };
39+
/**
40+
* 错误信息
41+
*/
42+
error?: Error;
43+
/**
44+
* 返回的数据
45+
*/
46+
data: T[];
47+
mutate: (params?: Partial<P> | ((prev: P) => P), options?: IMutateOptions) => void;
48+
/**
49+
* 清空所有数据和状态
50+
*/
51+
clear: () => void;
52+
/**
53+
* 清空数据
54+
*/
55+
clearData: () => void;
56+
}
57+
2458
export default function useList<T extends Record<string, any>, P extends Record<string, any>>(
2559
fetcher: Fetcher<T, P>,
2660
initialParams: P | (() => P),
2761
rawOptions: IUseListOptions = { immediate: true }
28-
) {
62+
): UseListResponseState<T, P> {
2963
const [error, setError] = useState<Error | undefined>(undefined);
3064
const [data, setData] = useState<T[]>([]);
3165
const [total, setTotal] = useState(0);
@@ -62,20 +96,22 @@ export default function useList<T extends Record<string, any>, P extends Record<
6296

6397
if (nextOptions.revalidate) {
6498
if (nextOptions.clearData) {
65-
setData([]);
66-
setTotal(0);
67-
setError(undefined);
99+
clearData();
68100
}
69101
performFetch(tmp);
70102
}
71103
};
72104

73-
const clear = () => {
105+
const clearData = () => {
74106
setData([]);
75107
setTotal(0);
108+
setError(undefined);
109+
};
110+
111+
const clear = () => {
112+
clearData();
76113
setParams(initialParams);
77114
setLoading(false);
78-
setError(undefined);
79115
};
80116

81117
useEffect(() => {
@@ -84,5 +120,5 @@ export default function useList<T extends Record<string, any>, P extends Record<
84120
}
85121
}, []);
86122

87-
return { loading, params: { ...params, total }, error, data, mutate, clear };
123+
return { loading, params: { ...params, total }, error, data, mutate, clear, clearData };
88124
}

0 commit comments

Comments
 (0)