Skip to content

Commit

Permalink
fix(autocomplete): do not trigger submit on ENTER
Browse files Browse the repository at this point in the history
When an option is selected with the ENTER key, it should not submit the form and prevent the default input behavior

Fixes angular#3159
  • Loading branch information
jefersonestevo committed Mar 22, 2017
1 parent c524438 commit 159a880
Show file tree
Hide file tree
Showing 2 changed files with 19 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 @@ -204,6 +204,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
18 changes: 18 additions & 0 deletions src/lib/autocomplete/autocomplete.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,24 @@ describe('MdAutocomplete', () => {
});
}));

it('should not call the default input behaviour when an option is selected with ENTER',
async(() => {
fixture.whenStable().then(() => {
fixture.componentInstance.trigger._handleKeydown(DOWN_ARROW_EVENT);

fixture.whenStable().then(() => {
fixture.detectChanges();

spyOn(ENTER_EVENT, 'preventDefault');

fixture.componentInstance.trigger._handleKeydown(ENTER_EVENT);
fixture.detectChanges();

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 159a880

Please sign in to comment.