Skip to content

fix(button-toggle): error when checked value is set via static attribute in Ivy #16587

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
Jul 25, 2019
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
14 changes: 14 additions & 0 deletions src/material/button-toggle/button-toggle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,20 @@ describe('MatButtonToggle without forms', () => {
expect(fixture.componentInstance.toggles.toArray()[1].checked).toBe(false);
expect(fixture.componentInstance.toggles.toArray()[2].checked).toBe(true);
});

it('should not throw if initial value is set during creation', () => {
const fixture = TestBed.createComponent(ButtonTogglesInsideButtonToggleGroupMultiple);

// In Ivy static inputs are set during creation. We simulate this by not calling
// `fixture.detectChanges` immediately, but getting a hold of the instance via the
// DebugElement and setting the value ourselves.
expect(() => {
const toggle = fixture.debugElement.query(By.css('mat-button-toggle')).componentInstance;
toggle.checked = true;
fixture.detectChanges();
}).not.toThrow();
});

});

@Component({
Expand Down
14 changes: 9 additions & 5 deletions src/material/button-toggle/button-toggle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ export class MatButtonToggleGroup implements ControlValueAccessor, OnInit, After

/** Selected button toggles in the group. */
get selected() {
const selected = this._selectionModel.selected;
const selected = this._selectionModel ? this._selectionModel.selected : [];
return this.multiple ? selected : (selected[0] || null);
}

Expand Down Expand Up @@ -276,10 +276,14 @@ export class MatButtonToggleGroup implements ControlValueAccessor, OnInit, After
(this.selected as MatButtonToggle).checked = false;
}

if (select) {
this._selectionModel.select(toggle);
if (this._selectionModel) {
if (select) {
this._selectionModel.select(toggle);
} else {
this._selectionModel.deselect(toggle);
}
} else {
this._selectionModel.deselect(toggle);
deferEvents = true;
}

// We need to defer in some cases in order to avoid "changed after checked errors", however
Expand All @@ -294,7 +298,7 @@ export class MatButtonToggleGroup implements ControlValueAccessor, OnInit, After

/** Checks whether a button toggle is selected. */
_isSelected(toggle: MatButtonToggle) {
return this._selectionModel.isSelected(toggle);
return this._selectionModel && this._selectionModel.isSelected(toggle);
}

/** Determines whether a button toggle should be checked on init. */
Expand Down