Skip to content

Commit deb31c7

Browse files
authored
[Autocomplete] Fix stuck with open popup (#19794)
1 parent eec5b60 commit deb31c7

File tree

2 files changed

+30
-14
lines changed

2 files changed

+30
-14
lines changed

packages/material-ui-lab/src/Autocomplete/Autocomplete.test.js

+22-6
Original file line numberDiff line numberDiff line change
@@ -913,6 +913,25 @@ describe('<Autocomplete />', () => {
913913
expect(textbox.selectionStart).to.equal(0);
914914
expect(textbox.selectionEnd).to.equal(3);
915915
});
916+
917+
it('should focus the input when clicking on the open action', () => {
918+
const { getByRole, queryByTitle } = render(
919+
<Autocomplete
920+
{...defaultProps}
921+
value="one"
922+
options={['one', 'two']}
923+
renderInput={params => <TextField {...params} />}
924+
/>,
925+
);
926+
927+
const textbox = getByRole('textbox');
928+
fireEvent.click(textbox);
929+
expect(textbox).to.have.focus;
930+
textbox.blur();
931+
932+
fireEvent.click(queryByTitle('Open'));
933+
expect(textbox).to.have.focus;
934+
});
916935
});
917936

918937
describe('controlled', () => {
@@ -1141,8 +1160,7 @@ describe('<Autocomplete />', () => {
11411160
fireEvent.click(firstOption);
11421161
expect(textbox).to.not.have.focus;
11431162

1144-
const opener = queryByTitle('Open');
1145-
fireEvent.click(opener);
1163+
fireEvent.click(queryByTitle('Open'));
11461164
expect(textbox).to.have.focus;
11471165
firstOption = getByRole('option');
11481166
fireEvent.touchStart(firstOption);
@@ -1166,8 +1184,7 @@ describe('<Autocomplete />', () => {
11661184
fireEvent.click(firstOption);
11671185
expect(textbox).to.have.focus;
11681186

1169-
const opener = queryByTitle('Open');
1170-
fireEvent.click(opener);
1187+
fireEvent.click(queryByTitle('Open'));
11711188
firstOption = getByRole('option');
11721189
fireEvent.touchStart(firstOption);
11731190
fireEvent.click(firstOption);
@@ -1191,8 +1208,7 @@ describe('<Autocomplete />', () => {
11911208
fireEvent.click(firstOption);
11921209
expect(textbox).to.have.focus;
11931210

1194-
const opener = queryByTitle('Open');
1195-
fireEvent.click(opener);
1211+
fireEvent.click(queryByTitle('Open'));
11961212
firstOption = getByRole('option');
11971213
fireEvent.click(firstOption);
11981214
expect(textbox).to.not.have.focus;

packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable no-constant-condition */
22
import React from 'react';
33
import PropTypes from 'prop-types';
4-
import { setRef, useEventCallback, useControlled } from '@material-ui/core/utils';
4+
import { setRef, useEventCallback, useControlled, ownerDocument } from '@material-ui/core/utils';
55

66
// https://stackoverflow.com/questions/990904/remove-accents-diacritics-in-a-string-in-javascript
77
// Give up on IE 11 support for this feature
@@ -767,24 +767,24 @@ export default function useAutocomplete(props) {
767767
}
768768
};
769769

770-
// Focus the input when first interacting with the combobox
770+
// Focus the input when interacting with the combobox
771771
const handleClick = () => {
772+
inputRef.current.focus();
773+
772774
if (
775+
selectOnFocus &&
773776
firstFocus.current &&
774777
inputRef.current.selectionEnd - inputRef.current.selectionStart === 0
775778
) {
776-
inputRef.current.focus();
777-
778-
if (selectOnFocus) {
779-
inputRef.current.select();
780-
}
779+
inputRef.current.select();
781780
}
782781

783782
firstFocus.current = false;
784783
};
785784

786785
const handleInputMouseDown = event => {
787-
if (inputValue === '' && (!disableOpenOnFocus || inputRef.current === document.activeElement)) {
786+
const doc = ownerDocument(inputRef.current);
787+
if (inputValue === '' && (!disableOpenOnFocus || inputRef.current === doc.activeElement)) {
788788
handlePopupIndicator(event);
789789
}
790790
};

0 commit comments

Comments
 (0)