From 53a76f0bf728c5e88708a31e602159b584225d6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=8C=E8=B4=A7=E6=9C=BA=E5=99=A8=E4=BA=BA?= Date: Tue, 28 Apr 2020 20:29:34 +0800 Subject: [PATCH] fix: Table should not crash pageSize is undefined (#23724) * fix: Table crash when pageSize is undefined * test case * fix test case --- .../table/__tests__/Table.pagination.test.js | 6 ++++ components/table/hooks/usePagination.ts | 29 +++++++++++++++---- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/components/table/__tests__/Table.pagination.test.js b/components/table/__tests__/Table.pagination.test.js index b1b936cc6f83..eeacb96a6757 100644 --- a/components/table/__tests__/Table.pagination.test.js +++ b/components/table/__tests__/Table.pagination.test.js @@ -37,6 +37,12 @@ describe('Table.pagination', () => { expect(wrapper.render()).toMatchSnapshot(); }); + it('not crash when pageSize is undefined', () => { + expect(() => { + mount(createTable({ pagination: { pageSIze: undefined } })); + }).not.toThrow(); + }); + it('should not show pager if pagination.hideOnSinglePage is true and only 1 page', () => { const wrapper = mount(createTable({ pagination: { pageSize: 3, hideOnSinglePage: true } })); expect(wrapper.find('.ant-pagination')).toHaveLength(1); diff --git a/components/table/hooks/usePagination.ts b/components/table/hooks/usePagination.ts index 079ed10a111d..b0b7170f8428 100644 --- a/components/table/hooks/usePagination.ts +++ b/components/table/hooks/usePagination.ts @@ -25,6 +25,23 @@ export function getPaginationParam( return param; } +function extendsObject(...list: T[]) { + const result: T = {} as T; + + list.forEach(obj => { + if (obj) { + Object.keys(obj).forEach(key => { + const val = (obj as any)[key]; + if (val !== undefined) { + (result as any)[key] = val; + } + }); + } + }); + + return result; +} + export default function usePagination( total: number, pagination: TablePaginationConfig | false | undefined, @@ -42,11 +59,13 @@ export default function usePagination( }); // ============ Basic Pagination Config ============ - const mergedPagination = { - ...innerPagination, - ...paginationObj, - total: paginationTotal > 0 ? paginationTotal : total, - }; + const mergedPagination = extendsObject>( + innerPagination, + paginationObj, + { + total: paginationTotal > 0 ? paginationTotal : total, + }, + ); if (!paginationTotal) { // Reset `current` if data length changed. Only reset when paginationObj do not have total