fix: Table with sort should reset to first page (#17020)

* sort reset pagination

* add test case
This commit is contained in:
zombieJ 2019-06-10 11:59:46 +08:00 committed by GitHub
parent 8c417e6cbf
commit e67270cd59
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 0 deletions

View File

@ -414,6 +414,9 @@ export default class Table<T> extends React.Component<TableProps<T>, TableState<
if (!column.sorter) {
return;
}
const pagination = { ...this.state.pagination };
const sortDirections = column.sortDirections || (this.props.sortDirections as SortOrder[]);
const { sortOrder, sortColumn } = this.state;
// 只同时允许一列进行排序,否则会导致排序顺序的逻辑问题
@ -428,7 +431,14 @@ export default class Table<T> extends React.Component<TableProps<T>, TableState<
newSortOrder = sortDirections[0];
}
if (this.props.pagination) {
// Reset current prop
pagination.current = 1;
pagination.onChange!(pagination.current);
}
const newState = {
pagination,
sortOrder: newSortOrder,
sortColumn: newSortOrder ? column : null,
};

View File

@ -587,4 +587,23 @@ describe('Table.sorter', () => {
wrapper.find('.ant-table-column-sorters').simulate('click');
expect(renderedNames(wrapper)).toEqual(['Jack', 'Lucy', 'Tom', 'Jerry']);
});
it('pagination back', () => {
const onPageChange = jest.fn();
const onChange = jest.fn();
const wrapper = mount(
createTable({
pagination: {
pageSize: 2,
onChange: onPageChange,
},
onChange,
}),
);
wrapper.find('.ant-table-column-sorters').simulate('click');
expect(onChange.mock.calls[0][0].current).toBe(1);
expect(onPageChange.mock.calls[0][0]).toBe(1);
});
});