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
This commit is contained in:
kalykun 2022-08-04 11:16:24 +08:00 committed by GitHub
parent 772c251ec5
commit 9e7e32b60c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 11 deletions

View File

@ -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>[];
@ -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>;
@ -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(

View File

@ -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', () => {

View File

@ -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 ===========
@ -259,11 +259,7 @@ function generateSorterInfo<RecordType>(
};
}
if (list.length <= 1) {
return list[0] || {};
}
return list;
return list[0] || {};
}
export function getSortData<RecordType>(
@ -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[];
@ -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),