Skip to content

Commit fde4774

Browse files
committed
refactor(material/slide-toggle): remove mixin class usages
Replaces mixin class usages in the slide toggle with input transforms.
1 parent ece4ddc commit fde4774

File tree

4 files changed

+47
-76
lines changed

4 files changed

+47
-76
lines changed

src/material/slide-toggle/BUILD.bazel

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ ng_module(
2121
),
2222
assets = [":slide_toggle_scss"] + glob(["**/*.html"]),
2323
deps = [
24-
"//src/cdk/coercion",
2524
"//src/material/core",
2625
"@npm//@angular/animations",
2726
"@npm//@angular/common",

src/material/slide-toggle/slide-toggle.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
[class.mdc-switch--unselected]="!checked"
99
[class.mdc-switch--checked]="checked"
1010
[class.mdc-switch--disabled]="disabled"
11-
[tabIndex]="tabIndex"
11+
[tabIndex]="disabled ? -1 : tabIndex"
1212
[disabled]="disabled"
1313
[attr.id]="buttonId"
1414
[attr.name]="name"

src/material/slide-toggle/slide-toggle.ts

Lines changed: 25 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import {
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';
2629
import {ControlValueAccessor, NG_VALUE_ACCESSOR} from '@angular/forms';
2730
import {ANIMATION_MODULE_TYPE} from '@angular/platform-browser/animations';
2831
import {FocusMonitor} from '@angular/cdk/a11y';
2932
import {
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 */
4638
export const MAT_SLIDE_TOGGLE_VALUE_ACCESSOR = {
@@ -62,20 +54,6 @@ export class MatSlideToggleChange {
6254
// Increasing integer for generating unique ids for slide-toggle components.
6355
let 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

tools/public_api_guard/material/slide-toggle.md

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,13 @@
44
55
```ts
66

7-
import { _AbstractConstructor } from '@angular/material/core';
87
import { AfterContentInit } from '@angular/core';
9-
import { BooleanInput } from '@angular/cdk/coercion';
10-
import { CanColor } from '@angular/material/core';
11-
import { CanDisable } from '@angular/material/core';
12-
import { CanDisableRipple } from '@angular/material/core';
138
import { ChangeDetectorRef } from '@angular/core';
149
import { CheckboxRequiredValidator } from '@angular/forms';
15-
import { _Constructor } from '@angular/material/core';
1610
import { ControlValueAccessor } from '@angular/forms';
1711
import { ElementRef } from '@angular/core';
1812
import { EventEmitter } from '@angular/core';
1913
import { FocusMonitor } from '@angular/cdk/a11y';
20-
import { HasTabIndex } from '@angular/material/core';
2114
import * as i0 from '@angular/core';
2215
import * as i3 from '@angular/material/core';
2316
import * as i4 from '@angular/common';
@@ -41,8 +34,8 @@ export const MAT_SLIDE_TOGGLE_VALUE_ACCESSOR: {
4134
};
4235

4336
// @public (undocumented)
44-
export class MatSlideToggle extends _MatSlideToggleMixinBase implements OnDestroy, AfterContentInit, ControlValueAccessor, CanDisable, CanColor, HasTabIndex, CanDisableRipple {
45-
constructor(elementRef: ElementRef, _focusMonitor: FocusMonitor, _changeDetectorRef: ChangeDetectorRef, tabIndex: string, defaults: MatSlideToggleDefaultOptions, animationMode?: string);
37+
export class MatSlideToggle implements OnDestroy, AfterContentInit, ControlValueAccessor {
38+
constructor(_elementRef: ElementRef, _focusMonitor: FocusMonitor, _changeDetectorRef: ChangeDetectorRef, tabIndex: string, defaults: MatSlideToggleDefaultOptions, animationMode?: string);
4639
ariaDescribedby: string;
4740
ariaLabel: string | null;
4841
ariaLabelledby: string | null;
@@ -51,9 +44,12 @@ export class MatSlideToggle extends _MatSlideToggleMixinBase implements OnDestro
5144
// (undocumented)
5245
protected _changeDetectorRef: ChangeDetectorRef;
5346
get checked(): boolean;
54-
set checked(value: BooleanInput);
47+
set checked(value: boolean);
48+
color: ThemePalette;
5549
// (undocumented)
5650
defaults: MatSlideToggleDefaultOptions;
51+
disabled: boolean;
52+
disableRipple: boolean;
5753
protected _emitChangeEvent(): void;
5854
focus(): void;
5955
_focused: boolean;
@@ -62,24 +58,35 @@ export class MatSlideToggle extends _MatSlideToggleMixinBase implements OnDestro
6258
// (undocumented)
6359
_getAriaLabelledBy(): string | null;
6460
_handleClick(): void;
65-
get hideIcon(): boolean;
66-
set hideIcon(value: BooleanInput);
61+
hideIcon: boolean;
6762
id: string;
6863
get inputId(): string;
6964
_labelId: string;
7065
labelPosition: 'before' | 'after';
7166
name: string | null;
7267
// (undocumented)
68+
static ngAcceptInputType_checked: unknown;
69+
// (undocumented)
70+
static ngAcceptInputType_disabled: unknown;
71+
// (undocumented)
72+
static ngAcceptInputType_disableRipple: unknown;
73+
// (undocumented)
74+
static ngAcceptInputType_hideIcon: unknown;
75+
// (undocumented)
76+
static ngAcceptInputType_required: unknown;
77+
// (undocumented)
78+
static ngAcceptInputType_tabIndex: unknown;
79+
// (undocumented)
7380
ngAfterContentInit(): void;
7481
// (undocumented)
7582
ngOnDestroy(): void;
7683
_noopAnimations: boolean;
7784
registerOnChange(fn: any): void;
7885
registerOnTouched(fn: any): void;
79-
get required(): boolean;
80-
set required(value: BooleanInput);
86+
required: boolean;
8187
setDisabledState(isDisabled: boolean): void;
8288
_switchElement: ElementRef<HTMLElement>;
89+
tabIndex: number;
8390
toggle(): void;
8491
readonly toggleChange: EventEmitter<void>;
8592
writeValue(value: any): void;

0 commit comments

Comments
 (0)