Skip to content

Commit dec91ce

Browse files
crisbetotinayuangao
authored andcommitted
fix(button-toggle): selected value not being maintained when changing value while toggles are being swapped out (#10707)
Fixes a regression that caused the button toggle group not maintaining the selected state if the selected value and the set of button toggles are swapped out at the same time. Fixes #10690.
1 parent 023e8f4 commit dec91ce

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

src/lib/button-toggle/button-toggle.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,22 @@ describe('MatButtonToggle without forms', () => {
677677
expect(fixture.componentInstance.toggleGroup.value).toBe('Two');
678678
expect(fixture.componentInstance.toggles.toArray()[1].checked).toBe(true);
679679
});
680+
681+
it('should maintain the selected state when the value and toggles are swapped out at ' +
682+
'the same time', () => {
683+
const fixture = TestBed.createComponent(RepeatedButtonTogglesWithPreselectedValue);
684+
fixture.detectChanges();
685+
686+
expect(fixture.componentInstance.toggleGroup.value).toBe('Two');
687+
expect(fixture.componentInstance.toggles.toArray()[1].checked).toBe(true);
688+
689+
fixture.componentInstance.possibleValues = ['Five', 'Six', 'Seven'];
690+
fixture.componentInstance.value = 'Seven';
691+
fixture.detectChanges();
692+
693+
expect(fixture.componentInstance.toggleGroup.value).toBe('Seven');
694+
expect(fixture.componentInstance.toggles.toArray()[2].checked).toBe(true);
695+
});
680696
});
681697

682698
@Component({

src/lib/button-toggle/button-toggle.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,12 @@ export class MatButtonToggleGroup extends _MatButtonToggleGroupMixinBase impleme
9595
private _selectionModel: SelectionModel<MatButtonToggle>;
9696

9797
/**
98-
* Used for storing a value temporarily, if it is assigned
99-
* before the button toggles are initialized.
98+
* Reference to the raw value that the consumer tried to assign. The real
99+
* value will exaclude any values from this one that don't correspond to a
100+
* toggle. Useful for the cases where the value is assigned before the toggles
101+
* have been initialized or at the same that they're being swapped out.
100102
*/
101-
private _tempValue: any;
103+
private _rawValue: any;
102104

103105
/**
104106
* The method to be called in order to update ngModel.
@@ -181,7 +183,6 @@ export class MatButtonToggleGroup extends _MatButtonToggleGroupMixinBase impleme
181183

182184
ngAfterContentInit() {
183185
this._selectionModel.select(...this._buttonToggles.filter(toggle => toggle.checked));
184-
this._tempValue = undefined;
185186
}
186187

187188
/**
@@ -257,22 +258,22 @@ export class MatButtonToggleGroup extends _MatButtonToggleGroupMixinBase impleme
257258

258259
/** Determines whether a button toggle should be checked on init. */
259260
_isPrechecked(toggle: MatButtonToggle) {
260-
if (typeof this._tempValue === 'undefined') {
261+
if (typeof this._rawValue === 'undefined') {
261262
return false;
262263
}
263264

264-
if (this.multiple && Array.isArray(this._tempValue)) {
265-
return !!this._tempValue.find(value => toggle.value != null && value === toggle.value);
265+
if (this.multiple && Array.isArray(this._rawValue)) {
266+
return !!this._rawValue.find(value => toggle.value != null && value === toggle.value);
266267
}
267268

268-
return toggle.value === this._tempValue;
269+
return toggle.value === this._rawValue;
269270
}
270271

271272
/** Updates the selection state of the toggles in the group based on a value. */
272273
private _setSelectionByValue(value: any|any[]) {
273-
// If the toggles haven't been initialized yet, save the value for later.
274+
this._rawValue = value;
275+
274276
if (!this._buttonToggles) {
275-
this._tempValue = value;
276277
return;
277278
}
278279

0 commit comments

Comments
 (0)