Skip to content

Commit 310a76e

Browse files
[Autocomplete] Sync highlighted index when popup is opened (#46894)
1 parent 71a2b15 commit 310a76e

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

packages/mui-material/src/Autocomplete/Autocomplete.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,28 @@ describe('<Autocomplete />', () => {
588588
expect(handleChange.callCount).to.equal(1);
589589
expect(handleChange.args[0][1]).to.deep.equal(['a']);
590590
});
591+
592+
it('should add new value when autoSelect & freeSolo & autoHighlight on blur', () => {
593+
const handleChange = spy();
594+
render(
595+
<Autocomplete
596+
autoSelect
597+
freeSolo
598+
autoHighlight
599+
onChange={handleChange}
600+
options={[]}
601+
renderInput={(params) => <TextField {...params} autoFocus />}
602+
/>,
603+
);
604+
605+
fireEvent.change(document.activeElement, { target: { value: 'a' } });
606+
act(() => {
607+
document.activeElement.blur();
608+
});
609+
610+
expect(handleChange.callCount).to.equal(1);
611+
expect(handleChange.args[0][1]).to.equal('a');
612+
});
591613
});
592614

593615
describe('prop: multiple', () => {

packages/mui-material/src/useAutocomplete/useAutocomplete.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -615,10 +615,10 @@ function useAutocomplete(props) {
615615
}
616616

617617
React.useEffect(() => {
618-
if (filteredOptionsChanged) {
618+
if (filteredOptionsChanged || popupOpen) {
619619
syncHighlightedIndex();
620620
}
621-
}, [syncHighlightedIndex, filteredOptionsChanged]);
621+
}, [syncHighlightedIndex, filteredOptionsChanged, popupOpen]);
622622

623623
const handleOpen = (event) => {
624624
if (open) {

0 commit comments

Comments
 (0)