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

[data grid] onFilterModelChange fires with stale field value #10711

Closed
wants to merge 11 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ const GridFilterForm = React.forwardRef<HTMLDivElement, GridFilterFormProps>(
...item,
field,
operator: newOperator.value,
value: eraseItemValue ? undefined : item.value,
value: eraseItemValue ? undefined : '',
});
},
[apiRef, applyFilterChanges, item, currentColumn, currentOperator],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,11 @@ describe('<DataGrid /> - Filter panel', () => {

setColumnValue('slogan');

expect(getColumnValues(0)).to.deep.equal([]);
expect(getColumnValues(0)).to.deep.equal(['Nike', 'Adidas', 'Puma']);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for looking into it!
While the fix is quite simple, it changed the filtering behavior in some valid use cases - this is why you had to update a few unit tests for them to pass.

Instead of always resetting the filter value, did you consider other options?
For example, resetting the filter value only if the type of the filtered column changes.
What do you think?

expect(screen.getByRole<HTMLSelectElement>('combobox', { name: 'Operator' }).value).to.equal(
'equals',
);
expect(screen.getByRole<HTMLInputElement>('textbox', { name: 'Value' }).value).to.equal('Puma');
expect(screen.getByRole<HTMLInputElement>('textbox', { name: 'Value' }).value).to.equal('');
});

it('should reset value if operator is not available for the new column', () => {
Expand Down Expand Up @@ -341,7 +341,7 @@ describe('<DataGrid /> - Filter panel', () => {

setColumnValue('origin');

expect(getColumnValues(0)).to.deep.equal(['REF_2', 'REF_3']);
expect(getColumnValues(0)).to.deep.equal(['REF_1', 'REF_2', 'REF_3']);
});

it('should reset filter value if not available in the new valueOptions with operator "isAnyOf"', () => {
Expand Down Expand Up @@ -412,7 +412,7 @@ describe('<DataGrid /> - Filter panel', () => {

expect(getColumnValues(0)).to.deep.equal(['REF_1']);
setColumnValue('origin');
expect(getColumnValues(0)).to.deep.equal(['REF_2', 'REF_3']);
expect(getColumnValues(0)).to.deep.equal(['REF_1', 'REF_2', 'REF_3']);
});

it('should reset filter value if moving from multiple to single value operator', () => {
Expand Down
Loading