99import {
1010 AfterContentInit ,
1111 Attribute ,
12+ booleanAttribute ,
1213 ChangeDetectionStrategy ,
1314 ChangeDetectorRef ,
1415 Component ,
@@ -17,30 +18,21 @@ import {
1718 forwardRef ,
1819 Inject ,
1920 Input ,
21+ numberAttribute ,
2022 OnDestroy ,
2123 Optional ,
2224 Output ,
2325 ViewChild ,
2426 ViewEncapsulation ,
2527} from '@angular/core' ;
28+ import { ThemePalette } from '@angular/material/core' ;
2629import { ControlValueAccessor , NG_VALUE_ACCESSOR } from '@angular/forms' ;
2730import { ANIMATION_MODULE_TYPE } from '@angular/platform-browser/animations' ;
2831import { FocusMonitor } from '@angular/cdk/a11y' ;
2932import {
3033 MAT_SLIDE_TOGGLE_DEFAULT_OPTIONS ,
3134 MatSlideToggleDefaultOptions ,
3235} from './slide-toggle-config' ;
33- import {
34- CanColor ,
35- CanDisable ,
36- CanDisableRipple ,
37- HasTabIndex ,
38- mixinColor ,
39- mixinDisabled ,
40- mixinDisableRipple ,
41- mixinTabIndex ,
42- } from '@angular/material/core' ;
43- import { BooleanInput , coerceBooleanProperty } from '@angular/cdk/coercion' ;
4436
4537/** @docs -private */
4638export const MAT_SLIDE_TOGGLE_VALUE_ACCESSOR = {
@@ -62,20 +54,6 @@ export class MatSlideToggleChange {
6254// Increasing integer for generating unique ids for slide-toggle components.
6355let nextUniqueId = 0 ;
6456
65- // Boilerplate for applying mixins to MatSlideToggle.
66- /** @docs -private */
67- const _MatSlideToggleMixinBase = mixinTabIndex (
68- mixinColor (
69- mixinDisableRipple (
70- mixinDisabled (
71- class {
72- constructor ( public _elementRef : ElementRef ) { }
73- } ,
74- ) ,
75- ) ,
76- ) ,
77- ) ;
78-
7957@Component ( {
8058 selector : 'mat-slide-toggle' ,
8159 templateUrl : 'slide-toggle.html' ,
@@ -92,28 +70,18 @@ const _MatSlideToggleMixinBase = mixinTabIndex(
9270 '[class.mat-mdc-slide-toggle-focused]' : '_focused' ,
9371 '[class.mat-mdc-slide-toggle-checked]' : 'checked' ,
9472 '[class._mat-animation-noopable]' : '_noopAnimations' ,
73+ '[class]' : 'color ? "mat-" + color : ""' ,
9574 } ,
9675 exportAs : 'matSlideToggle' ,
9776 encapsulation : ViewEncapsulation . None ,
9877 changeDetection : ChangeDetectionStrategy . OnPush ,
9978 providers : [ MAT_SLIDE_TOGGLE_VALUE_ACCESSOR ] ,
10079} )
101- export class MatSlideToggle
102- extends _MatSlideToggleMixinBase
103- implements
104- OnDestroy ,
105- AfterContentInit ,
106- ControlValueAccessor ,
107- CanDisable ,
108- CanColor ,
109- HasTabIndex ,
110- CanDisableRipple
111- {
80+ export class MatSlideToggle implements OnDestroy , AfterContentInit , ControlValueAccessor {
11281 private _onChange = ( _ : any ) => { } ;
11382 private _onTouched = ( ) => { } ;
11483
11584 private _uniqueId : string ;
116- private _required : boolean = false ;
11785 private _checked : boolean = false ;
11886
11987 private _createChangeEvent ( isChecked : boolean ) {
@@ -160,35 +128,33 @@ export class MatSlideToggle
160128 @Input ( 'aria-describedby' ) ariaDescribedby : string ;
161129
162130 /** Whether the slide-toggle is required. */
163- @Input ( )
164- get required ( ) : boolean {
165- return this . _required ;
166- }
131+ @Input ( { transform : booleanAttribute } ) required : boolean ;
167132
168- set required ( value : BooleanInput ) {
169- this . _required = coerceBooleanProperty ( value ) ;
170- }
133+ /** Palette color of slide toggle. */
134+ @Input ( ) color : ThemePalette ;
135+
136+ /** Whether the slide toggle is disabled. */
137+ @Input ( { transform : booleanAttribute } ) disabled : boolean ;
138+
139+ /** Whether the slide toggle has a ripple. */
140+ @Input ( { transform : booleanAttribute } ) disableRipple : boolean ;
141+
142+ /** Tabindex of slide toggle. */
143+ @Input ( { transform : ( value : unknown ) => ( value == null ? 0 : numberAttribute ( value ) ) } )
144+ tabIndex : number = 0 ;
171145
172146 /** Whether the slide-toggle element is checked or not. */
173- @Input ( )
147+ @Input ( { transform : booleanAttribute } )
174148 get checked ( ) : boolean {
175149 return this . _checked ;
176150 }
177-
178- set checked ( value : BooleanInput ) {
179- this . _checked = coerceBooleanProperty ( value ) ;
151+ set checked ( value : boolean ) {
152+ this . _checked = value ;
180153 this . _changeDetectorRef . markForCheck ( ) ;
181154 }
182155
183156 /** Whether to hide the icon inside of the slide toggle. */
184- @Input ( )
185- get hideIcon ( ) : boolean {
186- return this . _hideIcon ;
187- }
188- set hideIcon ( value : BooleanInput ) {
189- this . _hideIcon = coerceBooleanProperty ( value ) ;
190- }
191- private _hideIcon = false ;
157+ @Input ( { transform : booleanAttribute } ) hideIcon : boolean ;
192158
193159 /** An event will be dispatched each time the slide-toggle changes its value. */
194160 @Output ( ) readonly change = new EventEmitter < MatSlideToggleChange > ( ) ;
@@ -206,19 +172,18 @@ export class MatSlideToggle
206172 }
207173
208174 constructor (
209- elementRef : ElementRef ,
175+ private _elementRef : ElementRef ,
210176 protected _focusMonitor : FocusMonitor ,
211177 protected _changeDetectorRef : ChangeDetectorRef ,
212178 @Attribute ( 'tabindex' ) tabIndex : string ,
213179 @Inject ( MAT_SLIDE_TOGGLE_DEFAULT_OPTIONS ) public defaults : MatSlideToggleDefaultOptions ,
214180 @Optional ( ) @Inject ( ANIMATION_MODULE_TYPE ) animationMode ?: string ,
215181 ) {
216- super ( elementRef ) ;
217182 this . tabIndex = parseInt ( tabIndex ) || 0 ;
218- this . color = this . defaultColor = defaults . color || 'accent' ;
183+ this . color = defaults . color || 'accent' ;
219184 this . _noopAnimations = animationMode === 'NoopAnimations' ;
220185 this . id = this . _uniqueId = `mat-mdc-slide-toggle-${ ++ nextUniqueId } ` ;
221- this . _hideIcon = defaults . hideIcon ?? false ;
186+ this . hideIcon = defaults . hideIcon ?? false ;
222187 this . _labelId = this . _uniqueId + '-label' ;
223188 }
224189
0 commit comments