9
9
import {
10
10
AfterContentInit ,
11
11
Attribute ,
12
+ booleanAttribute ,
12
13
ChangeDetectionStrategy ,
13
14
ChangeDetectorRef ,
14
15
Component ,
@@ -17,6 +18,7 @@ import {
17
18
forwardRef ,
18
19
Inject ,
19
20
Input ,
21
+ numberAttribute ,
20
22
OnDestroy ,
21
23
Optional ,
22
24
Output ,
@@ -30,17 +32,6 @@ import {
30
32
MAT_SLIDE_TOGGLE_DEFAULT_OPTIONS ,
31
33
MatSlideToggleDefaultOptions ,
32
34
} 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' ;
44
35
45
36
/** @docs -private */
46
37
export const MAT_SLIDE_TOGGLE_VALUE_ACCESSOR = {
@@ -62,20 +53,6 @@ export class MatSlideToggleChange {
62
53
// Increasing integer for generating unique ids for slide-toggle components.
63
54
let nextUniqueId = 0 ;
64
55
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
-
79
56
@Component ( {
80
57
selector : 'mat-slide-toggle' ,
81
58
templateUrl : 'slide-toggle.html' ,
@@ -92,28 +69,18 @@ const _MatSlideToggleMixinBase = mixinTabIndex(
92
69
'[class.mat-mdc-slide-toggle-focused]' : '_focused' ,
93
70
'[class.mat-mdc-slide-toggle-checked]' : 'checked' ,
94
71
'[class._mat-animation-noopable]' : '_noopAnimations' ,
72
+ '[class]' : 'color ? "mat-" + color : ""' ,
95
73
} ,
96
74
exportAs : 'matSlideToggle' ,
97
75
encapsulation : ViewEncapsulation . None ,
98
76
changeDetection : ChangeDetectionStrategy . OnPush ,
99
77
providers : [ MAT_SLIDE_TOGGLE_VALUE_ACCESSOR ] ,
100
78
} )
101
- export class MatSlideToggle
102
- extends _MatSlideToggleMixinBase
103
- implements
104
- OnDestroy ,
105
- AfterContentInit ,
106
- ControlValueAccessor ,
107
- CanDisable ,
108
- CanColor ,
109
- HasTabIndex ,
110
- CanDisableRipple
111
- {
79
+ export class MatSlideToggle implements OnDestroy , AfterContentInit , ControlValueAccessor {
112
80
private _onChange = ( _ : any ) => { } ;
113
81
private _onTouched = ( ) => { } ;
114
82
115
83
private _uniqueId : string ;
116
- private _required : boolean = false ;
117
84
private _checked : boolean = false ;
118
85
119
86
private _createChangeEvent ( isChecked : boolean ) {
@@ -160,35 +127,35 @@ export class MatSlideToggle
160
127
@Input ( 'aria-describedby' ) ariaDescribedby : string ;
161
128
162
129
/** Whether the slide-toggle is required. */
163
- @Input ( )
164
- get required ( ) : boolean {
165
- return this . _required ;
166
- }
130
+ @Input ( { transform : booleanAttribute } ) required : boolean ;
167
131
168
- set required ( value : BooleanInput ) {
169
- this . _required = coerceBooleanProperty ( value ) ;
170
- }
132
+ // TODO(crisbeto): this should be a ThemePalette, but some internal apps were abusing
133
+ // the lack of type checking previously and assigning random strings.
134
+ /** Palette color of slide toggle. */
135
+ @Input ( ) color : string | undefined ;
136
+
137
+ /** Whether the slide toggle is disabled. */
138
+ @Input ( { transform : booleanAttribute } ) disabled : boolean = false ;
139
+
140
+ /** Whether the slide toggle has a ripple. */
141
+ @Input ( { transform : booleanAttribute } ) disableRipple : boolean = false ;
142
+
143
+ /** Tabindex of slide toggle. */
144
+ @Input ( { transform : ( value : unknown ) => ( value == null ? 0 : numberAttribute ( value ) ) } )
145
+ tabIndex : number = 0 ;
171
146
172
147
/** Whether the slide-toggle element is checked or not. */
173
- @Input ( )
148
+ @Input ( { transform : booleanAttribute } )
174
149
get checked ( ) : boolean {
175
150
return this . _checked ;
176
151
}
177
-
178
- set checked ( value : BooleanInput ) {
179
- this . _checked = coerceBooleanProperty ( value ) ;
152
+ set checked ( value : boolean ) {
153
+ this . _checked = value ;
180
154
this . _changeDetectorRef . markForCheck ( ) ;
181
155
}
182
156
183
157
/** 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 ;
158
+ @Input ( { transform : booleanAttribute } ) hideIcon : boolean ;
192
159
193
160
/** An event will be dispatched each time the slide-toggle changes its value. */
194
161
@Output ( ) readonly change = new EventEmitter < MatSlideToggleChange > ( ) ;
@@ -206,19 +173,18 @@ export class MatSlideToggle
206
173
}
207
174
208
175
constructor (
209
- elementRef : ElementRef ,
176
+ private _elementRef : ElementRef ,
210
177
protected _focusMonitor : FocusMonitor ,
211
178
protected _changeDetectorRef : ChangeDetectorRef ,
212
179
@Attribute ( 'tabindex' ) tabIndex : string ,
213
180
@Inject ( MAT_SLIDE_TOGGLE_DEFAULT_OPTIONS ) public defaults : MatSlideToggleDefaultOptions ,
214
181
@Optional ( ) @Inject ( ANIMATION_MODULE_TYPE ) animationMode ?: string ,
215
182
) {
216
- super ( elementRef ) ;
217
183
this . tabIndex = parseInt ( tabIndex ) || 0 ;
218
- this . color = this . defaultColor = defaults . color || 'accent' ;
184
+ this . color = defaults . color || 'accent' ;
219
185
this . _noopAnimations = animationMode === 'NoopAnimations' ;
220
186
this . id = this . _uniqueId = `mat-mdc-slide-toggle-${ ++ nextUniqueId } ` ;
221
- this . _hideIcon = defaults . hideIcon ?? false ;
187
+ this . hideIcon = defaults . hideIcon ?? false ;
222
188
this . _labelId = this . _uniqueId + '-label' ;
223
189
}
224
190
0 commit comments