Skip to content

Commit

Permalink
[Autocomplete] Fix stuck with open popup (#19794)
Browse files Browse the repository at this point in the history
  • Loading branch information
hasanozacar authored Feb 22, 2020
1 parent eec5b60 commit deb31c7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
28 changes: 22 additions & 6 deletions packages/material-ui-lab/src/Autocomplete/Autocomplete.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -913,6 +913,25 @@ describe('<Autocomplete />', () => {
expect(textbox.selectionStart).to.equal(0);
expect(textbox.selectionEnd).to.equal(3);
});

it('should focus the input when clicking on the open action', () => {
const { getByRole, queryByTitle } = render(
<Autocomplete
{...defaultProps}
value="one"
options={['one', 'two']}
renderInput={params => <TextField {...params} />}
/>,
);

const textbox = getByRole('textbox');
fireEvent.click(textbox);
expect(textbox).to.have.focus;
textbox.blur();

fireEvent.click(queryByTitle('Open'));
expect(textbox).to.have.focus;
});
});

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

const opener = queryByTitle('Open');
fireEvent.click(opener);
fireEvent.click(queryByTitle('Open'));
expect(textbox).to.have.focus;
firstOption = getByRole('option');
fireEvent.touchStart(firstOption);
Expand All @@ -1166,8 +1184,7 @@ describe('<Autocomplete />', () => {
fireEvent.click(firstOption);
expect(textbox).to.have.focus;

const opener = queryByTitle('Open');
fireEvent.click(opener);
fireEvent.click(queryByTitle('Open'));
firstOption = getByRole('option');
fireEvent.touchStart(firstOption);
fireEvent.click(firstOption);
Expand All @@ -1191,8 +1208,7 @@ describe('<Autocomplete />', () => {
fireEvent.click(firstOption);
expect(textbox).to.have.focus;

const opener = queryByTitle('Open');
fireEvent.click(opener);
fireEvent.click(queryByTitle('Open'));
firstOption = getByRole('option');
fireEvent.click(firstOption);
expect(textbox).to.not.have.focus;
Expand Down
16 changes: 8 additions & 8 deletions packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable no-constant-condition */
import React from 'react';
import PropTypes from 'prop-types';
import { setRef, useEventCallback, useControlled } from '@material-ui/core/utils';
import { setRef, useEventCallback, useControlled, ownerDocument } from '@material-ui/core/utils';

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

// Focus the input when first interacting with the combobox
// Focus the input when interacting with the combobox
const handleClick = () => {
inputRef.current.focus();

if (
selectOnFocus &&
firstFocus.current &&
inputRef.current.selectionEnd - inputRef.current.selectionStart === 0
) {
inputRef.current.focus();

if (selectOnFocus) {
inputRef.current.select();
}
inputRef.current.select();
}

firstFocus.current = false;
};

const handleInputMouseDown = event => {
if (inputValue === '' && (!disableOpenOnFocus || inputRef.current === document.activeElement)) {
const doc = ownerDocument(inputRef.current);
if (inputValue === '' && (!disableOpenOnFocus || inputRef.current === doc.activeElement)) {
handlePopupIndicator(event);
}
};
Expand Down

0 comments on commit deb31c7

Please sign in to comment.