Skip to content

fix(button-toggle): value cleared when replacing with list that still contains the selected value #15508

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
May 13, 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
25 changes: 25 additions & 0 deletions src/material/button-toggle/button-toggle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,31 @@ describe('MatButtonToggle with forms', () => {

expect(groupElement.querySelectorAll('.mat-ripple-element').length).toBe(0);
});

it('should maintain the selected value when swapping out the list of toggles with one ' +
'that still contains the value', fakeAsync(() => {
expect(buttonToggleInstances[0].checked).toBe(false);
expect(fixture.componentInstance.modelValue).toBeFalsy();
expect(groupInstance.value).toBeFalsy();

groupInstance.value = 'red';
fixture.detectChanges();

expect(buttonToggleInstances[0].checked).toBe(true);
expect(groupInstance.value).toBe('red');

fixture.componentInstance.options = [...fixture.componentInstance.options];
fixture.detectChanges();
tick();
fixture.detectChanges();

buttonToggleDebugElements = fixture.debugElement.queryAll(By.directive(MatButtonToggle));
buttonToggleInstances = buttonToggleDebugElements.map(debugEl => debugEl.componentInstance);

expect(buttonToggleInstances[0].checked).toBe(true);
expect(groupInstance.value).toBe('red');
}));

});
});

Expand Down
34 changes: 25 additions & 9 deletions src/material/button-toggle/button-toggle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,12 @@ export class MatButtonToggleGroup implements ControlValueAccessor, OnInit, After
* @param toggle Toggle to be synced.
* @param select Whether the toggle should be selected.
* @param isUserInput Whether the change was a result of a user interaction.
* @param deferEvents Whether to defer emitting the change events.
*/
_syncButtonToggle(toggle: MatButtonToggle, select: boolean, isUserInput = false) {
_syncButtonToggle(toggle: MatButtonToggle,
select: boolean,
isUserInput = false,
deferEvents = false) {
// Deselect the currently-selected toggle, if we're in single-selection
// mode and the button being toggled isn't selected at the moment.
if (!this.multiple && this.selected && !toggle.checked) {
Expand All @@ -278,14 +282,14 @@ export class MatButtonToggleGroup implements ControlValueAccessor, OnInit, After
this._selectionModel.deselect(toggle);
}

// Only emit the change event for user input.
if (isUserInput) {
this._emitChangeEvent();
// We need to defer in some cases in order to avoid "changed after checked errors", however
// the side-effect is that we may end up updating the model value out of sequence in others
// The `deferEvents` flag allows us to decide whether to do it on a case-by-case basis.
if (deferEvents) {
Promise.resolve(() => this._updateModelValue(isUserInput));
} else {
this._updateModelValue(isUserInput);
}

// Note: we emit this one no matter whether it was a user interaction, because
// it is used by Angular to sync up the two-way data binding.
this.valueChange.emit(this.value);
}

/** Checks whether a button toggle is selected. */
Expand Down Expand Up @@ -344,6 +348,18 @@ export class MatButtonToggleGroup implements ControlValueAccessor, OnInit, After
this._selectionModel.select(correspondingOption);
}
}

/** Syncs up the group's value with the model and emits the change event. */
private _updateModelValue(isUserInput: boolean) {
// Only emit the change event for user input.
if (isUserInput) {
this._emitChangeEvent();
}

// Note: we emit this one no matter whether it was a user interaction, because
// it is used by Angular to sync up the two-way data binding.
this.valueChange.emit(this.value);
}
}

// Boilerplate for applying mixins to the MatButtonToggle class.
Expand Down Expand Up @@ -497,7 +513,7 @@ export class MatButtonToggle extends _MatButtonToggleMixinBase implements OnInit
// Remove the toggle from the selection once it's destroyed. Needs to happen
// on the next tick in order to avoid "changed after checked" errors.
if (group && group._isSelected(this)) {
Promise.resolve().then(() => group._syncButtonToggle(this, false));
group._syncButtonToggle(this, false, false, true);
}
}

Expand Down
2 changes: 1 addition & 1 deletion tools/public_api_guard/material/button-toggle.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export declare class MatButtonToggleGroup implements ControlValueAccessor, OnIni
_emitChangeEvent(): void;
_isPrechecked(toggle: MatButtonToggle): boolean;
_isSelected(toggle: MatButtonToggle): boolean;
_syncButtonToggle(toggle: MatButtonToggle, select: boolean, isUserInput?: boolean): void;
_syncButtonToggle(toggle: MatButtonToggle, select: boolean, isUserInput?: boolean, deferEvents?: boolean): void;
ngAfterContentInit(): void;
ngOnInit(): void;
registerOnChange(fn: (value: any) => void): void;
Expand Down