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

[material-ui][joy-ui][base-ui][Autocomplete] Keep in sync highlighted index when the option still exists #41306

Conversation

CGNonofr
Copy link
Contributor

@CGNonofr CGNonofr commented Feb 28, 2024

fix #41386

If the highlighted option still exists, it doesn't mean its index in the filtered list didn't change.

The index being out-of-sync with the visually highlighted item create weird behavior when interacting with the keyboard arrows (jumps)

Before: https://codesandbox.io/p/sandbox/mui-autocomplete-bug-94thpd
After: https://codesandbox.io/p/sandbox/mui-autocomplete-bug-forked-87nw9h

@mui-bot
Copy link

mui-bot commented Feb 28, 2024

Netlify deploy preview

https://deploy-preview-41306--material-ui.netlify.app/

Bundle size report

Details of bundle changes (Toolpad)
Details of bundle changes

Generated by 🚫 dangerJS against 541c3f6

@zannager zannager added the component: autocomplete This is the name of the generic UI component, not the React module! label Feb 29, 2024
@CGNonofr CGNonofr force-pushed the fix-autocomplete-out-of-sync-highlighted-index branch 2 times, most recently from 76d899c to 920b7e5 Compare February 29, 2024 17:40
Copy link
Member

@ZeeshanTamboli ZeeshanTamboli left a comment

Choose a reason for hiding this comment

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

@CGNonofr Are you trying to solve an existing GitHub issue? If not, could you start by creating a new issue and provide a CodeSandbox reproduction?

@ZeeshanTamboli ZeeshanTamboli added the package: material-ui Specific to @mui/material label Mar 6, 2024
@ZeeshanTamboli ZeeshanTamboli changed the title Keep in sync highlighted index when the option still exists [material-ui][Autocomplete] Keep in sync highlighted index when the option still exists Mar 6, 2024
@CGNonofr
Copy link
Contributor Author

CGNonofr commented Mar 6, 2024

@CGNonofr Are you trying to solve an existing GitHub issue? If not, could you please create a new issue with a CodeSandbox reproduction?

No, I didn't find any issue related to it

I sure can create an issue if you process needs it.

Regarding the CodeSandbox reproduction, I've already changed a test (the last commit) that highlight the issue, is that enough?

@ZeeshanTamboli
Copy link
Member

I sure can create an issue if you process needs it.

Regarding the CodeSandbox reproduction, I've already changed a test (the last commit) that highlight the issue, is that enough

A codesandbox reproduction would still help.

@CGNonofr
Copy link
Contributor Author

CGNonofr commented Mar 6, 2024

I sure can create an issue if you process needs it.
Regarding the CodeSandbox reproduction, I've already changed a test (the last commit) that highlight the issue, is that enough

A codesandbox reproduction would still help.

