fix(autocomplete): do not trigger submit on ENTER (#3727)

* fix(autocomplete): do not trigger submit on ENTER

When an option is selected with the ENTER key, it should not submit the form and prevent the default input behavior

Fixes #3159

* test(autocomplete): removing extra detectChanges
This commit is contained in:
Jeferson Estevo
2017-03-29 18:31:28 -03:00
committed by tinayuangao
parent 6d6c12dc16
commit bedf5a1e42
2 changed files with 15 additions and 0 deletions
@@ -205,6 +205,7 @@ export class MdAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
_handleKeydown(event: KeyboardEvent): void {
if (this.activeOption && event.keyCode === ENTER) {
this.activeOption._selectViaInteraction();
event.preventDefault();
} else {
this.autocomplete._keyManager.onKeydown(event);
if (event.keyCode === UP_ARROW || event.keyCode === DOWN_ARROW) {
+14
View File
@@ -631,6 +631,20 @@ describe('MdAutocomplete', () => {
});
}));
it('should prevent the default enter key action', async(() => {
fixture.whenStable().then(() => {
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();
});
});
}));
it('should fill the text field, not select an option, when SPACE is entered', async(() => {
fixture.whenStable().then(() => {
typeInElement('New', input);