Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: warn about miss matched filter key #30882

Merged
merged 4 commits into from
Jun 9, 2021
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion components/table/hooks/useFilter/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import isEqual from 'lodash/isEqual';
import sortBy from 'lodash/sortBy';
import * as React from 'react';
import {
TransformColumns,
Expand Down Expand Up @@ -207,11 +209,30 @@ function useFilter<RecordType>({
const mergedFilterStates = React.useMemo(() => {
const collectedStates = collectFilterStates(mergedColumns, false);

const filteredKeysIsNotControlled = collectedStates.every(
({ filteredKeys }) => filteredKeys === undefined,
);
wzhudev marked this conversation as resolved.
Show resolved Hide resolved

// Return if not controlled
if (collectedStates.every(({ filteredKeys }) => filteredKeys === undefined)) {
if (filteredKeysIsNotControlled) {
return filterStates;
}

// If `filterStates` is controlled has different filteredKeys then warn the user
const hasMismatchedControlledKeys = collectedStates.some(
({ key, filteredKeys }) =>
!isEqual(
sortBy(filterStates.find(item => item.key === key)?.filteredKeys || []),
sortBy(filteredKeys),
),
);
if (hasMismatchedControlledKeys) {
// eslint-disable-next-line no-console
console.warn(
'You pass `filteredKeys` in to make it controlled but not change it when filter states changes. Please change it in `onFilterChange` callback.',
);
}

return collectedStates;
}, [mergedColumns, filterStates]);

Expand Down