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] Autocomplete within filters not working as expected #16065

Closed
nicolasfripp opened this issue Jan 2, 2025 · 7 comments
Closed

[data grid] Autocomplete within filters not working as expected #16065

nicolasfripp opened this issue Jan 2, 2025 · 7 comments
Labels
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

Comments

@nicolasfripp
Copy link

nicolasfripp commented Jan 2, 2025

Steps to reproduce

Steps:

  1. Open this link to live example: https://codesandbox.io/p/sandbox/5p35km
  2. Click on filters.
  3. Add a filter for the "Age" column
  4. add a filter to another column
  5. See that the autocomplete gets re-rendered
Screen.Recording.2025-01-02.at.13.07.31.mov

Current behavior

The Autocomplete is re-drawn

Expected behavior

I hope that if the Datagrid component is re-rendered, the autocomplete won't be re-drawn (like the Select component).

Context

Im updating some states after applying some filter, which causes this weird behaviour on the Autocomplete. It is only happening in that component, if I use a Select, that does not happen

Your environment

No response

Search keywords: Autocomplete DataGrid DataGridPro filters

@nicolasfripp nicolasfripp added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Jan 2, 2025
@zannager zannager added the component: data grid This is the name of the generic UI component, not the React module! label Jan 3, 2025
@zannager zannager transferred this issue from mui/material-ui Jan 3, 2025
@github-actions github-actions bot changed the title [DataGridPro] Autocomplete within filters not working as expected [data grid] Autocomplete within filters not working as expected Jan 3, 2025
@michelengelen
Copy link
Member

Thanks for opening an issue @nicolasfripp ... this is due to the fact that we are rendering the forms via a mapping and every state change rerenders this.

{validFilters.map((item, index) => (
<GridFilterForm
key={item.id == null ? index + readOnlyFilters.length : item.id}
item={item}
applyFilterChanges={applyFilter}
deleteFilter={deleteFilter}
hasMultipleFilters={hasMultipleFilters}
showMultiFilterOperators={readOnlyFilters.length + index > 0}
disableMultiFilterOperator={readOnlyFilters.length + index !== 1}
applyMultiFilterOperatorChanges={applyFilterLogicOperator}
focusElementRef={index === validFilters.length - 1 ? lastFilterRef : null}
logicOperators={logicOperators}
columnsSort={columnsSort}
{...filterFormProps}
/>
))}

I know that @KenanYusuf is working on a new UI for the panels, so he probably is currently the best to talk to bout this. The main problem is that the filters are of a flexible amount and there is no easy way to memoize them.

For now I'll add this to the board and see what the team has to say about this. 👍🏼

@michelengelen michelengelen added bug 🐛 Something doesn't work feature: Filtering Related to the data grid Filtering feature and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Jan 6, 2025
@github-project-automation github-project-automation bot moved this to 🆕 Needs refinement in MUI X Data Grid Jan 6, 2025
@michelengelen
Copy link
Member

We just discussed this in the team and @cherniavskii did point out that you need to make the columns array static for this to work, otherwise the InputComponent will render on every input due to state changes.

He did also create a quick Demo based on your implementation: here.

In this case moving the columns array outside of the component was enough, but in more complex situations you might need to implement a memoization (e.g. if you need to access component specific variables/functions).

So regarding this I will close the issue as it was implementation specific.

Copy link

github-actions bot commented Jan 6, 2025

This issue has been closed. If you have a similar problem but not exactly the same, please open a new issue.
Now, if you have additional information related to this issue or things that could help future readers, feel free to leave a comment.

Note

@nicolasfripp How did we do? Your experience with our support team matters to us. If you have a moment, please share your thoughts in this short Support Satisfaction survey.

@nicolasfripp
Copy link
Author

@michelengelen @cherniavskii Thanks for the response!
Unfortunately in our project it's not as simple as moving the columns outside! Could you tell me why this doesn't happen if I use a Select?

@nicolasfripp
Copy link
Author

nicolasfripp commented Jan 6, 2025

@michelengelen @cherniavskii , Here you have the same example but using a select instead:

https://codesandbox.io/p/sandbox/5p35km

Screen.Recording.2025-01-06.at.14.46.34.mov

@cherniavskii
Copy link
Member

Hi @nicolasfripp

Unfortunately in our project it's not as simple as moving the columns outside!

Can you share more details?
It's recommended to keep the same reference for the columns prop:

Anyway, you can extract ageFilterOperators (or even InputComponent): https://codesandbox.io/p/sandbox/trying-something-forked-qm7zn3?file=%2Fsrc%2FDataGridDemo.tsx%3A104%2C1

Could you tell me why this doesn't happen if I use a Select?

I believe that's because Select doesn't use TextField (even though Select and Autocomplete are visually similar).
But the root issue persists for Select as well – it's unmounted and mounted again on each rerender.
The main takeaway is that a React component definition (InputComponent) must be referentially stable. The same problem is explained in https://mui.com/x/common-concepts/custom-components/#correct-usage

Let me know if this works for you.

@cherniavskii cherniavskii removed the bug 🐛 Something doesn't work label Jan 6, 2025
@nicolasfripp
Copy link
Author

@cherniavskii Thanks for your help! I was able to make it work. Memoizing the InputComponent element was the key!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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
Status: 🆕 Needs refinement
Development

No branches or pull requests

5 participants