mirror of
https://github.com/ant-design/ant-design.git
synced 2025-06-08 01:53:34 +08:00
perf: optimize calculation of filteredKeysIsAllControlled (#35064)
* perf: optimize calculation of filteredKeysIsAllControlled * fix: ci failure * fix: modify codes by suggestion
This commit is contained in:
parent
78eed85cba
commit
a9806a2be0
@ -2252,4 +2252,57 @@ describe('Table.filter', () => {
|
||||
expect(wrapper.find('.ant-tree-checkbox-checked').length).toBe(1);
|
||||
expect(wrapper.find('.ant-tree-checkbox-checked+span').text()).toBe('Girl');
|
||||
});
|
||||
|
||||
it('filteredKeys should all be controlled or not controlled', () => {
|
||||
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
|
||||
errorSpy.mockReset();
|
||||
const tableData = [
|
||||
{
|
||||
key: '1',
|
||||
name: 'John Brown',
|
||||
age: 32,
|
||||
},
|
||||
];
|
||||
const columns = [
|
||||
{
|
||||
title: 'name',
|
||||
dataIndex: 'name',
|
||||
key: 'name',
|
||||
filters: [],
|
||||
},
|
||||
{
|
||||
title: 'age',
|
||||
dataIndex: 'age',
|
||||
key: 'age',
|
||||
filters: [],
|
||||
},
|
||||
];
|
||||
render(
|
||||
createTable({
|
||||
columns,
|
||||
data: tableData,
|
||||
}),
|
||||
);
|
||||
expect(errorSpy).not.toBeCalled();
|
||||
errorSpy.mockReset();
|
||||
columns[0].filteredValue = [];
|
||||
render(
|
||||
createTable({
|
||||
columns,
|
||||
data: tableData,
|
||||
}),
|
||||
);
|
||||
expect(errorSpy).toBeCalledWith(
|
||||
'Warning: [antd: Table] Columns should all contain `filteredValue` or not contain `filteredValue`.',
|
||||
);
|
||||
errorSpy.mockReset();
|
||||
columns[1].filteredValue = [];
|
||||
render(
|
||||
createTable({
|
||||
columns,
|
||||
data: tableData,
|
||||
}),
|
||||
);
|
||||
expect(errorSpy).not.toBeCalled();
|
||||
});
|
||||
});
|
||||
|
@ -211,24 +211,25 @@ function useFilter<RecordType>({
|
||||
|
||||
const mergedFilterStates = React.useMemo(() => {
|
||||
const collectedStates = collectFilterStates(mergedColumns, false);
|
||||
|
||||
const filteredKeysIsNotControlled = collectedStates.every(
|
||||
({ filteredKeys }) => filteredKeys === undefined,
|
||||
);
|
||||
let filteredKeysIsAllNotControlled = true;
|
||||
let filteredKeysIsAllControlled = true;
|
||||
collectedStates.forEach(({ filteredKeys }) => {
|
||||
if (filteredKeys !== undefined) {
|
||||
filteredKeysIsAllNotControlled = false;
|
||||
} else {
|
||||
filteredKeysIsAllControlled = false;
|
||||
}
|
||||
});
|
||||
|
||||
// Return if not controlled
|
||||
if (filteredKeysIsNotControlled) {
|
||||
if (filteredKeysIsAllNotControlled) {
|
||||
return filterStates;
|
||||
}
|
||||
|
||||
const filteredKeysIsAllControlled = collectedStates.every(
|
||||
({ filteredKeys }) => filteredKeys !== undefined,
|
||||
);
|
||||
|
||||
devWarning(
|
||||
filteredKeysIsNotControlled || filteredKeysIsAllControlled,
|
||||
filteredKeysIsAllControlled,
|
||||
'Table',
|
||||
'`FilteredKeys` should all be controlled or not controlled.',
|
||||
'Columns should all contain `filteredValue` or not contain `filteredValue`.',
|
||||
);
|
||||
|
||||
return collectedStates;
|
||||
|
Loading…
Reference in New Issue
Block a user