Skip to content

Commit

Permalink
fix(autocomplete): don't prevent default enter action if panel is closed
Browse files Browse the repository at this point in the history
Currently the autocomplete will always prevented the default enter key action, even if the panel is closed, which goes against the native behavior.

Fixes angular#5976.
  • Loading branch information
crisbeto committed Jul 23, 2017
1 parent 374aaff commit 6a7dd17
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/lib/autocomplete/autocomplete-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,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 {
Expand Down
19 changes: 16 additions & 3 deletions src/lib/autocomplete/autocomplete.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -688,15 +688,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);
Expand Down

0 comments on commit 6a7dd17

Please sign in to comment.