Skip to content

Commit ba36d3a

Browse files
crisbetojosephperrott
authored andcommitted
fix(select): error when attempting to open before init (#8242)
1 parent b6712f8 commit ba36d3a

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

src/lib/select/select.spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2511,6 +2511,11 @@ describe('MatSelect', () => {
25112511
expect(label.textContent).toContain('azziP',
25122512
'Expected the displayed text to be "Pizza" in reverse.');
25132513
}));
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+
});
25142519
});
25152520

25162521
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)