From 112c2efc96eda6c5ad1161070115ac4fd6b87d5c Mon Sep 17 00:00:00 2001 From: huangcheng Date: Sun, 30 Apr 2023 11:20:49 +0800 Subject: [PATCH] fix(useAntdTable): reset pageSize (#2178) * Revert "fix(useAntdTable): reset pageSize (#2090)" This reverts commit 2d2cd69a9aa257b7e5811ec2a2241e520dab349d. * fix: use defaultParams pageSize when defaultPageSize is not passed * fix: does not affect submit --- .../src/useAntdTable/__tests__/index.test.ts | 46 ++++++++++++++++++- packages/hooks/src/useAntdTable/index.tsx | 11 ++--- 2 files changed, 50 insertions(+), 7 deletions(-) diff --git a/packages/hooks/src/useAntdTable/__tests__/index.test.ts b/packages/hooks/src/useAntdTable/__tests__/index.test.ts index 93f19916ec..e2ba06887e 100644 --- a/packages/hooks/src/useAntdTable/__tests__/index.test.ts +++ b/packages/hooks/src/useAntdTable/__tests__/index.test.ts @@ -5,6 +5,7 @@ import useAntdTable from '../index'; interface Query { current: number; pageSize: number; + [key: string]: any; } @@ -233,7 +234,7 @@ describe('useAntdTable', () => { expect(queryArgs.name).toBe('change name'); }); - it('should reset params work', async () => { + it('should reset pageSize in defaultParams', async () => { queryArgs = undefined; form.resetFields(); act(() => { @@ -276,4 +277,47 @@ describe('useAntdTable', () => { expect(queryArgs.pageSize).toBe(10); }); }); + + it('should reset pageSize in defaultPageSize', async () => { + queryArgs = undefined; + form.resetFields(); + act(() => { + hook = setUp(asyncFn, { + form, + defaultParams: { + current: 1, + pageSize: 10, + }, + defaultPageSize: 20, + }); + }); + + const { search, tableProps } = hook.result.current; + expect(tableProps.loading).toBe(false); + await waitFor(() => expect(queryArgs.current).toBe(1)); + expect(queryArgs.pageSize).toBe(20); + + // change params + act(() => { + tableProps.onChange({ + current: 2, + pageSize: 5, + }); + }); + + await waitFor(() => { + expect(queryArgs.current).toBe(2); + expect(queryArgs.pageSize).toBe(5); + }); + + // reset params + act(() => { + search.reset(); + }); + + await waitFor(() => { + expect(queryArgs.current).toBe(1); + expect(queryArgs.pageSize).toBe(20); + }); + }); }); diff --git a/packages/hooks/src/useAntdTable/index.tsx b/packages/hooks/src/useAntdTable/index.tsx index 01b27f4824..241e422a3f 100644 --- a/packages/hooks/src/useAntdTable/index.tsx +++ b/packages/hooks/src/useAntdTable/index.tsx @@ -155,12 +155,11 @@ const useAntdTable = ( if (form) { form.resetFields(); } - _submit( - defaultParams?.[0] || { - pageSize: options.defaultPageSize || 10, - current: 1, - }, - ); + _submit({ + ...(defaultParams?.[0] || {}), + pageSize: options.defaultPageSize || options.defaultParams?.[0]?.pageSize || 10, + current: 1, + }); }; const submit = (e?: any) => {