Skip to content
Closed
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 @@ -458,6 +458,42 @@ describe('<Autocomplete />', () => {
});
checkHighlightIs(screen.getByRole('listbox'), 'two');
});

it('should restore visual highlight on selected option when reopening popup in multiple mode', () => {
const handleChange = spy();
render(
<Autocomplete
multiple
onChange={handleChange}
options={['one', 'two', 'three']}
defaultValue={['one', 'two']}
disableCloseOnSelect
renderInput={(params) => <TextField {...params} autoFocus />}
/>,
);
const textbox = screen.getByRole('combobox');

// Open popup
fireEvent.keyDown(textbox, { key: 'ArrowDown' });
let listbox = screen.getByRole('listbox');
// With value=['one', 'two'], sync highlights 'one' (the first value item)
checkHighlightIs(listbox, 'one');

// Navigate down to 'two' (also selected)
fireEvent.keyDown(textbox, { key: 'ArrowDown' });
checkHighlightIs(listbox, 'two');

// Close popup by pressing Escape
fireEvent.keyDown(textbox, { key: 'Escape' });
expect(screen.queryByRole('listbox')).to.equal(null);

// Reopen popup - the highlight should be on a selected option
fireEvent.keyDown(textbox, { key: 'ArrowDown' });
listbox = screen.getByRole('listbox');

// Should have visual focus on the first selected item (one) per sync logic
checkHighlightIs(listbox, 'one');
});
});

describe('prop: limitTags', () => {
Expand Down
4 changes: 3 additions & 1 deletion packages/mui-material/src/useAutocomplete/useAutocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,7 @@ function useAutocomplete(props) {

if (
highlightedIndexRef.current !== -1 &&
previousProps.filteredOptions?.length > 0 &&
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.

What does this do?

!areArraysSame({
array1: previousProps.filteredOptions,
array2: filteredOptions,
Expand Down Expand Up @@ -538,7 +539,7 @@ function useAutocomplete(props) {
// If it exists and the value and the inputValue haven't changed, just update its index, otherwise continue execution
const previousHighlightedOptionIndex = getPreviousHighlightedOptionIndex();
if (previousHighlightedOptionIndex !== -1) {
highlightedIndexRef.current = previousHighlightedOptionIndex;
setHighlightedIndex({ index: previousHighlightedOptionIndex });
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.

This breaks scroll preservation

Refer to these comments in the code below:

// Restore the focus to the previous index.
// Ignore filteredOptions (and options, isOptionEqualToValue, getOptionLabel) not to break the scroll position

return;
}

Expand All @@ -564,6 +565,7 @@ function useAutocomplete(props) {
currentOption &&
value.findIndex((val) => isOptionEqualToValue(currentOption, val)) !== -1
) {
setHighlightedIndex({ index: highlightedIndexRef.current });
return;
}

Expand Down
Loading