Browse Source

fix: Table filter do not persist filter status when filter dropdown is visible (#41445)

* Fix 36946 by memoizing a hook dependency to remain stable.

* Update Table.filter.test.tsx

* Update Table.filter.test.tsx
pull/41501/head
Andrew Blakey 2 years ago
committed by GitHub
parent
commit
0c555265b9
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 43
      components/table/__tests__/Table.filter.test.tsx
  2. 5
      components/table/hooks/useFilter/index.tsx

43
components/table/__tests__/Table.filter.test.tsx

@ -2777,4 +2777,47 @@ describe('Table.filter', () => {
expect(renderedNames(container)).toEqual(['Jack']);
});
it('changes to table data should not reset the filter dropdown state being changed by a user', () => {
const tableProps = {
key: 'stabletable',
rowKey: 'name',
dataSource: [],
columns: [
{
title: 'Name',
dataIndex: 'name',
filteredValue: [], // User is controlling filteredValue. It begins with no items checked.
filters: [{ text: 'J', value: 'J' }],
onFilter: (value: any, record: any) => record.name.includes(value),
},
],
};
const { container, rerender } = render(createTable(tableProps));
// User opens filter Dropdown.
fireEvent.click(container.querySelector('.ant-dropdown-trigger.ant-table-filter-trigger')!);
// There is one checkbox and it begins unchecked.
expect(container.querySelector<HTMLInputElement>('input[type="checkbox"]')!.checked).toEqual(
false,
);
// User checks it.
fireEvent.click(container.querySelector('input[type="checkbox"]')!);
// The checkbox is now checked.
expect(container.querySelector<HTMLInputElement>('input[type="checkbox"]')!.checked).toEqual(
true,
);
// Table data changes while the dropdown is open and a user is setting filters.
rerender(createTable({ ...tableProps, dataSource: [{ name: 'Foo' }] }));
// The checkbox is still checked.
expect(container.querySelector<HTMLInputElement>('input[type="checkbox"]')!.checked).toEqual(
true,
);
});
});

5
components/table/hooks/useFilter/index.tsx

@ -215,7 +215,10 @@ function useFilter<RecordType>({
FilterState<RecordType>[],
Record<string, FilterValue | null>,
] {
const mergedColumns = getMergedColumns(rawMergedColumns || []);
const mergedColumns = React.useMemo(
() => getMergedColumns(rawMergedColumns || []),
[rawMergedColumns],
);
const [filterStates, setFilterStates] = React.useState<FilterState<RecordType>[]>(() =>
collectFilterStates(mergedColumns, true),

Loading…
Cancel
Save