diff --git a/components/table/__tests__/Table.filter.test.js b/components/table/__tests__/Table.filter.test.js index 310abaec5f..cf74e8d1cd 100644 --- a/components/table/__tests__/Table.filter.test.js +++ b/components/table/__tests__/Table.filter.test.js @@ -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(); + }); }); diff --git a/components/table/hooks/useFilter/index.tsx b/components/table/hooks/useFilter/index.tsx index 18b7593dab..e222e75713 100644 --- a/components/table/hooks/useFilter/index.tsx +++ b/components/table/hooks/useFilter/index.tsx @@ -211,24 +211,25 @@ function useFilter({ 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;