Skip to content

Commit

Permalink
fix(autocomplete): don't prevent default enter action if panel is clo…
Browse files Browse the repository at this point in the history
…sed (#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.
  • Loading branch information
crisbeto authored and andrewseguin committed Jul 27, 2017
1 parent 63505dc commit fdded66
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
Expand Up @@ -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 {
Expand Down
19 changes: 16 additions & 3 deletions src/lib/autocomplete/autocomplete.spec.ts
Expand Up @@ -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);
Expand Down

0 comments on commit fdded66

Please sign in to comment.