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 clearOnEscape + multiple combination #20065

Merged
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
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 @@ -649,7 +649,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