fix(table):table grouping columns does not apply sorting (#50086)

* 增加函数循环收集mergedColumns中所有key

* 新增测试用例

* ci问题调整,收集列keys函数调整
This commit is contained in:
zyf 2024-08-01 20:39:01 +08:00 committed by GitHub
parent 03c5332e47
commit c6289dd615
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 65 additions and 5 deletions

View File

@ -63,7 +63,58 @@ describe('Table.sorter', () => {
expect(renderedNames(container)).toEqual(['Tom', 'Lucy', 'Jack', 'Jerry']);
expect(container.querySelector('th')?.getAttribute('aria-sort')).toEqual('descending');
});
it('sort will work when column with children', () => {
const onChange = jest.fn();
const tableData = [
{
key: '1',
estimatedTicketsLeft: 100,
estimatedTicketsLeftPercentage: 50.25,
},
{
key: '2',
estimatedTicketsLeft: 200,
estimatedTicketsLeftPercentage: 75.5,
},
{
key: '3',
estimatedTicketsLeft: 50,
estimatedTicketsLeftPercentage: 25.75,
},
];
const columns = [
{
title: 'Tickets',
children: [
{
title: 'Amount',
dataIndex: 'estimatedTicketsLeft',
sorter: (a:any, b:any) => a.estimatedTicketsLeft - b.estimatedTicketsLeft,
sortDirections: ['descend', 'ascend'],
render: (text:any) => `${text} left`,
},
{
title: '[%]',
dataIndex: 'estimatedTicketsLeftPercentage',
sorter: (a:any, b:any) => a.estimatedTicketsLeftPercentage - b.estimatedTicketsLeftPercentage,
sortDirections: ['descend', 'ascend'],
render: (text:any) => `${text.toFixed(2)}%`,
},
],
},
];
const TableSorter: React.FC = () => (
<Table
columns={columns}
dataSource={tableData}
onChange={onChange}
showSorterTooltip={{ target: 'sorter-icon' }}
/>
);
const { container } = render(<TableSorter />);
fireEvent.click(container.querySelector('.ant-table-column-sorters')!);
expect(renderedNames(container)).toEqual(['200 left', '100 left', '50 left']);
});
it('should change aria-sort when default sort order is set to descend', () => {
const { container } = render(
createTable({ sortDirections: ['descend', 'ascend'] }, { defaultSortOrder: 'descend' }),

View File

@ -394,16 +394,25 @@ export default function useFilterSorter<RecordType>({
collectSortStates(mergedColumns, true),
);
const getColumnKeys = (columns: ColumnsType<RecordType>, pos?: string): Key[] => {
const newKeys: Key[] = [];
columns.forEach((item, index) => {
const columnPos = getColumnPos(index, pos);
newKeys.push(getColumnKey(item, columnPos));
if (Array.isArray((item as ColumnGroupType<RecordType>).children)) {
const childKeys = getColumnKeys((item as ColumnGroupType<RecordType>).children, columnPos);
newKeys.push(...childKeys);
}
});
return newKeys;
};
const mergedSorterStates = React.useMemo<SortState<RecordType>[]>(() => {
let validate = true;
const collectedStates = collectSortStates(mergedColumns, false);
// Return if not controlled
if (!collectedStates.length) {
const mergedColumnsKeys = mergedColumns.map((item, index) =>
getColumnKey(item, getColumnPos(index)),
);
const mergedColumnsKeys = getColumnKeys(mergedColumns);
return sortStates.filter(({ key }) => mergedColumnsKeys.includes(key));
}