fix: table multiple sorter (#24852)

* fix sort callback

* add test case
This commit is contained in:
二货机器人 2020-06-08 22:45:53 +08:00 committed by GitHub
parent af5ff52a27
commit d6d3de8a1b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 2 deletions

View File

@ -272,7 +272,7 @@ describe('Table.sorter', () => {
});
// https://github.com/ant-design/ant-design/pull/12264#discussion_r218053034
it('should sort from begining state when toggle from different columns', () => {
it('should sort from beginning state when toggle from different columns', () => {
const columns = [
{
title: 'name',
@ -790,4 +790,55 @@ describe('Table.sorter', () => {
.hasClass('active'),
).toBeTruthy();
});
it('onChange with correct sorter for multiple', () => {
const groupColumns = [
{
title: 'Math Score',
dataIndex: 'math',
sorter: { multiple: 1 },
},
{
title: 'English Score',
dataIndex: 'english',
sorter: { multiple: 2 },
},
];
const groupData = [
{
key: '1',
name: 'John Brown',
chinese: 98,
math: 60,
english: 70,
},
];
const onChange = jest.fn();
const wrapper = mount(<Table columns={groupColumns} data={groupData} onChange={onChange} />);
function clickToMatchExpect(index, sorter) {
wrapper.find('.ant-table-column-sorters').at(index).simulate('click');
expect(onChange).toHaveBeenCalledWith(
expect.anything(),
expect.anything(),
expect.objectContaining(sorter),
expect.anything(),
);
onChange.mockReset();
}
// First
clickToMatchExpect(0, { field: 'math', order: 'ascend' });
clickToMatchExpect(0, { field: 'math', order: 'descend' });
clickToMatchExpect(0, { field: 'math', order: undefined });
// Last
clickToMatchExpect(1, { field: 'english', order: 'ascend' });
clickToMatchExpect(1, { field: 'english', order: 'descend' });
clickToMatchExpect(1, { field: 'english', order: undefined });
});
});

View File

@ -231,7 +231,7 @@ function generateSorterInfo<RecordType>(
// https://github.com/ant-design/ant-design/pull/19226
if (list.length === 0 && sorterStates.length) {
return {
...stateToInfo(sorterStates[0]),
...stateToInfo(sorterStates[sorterStates.length - 1]),
column: undefined,
};
}