Skip to content

[autocomplete] Improve highlight tracking and selection state#48219

Merged
mj12albert merged 10 commits intomui:masterfrom
mj12albert:autocomplete/highlight-and-selection-1of5
Apr 17, 2026
Merged

[autocomplete] Improve highlight tracking and selection state#48219
mj12albert merged 10 commits intomui:masterfrom
mj12albert:autocomplete/highlight-and-selection-1of5

Conversation

@mj12albert
Copy link
Copy Markdown
Member

@mj12albert mj12albert commented Apr 7, 2026

Fixes #20602
Fixes #27137
Fixes #46718

Adds highlightReasonRef internally to better handle edge cases created by the item highlight and other interactions.

Codex suggests using pointer events as a better touch/mouse detection strategy to what is currently being used, which is more robust for hybrid devices (iPad with a bluetooth keyboard) but we could do that separately.

Manual testing steps:

1. autoSelect should not select mouse-hovered options on blur: #20602

  1. Combo box demo → add autoSelect via the code editor
  2. Click the input to open the popup
  3. Hover your mouse over an option (don’t click)
  4. Click outside the component to blur

Expected: No option selected — input stays empty

Current: The hovered option IS selected — its value appears in the input after blur

Variation with autoHighlight:

  1. Combo box demo → add both autoSelect and autoHighlight
  2. Click the input to open the popup, observe first option is auto-highlighted
  3. Hover your mouse over that same first option
  4. Click outside to blur

Expected: No option selected (the hover overrides the programmatic highlight reason)

Current: The first option is selected on blur, even though the user only hovered over it.


2. freeSolo Enter should prefer typed text over auto-highlighted match #27137

  1. Free solo demo
  2. Type "The Godfather" and press Enter to select it
  3. Click back into the input
  4. Press Backspace 5 times (input becomes "The Godf" — still matches "The Godfather" in the dropdown)
  5. Press Enter

Expected: "The Godf" is accepted as free text, NOT "The Godfather"

Current (master): "The Godfather" is re-selected (the full original value), not "The Godf". The programmatic highlight from syncHighlightedIndex (which matches the old value) wins over the typed text.


3. Touch-scroll + Enter selects wrong item #46718

This requires a touch device + software keyboard

  1. Combo box demo
  2. Touch the textbox to open the popup
  3. Touch an option to start a drag, then drag to scroll the list (don't lift or tap, it must be dragged so the browser treats it as a scroll gesture)
  4. When the list scrolls a bit, release finger, press Enter ("Return" on iPhone keyboard)

Expected: Popup closes with no option selected

Current (master): The option that was under the initial touch point (before scrolling) is selected. The stale highlight from touchstart is persisted through the scroll, and Enter commits it.

@mj12albert mj12albert added type: bug It doesn't behave as expected. scope: autocomplete Changes related to the autocomplete. This includes ComboBox. labels Apr 7, 2026
@mui-bot
Copy link
Copy Markdown

mui-bot commented Apr 7, 2026

Netlify deploy preview

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

Bundle size report

Bundle Parsed size Gzip size
@mui/material 🔺+435B(+0.09%) 🔺+136B(+0.09%)
@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 d1087f6

Comment on lines +978 to +985
if (
highlightedIndexRef.current !== -1 &&
popupOpen &&
shouldSelectHighlighted &&
// After a touch-scroll the highlight is stale (the user scrolled
// past it), so skip selection until the next deliberate interaction.
!touchScrolledRef.current
) {
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This if now accounts for both #27137 and #46718

@mj12albert mj12albert force-pushed the autocomplete/highlight-and-selection-1of5 branch 2 times, most recently from c4c814d to 587749d Compare April 7, 2026 23:24
@mj12albert mj12albert force-pushed the autocomplete/highlight-and-selection-1of5 branch from 587749d to b04c1f0 Compare April 10, 2026 16:28
@mj12albert mj12albert marked this pull request as ready for review April 10, 2026 16:28
Comment thread packages/mui-material/src/Autocomplete/Autocomplete.test.js
Comment thread packages/mui-material/src/useAutocomplete/useAutocomplete.js
Comment thread packages/mui-material/src/useAutocomplete/useAutocomplete.js
Comment thread packages/mui-material/src/useAutocomplete/useAutocomplete.js
Comment thread packages/mui-material/src/useAutocomplete/useAutocomplete.js
Copy link
Copy Markdown
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.

I think we should remove the text "without highlighting an option" from the autoSelect JSdoc description: https://mui.com/material-ui/api/autocomplete/#autocomplete-prop-autoSelect.

So now it should be:

If true, the selected option becomes the value of the input when the Autocomplete loses focus unless the user chooses a different option or changes the character string in the input.
When using the freeSolo mode, the typed value will be the input value if the Autocomplete loses focus.

Comment thread packages/mui-material/src/useAutocomplete/useAutocomplete.js Outdated
Copy link
Copy Markdown
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.

Left two comments.

Looks good to me. Great fixes!

const highlightReasonRef = React.useRef(null);

const touchScrolledRef = React.useRef(false);
const isTouch = React.useRef(false);
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.

add Ref to the variable, for consistency

// Mouse hover and touch should not trigger selection — the user may have
// moved the pointer over an option without intending to commit to it.
if (
autoSelect &&
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.

So are we not anymore selecting the highlighted item when we blur by mouse? It still works when blurring by Tab.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It's not the blur itself, but what caused the highlight.

If the highlight is by hover or touch, blur (Tab or outside click) will not autoSelect.

Otherwise if the highlight is from keyboard interaction or was set automatically (autoHighlight or internal syncing), blur will autoSelect it

@mj12albert mj12albert force-pushed the autocomplete/highlight-and-selection-1of5 branch from d734437 to 1518e59 Compare April 16, 2026 18:26
@mj12albert mj12albert force-pushed the autocomplete/highlight-and-selection-1of5 branch from 1518e59 to 8a511b9 Compare April 16, 2026 18:57
@mj12albert mj12albert added the needs cherry-pick The PR should be cherry-picked to master after merge. label Apr 17, 2026
@mj12albert mj12albert enabled auto-merge (squash) April 17, 2026 12:27
@code-infra-dashboard
Copy link
Copy Markdown

Bundle size

Bundle Parsed size Gzip size
@mui/material 🔺+439B(+0.09%) 🔺+135B(+0.09%)
@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

Deploy preview

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


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

@mj12albert mj12albert merged commit 47710f7 into mui:master Apr 17, 2026
23 checks passed
@github-actions
Copy link
Copy Markdown

Cherry-pick PRs will be created targeting branches: v7.x

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

Labels

needs cherry-pick The PR should be cherry-picked to master after merge. scope: autocomplete Changes related to the autocomplete. This includes ComboBox. type: bug It doesn't behave as expected. v7.x

Projects

None yet

4 participants