Skip to content

[material-ui][Autocomplete] Keep the user's highlighted option when options change - #49864

Open
shivastuu wants to merge 4 commits into
mui:masterfrom
shivastuu:fix/autocomplete-highlight-identity
Open

shivastuu wants to merge 4 commits into
mui:masterfrom
shivastuu:fix/autocomplete-highlight-identity

Conversation

@shivastuu

Copy link
Copy Markdown

Closes #49863

When options change while the listbox is open, useAutocomplete re-finds the previously highlighted option by its label, and syncHighlightedIndex is memoised on filteredOptions.length. So:

  • with duplicate labels and getOptionKey, a same-length reorder leaves the highlight on the old index: the right row looks highlighted, but Enter selects the other option;
  • with a custom isOptionEqualToValue, a refetch that changes the highlighted option's label drops the highlight.

This PR keeps a highlight the user set (keyboard, mouse or touch) on the same option at its new index. "Same option" is decided in this order:

  1. equal getOptionKey, when it is provided;
  2. otherwise, when a custom isOptionEqualToValue or getOptionValue is passed, the existing isOptionEqualToValue(option, getOptionValue(other)) from useOptionValue;
  3. otherwise equal labels, as today.

Unchanged behaviour:

Implementation notes:

  • syncHighlightedIndex keeps its dependency list, so it still runs at the same times; its body now reads the latest render through useEventCallback.
  • The sync effect also runs when a new options array arrives, so a remounted row (a label-keyed <li>) gets the focused class back.
  • The JSDoc of getOptionKey, isOptionEqualToValue and getOptionValue documents the rule. PropTypes and the API docs are regenerated.

Tests added under highlight synchronisation: duplicate labels with getOptionKey, a label change with isOptionEqualToValue (including two refetches in a row), getOptionValue, a user highlight with autoHighlight, a mouse highlight, disableCloseOnSelect, and the reset when the option is removed. Six of the seven fail on master; the reset test passes on master and guards existing behaviour.

@code-infra-dashboard

code-infra-dashboard Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

Deploy preview

https://deploy-preview-49864--material-ui.netlify.app/
QR code for https://deploy-preview-49864--material-ui.netlify.app/

Bundle size

Bundle Parsed size Gzip size
@mui/material 🔺+157B(+0.03%) 🔺+90B(+0.06%)
@mui/lab 0B(0.00%) 0B(0.00%)
@mui/private-theming 0B(0.00%) 0B(0.00%)
@mui/system 0B(0.00%) 0B(0.00%)
@mui/utils 0B(0.00%) 0B(0.00%)

Details of bundle changes

Performance

No regressions in 32 benchmarks · precise to ±8.3% (±16.8% at worst) · ran 4m 01s · details


Check out the code infra dashboard for more information about this PR.

@zannager zannager added the scope: autocomplete Changes related to the autocomplete. This includes ComboBox. label Nov 25, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

scope: autocomplete Changes related to the autocomplete. This includes ComboBox.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[autocomplete] After options change, Enter can select a different option than the highlighted one

2 participants