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`.
This commit is contained in:
Kristiyan Kostadinov
2018-04-14 00:32:25 +02:00
committed by tinayuangao
parent eee14464b1
commit c9651bbb05
+9 -3
View File
@@ -19,6 +19,7 @@ import {
EventEmitter,
forwardRef,
Input,
OnDestroy,
OnInit,
Optional,
Output,
@@ -332,7 +333,8 @@ export const _MatButtonToggleMixinBase = mixinDisableRipple(MatButtonToggleBase)
'[attr.id]': 'id',
}
})
export class MatButtonToggle extends _MatButtonToggleMixinBase implements OnInit, CanDisableRipple {
export class MatButtonToggle extends _MatButtonToggleMixinBase implements OnInit,
CanDisableRipple, OnDestroy {
private _isSingleSelector = false;
private _checked = false;
@@ -351,7 +353,7 @@ export class MatButtonToggle extends _MatButtonToggleMixinBase implements OnInit
/** Type of the button toggle. Either 'radio' or 'checkbox'. */
_type: ToggleType;
@ViewChild('input') _inputElement: ElementRef;
@ViewChild('input') _inputElement: ElementRef<HTMLInputElement>;
/** The parent button toggle group (exclusive selection). Optional. */
buttonToggleGroup: MatButtonToggleGroup;
@@ -401,7 +403,7 @@ export class MatButtonToggle extends _MatButtonToggleMixinBase implements OnInit
constructor(@Optional() toggleGroup: MatButtonToggleGroup,
private _changeDetectorRef: ChangeDetectorRef,
private _elementRef: ElementRef,
private _elementRef: ElementRef<HTMLElement>,
private _focusMonitor: FocusMonitor) {
super();
@@ -424,6 +426,10 @@ export class MatButtonToggle extends _MatButtonToggleMixinBase implements OnInit
this._focusMonitor.monitor(this._elementRef.nativeElement, true);
}
ngOnDestroy() {
this._focusMonitor.stopMonitoring(this._elementRef.nativeElement);
}
/** Focuses the button. */
focus(): void {
this._inputElement.nativeElement.focus();