Skip to content

Commit

Permalink
fix(autocomplete): do not trigger submit on ENTER (#3727)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
jefersonestevo authored and tinayuangao committed Mar 29, 2017
1 parent 6d6c12d commit bedf5a1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib/autocomplete/autocomplete-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
14 changes: 14 additions & 0 deletions src/lib/autocomplete/autocomplete.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit bedf5a1

Please sign in to comment.