Skip to content

Commit 0ddd7f0

Browse files
committed
fix(autocomplete): restore focus to input when click on option
Restores focus to the autocomplete input when an option is clicked, instead of dumping it into the body. Fixes #4645.
1 parent e2f67f5 commit 0ddd7f0

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/lib/autocomplete/autocomplete-trigger.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ export class MdAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
339339
this._clearPreviousSelectedOption(event.source);
340340
this._setTriggerValue(event.source.value);
341341
this._onChange(event.source.value);
342+
this._element.nativeElement.focus();
342343
}
343344

344345
this.closePanel();

src/lib/autocomplete/autocomplete.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,22 @@ describe('MdAutocomplete', () => {
907907

908908
});
909909

910+
it('should restore focus to the input when clicking to select a value', async(() => {
911+
fixture.componentInstance.trigger.openPanel();
912+
fixture.detectChanges();
913+
914+
fixture.whenStable().then(() => {
915+
const option = overlayContainerElement.querySelector('md-option') as HTMLElement;
916+
917+
// Focus the option manually since the synthetic click may not do it.
918+
option.focus();
919+
option.click();
920+
fixture.detectChanges();
921+
922+
expect(document.activeElement).toBe(input, 'Expected focus to be restored to the input.');
923+
});
924+
}));
925+
910926
});
911927

912928
describe('Fallback positions', () => {

0 commit comments

Comments
 (0)