[autocomplete] Improve highlight tracking and selection state#48219
[autocomplete] Improve highlight tracking and selection state#48219mj12albert merged 10 commits intomui:masterfrom
Conversation
Netlify deploy previewhttps://deploy-preview-48219--material-ui.netlify.app/ Bundle size report
|
| 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 | ||
| ) { |
c4c814d to
587749d
Compare
587749d to
b04c1f0
Compare
ZeeshanTamboli
left a comment
There was a problem hiding this comment.
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 thefreeSolomode, the typed value will be the input value if the Autocomplete loses focus.
ZeeshanTamboli
left a comment
There was a problem hiding this comment.
Left two comments.
Looks good to me. Great fixes!
| const highlightReasonRef = React.useRef(null); | ||
|
|
||
| const touchScrolledRef = React.useRef(false); | ||
| const isTouch = React.useRef(false); |
There was a problem hiding this comment.
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 && |
There was a problem hiding this comment.
So are we not anymore selecting the highlighted item when we blur by mouse? It still works when blurring by Tab.
There was a problem hiding this comment.
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
d734437 to
1518e59
Compare
1518e59 to
8a511b9
Compare
Bundle size
Deploy previewhttps://deploy-preview-48219--material-ui.netlify.app/ Check out the code infra dashboard for more information about this PR. |
|
Cherry-pick PRs will be created targeting branches: v7.x |
Fixes #20602
Fixes #27137
Fixes #46718
Adds
highlightReasonRefinternally 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.
autoSelectshould not select mouse-hovered options on blur: #20602autoSelectvia the code editorExpected: No option selected — input stays empty
Current: The hovered option IS selected — its value appears in the input after blur
Variation with
autoHighlight:autoSelectandautoHighlightExpected: 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.
freeSoloEnter should prefer typed text over auto-highlighted match #27137Expected: "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
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.