Skip to content

Commit

Permalink
[Autocomplete] Fixed autocomplete's existing option selection (#37012)
Browse files Browse the repository at this point in the history
  • Loading branch information
bencevoros committed Jun 21, 2023
1 parent cc045c6 commit fe69429
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
6 changes: 4 additions & 2 deletions packages/mui-base/src/useAutocomplete/useAutocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ export default function useAutocomplete(props) {
const previousProps = usePreviousProps({
filteredOptions,
value,
inputValue,
});

React.useEffect(() => {
Expand Down Expand Up @@ -478,6 +479,7 @@ export default function useAutocomplete(props) {
highlightedIndexRef.current !== -1 &&
previousProps.filteredOptions &&
previousProps.filteredOptions.length !== filteredOptions.length &&
previousProps.inputValue === inputValue &&
(multiple
? value.length === previousProps.value.length &&
previousProps.value.every((val, i) => getOptionLabel(value[i]) === getOptionLabel(val))
Expand All @@ -503,8 +505,8 @@ export default function useAutocomplete(props) {
return;
}

// Check if the previously highlighted option still exists in the updated filtered options list and if the value hasn't changed
// If it exists and the value hasn't changed, return, otherwise continue execution
// Check if the previously highlighted option still exists in the updated filtered options list and if the value and inputValue haven't changed
// If it exists and the value and the inputValue haven't changed, return, otherwise continue execution
if (checkHighlightedOptionExists()) {
return;
}
Expand Down
24 changes: 24 additions & 0 deletions packages/mui-joy/src/Autocomplete/Autocomplete.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1428,6 +1428,30 @@ describe('Joy <Autocomplete />', () => {
checkHighlightIs(listbox, null);
});

it('should reset the highlight when the input changed', () => {
const filterOptions = createFilterOptions({});
const { getByRole } = render(
<Autocomplete
open
autoHighlight
autoFocus
options={['one', 'two', 'three']}
filterOptions={filterOptions}
/>,
);
const textbox = getByRole('combobox');
const listbox = getByRole('listbox');

fireEvent.change(textbox, { target: { value: 't' } });
checkHighlightIs(listbox, 'two');

fireEvent.change(textbox, { target: { value: '' } });
checkHighlightIs(listbox, 'one');

fireEvent.keyDown(textbox, { key: 'Enter' });
expect(textbox).has.value('one');
});

it('should not select undefined', () => {
const handleChange = spy();
const { getByRole } = render(
Expand Down
25 changes: 25 additions & 0 deletions packages/mui-material/src/Autocomplete/Autocomplete.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1698,6 +1698,31 @@ describe('<Autocomplete />', () => {
checkHighlightIs(listbox, 'two');
});

it('should reset the highlight when the input changed', () => {
const filterOptions = createFilterOptions({});
render(
<Autocomplete
open
autoFocus
autoHighlight
options={['one', 'two', 'three']}
renderInput={(params) => <TextField {...params} autoFocus />}
filterOptions={filterOptions}
/>,
);
const textbox = screen.getByRole('combobox');
const listbox = screen.getByRole('listbox');

fireEvent.change(textbox, { target: { value: 't' } });
checkHighlightIs(listbox, 'two');

fireEvent.change(textbox, { target: { value: '' } });
checkHighlightIs(listbox, 'one');

fireEvent.keyDown(textbox, { key: 'Enter' });
expect(textbox).has.value('one');
});

it("should reset the highlight when previously highlighted option doesn't exists in new options", () => {
const { setProps } = render(
<Autocomplete
Expand Down

0 comments on commit fe69429

Please sign in to comment.