Skip to content

Commit f572940

Browse files
k-rajat19arminmeh
andauthored
[DataGrid] Fix header filters showing clear button while empty (#15829)
Co-authored-by: Armin Mehinovic <[email protected]>
1 parent 26b1463 commit f572940

File tree

8 files changed

+36
-35
lines changed

8 files changed

+36
-35
lines changed

docs/data/migration/migration-data-grid-v7/migration-data-grid-v7.md

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ Below are described the steps you need to make to migrate from v7 to v8.
5252
- The `rowPositionsDebounceMs` prop was removed.
5353
- The `apiRef.current.resize()` method was removed.
5454
- The `<GridOverlays />` component is not exported anymore.
55+
- The `sanitizeFilterItemValue()` utility is not exported anymore.
5556
- `gridRowsDataRowIdToIdLookupSelector` was removed. Use `gridRowsLookupSelector` in combination with `getRowId()` API method instead.
5657

5758
```diff

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

+2-13
Original file line numberDiff line numberDiff line change
@@ -165,17 +165,6 @@ const GridHeaderFilterCell = forwardRef<HTMLDivElement, GridHeaderFilterCellProp
165165
? (currentOperator.InputComponent ?? defaultInputComponents[colDef.type as GridColType])
166166
: null;
167167

168-
const applyFilterChanges = React.useCallback(
169-
(updatedItem: GridFilterItem) => {
170-
if (item.value && updatedItem.value === undefined) {
171-
apiRef.current.deleteFilterItem(updatedItem);
172-
return;
173-
}
174-
apiRef.current.upsertFilterItem(updatedItem);
175-
},
176-
[apiRef, item],
177-
);
178-
179168
const clearFilterItem = React.useCallback(() => {
180169
apiRef.current.deleteFilterItem(item);
181170
}, [apiRef, item]);
@@ -346,7 +335,7 @@ const GridHeaderFilterCell = forwardRef<HTMLDivElement, GridHeaderFilterCellProp
346335
apiRef={apiRef}
347336
item={item}
348337
inputRef={inputRef}
349-
applyValue={applyFilterChanges}
338+
applyValue={apiRef.current.upsertFilterItem}
350339
onFocus={() => apiRef.current.startHeaderFilterEditMode(colDef.field)}
351340
onBlur={(event: React.FocusEvent) => {
352341
apiRef.current.stopHeaderFilterEditMode();
@@ -387,7 +376,7 @@ const GridHeaderFilterCell = forwardRef<HTMLDivElement, GridHeaderFilterCellProp
387376
item={item}
388377
field={colDef.field}
389378
disabled={isFilterReadOnly}
390-
applyFilterChanges={applyFilterChanges}
379+
applyFilterChanges={apiRef.current.upsertFilterItem}
391380
headerFilterMenuRef={headerFilterMenuRef}
392381
buttonRef={buttonRef}
393382
/>

packages/x-data-grid/src/components/panel/filterPanel/GridFilterInputBoolean.tsx

+10-10
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,6 @@ export type GridFilterInputBooleanProps = GridFilterInputValueProps &
1616
isFilterActive?: boolean;
1717
};
1818

19-
export const sanitizeFilterItemValue = (value: any): boolean | undefined => {
20-
if (String(value).toLowerCase() === 'true') {
21-
return true;
22-
}
23-
if (String(value).toLowerCase() === 'false') {
24-
return false;
25-
}
26-
return undefined;
27-
};
28-
2919
const BooleanOperatorContainer = styled('div')({
3020
display: 'flex',
3121
alignItems: 'center',
@@ -133,6 +123,16 @@ function GridFilterInputBoolean(props: GridFilterInputBooleanProps) {
133123
);
134124
}
135125

126+
export function sanitizeFilterItemValue(value: any): boolean | undefined {
127+
if (String(value).toLowerCase() === 'true') {
128+
return true;
129+
}
130+
if (String(value).toLowerCase() === 'false') {
131+
return false;
132+
}
133+
return undefined;
134+
}
135+
136136
GridFilterInputBoolean.propTypes = {
137137
// ----------------------------- Warning --------------------------------
138138
// | These PropTypes are generated from the TypeScript type definitions |

packages/x-data-grid/src/components/panel/filterPanel/GridFilterInputValue.tsx

+21-8
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,25 @@ function GridFilterInputValue(props: GridTypeFilterInputValueProps) {
3535
variant = 'standard',
3636
...others
3737
} = props;
38+
3839
const filterTimeout = useTimeout();
39-
const [filterValueState, setFilterValueState] = React.useState<string>(item.value ?? '');
40+
const [filterValueState, setFilterValueState] = React.useState<string | number | undefined>(
41+
sanitizeFilterItemValue(item.value, type),
42+
);
4043
const [applying, setIsApplying] = React.useState(false);
4144
const id = useId();
4245
const rootProps = useGridRootProps();
4346

4447
const onFilterChange = React.useCallback(
4548
(event: React.ChangeEvent<HTMLInputElement>) => {
46-
const { value } = event.target;
47-
setFilterValueState(String(value));
49+
const value = sanitizeFilterItemValue(event.target.value, type);
50+
setFilterValueState(value);
4851

4952
setIsApplying(true);
5053
filterTimeout.start(rootProps.filterDebounceMs, () => {
5154
const newItem = {
5255
...item,
53-
value: type === 'number' ? Number(value) : value,
56+
value,
5457
fromInput: id!,
5558
};
5659
applyValue(newItem);
@@ -62,17 +65,17 @@ function GridFilterInputValue(props: GridTypeFilterInputValueProps) {
6265

6366
React.useEffect(() => {
6467
const itemPlusTag = item as ItemPlusTag;
65-
if (itemPlusTag.fromInput !== id || item.value === undefined) {
66-
setFilterValueState(String(item.value ?? ''));
68+
if (itemPlusTag.fromInput !== id || item.value == null) {
69+
setFilterValueState(sanitizeFilterItemValue(item.value, type));
6770
}
68-
}, [id, item]);
71+
}, [id, item, type]);
6972

7073
return (
7174
<rootProps.slots.baseTextField
7275
id={id}
7376
label={apiRef.current.getLocaleText('filterPanelInputLabel')}
7477
placeholder={apiRef.current.getLocaleText('filterPanelInputPlaceholder')}
75-
value={filterValueState}
78+
value={filterValueState === undefined ? '' : String(filterValueState)}
7679
onChange={onFilterChange}
7780
variant={variant}
7881
type={type || 'text'}
@@ -103,6 +106,16 @@ function GridFilterInputValue(props: GridTypeFilterInputValueProps) {
103106
);
104107
}
105108

109+
function sanitizeFilterItemValue(value: any, type: GridTypeFilterInputValueProps['type']) {
110+
if (value == null || value === '') {
111+
return undefined;
112+
}
113+
if (type === 'number') {
114+
return Number(value);
115+
}
116+
return String(value);
117+
}
118+
106119
GridFilterInputValue.propTypes = {
107120
// ----------------------------- Warning --------------------------------
108121
// | These PropTypes are generated from the TypeScript type definitions |

packages/x-data-grid/src/components/panel/filterPanel/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ export * from './GridFilterForm';
22
export * from './GridFilterInputValue';
33
export * from './GridFilterInputDate';
44
export * from './GridFilterInputSingleSelect';
5-
export * from './GridFilterInputBoolean';
5+
export { GridFilterInputBoolean } from './GridFilterInputBoolean';
6+
export type { GridFilterInputBooleanProps } from './GridFilterInputBoolean';
67
export * from './GridFilterInputValueProps';
78
export { GridFilterPanel } from './GridFilterPanel';
89
export type { GetColumnForNewFilterArgs } from './GridFilterPanel';

scripts/x-data-grid-premium.exports.json

-1
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,6 @@
663663
{ "name": "renderEditSingleSelectCell", "kind": "Variable" },
664664
{ "name": "renderRowReorderCell", "kind": "Variable" },
665665
{ "name": "RowPropsOverrides", "kind": "Interface" },
666-
{ "name": "sanitizeFilterItemValue", "kind": "Variable" },
667666
{ "name": "selectedGridRowsCountSelector", "kind": "Variable" },
668667
{ "name": "selectedGridRowsSelector", "kind": "Variable" },
669668
{ "name": "selectedIdsLookupSelector", "kind": "Variable" },

scripts/x-data-grid-pro.exports.json

-1
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,6 @@
612612
{ "name": "renderEditSingleSelectCell", "kind": "Variable" },
613613
{ "name": "renderRowReorderCell", "kind": "Variable" },
614614
{ "name": "RowPropsOverrides", "kind": "Interface" },
615-
{ "name": "sanitizeFilterItemValue", "kind": "Variable" },
616615
{ "name": "selectedGridRowsCountSelector", "kind": "Variable" },
617616
{ "name": "selectedGridRowsSelector", "kind": "Variable" },
618617
{ "name": "selectedIdsLookupSelector", "kind": "Variable" },

scripts/x-data-grid.exports.json

-1
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,6 @@
560560
{ "name": "renderEditInputCell", "kind": "Variable" },
561561
{ "name": "renderEditSingleSelectCell", "kind": "Variable" },
562562
{ "name": "RowPropsOverrides", "kind": "Interface" },
563-
{ "name": "sanitizeFilterItemValue", "kind": "Variable" },
564563
{ "name": "selectedGridRowsCountSelector", "kind": "Variable" },
565564
{ "name": "selectedGridRowsSelector", "kind": "Variable" },
566565
{ "name": "selectedIdsLookupSelector", "kind": "Variable" },

0 commit comments

Comments
 (0)