Created the issue and linked it in the PR description (#41386)

Copy link
Member

@ZeeshanTamboli ZeeshanTamboli left a comment

Choose a reason for hiding this comment

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

The fix doesn't work as expected. I tested it using this PR's build: "@mui/material": "https://pkg.csb.dev/mui/material-ui/commit/920b7e5f/@mui/material". See the CodeSandbox - https://codesandbox.io/p/sandbox/mui-autocomplete-bug-forked-tg875l.

@ZeeshanTamboli ZeeshanTamboli added the bug 🐛 Something doesn't work label Mar 7, 2024
@CGNonofr
Copy link
Contributor Author

CGNonofr commented Mar 7, 2024

The fix doesn't work as expected. I tested it using this PR's build: "@mui/material": "https://pkg.csb.dev/mui/material-ui/commit/920b7e5f/@mui/material". See the CodeSandbox - https://codesandbox.io/p/sandbox/mui-autocomplete-bug-forked-tg875l.

for it to work, we also need to add isOptionEqualToValue={(a, b) => a.id === b.id}

@ZeeshanTamboli
Copy link
Member

ZeeshanTamboli commented Mar 7, 2024

for it to work, we also need to add isOptionEqualToValue={(a, b) => a.id === b.id}

Why is it necessary? The prop is optional. Keyboard navigation works in general (no option insertion) without it being required. What's the rationale behind needing it in this specific case?

@CGNonofr
Copy link
Contributor Author

CGNonofr commented Mar 7, 2024

for it to work, we also need to add isOptionEqualToValue={(a, b) => a.id === b.id}

Why is it necessary? The prop is optional. Keyboard navigation works in general (no option insertion) without it being required. What's the rationale behind needing it in this specific case?

because all the items are changed, the alternative is to reuse the items: https://codesandbox.io/p/sandbox/mui-autocomplete-bug-forked-mxsvf7?file=%2Fsrc%2FApp.js%3A22%2C68

@ZeeshanTamboli
Copy link
Member

for it to work, we also need to add isOptionEqualToValue={(a, b) => a.id === b.id}

Why is it necessary? The prop is optional. Keyboard navigation works in general (no option insertion) without it being required. What's the rationale behind needing it in this specific case?

because all the items are changed, the alternative is to reuse the items: https://codesandbox.io/p/sandbox/mui-autocomplete-bug-forked-mxsvf7?file=%2Fsrc%2FApp.js%3A22%2C68

How would developers know they need to use that prop? Also, the above CodeSandbox is inaccessible. Perhaps you need to grant permissions to me.

@CGNonofr
Copy link
Contributor Author

CGNonofr commented Mar 7, 2024

for it to work, we also need to add isOptionEqualToValue={(a, b) => a.id === b.id}

Why is it necessary? The prop is optional. Keyboard navigation works in general (no option insertion) without it being required. What's the rationale behind needing it in this specific case?

because all the items are changed, the alternative is to reuse the items: https://codesandbox.io/p/sandbox/mui-autocomplete-bug-forked-mxsvf7?file=%2Fsrc%2FApp.js%3A22%2C68

How would developers know they need to use that prop?

You're right, the code is not correct, I will come up with something better

It still feel weird to me that the option comparison rely exclusively on the label without any way to override that behavior (so it's impossible to rename an option without losing it's highlighting)

Also, the above CodeSandbox is inaccessible. Perhaps you need to grant permissions to me.

my bad, it's now shared

@ZeeshanTamboli
Copy link
Member

It still feel weird to me that the option comparison rely exclusively on the label without any way to override that behavior (so it's impossible to rename an option without losing it's highlighting)

I didn't understand what you mean. You can use getOptionLabel to rename an option label.

@CGNonofr CGNonofr force-pushed the fix-autocomplete-out-of-sync-highlighted-index branch 2 times, most recently from e59410a to 41d7cfa Compare March 7, 2024 10:13
@CGNonofr
Copy link
Contributor Author

CGNonofr commented Mar 7, 2024

@ZeeshanTamboli ok I've rewritten the PR

Copy link
Member

@ZeeshanTamboli ZeeshanTamboli left a comment

Choose a reason for hiding this comment

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

Thanks for this fix. The logic and the fix seem solid, but the tests are passing on the master HEAD branch which indicate they're not covering the necessary scenario. Maybe add a new test instead of modifying an existing one. Also, need to update the comment.

@CGNonofr
Copy link
Contributor Author

CGNonofr commented Mar 7, 2024

Thanks for this fix. The logic and the fix seem solid, but the tests are passing on the master HEAD branch which indicate they're not covering the necessary scenario. Maybe add a new test instead of modifying an existing one. Also, need to update the comment.

Rewritten the PR once again!

@CGNonofr CGNonofr force-pushed the fix-autocomplete-out-of-sync-highlighted-index branch from 41d7cfa to b587c21 Compare March 7, 2024 15:55
@CGNonofr CGNonofr force-pushed the fix-autocomplete-out-of-sync-highlighted-index branch from b587c21 to 541c3f6 Compare March 7, 2024 16:14
@ZeeshanTamboli ZeeshanTamboli changed the title [material-ui][Autocomplete] Keep in sync highlighted index when the option still exists [material-ui][joy-ui][base-ui][Autocomplete] Keep in sync highlighted index when the option still exists Mar 8, 2024
mnajdova pushed a commit to mnajdova/material-ui that referenced this pull request Mar 8, 2024
… index when the option still exists (mui#41306)

Co-authored-by: Loïc Mangeonjean <[email protected]>
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: autocomplete This is the name of the generic UI component, not the React module! package: base-ui Specific to @mui/base package: joy-ui Specific to @mui/joy package: material-ui Specific to @mui/material
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[material-ui][joy-ui][base-ui][Autocompelte] Highlighted index is broken when inserting new item
4 participants