Skip to content

Commit

Permalink
perf: optimize calculation of filteredKeysIsAllControlled (#35064)
Browse files Browse the repository at this point in the history
* perf: optimize calculation of filteredKeysIsAllControlled

* fix: ci failure

* fix: modify codes by suggestion
  • Loading branch information
nieyuyao committed Apr 23, 2022
1 parent 78eed85 commit a9806a2
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 11 deletions.
53 changes: 53 additions & 0 deletions components/table/__tests__/Table.filter.test.js
Expand Up @@ -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();
});
});
23 changes: 12 additions & 11 deletions components/table/hooks/useFilter/index.tsx
Expand Up @@ -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;
Expand Down

0 comments on commit a9806a2

Please sign in to comment.