Skip to content

Commit 8683f47

Browse files
committed
fix(material/button-toggle): incorrect event source in some cases (#25544)
Removes some logic that was trying to incorrectly match the button toggle being clicked with the model value. The logic wasn't necessary since we already know which toggle the user interacted with. Fixes #25489. (cherry picked from commit 335aa0a)
1 parent 84388dd commit 8683f47

File tree

2 files changed

+7
-9
lines changed

2 files changed

+7
-9
lines changed

src/material/button-toggle/button-toggle.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -264,10 +264,8 @@ export class MatButtonToggleGroup implements ControlValueAccessor, OnInit, After
264264
}
265265

266266
/** Dispatch change event with current selection and group value. */
267-
_emitChangeEvent(): void {
268-
const selected = this.selected;
269-
const source = Array.isArray(selected) ? selected[selected.length - 1] : selected;
270-
const event = new MatButtonToggleChange(source!, this.value);
267+
_emitChangeEvent(toggle: MatButtonToggle): void {
268+
const event = new MatButtonToggleChange(toggle, this.value);
271269
this._controlValueAccessorChangeFn(event.value);
272270
this.change.emit(event);
273271
}
@@ -305,9 +303,9 @@ export class MatButtonToggleGroup implements ControlValueAccessor, OnInit, After
305303
// the side-effect is that we may end up updating the model value out of sequence in others
306304
// The `deferEvents` flag allows us to decide whether to do it on a case-by-case basis.
307305
if (deferEvents) {
308-
Promise.resolve().then(() => this._updateModelValue(isUserInput));
306+
Promise.resolve().then(() => this._updateModelValue(toggle, isUserInput));
309307
} else {
310-
this._updateModelValue(isUserInput);
308+
this._updateModelValue(toggle, isUserInput);
311309
}
312310
}
313311

@@ -369,10 +367,10 @@ export class MatButtonToggleGroup implements ControlValueAccessor, OnInit, After
369367
}
370368

371369
/** Syncs up the group's value with the model and emits the change event. */
372-
private _updateModelValue(isUserInput: boolean) {
370+
private _updateModelValue(toggle: MatButtonToggle, isUserInput: boolean) {
373371
// Only emit the change event for user input.
374372
if (isUserInput) {
375-
this._emitChangeEvent();
373+
this._emitChangeEvent(toggle);
376374
}
377375

378376
// Note: we emit this one no matter whether it was a user interaction, because

tools/public_api_guard/material/button-toggle.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ export class MatButtonToggleGroup implements ControlValueAccessor, OnInit, After
9292
_controlValueAccessorChangeFn: (value: any) => void;
9393
get disabled(): boolean;
9494
set disabled(value: BooleanInput);
95-
_emitChangeEvent(): void;
95+
_emitChangeEvent(toggle: MatButtonToggle): void;
9696
_isPrechecked(toggle: MatButtonToggle): boolean;
9797
_isSelected(toggle: MatButtonToggle): boolean;
9898
get multiple(): boolean;

0 commit comments

Comments
 (0)