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 quick filter input lag #9630

Merged
merged 6 commits into from
Jul 12, 2023

Conversation

cherniavskii
Copy link
Member

@cherniavskii cherniavskii commented Jul 10, 2023

@cherniavskii cherniavskii added bug 🐛 Something doesn't work component: data grid This is the name of the generic UI component, not the React module! feature: Filtering Related to the data grid Filtering feature labels Jul 10, 2023
@mui-bot
Copy link

mui-bot commented Jul 10, 2023

Netlify deploy preview

Netlify deploy preview: https://deploy-preview-9630--material-ui-x.netlify.app/

Updated pages

No updates.

These are the results for the performance tests:

Test case Unit Min Max Median Mean σ
Filter 100k rows ms 308.2 415.2 387 363.16 43.973
Sort 100k rows ms 384 789.3 608.3 604.42 130.642
Select 100k rows ms 133.4 206.6 165 163.5 25.999
Deselect 100k rows ms 103.4 246.5 202.6 177.26 58.993

Generated by 🚫 dangerJS against 5c99180

@@ -101,7 +101,9 @@ function GridToolbarQuickFilter(props: GridToolbarQuickFilterProps) {

const updateSearchValue = React.useCallback(
(newSearchValue: string) => {
apiRef.current.setQuickFilterValues(quickFilterParser(newSearchValue));
const newQuickFilterValues = quickFilterParser(newSearchValue);
setPrevQuickFilterValues(newQuickFilterValues);
Copy link
Member Author

Choose a reason for hiding this comment

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

The root cause of this issue was here:

React.useEffect(() => {
if (!isDeepEqual(prevQuickFilterValues, quickFilterValues)) {
// The model of quick filter value has been updated
setPrevQuickFilterValues(quickFilterValues);
// Update the input value if needed to match the new model
setSearchValue((prevSearchValue) =>
isDeepEqual(quickFilterParser(prevSearchValue), quickFilterValues)
? prevSearchValue
: quickFilterFormatter(quickFilterValues ?? []),
);
}
}, [prevQuickFilterValues, quickFilterValues, quickFilterFormatter, quickFilterParser]);

Here is what was happening:

  1. type 'a': setSearchValue('a')
  2. debounced setQuickFilterValues(['a']) is called
  3. type 'b': setSearchValue('ab')
  4. useEffect: prevQuickFilterValues /* [] */ !== quickFilterValues /* ['a'] */, reset search value: setSearchValue('a')
  5. type 'c': setSearchValue('ac') // b was lost in the previous step

We should update prevQuickFilterValues as well when updating quickFilterValues internally.

Copy link
Member

Choose a reason for hiding this comment

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

It seems that prevQuickFilterValues should be a ref instead, what do you think? We only use it to ensure that the effect above only runs when quickFilterValues changes.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point, it should be a ref indeed!

if (isJSDOM) {
this.skip();
}
// Warning: this test doesn't fail consistently as it is timing-sensitive.
Copy link
Member Author

Choose a reason for hiding this comment

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

While this test doesn't consistently fail without the fix, I thought it was still better than nothing 🙂
What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

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

Not ideal but it's fine as it is.

@@ -59,7 +59,7 @@ export type GridToolbarQuickFilterProps = TextFieldProps & {
* @param {any[]} values The new values passed to the quick filter model
* @returns {string} The string to display in the text field
*/
quickFilterFormatter?: (values: GridFilterModel['quickFilterValues']) => string;
quickFilterFormatter?: (values: NonNullable<GridFilterModel['quickFilterValues']>) => string;
Copy link
Member Author

Choose a reason for hiding this comment

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

We never pass undefined as values, so we should exclude it here

@cherniavskii cherniavskii marked this pull request as ready for review July 10, 2023 13:35
@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Jul 12, 2023
@github-actions
Copy link

This pull request has conflicts, please resolve those before we can evaluate the pull request.

@@ -101,7 +101,9 @@ function GridToolbarQuickFilter(props: GridToolbarQuickFilterProps) {

const updateSearchValue = React.useCallback(
(newSearchValue: string) => {
apiRef.current.setQuickFilterValues(quickFilterParser(newSearchValue));
const newQuickFilterValues = quickFilterParser(newSearchValue);
setPrevQuickFilterValues(newQuickFilterValues);
Copy link
Member

Choose a reason for hiding this comment

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

It seems that prevQuickFilterValues should be a ref instead, what do you think? We only use it to ensure that the effect above only runs when quickFilterValues changes.

@github-actions github-actions bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged label Jul 12, 2023
@cherniavskii cherniavskii enabled auto-merge (squash) July 12, 2023 19:27
@cherniavskii cherniavskii merged commit 4f136ba into mui:master Jul 12, 2023
@cherniavskii cherniavskii deleted the fix-quick-filter-lag branch July 14, 2023 11:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something doesn't work component: data grid This is the name of the generic UI component, not the React module! feature: Filtering Related to the data grid Filtering feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[data grid] Input lag in GridToolbarQuickFilter on slowly typing
4 participants