9
9
import {
10
10
AfterContentInit ,
11
11
Attribute ,
12
+ booleanAttribute ,
12
13
ChangeDetectionStrategy ,
13
14
ChangeDetectorRef ,
14
15
Component ,
@@ -17,30 +18,21 @@ import {
17
18
forwardRef ,
18
19
Inject ,
19
20
Input ,
21
+ numberAttribute ,
20
22
OnDestroy ,
21
23
Optional ,
22
24
Output ,
23
25
ViewChild ,
24
26
ViewEncapsulation ,
25
27
} from '@angular/core' ;
28
+ import { ThemePalette } from '@angular/material/core' ;
26
29
import { ControlValueAccessor , NG_VALUE_ACCESSOR } from '@angular/forms' ;
27
30
import { ANIMATION_MODULE_TYPE } from '@angular/platform-browser/animations' ;
28
31
import { FocusMonitor } from '@angular/cdk/a11y' ;
29
32
import {
30
33
MAT_SLIDE_TOGGLE_DEFAULT_OPTIONS ,
31
34
MatSlideToggleDefaultOptions ,
32
35
} 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
36
45
37
/** @docs -private */
46
38
export const MAT_SLIDE_TOGGLE_VALUE_ACCESSOR = {
@@ -62,20 +54,6 @@ export class MatSlideToggleChange {
62
54
// Increasing integer for generating unique ids for slide-toggle components.
63
55
let nextUniqueId = 0 ;
64
56
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
57
@Component ( {
80
58
selector : 'mat-slide-toggle' ,
81
59
templateUrl : 'slide-toggle.html' ,
@@ -92,28 +70,18 @@ const _MatSlideToggleMixinBase = mixinTabIndex(
92
70
'[class.mat-mdc-slide-toggle-focused]' : '_focused' ,
93
71
'[class.mat-mdc-slide-toggle-checked]' : 'checked' ,
94
72
'[class._mat-animation-noopable]' : '_noopAnimations' ,
73
+ '[class]' : 'color ? "mat-" + color : ""' ,
95
74
} ,
96
75
exportAs : 'matSlideToggle' ,
97
76
encapsulation : ViewEncapsulation . None ,
98
77
changeDetection : ChangeDetectionStrategy . OnPush ,
99
78
providers : [ MAT_SLIDE_TOGGLE_VALUE_ACCESSOR ] ,
100
79
} )
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 {
112
81
private _onChange = ( _ : any ) => { } ;
113
82
private _onTouched = ( ) => { } ;
114
83
115
84
private _uniqueId : string ;
116
- private _required : boolean = false ;
117
85
private _checked : boolean = false ;
118
86
119
87
private _createChangeEvent ( isChecked : boolean ) {
@@ -160,35 +128,33 @@ export class MatSlideToggle
160
128
@Input ( 'aria-describedby' ) ariaDescribedby : string ;
161
129
162
130
/** Whether the slide-toggle is required. */
163
- @Input ( )
164
- get required ( ) : boolean {
165
- return this . _required ;
166
- }
131
+ @Input ( { transform : booleanAttribute } ) required : boolean ;
167
132
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 ;
171
145
172
146
/** Whether the slide-toggle element is checked or not. */
173
- @Input ( )
147
+ @Input ( { transform : booleanAttribute } )
174
148
get checked ( ) : boolean {
175
149
return this . _checked ;
176
150
}
177
-
178
- set checked ( value : BooleanInput ) {
179
- this . _checked = coerceBooleanProperty ( value ) ;
151
+ set checked ( value : boolean ) {
152
+ this . _checked = value ;
180
153
this . _changeDetectorRef . markForCheck ( ) ;
181
154
}
182
155
183
156
/** 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 ;
192
158
193
159
/** An event will be dispatched each time the slide-toggle changes its value. */
194
160
@Output ( ) readonly change = new EventEmitter < MatSlideToggleChange > ( ) ;
@@ -206,19 +172,18 @@ export class MatSlideToggle
206
172
}
207
173
208
174
constructor (
209
- elementRef : ElementRef ,
175
+ private _elementRef : ElementRef ,
210
176
protected _focusMonitor : FocusMonitor ,
211
177
protected _changeDetectorRef : ChangeDetectorRef ,
212
178
@Attribute ( 'tabindex' ) tabIndex : string ,
213
179
@Inject ( MAT_SLIDE_TOGGLE_DEFAULT_OPTIONS ) public defaults : MatSlideToggleDefaultOptions ,
214
180
@Optional ( ) @Inject ( ANIMATION_MODULE_TYPE ) animationMode ?: string ,
215
181
) {
216
- super ( elementRef ) ;
217
182
this . tabIndex = parseInt ( tabIndex ) || 0 ;
218
- this . color = this . defaultColor = defaults . color || 'accent' ;
183
+ this . color = defaults . color || 'accent' ;
219
184
this . _noopAnimations = animationMode === 'NoopAnimations' ;
220
185
this . id = this . _uniqueId = `mat-mdc-slide-toggle-${ ++ nextUniqueId } ` ;
221
- this . _hideIcon = defaults . hideIcon ?? false ;
186
+ this . hideIcon = defaults . hideIcon ?? false ;
222
187
this . _labelId = this . _uniqueId + '-label' ;
223
188
}
224
189
0 commit comments