Skip to content

Commit 1afb2ba

Browse files
committed
fix(select): error when attempting to open before init
Fixes an error that could be thrown by `mat-select` if it is opened before everything is done initializing. Fixes #8236.
1 parent 8b3c8a5 commit 1afb2ba

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

src/lib/select/select.spec.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1870,7 +1870,6 @@ describe('MatSelect', () => {
18701870

18711871
// There appears to be a small rounding error on IE, so we verify that the value is close,
18721872
// not exact.
1873-
let platform = new Platform();
18741873
if (platform.TRIDENT) {
18751874
let difference =
18761875
Math.abs(optionTop + (menuItemHeight - triggerHeight) / 2 - triggerTop);
@@ -2512,6 +2511,11 @@ describe('MatSelect', () => {
25122511
expect(label.textContent).toContain('azziP',
25132512
'Expected the displayed text to be "Pizza" in reverse.');
25142513
}));
2514+
2515+
it('should not throw when attempting to open too early', () => {
2516+
const fixture = TestBed.createComponent(BasicSelect);
2517+
expect(() => fixture.componentInstance.select.open()).not.toThrow();
2518+
});
25152519
});
25162520

25172521
describe('change event', () => {

src/lib/select/select.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ export class MatSelect extends _MatSelectMixinBase implements AfterContentInit,
515515

516516
/** Opens the overlay panel. */
517517
open(): void {
518-
if (this.disabled || !this.options.length) {
518+
if (this.disabled || !this.options || !this.options.length) {
519519
return;
520520
}
521521

@@ -605,7 +605,7 @@ export class MatSelect extends _MatSelectMixinBase implements AfterContentInit,
605605

606606
/** The value displayed in the trigger. */
607607
get triggerValue(): string {
608-
if (!this._selectionModel || this._selectionModel.isEmpty()) {
608+
if (this.empty) {
609609
return '';
610610
}
611611

@@ -912,10 +912,12 @@ export class MatSelect extends _MatSelectMixinBase implements AfterContentInit,
912912
* the first item instead.
913913
*/
914914
private _highlightCorrectOption(): void {
915-
if (this._selectionModel.isEmpty()) {
916-
this._keyManager.setFirstItemActive();
917-
} else {
918-
this._keyManager.setActiveItem(this._getOptionIndex(this._selectionModel.selected[0])!);
915+
if (this._keyManager) {
916+
if (this.empty) {
917+
this._keyManager.setFirstItemActive();
918+
} else {
919+
this._keyManager.setActiveItem(this._getOptionIndex(this._selectionModel.selected[0])!);
920+
}
919921
}
920922
}
921923

0 commit comments

Comments
 (0)