From 1c1a4a773d3fabdfee3dd5090dfe247141618e0c 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: Mon, 6 Jan 2020 20:28:47 +0800 Subject: [PATCH] fix: Table should not crash with empty column children (#20703) --- components/table/__tests__/Table.test.js | 14 ++++++++++++++ components/table/hooks/useFilter/index.tsx | 2 +- components/table/hooks/useSorter.tsx | 4 ++-- components/table/interface.tsx | 1 - 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/components/table/__tests__/Table.test.js b/components/table/__tests__/Table.test.js index fe98a2045e..7a8c08a3d9 100644 --- a/components/table/__tests__/Table.test.js +++ b/components/table/__tests__/Table.test.js @@ -142,4 +142,18 @@ describe('Table', () => { wrapper.find('th').simulate('click'); expect(onClick).toHaveBeenCalled(); }); + + it('should not crash when column children is empty', () => { + mount( + , + ); + }); }); diff --git a/components/table/hooks/useFilter/index.tsx b/components/table/hooks/useFilter/index.tsx index f20341d86f..874b8fac8b 100644 --- a/components/table/hooks/useFilter/index.tsx +++ b/components/table/hooks/useFilter/index.tsx @@ -27,7 +27,7 @@ function collectFilterStates( ): FilterState[] { let filterStates: FilterState[] = []; - columns.forEach((column, index) => { + (columns || []).forEach((column, index) => { const columnPos = getColumnPos(index, pos); if ('children' in column) { diff --git a/components/table/hooks/useSorter.tsx b/components/table/hooks/useSorter.tsx index 4db160c331..689e96a173 100644 --- a/components/table/hooks/useSorter.tsx +++ b/components/table/hooks/useSorter.tsx @@ -54,7 +54,7 @@ function collectSortStates( ): SortState[] { let sortStates: SortState[] = []; - columns.forEach((column, index) => { + (columns || []).forEach((column, index) => { const columnPos = getColumnPos(index, pos); if ('children' in column) { @@ -91,7 +91,7 @@ function injectSorter( defaultSortDirections: SortOrder[], pos?: string, ): ColumnsType { - return columns.map((column, index) => { + return (columns || []).map((column, index) => { const columnPos = getColumnPos(index, pos); let newColumn: ColumnsType[number] = column; diff --git a/components/table/interface.tsx b/components/table/interface.tsx index 9206fa5c39..c2bb7bdfb5 100644 --- a/components/table/interface.tsx +++ b/components/table/interface.tsx @@ -63,7 +63,6 @@ export interface ColumnType extends RcColumnType { title?: ColumnTitle; // Sorter - // TODO: Doc this update sorter?: | boolean | CompareFn