From 9e7e32b60ce80363c5865eb10bdf44d2938b3721 Mon Sep 17 00:00:00 2001 From: kalykun <984757534@qq.com> Date: Thu, 4 Aug 2022 11:16:24 +0800 Subject: [PATCH] fix(Table): onChange `sorter` param types (#36710) * fix(types): fixes `sorter` param types * chore: add test case * Update type.test.tsx * Update type.test.tsx --- components/table/Table.tsx | 6 +++--- components/table/__tests__/type.test.tsx | 6 ++++++ components/table/hooks/useSorter.tsx | 12 ++++-------- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/components/table/Table.tsx b/components/table/Table.tsx index 212536d5b28e..a8c524f22e34 100644 --- a/components/table/Table.tsx +++ b/components/table/Table.tsx @@ -61,7 +61,7 @@ interface ChangeEventInfo { total?: number; }; filters: Record; - sorter: SorterResult | SorterResult[]; + sorter: SorterResult | SorterResult; filterStates: FilterState[]; sorterStates: SortState[]; @@ -92,7 +92,7 @@ export interface TableProps onChange?: ( pagination: TablePaginationConfig, filters: Record, - sorter: SorterResult | SorterResult[], + sorter: SorterResult | SorterResult, extra: TableCurrentDataSource, ) => void; rowSelection?: TableRowSelection; @@ -263,7 +263,7 @@ function InternalTable( // ============================ Sorter ============================= const onSorterChange = ( - sorter: SorterResult | SorterResult[], + sorter: SorterResult | SorterResult, sorterStates: SortState[], ) => { triggerOnChange( diff --git a/components/table/__tests__/type.test.tsx b/components/table/__tests__/type.test.tsx index a064b0f69fe1..e33dad2f8f66 100644 --- a/components/table/__tests__/type.test.tsx +++ b/components/table/__tests__/type.test.tsx @@ -36,6 +36,12 @@ describe('Table.typescript', () => { const table = dataSource={[{ key: 'Bamboo' }]} />; expect(table).toBeTruthy(); }); + + it('Sorter types', () => { + const table = sorter.field} />; + + expect(table).toBeTruthy(); + }); }); describe('Table.typescript types', () => { diff --git a/components/table/hooks/useSorter.tsx b/components/table/hooks/useSorter.tsx index 2c08a33ab642..9784f92ded0c 100644 --- a/components/table/hooks/useSorter.tsx +++ b/components/table/hooks/useSorter.tsx @@ -247,7 +247,7 @@ function stateToInfo(sorterStates: SortState) { function generateSorterInfo( sorterStates: SortState[], -): SorterResult | SorterResult[] { +): SorterResult | SorterResult { const list = sorterStates.filter(({ sortOrder }) => sortOrder).map(stateToInfo); // =========== Legacy compatible support =========== @@ -259,11 +259,7 @@ function generateSorterInfo( }; } - if (list.length <= 1) { - return list[0] || {}; - } - - return list; + return list[0] || {}; } export function getSortData( @@ -324,7 +320,7 @@ interface SorterConfig { prefixCls: string; mergedColumns: ColumnsType; onSorterChange: ( - sorterResult: SorterResult | SorterResult[], + sorterResult: SorterResult | SorterResult, sortStates: SortState[], ) => void; sortDirections: SortOrder[]; @@ -343,7 +339,7 @@ export default function useFilterSorter({ TransformColumns, SortState[], ColumnTitleProps, - () => SorterResult | SorterResult[], + () => SorterResult | SorterResult, ] { const [sortStates, setSortStates] = React.useState[]>( collectSortStates(mergedColumns, true),