Skip to content

Commit e84b993

Browse files
crisbetotinayuangao
authored andcommitted
fix(autocomplete): escape key inconsistency on IE (#9777)
Brings the autocomplete escape key behavior on IE in line with other browsers. Currently pressing escape on IE will cause it to revert the display value to the one it had on focus, however it won't dispatch any events and the model value will be out of sync. None of the other browsers (event Edge) have this behavior.
1 parent afe1fa4 commit e84b993

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/lib/autocomplete/autocomplete-trigger.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,14 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
277277
_handleKeydown(event: KeyboardEvent): void {
278278
const keyCode = event.keyCode;
279279

280+
// Prevent the default action on all escape key presses. This is here primarily to bring IE
281+
// in line with other browsers. By default, pressing escape on IE will cause it to revert
282+
// the input value to the one that it had on focus, however it won't dispatch any events
283+
// which means that the model value will be out of sync with the view.
284+
if (keyCode === ESCAPE) {
285+
event.preventDefault();
286+
}
287+
280288
// Close when pressing ESCAPE or ALT + UP_ARROW, based on the a11y guidelines.
281289
// See: https://www.w3.org/TR/wai-aria-practices-1.1/#textbox-keyboard-interaction
282290
if (this.panelOpen && (keyCode === ESCAPE || (keyCode === UP_ARROW && event.altKey))) {

src/lib/autocomplete/autocomplete.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -946,6 +946,13 @@ describe('MatAutocomplete', () => {
946946
expect(stopPropagationSpy).toHaveBeenCalled();
947947
}));
948948

949+
it('should prevent the default action when pressing escape', fakeAsync(() => {
950+
const escapeEvent = dispatchKeyboardEvent(input, 'keydown', ESCAPE);
951+
fixture.detectChanges();
952+
953+
expect(escapeEvent.defaultPrevented).toBe(true);
954+
}));
955+
949956
it('should close the panel when pressing ALT + UP_ARROW', fakeAsync(() => {
950957
const trigger = fixture.componentInstance.trigger;
951958
const upArrowEvent = createKeyboardEvent('keydown', UP_ARROW);

0 commit comments

Comments
 (0)