Skip to content

fix(autocomplete): escape key inconsistency on IE #9777

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
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
8 changes: 8 additions & 0 deletions src/lib/autocomplete/autocomplete-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,14 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
_handleKeydown(event: KeyboardEvent): void {
const keyCode = event.keyCode;

// Prevent the default action on all escape key presses. This is here primarily to bring IE
// in line with other browsers. By default, pressing escape on IE will cause it to revert
// the input value to the one that it had on focus, however it won't dispatch any events
// which means that the model value will be out of sync with the view.
if (keyCode === ESCAPE) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to specifically target IE with this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose, but other browsers don't do anything with it anyway so it wasn't worth changing the constructor signature.

event.preventDefault();
}

// Close when pressing ESCAPE or ALT + UP_ARROW, based on the a11y guidelines.
// See: https://www.w3.org/TR/wai-aria-practices-1.1/#textbox-keyboard-interaction
if (this.panelOpen && (keyCode === ESCAPE || (keyCode === UP_ARROW && event.altKey))) {
Expand Down
7 changes: 7 additions & 0 deletions src/lib/autocomplete/autocomplete.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,13 @@ describe('MatAutocomplete', () => {
expect(stopPropagationSpy).toHaveBeenCalled();
}));

it('should prevent the default action when pressing escape', fakeAsync(() => {
const escapeEvent = dispatchKeyboardEvent(input, 'keydown', ESCAPE);
fixture.detectChanges();

expect(escapeEvent.defaultPrevented).toBe(true);
}));

it('should close the panel when pressing ALT + UP_ARROW', fakeAsync(() => {
const trigger = fixture.componentInstance.trigger;
const upArrowEvent = createKeyboardEvent('keydown', UP_ARROW);
Expand Down