From 1cdd21b1c165560da21d4e3a2a7a6e76a18b264e Mon Sep 17 00:00:00 2001 From: Bilal Shafi Date: Fri, 7 Mar 2025 14:05:30 +0500 Subject: [PATCH] [DataGrid] Fix header filters not displaying restored values --- .../src/tests/filtering.DataGridPro.test.tsx | 29 +++++++++++++++++++ .../filterPanel/GridFilterInputValue.tsx | 11 +++++-- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/packages/x-data-grid-pro/src/tests/filtering.DataGridPro.test.tsx b/packages/x-data-grid-pro/src/tests/filtering.DataGridPro.test.tsx index b44dca42fbdcc..bc18811dfef21 100644 --- a/packages/x-data-grid-pro/src/tests/filtering.DataGridPro.test.tsx +++ b/packages/x-data-grid-pro/src/tests/filtering.DataGridPro.test.tsx @@ -939,6 +939,35 @@ describe(' - Filter', () => { expect(filterCellInput).to.have.value('a'); }); + // See https://github.com/mui/mui-x/issues/16826 + it('should set the input state properly when using the `apiRef.current.restoreState` method', async () => { + const changeSpy = spy(); + render(); + + const filterCellInput = getColumnHeaderCell(0, 1).querySelector('input')!; + fireEvent.mouseDown(filterCellInput); + expect(filterCellInput).toHaveFocus(); + fireEvent.change(filterCellInput, { target: { value: 'p' } }); + clock.tick(SUBMIT_FILTER_STROKE_TIME); + expect(getColumnValues(0)).to.deep.equal(['Puma']); + expect(filterCellInput).to.have.value('p'); + + fireEvent.change(filterCellInput, { target: { value: '' } }); + expect(filterCellInput).to.have.value(''); + + act(() => + apiRef.current?.restoreState({ + filter: { + filterModel: { + items: [{ field: 'brand', operator: 'contains', value: 'b' }], + }, + }, + }), + ); + + expect(filterCellInput).to.have.value('b'); + }); + it('should apply filters on type when the focus is on cell', () => { render(); diff --git a/packages/x-data-grid/src/components/panel/filterPanel/GridFilterInputValue.tsx b/packages/x-data-grid/src/components/panel/filterPanel/GridFilterInputValue.tsx index 66c86b0d48c41..fb16e10371f2a 100644 --- a/packages/x-data-grid/src/components/panel/filterPanel/GridFilterInputValue.tsx +++ b/packages/x-data-grid/src/components/panel/filterPanel/GridFilterInputValue.tsx @@ -34,6 +34,7 @@ function GridFilterInputValue(props: GridTypeFilterInputValueProps) { const [filterValueState, setFilterValueState] = React.useState( sanitizeFilterItemValue(item.value), ); + const lastValue = React.useRef(filterValueState); const [applying, setIsApplying] = React.useState(false); const id = useId(); const rootProps = useGridRootProps(); @@ -59,8 +60,14 @@ function GridFilterInputValue(props: GridTypeFilterInputValueProps) { React.useEffect(() => { const itemPlusTag = item as ItemPlusTag; - if (itemPlusTag.fromInput !== id || item.value == null) { - setFilterValueState(sanitizeFilterItemValue(item.value)); + const sanitizedValue = sanitizeFilterItemValue(item.value); + if ( + itemPlusTag.fromInput !== id || + item.value == null || + sanitizedValue !== lastValue.current + ) { + setFilterValueState(sanitizedValue); + lastValue.current = sanitizedValue; } }, [id, item]);