Skip to content

Commit

Permalink
fix(autocomplete): restore focus to input when click on option (#4702)
Browse files Browse the repository at this point in the history
Restores focus to the autocomplete input when an option is clicked, instead of dumping it into the body.

Fixes #4645.
  • Loading branch information
crisbeto authored and tinayuangao committed May 22, 2017
1 parent b5b762a commit eecf897
Show file tree
Hide file tree
Showing 2 changed files with 17 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 @@ -339,6 +339,7 @@ export class MdAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
this._clearPreviousSelectedOption(event.source);
this._setTriggerValue(event.source.value);
this._onChange(event.source.value);
this._element.nativeElement.focus();
}

this.closePanel();
Expand Down
16 changes: 16 additions & 0 deletions src/lib/autocomplete/autocomplete.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,22 @@ describe('MdAutocomplete', () => {

});

it('should restore focus to the input when clicking to select a value', async(() => {
fixture.componentInstance.trigger.openPanel();
fixture.detectChanges();

fixture.whenStable().then(() => {
const option = overlayContainerElement.querySelector('md-option') as HTMLElement;

// Focus the option manually since the synthetic click may not do it.
option.focus();
option.click();
fixture.detectChanges();

expect(document.activeElement).toBe(input, 'Expected focus to be restored to the input.');
});
}));

});

describe('Fallback positions', () => {
Expand Down

0 comments on commit eecf897

Please sign in to comment.