Skip to content

Commit

Permalink
fix(select): losing focus when selecting values through binding (#7296)
Browse files Browse the repository at this point in the history
Fixes an issue that caused focus to be lost after selecting a value on a select that doesn't use Angular forms.

Fixes #7092.
  • Loading branch information
crisbeto authored and andrewseguin committed Sep 29, 2017
1 parent ce6cc0b commit 86bea91
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
15 changes: 15 additions & 0 deletions src/lib/select/select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
6 changes: 4 additions & 2 deletions src/lib/select/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 86bea91

Please sign in to comment.