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

[DataGrid] Fix header filters' issue with custom filters #13255

Merged
merged 3 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,12 @@ const GridHeaderFilterCell = React.forwardRef<HTMLDivElement, GridHeaderFilterCe
const isMenuOpen = menuOpenField === colDef.field;

// TODO: Support for `isAnyOf` operator
const filterOperators =
colDef.filterOperators?.filter((operator) => operator.value !== 'isAnyOf') ?? [];
const filterOperators = React.useMemo(() => {
if (!colDef.filterOperators) {
return [];
}
return colDef.filterOperators.filter((operator) => operator.value !== 'isAnyOf');
}, [colDef.filterOperators]);
const filterModel = useGridSelector(apiRef, gridFilterModelSelector);
const filterableColumnsLookup = useGridSelector(apiRef, gridFilterableColumnLookupSelector);

Expand All @@ -136,7 +140,11 @@ const GridHeaderFilterCell = React.forwardRef<HTMLDivElement, GridHeaderFilterCe
return filterModelItem ? !filterableColumnsLookup[filterModelItem.field] : false;
}, [colDef.field, filterModel, filterableColumnsLookup]);

const currentOperator = filterOperators![0];
const currentOperator = React.useMemo(
() =>
filterOperators.find((operator) => operator.value === item.operator) ?? filterOperators![0],
[item.operator, filterOperators],
);

const InputComponent =
colDef.filterable || isFilterReadOnly ? currentOperator!.InputComponent : null;
Expand Down Expand Up @@ -286,10 +294,10 @@ const GridHeaderFilterCell = React.forwardRef<HTMLDivElement, GridHeaderFilterCe

const classes = useUtilityClasses(ownerState as OwnerState);

const isNoInputOperator =
filterOperators?.find(({ value }) => item.operator === value)?.requiresFilterValue === false;
const isNoInputOperator = currentOperator.requiresFilterValue === false;

const isApplied = Boolean(item?.value) || isNoInputOperator;

const label =
currentOperator.headerLabel ??
apiRef.current.getLocaleText(
Expand Down
41 changes: 41 additions & 0 deletions packages/x-data-grid-pro/src/tests/filtering.DataGridPro.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
gridExpandedSortedRowEntriesSelector,
gridClasses,
GridColDef,
getGridStringOperators,
} from '@mui/x-data-grid-pro';
import { createRenderer, fireEvent, screen, act, within } from '@mui-internal/test-utils';
import { expect } from 'chai';
Expand Down Expand Up @@ -1057,6 +1058,46 @@ describe('<DataGridPro /> - Filter', () => {

expect(getColumnHeaderCell(0, 1).textContent).to.equal('Custom Input');
});

// See https://github.com/mui/mui-x/issues/13217
it('should not throw when custom filter operator is used with an initilaized value', () => {
expect(() => {
render(
<TestCase
columns={[
{
field: 'brand',
headerName: 'Brand',
filterOperators: [
...getGridStringOperators(),
{
value: 'looksLike',
label: 'Looks Like',
headerLabel: 'Looks Like',
getApplyFilterFn: () => () => true,
InputComponent: () => <div>Custom Input</div>,
},
],
},
]}
initialState={{
filter: {
filterModel: {
items: [
{
field: 'brand',
operator: 'looksLike',
value: 'a',
},
],
},
},
}}
headerFilters
/>,
);
}).not.toErrorDev();
});
});

describe('Read-only filters', () => {
Expand Down