mirror of
https://github.com/angular/angular.git
synced 2026-09-14 13:54:52 +08:00
fix(autocomplete): handle escape key (#4703)
* Closes the autocomplete panel when pressing escape. * Switches the autocomplete unit tests to using the common utility for creating fake keyboard events.
This commit is contained in:
committed by
tinayuangao
parent
eadccc2f76
commit
9e2a66cbc2
@@ -19,7 +19,7 @@ import {PositionStrategy} from '../core/overlay/position/position-strategy';
|
||||
import {ConnectedPositionStrategy} from '../core/overlay/position/connected-position-strategy';
|
||||
import {Observable} from 'rxjs/Observable';
|
||||
import {MdOptionSelectionChange, MdOption} from '../core/option/option';
|
||||
import {ENTER, UP_ARROW, DOWN_ARROW} from '../core/keyboard/keycodes';
|
||||
import {ENTER, UP_ARROW, DOWN_ARROW, ESCAPE} from '../core/keyboard/keycodes';
|
||||
import {Dir} from '../core/rtl/dir';
|
||||
import {MdInputContainer} from '../input/input-container';
|
||||
import {ScrollDispatcher} from '../core/overlay/scroll/scroll-dispatcher';
|
||||
@@ -231,7 +231,9 @@ export class MdAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
|
||||
}
|
||||
|
||||
_handleKeydown(event: KeyboardEvent): void {
|
||||
if (this.activeOption && event.keyCode === ENTER) {
|
||||
if (event.keyCode === ESCAPE && this.panelOpen) {
|
||||
this.closePanel();
|
||||
} else if (this.activeOption && event.keyCode === ENTER) {
|
||||
this.activeOption._selectViaInteraction();
|
||||
event.preventDefault();
|
||||
} else {
|
||||
|
||||
@@ -16,7 +16,7 @@ import {MdInputModule} from '../input/index';
|
||||
import {Dir, LayoutDirection} from '../core/rtl/dir';
|
||||
import {FormControl, FormsModule, ReactiveFormsModule} from '@angular/forms';
|
||||
import {Subscription} from 'rxjs/Subscription';
|
||||
import {ENTER, DOWN_ARROW, SPACE, UP_ARROW} from '../core/keyboard/keycodes';
|
||||
import {ENTER, DOWN_ARROW, SPACE, UP_ARROW, ESCAPE} from '../core/keyboard/keycodes';
|
||||
import {MdOption} from '../core/option/option';
|
||||
import {MdAutocomplete} from './autocomplete';
|
||||
import {MdInputContainer} from '../input/input-container';
|
||||
@@ -758,6 +758,23 @@ describe('MdAutocomplete', () => {
|
||||
expect(scrollContainer.scrollTop).toEqual(272, `Expected panel to reveal last option.`);
|
||||
}));
|
||||
|
||||
it('should close the panel when pressing escape', async(() => {
|
||||
const trigger = fixture.componentInstance.trigger;
|
||||
const escapeEvent = createKeyboardEvent('keydown', ESCAPE);
|
||||
|
||||
input.focus();
|
||||
|
||||
fixture.whenStable().then(() => {
|
||||
expect(document.activeElement).toBe(input, 'Expected input to be focused.');
|
||||
expect(trigger.panelOpen).toBe(true, 'Expected panel to be open.');
|
||||
|
||||
trigger._handleKeydown(escapeEvent);
|
||||
|
||||
expect(document.activeElement).toBe(input, 'Expected input to continue to be focused.');
|
||||
expect(trigger.panelOpen).toBe(false, 'Expected panel to be closed.');
|
||||
});
|
||||
}));
|
||||
|
||||
});
|
||||
|
||||
describe('aria', () => {
|
||||
|
||||
Reference in New Issue
Block a user