Skip to content

[autocomplete][multiple] Fixed Active option loses visual highlight after reopening the popup, but Enter still removes it #48177#48241

Open
taralshah09 wants to merge 5 commits intomui:masterfrom
taralshah09:fix/autocomplete-visual-highlight-reopen
Open

[autocomplete][multiple] Fixed Active option loses visual highlight after reopening the popup, but Enter still removes it #48177#48241
taralshah09 wants to merge 5 commits intomui:masterfrom
taralshah09:fix/autocomplete-visual-highlight-reopen

Conversation

@taralshah09
Copy link
Copy Markdown

@taralshah09 taralshah09 commented Apr 8, 2026

Fixes #48177

Problem

When reopening the popup in multiple-values mode, the internal highlighted option reference was preserved but the visual CSS class (Mui-focused) was not applied. This created a state mismatch where:

  • No option appeared visually highlighted
  • Pressing Enter still acted on the internally highlighted option
  • This could unexpectedly toggle off a previously selected option without visual indication

Root Cause

The syncHighlightedIndex() function in useAutocomplete.js was updating only the internal ref (highlightedIndexRef.current) when a previously highlighted option was found, but returned early without calling setHighlightedIndex(). The setHighlightedIndex() function is responsible for applying the visual CSS class and synchronizing the DOM.

Solution

Changed the early return path to call setHighlightedIndex() instead of directly updating the ref, ensuring both internal state and visual styling are synchronized when restoring a previously highlighted option on popup reopen.

Manual Testing Steps

Test Scenario: Multiple values mode popup reopen highlight restoration

  1. Open the Autocomplete multiple values demo
  2. Click the input to open the popup
  3. Press ArrowDown to highlight an option (e.g., "Two") — verify it's visually highlighted
  4. Press Escape to close the popup
  5. Press ArrowDown again to reopen the popup
  6. Expected: The previously highlighted option ("Two") should be visually highlighted with the Mui-focused class
  7. Press Enter
  8. Expected: The option is added to the selection (no option is removed)
  9. Verify: The visual highlight matches the option that will be acted upon by keyboard input

Verification

  • Visual highlight is restored on popup reopen in multiple mode
  • Enter key behavior matches the visually highlighted option
  • No unintended option deselection or toggle
  • Existing highlight tests pass
bandicam.2026-04-09.00-39-54-325.mp4

@mui-bot
Copy link
Copy Markdown

mui-bot commented Apr 8, 2026

Netlify deploy preview

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

Bundle size report

Bundle Parsed size Gzip size
@mui/material 🔺+55B(+0.01%) 🔺+4B(0.00%)
@mui/lab 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

Generated by 🚫 dangerJS against d518378

When reopening the popup in multiple-values mode, the internal highlighted
option reference was restored but the visual CSS class was not applied,
causing a state mismatch where Enter would act on an invisible highlight.

This fix ensures that both cases where the highlight is preserved apply the
visual CSS class:
1. When a previously highlighted option still exists in the filtered list
2. When the current highlight is a selected option (in multiple mode)

Both calls use 'mouse' as the reason to apply visual styling without
triggering automatic scroll logic.
@taralshah09 taralshah09 force-pushed the fix/autocomplete-visual-highlight-reopen branch from d14f3d7 to 2bc8e91 Compare April 8, 2026 19:52
taralshah09 and others added 3 commits April 9, 2026 01:45
…le mode

- Fix syncHighlightedIndex to trigger on popupOpen regardless of disableCloseOnSelect
- Add check to prevent restoring previously highlighted option when popup was closed
- Ensures first selected item is highlighted when reopening popup with multiple selections
  revert useEffect condition

  - Fix TypeError on first render: previousProps.filteredOptions
   is undefined
  - Revert useEffect trigger to original condition to prevent
  scroll/highlight resets
@zannager zannager added the scope: autocomplete Changes related to the autocomplete. This includes ComboBox. label Apr 9, 2026
@zannager zannager requested a review from ZeeshanTamboli April 9, 2026 14:07
const previousHighlightedOptionIndex = getPreviousHighlightedOptionIndex();
if (previousHighlightedOptionIndex !== -1) {
highlightedIndexRef.current = previousHighlightedOptionIndex;
setHighlightedIndex({ index: previousHighlightedOptionIndex });
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This breaks scroll preservation

Refer to these comments in the code below:

// Restore the focus to the previous index.
// Ignore filteredOptions (and options, isOptionEqualToValue, getOptionLabel) not to break the scroll position


if (
highlightedIndexRef.current !== -1 &&
previousProps.filteredOptions?.length > 0 &&
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What does this do?

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][multiple] Active option loses visual highlight after reopening the popup, but Enter still removes it

4 participants