66 * found in the LICENSE file at https://angular.io/license
77 */
88
9- import { FocusMonitor , FocusOrigin } from '@angular/cdk/a11y' ;
9+ import { FocusMonitor } from '@angular/cdk/a11y' ;
1010import { Directionality } from '@angular/cdk/bidi' ;
1111import { coerceBooleanProperty } from '@angular/cdk/coercion' ;
1212import { Platform } from '@angular/cdk/platform' ;
@@ -35,12 +35,10 @@ import {
3535 CanDisableRipple ,
3636 HammerInput ,
3737 HasTabIndex ,
38- MatRipple ,
3938 mixinColor ,
4039 mixinDisabled ,
4140 mixinDisableRipple ,
4241 mixinTabIndex ,
43- RippleRef ,
4442} from '@angular/material/core' ;
4543import { ANIMATION_MODULE_TYPE } from '@angular/platform-browser/animations' ;
4644import {
@@ -104,9 +102,6 @@ export class MatSlideToggle extends _MatSlideToggleMixinBase implements OnDestro
104102 private _required : boolean = false ;
105103 private _checked : boolean = false ;
106104
107- /** Reference to the focus state ripple. */
108- private _focusRipple : RippleRef | null ;
109-
110105 /** Whether the thumb is currently being dragged. */
111106 private _dragging = false ;
112107
@@ -179,9 +174,6 @@ export class MatSlideToggle extends _MatSlideToggleMixinBase implements OnDestro
179174 /** Reference to the underlying input element. */
180175 @ViewChild ( 'input' ) _inputElement : ElementRef ;
181176
182- /** Reference to the ripple directive on the thumb container. */
183- @ViewChild ( MatRipple ) _ripple : MatRipple ;
184-
185177 constructor ( elementRef : ElementRef ,
186178 /**
187179 * @deprecated The `_platform` parameter to be removed.
@@ -202,12 +194,21 @@ export class MatSlideToggle extends _MatSlideToggleMixinBase implements OnDestro
202194
203195 ngAfterContentInit ( ) {
204196 this . _focusMonitor
205- . monitor ( this . _inputElement . nativeElement )
206- . subscribe ( focusOrigin => this . _onInputFocusChange ( focusOrigin ) ) ;
197+ . monitor ( this . _elementRef . nativeElement , true )
198+ . subscribe ( focusOrigin => {
199+ if ( ! focusOrigin ) {
200+ // When a focused element becomes disabled, the browser *immediately* fires a blur event.
201+ // Angular does not expect events to be raised during change detection, so any state
202+ // change (such as a form control's 'ng-touched') will cause a changed-after-checked
203+ // error. See https://github.com/angular/angular/issues/17793. To work around this,
204+ // we defer telling the form control it has been touched until the next tick.
205+ Promise . resolve ( ) . then ( ( ) => this . onTouched ( ) ) ;
206+ }
207+ } ) ;
207208 }
208209
209210 ngOnDestroy ( ) {
210- this . _focusMonitor . stopMonitoring ( this . _inputElement . nativeElement ) ;
211+ this . _focusMonitor . stopMonitoring ( this . _elementRef . nativeElement ) ;
211212 }
212213
213214 /** Method being called whenever the underlying input emits a change event. */
@@ -282,28 +283,6 @@ export class MatSlideToggle extends _MatSlideToggleMixinBase implements OnDestro
282283 this . onChange ( this . checked ) ;
283284 }
284285
285- /** Function is called whenever the focus changes for the input element. */
286- private _onInputFocusChange ( focusOrigin : FocusOrigin ) {
287- // TODO(paul): support `program`. See https://github.com/angular/material2/issues/9889
288- if ( ! this . _focusRipple && focusOrigin === 'keyboard' ) {
289- // For keyboard focus show a persistent ripple as focus indicator.
290- this . _focusRipple = this . _ripple . launch ( 0 , 0 , { persistent : true } ) ;
291- } else if ( ! focusOrigin ) {
292- // When a focused element becomes disabled, the browser *immediately* fires a blur event.
293- // Angular does not expect events to be raised during change detection, so any state change
294- // (such as a form control's 'ng-touched') will cause a changed-after-checked error.
295- // See https://github.com/angular/angular/issues/17793. To work around this, we defer telling
296- // the form control it has been touched until the next tick.
297- Promise . resolve ( ) . then ( ( ) => this . onTouched ( ) ) ;
298-
299- // Fade out and clear the focus ripple if one is currently present.
300- if ( this . _focusRipple ) {
301- this . _focusRipple . fadeOut ( ) ;
302- this . _focusRipple = null ;
303- }
304- }
305- }
306-
307286 /**
308287 * Emits a change event on the `change` output. Also notifies the FormControl about the change.
309288 */
0 commit comments