Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions packages/mui-material/src/Autocomplete/Autocomplete.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3752,4 +3752,40 @@ describe('<Autocomplete />', () => {

expect(screen.getByTestId('label')).to.have.attribute('data-shrink', 'false');
});

// https://github.com/mui/material-ui/issues/47203
it('should not scroll the listbox to the top when listbox is scrolled down and one of the end option is clicked', function test() {
if (window.navigator.userAgent.includes('jsdom')) {
this.skip();
}

render(
<Autocomplete
multiple
disableCloseOnSelect
options={['one', 'two', 'three', 'four', 'five']}
renderInput={(params) => <TextField {...params} />}
slotProps={{ listbox: { style: { padding: 0, maxHeight: '100px' } } }}
/>,
);
const textbox = screen.getByRole('combobox');

// open listbox
fireEvent.mouseDown(textbox);

// close listbox
fireEvent.mouseDown(textbox);

// re-open listbox
fireEvent.mouseDown(textbox);

const listbox = screen.getByRole('listbox');
const options = screen.getAllByRole('option');

listbox.scrollBy(0, 180);

fireEvent.click(options[4]);

expect(listbox).not.to.have.property('scrollTop', 0);
});
});
4 changes: 2 additions & 2 deletions packages/mui-material/src/useAutocomplete/useAutocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -615,10 +615,10 @@ function useAutocomplete(props) {
}

React.useEffect(() => {
if (filteredOptionsChanged || popupOpen) {
if (filteredOptionsChanged || (popupOpen && !disableCloseOnSelect)) {
syncHighlightedIndex();
}
}, [syncHighlightedIndex, filteredOptionsChanged, popupOpen]);
}, [syncHighlightedIndex, filteredOptionsChanged, popupOpen, disableCloseOnSelect]);

const handleOpen = (event) => {
if (open) {
Expand Down
Loading