Skip to content

[Autocomplete] Fix stuck with open popup #19794

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 22, 2020
Merged
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
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
@@ -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', () => {
@@ -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);
@@ -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);
@@ -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;
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
@@ -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);
}
};