Skip to content

fix(button-toggle): changed after checked error for repeated toggles with a preselected value #10612

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
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
29 changes: 28 additions & 1 deletion src/lib/button-toggle/button-toggle.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {fakeAsync, tick, ComponentFixture, TestBed} from '@angular/core/testing';
import {dispatchMouseEvent} from '@angular/cdk/testing';
import {NgModel, FormsModule, ReactiveFormsModule, FormControl} from '@angular/forms';
import {Component, DebugElement} from '@angular/core';
import {Component, DebugElement, ViewChild, ViewChildren, QueryList} from '@angular/core';
import {By} from '@angular/platform-browser';
import {
MatButtonToggleGroup,
Expand Down Expand Up @@ -209,6 +209,7 @@ describe('MatButtonToggle without forms', () => {
StandaloneButtonToggle,
ButtonToggleWithAriaLabel,
ButtonToggleWithAriaLabelledby,
RepeatedButtonTogglesWithPreselectedValue,
],
});

Expand Down Expand Up @@ -662,6 +663,14 @@ describe('MatButtonToggle without forms', () => {
expect(inputElement.getAttribute('aria-labelledby')).toBe(null);
});
});

it('should not throw on init when toggles are repeated and there is an initial value', () => {
const fixture = TestBed.createComponent(RepeatedButtonTogglesWithPreselectedValue);

expect(() => fixture.detectChanges()).not.toThrow();
expect(fixture.componentInstance.toggleGroup.value).toBe('Two');
expect(fixture.componentInstance.toggles.toArray()[1].checked).toBe(true);
});
});

@Component({
Expand Down Expand Up @@ -759,3 +768,21 @@ class ButtonToggleWithAriaLabel { }
template: `<mat-button-toggle aria-labelledby="some-id"></mat-button-toggle>`
})
class ButtonToggleWithAriaLabelledby {}


@Component({
template: `
<mat-button-toggle-group [(value)]="value">
<mat-button-toggle *ngFor="let toggle of possibleValues" [value]="toggle">
{{toggle}}
</mat-button-toggle>
</mat-button-toggle-group>
`
})
class RepeatedButtonTogglesWithPreselectedValue {
@ViewChild(MatButtonToggleGroup) toggleGroup: MatButtonToggleGroup;
@ViewChildren(MatButtonToggle) toggles: QueryList<MatButtonToggle>;

possibleValues = ['One', 'Two', 'Three'];
value = 'Two';
}
31 changes: 21 additions & 10 deletions src/lib/button-toggle/button-toggle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,8 @@ export class MatButtonToggleGroup extends _MatButtonToggleGroupMixinBase impleme
}

ngAfterContentInit() {
// If there was an attempt to assign a value before init, use it to set the
// initial selection, otherwise check the `checked` state of the toggles.
if (typeof this._tempValue !== 'undefined') {
this._setSelectionByValue(this._tempValue);
this._tempValue = undefined;
} else {
this._selectionModel.select(...this._buttonToggles.filter(toggle => toggle.checked));
}
this._selectionModel.select(...this._buttonToggles.filter(toggle => toggle.checked));
this._tempValue = undefined;
}

/**
Expand Down Expand Up @@ -261,6 +255,19 @@ export class MatButtonToggleGroup extends _MatButtonToggleGroupMixinBase impleme
return this._selectionModel.isSelected(toggle);
}

/** Determines whether a button toggle should be checked on init. */
_isPrechecked(toggle: MatButtonToggle) {
if (typeof this._tempValue === 'undefined') {
return false;
}

if (this.multiple && Array.isArray(this._tempValue)) {
return !!this._tempValue.find(value => toggle.value != null && value === toggle.value);
}

return toggle.value === this._tempValue;
}

/** Updates the selection state of the toggles in the group based on a value. */
private _setSelectionByValue(value: any|any[]) {
// If the toggles haven't been initialized yet, save the value for later.
Expand Down Expand Up @@ -409,6 +416,10 @@ export class MatButtonToggle extends _MatButtonToggleMixinBase implements OnInit
this.name = this.buttonToggleGroup.name;
}

if (this.buttonToggleGroup && this.buttonToggleGroup._isPrechecked(this)) {
this.checked = true;
}

this._focusMonitor.monitor(this._elementRef.nativeElement, true);
}

Expand Down Expand Up @@ -449,8 +460,8 @@ export class MatButtonToggle extends _MatButtonToggleMixinBase implements OnInit
* update bound properties of the radio button.
*/
_markForCheck() {
// When group value changes, the button will not be notified. Use `markForCheck` to explicit
// update button toggle's status
// When the group value changes, the button will not be notified.
// Use `markForCheck` to explicit update button toggle's status.
this._changeDetectorRef.markForCheck();
}
}