From 86bea9174b68c86a9c7bdd8c9c61be2d287014f9 Mon Sep 17 00:00:00 2001 From: Kristiyan Kostadinov Date: Fri, 29 Sep 2017 22:04:13 +0200 Subject: [PATCH] fix(select): losing focus when selecting values through binding (#7296) Fixes an issue that caused focus to be lost after selecting a value on a select that doesn't use Angular forms. Fixes #7092. --- src/lib/select/select.spec.ts | 15 +++++++++++++++ src/lib/select/select.ts | 6 ++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/lib/select/select.spec.ts b/src/lib/select/select.spec.ts index e6fc6bbe910..57e9d54b2ce 100644 --- a/src/lib/select/select.spec.ts +++ b/src/lib/select/select.spec.ts @@ -872,6 +872,21 @@ describe('MatSelect', () => { expect(trigger.textContent).toContain('Steak, Pizza, Sandwich'); }); + it('should restore focus to the host element', () => { + const fixture = TestBed.createComponent(BasicSelectWithoutForms); + + fixture.detectChanges(); + fixture.debugElement.query(By.css('.mat-select-trigger')).nativeElement.click(); + fixture.detectChanges(); + + (overlayContainerElement.querySelector('md-option') as HTMLElement).click(); + fixture.detectChanges(); + + const select = fixture.debugElement.nativeElement.querySelector('md-select'); + + expect(document.activeElement).toBe(select, 'Expected trigger to be focused.'); + }); + }); describe('disabled behavior', () => { diff --git a/src/lib/select/select.ts b/src/lib/select/select.ts index 7e5dfacbb07..634617007a5 100644 --- a/src/lib/select/select.ts +++ b/src/lib/select/select.ts @@ -351,8 +351,10 @@ export class MatSelect extends _MatSelectMixinBase implements AfterContentInit, @Input() get value() { return this._value; } set value(newValue: any) { - this.writeValue(newValue); - this._value = newValue; + if (newValue !== this._value) { + this.writeValue(newValue); + this._value = newValue; + } } private _value: any;