Skip to content

Commit 7454dca

Browse files
committed
fix(button-toggle): focus monitoring not being cleaned up on destroy
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 edb57f9 commit 7454dca

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,
@@ -331,7 +332,8 @@ export const _MatButtonToggleMixinBase = mixinDisableRipple(MatButtonToggleBase)
331332
'[attr.id]': 'id',
332333
}
333334
})
334-
export class MatButtonToggle extends _MatButtonToggleMixinBase implements OnInit, CanDisableRipple {
335+
export class MatButtonToggle extends _MatButtonToggleMixinBase implements OnInit,
336+
CanDisableRipple, OnDestroy {
335337

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

353-
@ViewChild('input') _inputElement: ElementRef;
355+
@ViewChild('input') _inputElement: ElementRef<HTMLInputElement>;
354356

355357
/** The parent button toggle group (exclusive selection). Optional. */
356358
buttonToggleGroup: MatButtonToggleGroup;
@@ -400,7 +402,7 @@ export class MatButtonToggle extends _MatButtonToggleMixinBase implements OnInit
400402

401403
constructor(@Optional() toggleGroup: MatButtonToggleGroup,
402404
private _changeDetectorRef: ChangeDetectorRef,
403-
private _elementRef: ElementRef,
405+
private _elementRef: ElementRef<HTMLElement>,
404406
private _focusMonitor: FocusMonitor) {
405407
super();
406408

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

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

0 commit comments

Comments
 (0)