fix(autocomplete): don't prevent default enter action if panel is closed (#5977)

Currently the autocomplete will always prevented the default enter key action, even if the panel is closed, which goes against the native behavior.

Fixes #5976.
This commit is contained in:
Kristiyan Kostadinov
2017-07-27 19:44:23 +03:00
committed by Andrew Seguin
parent 63505dc371
commit fdded66b5a
2 changed files with 17 additions and 4 deletions
+1 -1
View File
@@ -298,7 +298,7 @@ export class MdAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
_handleKeydown(event: KeyboardEvent): void {
if (event.keyCode === ESCAPE && this.panelOpen) {
this.closePanel();
} else if (this.activeOption && event.keyCode === ENTER) {
} else if (this.activeOption && event.keyCode === ENTER && this.panelOpen) {
this.activeOption._selectViaInteraction();
event.preventDefault();
} else {
+16 -3
View File
@@ -719,15 +719,28 @@ describe('MdAutocomplete', () => {
fixture.componentInstance.trigger._handleKeydown(DOWN_ARROW_EVENT);
fixture.whenStable().then(() => {
spyOn(ENTER_EVENT, 'preventDefault');
fixture.componentInstance.trigger._handleKeydown(ENTER_EVENT);
expect(ENTER_EVENT.preventDefault).toHaveBeenCalled();
expect(ENTER_EVENT.defaultPrevented)
.toBe(true, 'Expected the default action to have been prevented.');
});
});
}));
it('should not prevent the default enter action for a closed panel after a user interaction',
fakeAsync(() => {
tick();
fixture.componentInstance.trigger._handleKeydown(UP_ARROW_EVENT);
tick();
fixture.detectChanges();
fixture.componentInstance.trigger.closePanel();
fixture.detectChanges();
fixture.componentInstance.trigger._handleKeydown(ENTER_EVENT);
expect(ENTER_EVENT.defaultPrevented).toBe(false, 'Default action should not be prevented.');
}));
it('should fill the text field, not select an option, when SPACE is entered', async(() => {
fixture.whenStable().then(() => {
typeInElement('New', input);