Skip to content
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

[Autocomplete] Fix Enter key clearing selected option #18229

Merged
merged 2 commits into from
Nov 7, 2019
Merged
Show file tree
Hide file tree
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
45 changes: 44 additions & 1 deletion packages/material-ui-lab/src/Autocomplete/Autocomplete.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ describe('<Autocomplete />', () => {
<Autocomplete
freeSolo
onChange={handleChange}
options={[{ name: 'test' }, { name: 'foo' }]}
options={[{ name: 'one' }, { name: 'two ' }]}
getOptionLabel={option => option.name}
renderInput={params => <TextField {...params} />}
/>,
Expand All @@ -437,4 +437,47 @@ describe('<Autocomplete />', () => {
);
});
});

describe('enter', () => {
it('select a single value when enter is pressed', () => {
const handleChange = spy();
const { container } = render(
<Autocomplete
onChange={handleChange}
options={['one', 'two ']}
renderInput={params => <TextField {...params} />}
/>,
);
const input = container.querySelector('input');
fireEvent.keyDown(input, { key: 'ArrowDown' });
fireEvent.keyDown(input, { key: 'ArrowDown' });
fireEvent.keyDown(input, { key: 'Enter' });
expect(handleChange.callCount).to.equal(1);
expect(handleChange.args[0][1]).to.equal('one');
fireEvent.keyDown(input, { key: 'Enter' });
expect(handleChange.callCount).to.equal(1);
});

it('select multiple value when enter is pressed', () => {
const handleChange = spy();
const options = [{ name: 'one' }, { name: 'two ' }];
const { container } = render(
<Autocomplete
multiple
onChange={handleChange}
options={options}
getOptionLabel={option => option.name}
renderInput={params => <TextField {...params} />}
/>,
);
const input = container.querySelector('input');
fireEvent.keyDown(input, { key: 'ArrowDown' });
fireEvent.keyDown(input, { key: 'ArrowDown' });
fireEvent.keyDown(input, { key: 'Enter' });
expect(handleChange.callCount).to.equal(1);
expect(handleChange.args[0][1]).to.deep.equal([options[0]]);
fireEvent.keyDown(input, { key: 'Enter' });
expect(handleChange.callCount).to.equal(1);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ export default function useAutocomplete(props) {
handleFocusTag(event, 'next');
break;
case 'Enter':
if (highlightedIndexRef.current !== -1) {
if (highlightedIndexRef.current !== -1 && popupOpen) {
// We don't want to validate the form.
event.preventDefault();
selectNewValue(event, filteredOptions[highlightedIndexRef.current]);
Expand Down