Skip to content

fix(select): error when attempting to open before init #8242

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/lib/select/select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1870,7 +1870,6 @@ describe('MatSelect', () => {

// There appears to be a small rounding error on IE, so we verify that the value is close,
// not exact.
let platform = new Platform();
if (platform.TRIDENT) {
let difference =
Math.abs(optionTop + (menuItemHeight - triggerHeight) / 2 - triggerTop);
Expand Down Expand Up @@ -2512,6 +2511,11 @@ describe('MatSelect', () => {
expect(label.textContent).toContain('azziP',
'Expected the displayed text to be "Pizza" in reverse.');
}));

it('should not throw when attempting to open too early', () => {
const fixture = TestBed.createComponent(BasicSelect);
expect(() => fixture.componentInstance.select.open()).not.toThrow();
});
});

describe('change event', () => {
Expand Down
14 changes: 8 additions & 6 deletions src/lib/select/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ export class MatSelect extends _MatSelectMixinBase implements AfterContentInit,

/** Opens the overlay panel. */
open(): void {
if (this.disabled || !this.options.length) {
if (this.disabled || !this.options || !this.options.length) {
return;
}

Expand Down Expand Up @@ -605,7 +605,7 @@ export class MatSelect extends _MatSelectMixinBase implements AfterContentInit,

/** The value displayed in the trigger. */
get triggerValue(): string {
if (!this._selectionModel || this._selectionModel.isEmpty()) {
if (this.empty) {
return '';
}

Expand Down Expand Up @@ -912,10 +912,12 @@ export class MatSelect extends _MatSelectMixinBase implements AfterContentInit,
* the first item instead.
*/
private _highlightCorrectOption(): void {
if (this._selectionModel.isEmpty()) {
this._keyManager.setFirstItemActive();
} else {
this._keyManager.setActiveItem(this._getOptionIndex(this._selectionModel.selected[0])!);
if (this._keyManager) {
if (this.empty) {
this._keyManager.setFirstItemActive();
} else {
this._keyManager.setActiveItem(this._getOptionIndex(this._selectionModel.selected[0])!);
}
}
}

Expand Down