Skip to content

fix(material/button-toggle): incorrect event source in some cases #25544

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
Aug 28, 2022
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: 6 additions & 8 deletions src/material/button-toggle/button-toggle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,8 @@ export class MatButtonToggleGroup implements ControlValueAccessor, OnInit, After
}

/** Dispatch change event with current selection and group value. */
_emitChangeEvent(): void {
const selected = this.selected;
const source = Array.isArray(selected) ? selected[selected.length - 1] : selected;
const event = new MatButtonToggleChange(source!, this.value);
_emitChangeEvent(toggle: MatButtonToggle): void {
const event = new MatButtonToggleChange(toggle, this.value);
this._controlValueAccessorChangeFn(event.value);
this.change.emit(event);
}
Expand Down Expand Up @@ -305,9 +303,9 @@ export class MatButtonToggleGroup implements ControlValueAccessor, OnInit, After
// 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().then(() => this._updateModelValue(isUserInput));
Promise.resolve().then(() => this._updateModelValue(toggle, isUserInput));
} else {
this._updateModelValue(isUserInput);
this._updateModelValue(toggle, isUserInput);
}
}

Expand Down Expand Up @@ -369,10 +367,10 @@ export class MatButtonToggleGroup implements ControlValueAccessor, OnInit, After
}

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

// Note: we emit this one no matter whether it was a user interaction, because
Expand Down
2 changes: 1 addition & 1 deletion tools/public_api_guard/material/button-toggle.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export class MatButtonToggleGroup implements ControlValueAccessor, OnInit, After
_controlValueAccessorChangeFn: (value: any) => void;
get disabled(): boolean;
set disabled(value: BooleanInput);
_emitChangeEvent(): void;
_emitChangeEvent(toggle: MatButtonToggle): void;
_isPrechecked(toggle: MatButtonToggle): boolean;
_isSelected(toggle: MatButtonToggle): boolean;
get multiple(): boolean;
Expand Down