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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with
47 additions and
1 deletions
-
components/table/__tests__/Table.filter.test.tsx
-
components/table/hooks/useFilter/index.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, |
|
|
|
); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
@ -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), |
|
|
|