Skip to content

Commit

Permalink
fix(Table): onChange sorter param types (ant-design#36710)
Browse files Browse the repository at this point in the history
* fix(types): fixes `sorter` param types

* chore: add test case

* Update type.test.tsx

* Update type.test.tsx
  • Loading branch information
kungege committed Aug 4, 2022
1 parent 772c251 commit 9e7e32b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
6 changes: 3 additions & 3 deletions components/table/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ interface ChangeEventInfo<RecordType> {
total?: number;
};
filters: Record<string, FilterValue | null>;
sorter: SorterResult<RecordType> | SorterResult<RecordType>[];
sorter: SorterResult<RecordType> | SorterResult<RecordType[]>;

filterStates: FilterState<RecordType>[];
sorterStates: SortState<RecordType>[];
Expand Down Expand Up @@ -92,7 +92,7 @@ export interface TableProps<RecordType>
onChange?: (
pagination: TablePaginationConfig,
filters: Record<string, FilterValue | null>,
sorter: SorterResult<RecordType> | SorterResult<RecordType>[],
sorter: SorterResult<RecordType> | SorterResult<RecordType[]>,
extra: TableCurrentDataSource<RecordType>,
) => void;
rowSelection?: TableRowSelection<RecordType>;
Expand Down Expand Up @@ -263,7 +263,7 @@ function InternalTable<RecordType extends object = any>(

// ============================ Sorter =============================
const onSorterChange = (
sorter: SorterResult<RecordType> | SorterResult<RecordType>[],
sorter: SorterResult<RecordType> | SorterResult<RecordType[]>,
sorterStates: SortState<RecordType>[],
) => {
triggerOnChange(
Expand Down
6 changes: 6 additions & 0 deletions components/table/__tests__/type.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ describe('Table.typescript', () => {
const table = <Table<RecordType> dataSource={[{ key: 'Bamboo' }]} />;
expect(table).toBeTruthy();
});

it('Sorter types', () => {
const table = <Table onChange={(_pagination, _filters, sorter) => sorter.field} />;

expect(table).toBeTruthy();
});
});

describe('Table.typescript types', () => {
Expand Down
12 changes: 4 additions & 8 deletions components/table/hooks/useSorter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ function stateToInfo<RecordType>(sorterStates: SortState<RecordType>) {

function generateSorterInfo<RecordType>(
sorterStates: SortState<RecordType>[],
): SorterResult<RecordType> | SorterResult<RecordType>[] {
): SorterResult<RecordType> | SorterResult<RecordType[]> {
const list = sorterStates.filter(({ sortOrder }) => sortOrder).map(stateToInfo);

// =========== Legacy compatible support ===========
Expand All @@ -259,11 +259,7 @@ function generateSorterInfo<RecordType>(
};
}

if (list.length <= 1) {
return list[0] || {};
}

return list;
return list[0] || {};
}

export function getSortData<RecordType>(
Expand Down Expand Up @@ -324,7 +320,7 @@ interface SorterConfig<RecordType> {
prefixCls: string;
mergedColumns: ColumnsType<RecordType>;
onSorterChange: (
sorterResult: SorterResult<RecordType> | SorterResult<RecordType>[],
sorterResult: SorterResult<RecordType> | SorterResult<RecordType[]>,
sortStates: SortState<RecordType>[],
) => void;
sortDirections: SortOrder[];
Expand All @@ -343,7 +339,7 @@ export default function useFilterSorter<RecordType>({
TransformColumns<RecordType>,
SortState<RecordType>[],
ColumnTitleProps<RecordType>,
() => SorterResult<RecordType> | SorterResult<RecordType>[],
() => SorterResult<RecordType> | SorterResult<RecordType[]>,
] {
const [sortStates, setSortStates] = React.useState<SortState<RecordType>[]>(
collectSortStates(mergedColumns, true),
Expand Down

0 comments on commit 9e7e32b

Please sign in to comment.