perf: optimize calculation of filteredKeysIsAllControlled (#35064)

* perf: optimize calculation of filteredKeysIsAllControlled

* fix: ci failure

* fix: modify codes by suggestion
This commit is contained in:
Yuyao Nie 2022-04-23 17:11:52 +08:00 committed by GitHub
parent 78eed85cba
commit a9806a2be0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 65 additions and 11 deletions

View File

@ -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();
});
});

View File

@ -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;