Skip to content

Commit c9651bb

Browse files
crisbetotinayuangao
authored andcommitted
fix(button-toggle): focus monitoring not being cleaned up on destroy (#10580)
Fixes the focus monitoring for the individual buttons toggles not being cleaned up when they're destroyed, causing the reference to stay inside the `FocusMonitor`.
1 parent eee1446 commit c9651bb

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
EventEmitter,
2020
forwardRef,
2121
Input,
22+
OnDestroy,
2223
OnInit,
2324
Optional,
2425
Output,
@@ -332,7 +333,8 @@ export const _MatButtonToggleMixinBase = mixinDisableRipple(MatButtonToggleBase)
332333
'[attr.id]': 'id',
333334
}
334335
})
335-
export class MatButtonToggle extends _MatButtonToggleMixinBase implements OnInit, CanDisableRipple {
336+
export class MatButtonToggle extends _MatButtonToggleMixinBase implements OnInit,
337+
CanDisableRipple, OnDestroy {
336338

337339
private _isSingleSelector = false;
338340
private _checked = false;
@@ -351,7 +353,7 @@ export class MatButtonToggle extends _MatButtonToggleMixinBase implements OnInit
351353
/** Type of the button toggle. Either 'radio' or 'checkbox'. */
352354
_type: ToggleType;
353355

354-
@ViewChild('input') _inputElement: ElementRef;
356+
@ViewChild('input') _inputElement: ElementRef<HTMLInputElement>;
355357

356358
/** The parent button toggle group (exclusive selection). Optional. */
357359
buttonToggleGroup: MatButtonToggleGroup;
@@ -401,7 +403,7 @@ export class MatButtonToggle extends _MatButtonToggleMixinBase implements OnInit
401403

402404
constructor(@Optional() toggleGroup: MatButtonToggleGroup,
403405
private _changeDetectorRef: ChangeDetectorRef,
404-
private _elementRef: ElementRef,
406+
private _elementRef: ElementRef<HTMLElement>,
405407
private _focusMonitor: FocusMonitor) {
406408
super();
407409

@@ -424,6 +426,10 @@ export class MatButtonToggle extends _MatButtonToggleMixinBase implements OnInit
424426
this._focusMonitor.monitor(this._elementRef.nativeElement, true);
425427
}
426428

429+
ngOnDestroy() {
430+
this._focusMonitor.stopMonitoring(this._elementRef.nativeElement);
431+
}
432+
427433
/** Focuses the button. */
428434
focus(): void {
429435
this._inputElement.nativeElement.focus();

0 commit comments

Comments
 (0)