Skip to content

Commit d7a6972

Browse files
MBilalShafithomasmoon
authored andcommitted
[DataGrid] Fix header filters' issue with custom filters (mui#13255)
1 parent d931d38 commit d7a6972

File tree

2 files changed

+54
-5
lines changed

2 files changed

+54
-5
lines changed

packages/x-data-grid-pro/src/components/headerFiltering/GridHeaderFilterCell.tsx

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,12 @@ const GridHeaderFilterCell = React.forwardRef<HTMLDivElement, GridHeaderFilterCe
123123
const isMenuOpen = menuOpenField === colDef.field;
124124

125125
// TODO: Support for `isAnyOf` operator
126-
const filterOperators =
127-
colDef.filterOperators?.filter((operator) => operator.value !== 'isAnyOf') ?? [];
126+
const filterOperators = React.useMemo(() => {
127+
if (!colDef.filterOperators) {
128+
return [];
129+
}
130+
return colDef.filterOperators.filter((operator) => operator.value !== 'isAnyOf');
131+
}, [colDef.filterOperators]);
128132
const filterModel = useGridSelector(apiRef, gridFilterModelSelector);
129133
const filterableColumnsLookup = useGridSelector(apiRef, gridFilterableColumnLookupSelector);
130134

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

139-
const currentOperator = filterOperators![0];
143+
const currentOperator = React.useMemo(
144+
() =>
145+
filterOperators.find((operator) => operator.value === item.operator) ?? filterOperators![0],
146+
[item.operator, filterOperators],
147+
);
140148

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

287295
const classes = useUtilityClasses(ownerState as OwnerState);
288296

289-
const isNoInputOperator =
290-
filterOperators?.find(({ value }) => item.operator === value)?.requiresFilterValue === false;
297+
const isNoInputOperator = currentOperator.requiresFilterValue === false;
291298

292299
const isApplied = Boolean(item?.value) || isNoInputOperator;
300+
293301
const label =
294302
currentOperator.headerLabel ??
295303
apiRef.current.getLocaleText(

packages/x-data-grid-pro/src/tests/filtering.DataGridPro.test.tsx

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
gridExpandedSortedRowEntriesSelector,
1616
gridClasses,
1717
GridColDef,
18+
getGridStringOperators,
1819
} from '@mui/x-data-grid-pro';
1920
import { createRenderer, fireEvent, screen, act, within } from '@mui/internal-test-utils';
2021
import { expect } from 'chai';
@@ -1057,6 +1058,46 @@ describe('<DataGridPro /> - Filter', () => {
10571058

10581059
expect(getColumnHeaderCell(0, 1).textContent).to.equal('Custom Input');
10591060
});
1061+
1062+
// See https://github.com/mui/mui-x/issues/13217
1063+
it('should not throw when custom filter operator is used with an initilaized value', () => {
1064+
expect(() => {
1065+
render(
1066+
<TestCase
1067+
columns={[
1068+
{
1069+
field: 'brand',
1070+
headerName: 'Brand',
1071+
filterOperators: [
1072+
...getGridStringOperators(),
1073+
{
1074+
value: 'looksLike',
1075+
label: 'Looks Like',
1076+
headerLabel: 'Looks Like',
1077+
getApplyFilterFn: () => () => true,
1078+
InputComponent: () => <div>Custom Input</div>,
1079+
},
1080+
],
1081+
},
1082+
]}
1083+
initialState={{
1084+
filter: {
1085+
filterModel: {
1086+
items: [
1087+
{
1088+
field: 'brand',
1089+
operator: 'looksLike',
1090+
value: 'a',
1091+
},
1092+
],
1093+
},
1094+
},
1095+
}}
1096+
headerFilters
1097+
/>,
1098+
);
1099+
}).not.toErrorDev();
1100+
});
10601101
});
10611102

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

0 commit comments

Comments
 (0)