@@ -10,40 +10,25 @@ import {
10
10
ChangeDetectionStrategy ,
11
11
Component ,
12
12
ViewEncapsulation ,
13
- AfterViewInit ,
14
- OnDestroy ,
15
13
forwardRef ,
16
14
ViewChild ,
17
15
ElementRef ,
18
- Input ,
19
- Output ,
20
- EventEmitter ,
21
16
ChangeDetectorRef ,
22
17
Attribute ,
23
18
Inject ,
24
19
Optional ,
25
20
} from '@angular/core' ;
26
- import { deprecated } from '@material/switch' ;
27
- import { ControlValueAccessor , NG_VALUE_ACCESSOR } from '@angular/forms' ;
28
- import {
29
- BooleanInput ,
30
- coerceBooleanProperty ,
31
- coerceNumberProperty ,
32
- NumberInput ,
33
- } from '@angular/cdk/coercion' ;
21
+ import { NG_VALUE_ACCESSOR } from '@angular/forms' ;
34
22
import { ANIMATION_MODULE_TYPE } from '@angular/platform-browser/animations' ;
35
- import { ThemePalette } from '@angular/material-experimental/mdc-core' ;
36
23
import { FocusMonitor } from '@angular/cdk/a11y' ;
24
+ import { _MatSlideToggleBase } from '@angular/material/slide-toggle' ;
37
25
import {
38
26
MAT_SLIDE_TOGGLE_DEFAULT_OPTIONS ,
39
27
MatSlideToggleDefaultOptions ,
40
28
} from './slide-toggle-config' ;
41
29
42
- // Increasing integer for generating unique ids for slide-toggle components.
43
- let nextUniqueId = 0 ;
44
-
45
30
/** @docs -private */
46
- export const MAT_SLIDE_TOGGLE_VALUE_ACCESSOR : any = {
31
+ export const MAT_SLIDE_TOGGLE_VALUE_ACCESSOR = {
47
32
provide : NG_VALUE_ACCESSOR ,
48
33
useExisting : forwardRef ( ( ) => MatSlideToggle ) ,
49
34
multi : true ,
@@ -63,6 +48,7 @@ export class MatSlideToggleChange {
63
48
selector : 'mat-slide-toggle' ,
64
49
templateUrl : 'slide-toggle.html' ,
65
50
styleUrls : [ 'slide-toggle.css' ] ,
51
+ inputs : [ 'disabled' , 'disableRipple' , 'color' , 'tabIndex' ] ,
66
52
host : {
67
53
'class' : 'mat-mdc-slide-toggle' ,
68
54
'[id]' : 'id' ,
@@ -71,9 +57,6 @@ export class MatSlideToggleChange {
71
57
'[attr.aria-label]' : 'null' ,
72
58
'[attr.name]' : 'null' ,
73
59
'[attr.aria-labelledby]' : 'null' ,
74
- '[class.mat-primary]' : 'color === "primary"' ,
75
- '[class.mat-accent]' : 'color !== "primary" && color !== "warn"' ,
76
- '[class.mat-warn]' : 'color === "warn"' ,
77
60
'[class.mat-mdc-slide-toggle-focused]' : '_focused' ,
78
61
'[class.mat-mdc-slide-toggle-checked]' : 'checked' ,
79
62
'[class._mat-animation-noopable]' : '_noopAnimations' ,
@@ -83,116 +66,9 @@ export class MatSlideToggleChange {
83
66
changeDetection : ChangeDetectionStrategy . OnPush ,
84
67
providers : [ MAT_SLIDE_TOGGLE_VALUE_ACCESSOR ] ,
85
68
} )
86
- export class MatSlideToggle implements ControlValueAccessor , AfterViewInit , OnDestroy {
87
- private _onChange = ( _ : any ) => { } ;
88
- private _onTouched = ( ) => { } ;
89
-
90
- private _uniqueId : string = `mat-mdc-slide-toggle-${ ++ nextUniqueId } ` ;
91
- private _required : boolean = false ;
92
- private _checked : boolean = false ;
93
- private _foundation : deprecated . MDCSwitchFoundation ;
94
- private _adapter : deprecated . MDCSwitchAdapter = {
95
- addClass : className => this . _switchElement . nativeElement . classList . add ( className ) ,
96
- removeClass : className => this . _switchElement . nativeElement . classList . remove ( className ) ,
97
- setNativeControlChecked : checked => ( this . _checked = checked ) ,
98
- setNativeControlDisabled : disabled => ( this . _disabled = disabled ) ,
99
- setNativeControlAttr : ( name , value ) => {
100
- this . _switchElement . nativeElement . setAttribute ( name , value ) ;
101
- } ,
102
- } ;
103
-
104
- /** Whether the slide toggle is currently focused. */
105
- _focused : boolean ;
106
-
107
- /** Whether noop animations are enabled. */
108
- _noopAnimations : boolean ;
109
-
69
+ export class MatSlideToggle extends _MatSlideToggleBase < MatSlideToggleChange > {
110
70
/** Unique ID for the label element. */
111
- _labelId = `mat-mdc-slide-toggle-label-${ ++ nextUniqueId } ` ;
112
-
113
- /** The color palette for this slide toggle. */
114
- @Input ( ) color : ThemePalette ;
115
-
116
- /** Name value will be applied to the button element if present. */
117
- @Input ( ) name : string | null = null ;
118
-
119
- /** A unique id for the slide-toggle button. If none is supplied, it will be auto-generated. */
120
- @Input ( ) id : string = this . _uniqueId ;
121
-
122
- /** Tabindex for the input element. */
123
- @Input ( )
124
- get tabIndex ( ) : number {
125
- return this . _tabIndex ;
126
- }
127
- set tabIndex ( value : NumberInput ) {
128
- this . _tabIndex = coerceNumberProperty ( value ) ;
129
- }
130
- private _tabIndex : number ;
131
-
132
- /** Whether the label should appear after or before the slide-toggle. Defaults to 'after'. */
133
- @Input ( ) labelPosition : 'before' | 'after' = 'after' ;
134
-
135
- /** Used to set the aria-label attribute on the underlying button element. */
136
- @Input ( 'aria-label' ) ariaLabel : string | null = null ;
137
-
138
- /** Used to set the aria-labelledby attribute on the underlying button element. */
139
- @Input ( 'aria-labelledby' ) ariaLabelledby : string | null = null ;
140
-
141
- /** Used to set the aria-describedby attribute on the underlying button element. */
142
- @Input ( 'aria-describedby' ) ariaDescribedby : string ;
143
-
144
- /** Whether the slide-toggle is required. */
145
- @Input ( )
146
- get required ( ) : boolean {
147
- return this . _required ;
148
- }
149
- set required ( value : BooleanInput ) {
150
- this . _required = coerceBooleanProperty ( value ) ;
151
- }
152
-
153
- /** Whether the slide-toggle element is checked or not. */
154
- @Input ( )
155
- get checked ( ) : boolean {
156
- return this . _checked ;
157
- }
158
- set checked ( value : BooleanInput ) {
159
- this . _checked = coerceBooleanProperty ( value ) ;
160
-
161
- if ( this . _foundation ) {
162
- this . _foundation . setChecked ( this . _checked ) ;
163
- }
164
- }
165
-
166
- /** Whether to disable the ripple on this checkbox. */
167
- @Input ( )
168
- get disableRipple ( ) : boolean {
169
- return this . _disableRipple ;
170
- }
171
- set disableRipple ( disableRipple : BooleanInput ) {
172
- this . _disableRipple = coerceBooleanProperty ( disableRipple ) ;
173
- }
174
- private _disableRipple = false ;
175
-
176
- /** Whether the slide toggle is disabled. */
177
- @Input ( )
178
- get disabled ( ) : boolean {
179
- return this . _disabled ;
180
- }
181
- set disabled ( disabled : BooleanInput ) {
182
- this . _disabled = coerceBooleanProperty ( disabled ) ;
183
-
184
- if ( this . _foundation ) {
185
- this . _foundation . setDisabled ( this . _disabled ) ;
186
- }
187
- }
188
- private _disabled = false ;
189
-
190
- /** An event will be dispatched each time the slide-toggle changes its value. */
191
- @Output ( ) readonly change : EventEmitter < MatSlideToggleChange > =
192
- new EventEmitter < MatSlideToggleChange > ( ) ;
193
-
194
- /** Event will be dispatched each time the slide-toggle input is toggled. */
195
- @Output ( ) readonly toggleChange : EventEmitter < void > = new EventEmitter < void > ( ) ;
71
+ _labelId : string ;
196
72
197
73
/** Returns the unique id for the visual hidden button. */
198
74
get buttonId ( ) : string {
@@ -203,51 +79,29 @@ export class MatSlideToggle implements ControlValueAccessor, AfterViewInit, OnDe
203
79
@ViewChild ( 'switch' ) _switchElement : ElementRef < HTMLElement > ;
204
80
205
81
constructor (
206
- private _elementRef : ElementRef ,
207
- private _focusMonitor : FocusMonitor ,
208
- private _changeDetectorRef : ChangeDetectorRef ,
82
+ elementRef : ElementRef ,
83
+ focusMonitor : FocusMonitor ,
84
+ changeDetectorRef : ChangeDetectorRef ,
209
85
@Attribute ( 'tabindex' ) tabIndex : string ,
210
86
@Inject ( MAT_SLIDE_TOGGLE_DEFAULT_OPTIONS )
211
- public defaults : MatSlideToggleDefaultOptions ,
87
+ defaults : MatSlideToggleDefaultOptions ,
212
88
@Optional ( ) @Inject ( ANIMATION_MODULE_TYPE ) animationMode ?: string ,
213
89
) {
214
- this . tabIndex = parseInt ( tabIndex ) || 0 ;
215
- this . color = defaults . color || 'accent' ;
216
- this . _noopAnimations = animationMode === 'NoopAnimations' ;
217
- }
218
-
219
- ngAfterViewInit ( ) {
220
- const foundation = ( this . _foundation = new deprecated . MDCSwitchFoundation ( this . _adapter ) ) ;
221
- foundation . setDisabled ( this . disabled ) ;
222
- foundation . setChecked ( this . checked ) ;
223
-
224
- this . _focusMonitor . monitor ( this . _elementRef , true ) . subscribe ( focusOrigin => {
225
- if ( focusOrigin === 'keyboard' || focusOrigin === 'program' ) {
226
- this . _focused = true ;
227
- } else if ( ! focusOrigin ) {
228
- // When a focused element becomes disabled, the browser *immediately* fires a blur event.
229
- // Angular does not expect events to be raised during change detection, so any state
230
- // change (such as a form control's ng-touched) will cause a changed-after-checked error.
231
- // See https://github.com/angular/angular/issues/17793. To work around this, we defer
232
- // telling the form control it has been touched until the next tick.
233
- Promise . resolve ( ) . then ( ( ) => {
234
- this . _focused = false ;
235
- this . _onTouched ( ) ;
236
- this . _changeDetectorRef . markForCheck ( ) ;
237
- } ) ;
238
- }
239
- } ) ;
240
- }
241
-
242
- ngOnDestroy ( ) {
243
- this . _focusMonitor . stopMonitoring ( this . _elementRef ) ;
244
- this . _foundation ?. destroy ( ) ;
90
+ super (
91
+ elementRef ,
92
+ focusMonitor ,
93
+ changeDetectorRef ,
94
+ tabIndex ,
95
+ defaults ,
96
+ animationMode ,
97
+ 'mat-mdc-slide-toggle-' ,
98
+ ) ;
99
+ this . _labelId = this . _uniqueId + '-label' ;
245
100
}
246
101
247
102
/** Method being called whenever the underlying button is clicked. */
248
- _handleClick ( event : Event ) {
103
+ _handleClick ( ) {
249
104
this . toggleChange . emit ( ) ;
250
- this . _foundation . handleChange ( event ) ;
251
105
252
106
if ( ! this . defaults . disableToggleValue ) {
253
107
this . checked = ! this . checked ;
@@ -256,37 +110,13 @@ export class MatSlideToggle implements ControlValueAccessor, AfterViewInit, OnDe
256
110
}
257
111
}
258
112
259
- /** Implemented as part of ControlValueAccessor. */
260
- writeValue ( value : any ) : void {
261
- this . checked = ! ! value ;
262
- this . _changeDetectorRef . markForCheck ( ) ;
263
- }
264
-
265
- /** Implemented as part of ControlValueAccessor. */
266
- registerOnChange ( fn : any ) : void {
267
- this . _onChange = fn ;
268
- }
269
-
270
- /** Implemented as part of ControlValueAccessor. */
271
- registerOnTouched ( fn : any ) : void {
272
- this . _onTouched = fn ;
273
- }
274
-
275
- /** Implemented as a part of ControlValueAccessor. */
276
- setDisabledState ( isDisabled : boolean ) : void {
277
- this . disabled = isDisabled ;
278
- this . _changeDetectorRef . markForCheck ( ) ;
279
- }
280
-
281
113
/** Focuses the slide-toggle. */
282
114
focus ( ) : void {
283
115
this . _switchElement . nativeElement . focus ( ) ;
284
116
}
285
117
286
- /** Toggles the checked state of the slide-toggle. */
287
- toggle ( ) : void {
288
- this . checked = ! this . checked ;
289
- this . _onChange ( this . checked ) ;
118
+ protected _createChangeEvent ( isChecked : boolean ) {
119
+ return new MatSlideToggleChange ( this , isChecked ) ;
290
120
}
291
121
292
122
_getAriaLabelledBy ( ) {
0 commit comments