material-ui: [Autocomplete] Adding items to options causes scroll to go to the top of the list (paging)
Duplicates
- I have searched the existing issues
Latest version
- I have tested the latest version
Current behavior 😯
I’m implementing async load with pagination with the Autocomplete. When the listbox is open and the user scroll to the bottom, we load the next page and add the new options. After adding, the position of the scroll on the listbox is reseted and the scroll goes to the top.
Expected behavior 🤔
The scroll position should not change so the user can continue the search for the option
Steps to reproduce 🕹
I’ve used the Select Country example in the docs and tweek to load the countries with paging. I was abe to reproduce the issue without any async request, just loading more items from the fixed array.
https://codesandbox.io/s/confident-kare-5n6i6
Context 🔦
No response
Your environment 🌎
@emotion/styled: 11.6.0
@mui/material: 5.2.4
react: 17.0.2
`npx @mui/envinfo`
System: OS: Windows 10 10.0.19044 Binaries: Node: 16.12.0 - C:\Program Files\nodejs\node.EXE Yarn: 1.22.17 - C:\Program Files\nodejs\yarn.CMD npm: 8.3.0 - C:\Program Files\nodejs\npm.CMD Browsers: Chrome: Not Found Edge: Spartan (44.19041.1266.0), Chromium (96.0.1054.57)
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 16
- Comments: 29 (5 by maintainers)
Hi, as an intermediate solution
observe
role="list-box",useAutocompleteexpects it to berole="listbox"this will also do away with hover and selection styles, to fix that, add the following somewhere globally
Hi, Is there any progress in here?
Thanks @aakash-lohono the problem was solved Actually, to solve this problem we just need to pass
role: "list-box"as to ListboxPropsIndeed this issue didn’t exist on v4.
Looks like the problem is this logic https://github.com/mui-org/material-ui/blob/3735493e98b0f15390642f4b134eff0212c652a6/packages/mui-base/src/AutocompleteUnstyled/useAutocomplete.js#L341 when there’s no selected item.
@MostafaKMilly by changing the role to “list-box”, can you still access to the list elements with the keyboard?. I tried but breaks accesibility for me.
I’ve been trying to get help with this same issue but have received: no response on one ticket, and closure of another ticket with no explanation.
This CSB also illustrates the issue: https://codesandbox.io/s/autocomplete-scroll-bug-39wfv?file=/src/App.js
Hi , everybody! @LuigiMaestrelli , until the bug is fixed, you can try my solution. I hope it gives a result similar to what you expected
https://codesandbox.io/s/clever-noether-5b93i2?file=/Autocomplete.js
yes @glaucoheitor thanks for checking in.yes! I am working on my PR
@Saul-Garcia Yes it breaks accessing options elements using keyboard 😃
Opened a PR #30719 proposing a fix. Please read the PR message provided with it which includes a question I have regarding the demos/docs part of the PR.
@ShivamTyagi12345 Hey how is your PR going ? I am working on this issue as well right now as well. Would love to help and collaborate. For anyone else also looking into this - https://github.com/mui-org/material-ui/blob/3735493e98b0f15390642f4b134eff0212c652a6/packages/mui-base/src/AutocompleteUnstyled/useAutocomplete.js#L373
The above line seems to be responsible for scrolling the list to the top and temporarily commenting it out seems to stop this happening. I don’t know why this is happening however, but I am looking into it rn.
@ShivamTyagi12345 if you need help, I would also like to help with this. Thanks.