Skip to content

Commit

Permalink
[Autocomplete] Fix clearOnEscape + multiple combination (mui#20065)
Browse files Browse the repository at this point in the history
  • Loading branch information
chaudharykiran authored and EsoterikStare committed Mar 30, 2020
1 parent 5b5d6f2 commit 1fd813f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
23 changes: 23 additions & 0 deletions packages/material-ui-lab/src/Autocomplete/Autocomplete.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,29 @@ describe('<Autocomplete />', () => {
});
});

describe('prop: clearOnEscape', () => {
it('should clear on escape', () => {
const handleChange = spy();
render(
<Autocomplete
{...defaultProps}
onChange={handleChange}
clearOnEscape
multiple
value={['one']}
options={['one', 'two']}
renderInput={params => <TextField {...params} autoFocus />}
/>,
);

fireEvent.keyDown(document.activeElement, { key: 'Escape' });
fireEvent.keyDown(document.activeElement, { key: 'Escape' });

expect(handleChange.callCount).to.equal(1);
expect(handleChange.args[0][1]).to.deep.equal([]);
});
});

describe('when popup open', () => {
it('closes the popup if Escape is pressed ', () => {
const handleClose = spy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ export default function useAutocomplete(props) {
// Avoid the Modal to handle the event.
event.stopPropagation();
handleClose(event);
} else if (clearOnEscape && inputValue !== '') {
} else if (clearOnEscape && (inputValue !== '' || (multiple && value.length > 0))) {
// Avoid Opera to exit fullscreen mode.
event.preventDefault();
// Avoid the Modal to handle the event.
Expand Down

0 comments on commit 1fd813f

Please sign in to comment.