@@ -33,6 +33,7 @@ import {
33
33
ViewChildren ,
34
34
ViewEncapsulation ,
35
35
} from '@angular/core' ;
36
+ import { ControlValueAccessor , NG_VALUE_ACCESSOR } from '@angular/forms' ;
36
37
import {
37
38
CanColorCtor ,
38
39
MatRipple ,
@@ -249,11 +250,16 @@ export class MatSliderVisualThumb implements AfterViewInit, OnDestroy {
249
250
host : {
250
251
'class' : 'mdc-slider__input' ,
251
252
'type' : 'range' ,
252
- '(blur)' : '_blur.emit ()' ,
253
+ '(blur)' : '_onBlur ()' ,
253
254
'(focus)' : '_focus.emit()' ,
254
255
} ,
256
+ providers : [ {
257
+ provide : NG_VALUE_ACCESSOR ,
258
+ useExisting : MatSliderThumb ,
259
+ multi : true
260
+ } ] ,
255
261
} )
256
- export class MatSliderThumb implements AfterViewInit {
262
+ export class MatSliderThumb implements AfterViewInit , ControlValueAccessor {
257
263
258
264
// ** IMPORTANT NOTE **
259
265
//
@@ -298,6 +304,20 @@ export class MatSliderThumb implements AfterViewInit {
298
304
/** Event emitted every time the MatSliderThumb is focused. */
299
305
@Output ( ) readonly _focus : EventEmitter < void > = new EventEmitter < void > ( ) ;
300
306
307
+ _disabled : boolean = false ;
308
+
309
+ /**
310
+ * A callback function that is called when the
311
+ * control's value changes in the UI (ControlValueAccessor).
312
+ */
313
+ _onChange : ( value : any ) => void = ( ) => { } ;
314
+
315
+ /**
316
+ * A callback function that is called by the forms API on
317
+ * initialization to update the form model on blur (ControlValueAccessor).
318
+ */
319
+ private _onTouched : ( ) => void = ( ) => { } ;
320
+
301
321
/** Indicates which slider thumb this input corresponds to. */
302
322
_thumbPosition : Thumb = this . _elementRef . nativeElement . hasAttribute ( 'matSliderStartThumb' )
303
323
? Thumb . START
@@ -331,6 +351,47 @@ export class MatSliderThumb implements AfterViewInit {
331
351
}
332
352
}
333
353
354
+ _onBlur ( ) : void {
355
+ this . _onTouched ( ) ;
356
+ this . _blur . emit ( ) ;
357
+ }
358
+
359
+ /**
360
+ * Sets the model value. Implemented as part of ControlValueAccessor.
361
+ * @param value
362
+ */
363
+ writeValue ( value : any ) : void {
364
+ this . value = value ;
365
+ }
366
+
367
+ /**
368
+ * Registers a callback to be triggered when the value has changed.
369
+ * Implemented as part of ControlValueAccessor.
370
+ * @param fn Callback to be registered.
371
+ */
372
+ registerOnChange ( fn : any ) : void {
373
+ this . _onChange = fn ;
374
+ }
375
+
376
+ /**
377
+ * Registers a callback to be triggered when the component is touched.
378
+ * Implemented as part of ControlValueAccessor.
379
+ * @param fn Callback to be registered.
380
+ */
381
+ registerOnTouched ( fn : any ) : void {
382
+ this . _onTouched = fn ;
383
+ }
384
+
385
+ /**
386
+ * Sets whether the component should be disabled.
387
+ * Implemented as part of ControlValueAccessor.
388
+ * @param isDisabled
389
+ */
390
+ setDisabledState ( isDisabled : boolean ) : void {
391
+ this . _disabled = isDisabled ;
392
+ this . _slider . _updateDisabled ( ) ;
393
+ }
394
+
334
395
/** Returns true if this slider input currently has focus. */
335
396
_isFocused ( ) : boolean {
336
397
return this . _document . activeElement === this . _hostElement ;
@@ -562,6 +623,11 @@ export class MatSlider extends _MatSliderMixinBase implements AfterViewInit, OnD
562
623
return this . _inputs . length === 2 ;
563
624
}
564
625
626
+ /** Sets the disabled state based on the disabled state of the inputs (ControlValueAccessor). */
627
+ _updateDisabled ( ) : void {
628
+ this . disabled = this . _inputs . some ( input => input . _disabled ) ;
629
+ }
630
+
565
631
/** Gets the slider thumb input of the given thumb position. */
566
632
_getInput ( thumbPosition : Thumb ) : MatSliderThumb {
567
633
return thumbPosition === Thumb . END ? this . _inputs . last : this . _inputs . first ;
@@ -709,7 +775,9 @@ class SliderAdapter implements MDCSliderAdapter {
709
775
}
710
776
// We ignore emitChangeEvent and emitInputEvent because the slider inputs
711
777
// are already exposed so users can just listen for those events directly themselves.
712
- emitChangeEvent = ( value : number , thumbPosition : Thumb ) : void => { } ;
778
+ emitChangeEvent = ( value : number , thumbPosition : Thumb ) : void => {
779
+ this . _delegate . _getInput ( thumbPosition ) . _onChange ( value ) ;
780
+ }
713
781
emitInputEvent = ( value : number , thumbPosition : Thumb ) : void => { } ;
714
782
emitDragStartEvent = ( value : number , thumbPosition : Thumb ) : void => {
715
783
const input = this . _delegate . _getInput ( thumbPosition ) ;
0 commit comments