-
-
Notifications
You must be signed in to change notification settings - Fork 32.8k
[autocomplete] Fix scroll resets to top on option selection when popup is reopened on clicking the input #47248
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
base: master
Are you sure you want to change the base?
Conversation
…is re-opened by clicking th input
Netlify deploy previewhttps://deploy-preview-47248--material-ui.netlify.app/ Bundle size report
|
|
@ZeeshanTamboli Could this be merged as there's no way to work around it? |
@levrik For that, I would need a review from the team. |
|
@siriwatknp @mj12albert Could you have a look? This bug is quite blocking for me. |
|
@ZeeshanTamboli I noticed that in the checkboxes docs demo, if I set In this example in main: https://mui.com/material-ui/react-autocomplete/#multiple-values If I open the popup, the scroll position is automatically adjusted to the initial selected option |
Yes, it is expected. In both the cases (when |
|
Any further review on this? |
Closes #47203
Fix: prevent listbox scroll from resetting to top after selection when reopened via input click
Issue explanation
When
disableCloseOnSelectis enabled in<Autocomplete />, the listbox's scroll position jumps back to the top after selecting an option and reopening the popup by clicking on the input.This does not happen when the popup is closed by clicking outside and then reopened.
Steps to Reproduce
<Autocomplete multiple disableCloseOnSelect />with enough options to make the listbox scrollable.Expected: The listbox should keep its current scroll position.
Actual: The listbox scrolls back to the top.
Why it Happened
The effect below was always re-running
syncHighlightedIndex()whenever the popup was open:When closing via input click, there is an intermediate render with
popupOpen = false, causingfilteredOptions = []to be stored inpreviousProps.filteredOptions.On reopening,
filteredOptionsbecomes non-empty again, sofilteredOptionsChangedistrue.The effect runs during option selection, and
syncHighlightedIndex()resets the highlighted index to-1, which setslistboxNode.scrollTop = 0, causing the scroll jump.When closing by clicking outside, the input blurs and the listbox ref unmounts; reopening triggers
handleListboxRef()to sync the highlighted index after mount instead. In this path,previousProps.filteredOptionsstays filled, sofilteredOptionsChangedisfalse, and the scroll position is preserved.Fix
Run the sync effect on popup open only when
disableCloseOnSelectis not set:This prevents unnecessary synchronization while the popup remains open for multi-select use cases.
Test Added
Added a regression test verifying that:
Before: https://stackblitz.com/edit/h6i4uehm-igrkcoz4?file=src%2FDemo.tsx
After: https://stackblitz.com/edit/jbaa3hry?file=src%2FApp.tsx