Skip to content

Commit

Permalink
feat(material/slide-toggle): allow for icon to be hidden (angular#27330)
Browse files Browse the repository at this point in the history
Adds an input that allows users to opt into hiding the slide toggle icon.

Fixes angular#27321.
  • Loading branch information
crisbeto authored Jun 20, 2023
1 parent dd9300e commit df30433
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 23 deletions.
26 changes: 6 additions & 20 deletions src/dev-app/slide-toggle/slide-toggle-demo.html
Original file line number Diff line number Diff line change
@@ -1,29 +1,15 @@
<div class="demo-slide-toggle">

<mat-slide-toggle color="primary" [(ngModel)]="firstToggle">
Default Slide Toggle
</mat-slide-toggle>

<mat-slide-toggle [(ngModel)]="firstToggle" disabled>
Disabled Slide Toggle
</mat-slide-toggle>

<mat-slide-toggle [disabled]="firstToggle">
Disable Bound
</mat-slide-toggle>
<mat-slide-toggle color="primary" [(ngModel)]="firstToggle">Default Slide Toggle</mat-slide-toggle>
<mat-slide-toggle [(ngModel)]="firstToggle" disabled>Disabled Slide Toggle</mat-slide-toggle>
<mat-slide-toggle [disabled]="firstToggle">Disable Bound</mat-slide-toggle>
<mat-slide-toggle hideIcon [(ngModel)]="firstToggle">No icon</mat-slide-toggle>

<p>Example where the slide toggle is required inside of a form.</p>

<form #form="ngForm" (ngSubmit)="onFormSubmit()">

<mat-slide-toggle name="slideToggle" [(ngModel)]="formToggle">
Slide Toggle
</mat-slide-toggle>

<mat-slide-toggle name="slideToggle" [(ngModel)]="formToggle">Slide Toggle</mat-slide-toggle>
<p>
<button mat-raised-button type="submit">Submit Form</button>
</p>

</form>

</div>
</div>
5 changes: 4 additions & 1 deletion src/material/slide-toggle/slide-toggle-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@ export interface MatSlideToggleDefaultOptions {

/** Default color for slide toggles. */
color?: ThemePalette;

/** Whether to hide the icon inside the slide toggle. */
hideIcon?: boolean;
}

/** Injection token to be used to override the default options for `mat-slide-toggle`. */
export const MAT_SLIDE_TOGGLE_DEFAULT_OPTIONS = new InjectionToken<MatSlideToggleDefaultOptions>(
'mat-slide-toggle-default-options',
{
providedIn: 'root',
factory: () => ({disableToggleValue: false}),
factory: () => ({disableToggleValue: false, hideIcon: false}),
},
);
2 changes: 1 addition & 1 deletion src/material/slide-toggle/slide-toggle.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
[matRippleDisabled]="disableRipple || disabled"
[matRippleCentered]="true"></div>
</div>
<div class="mdc-switch__icons">
<div class="mdc-switch__icons" *ngIf="!hideIcon">
<svg
class="mdc-switch__icon mdc-switch__icon--on"
viewBox="0 0 24 24"
Expand Down
11 changes: 11 additions & 0 deletions src/material/slide-toggle/slide-toggle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,15 @@ describe('MDC-based MatSlideToggle without forms', () => {
const rippleElement = slideToggleElement.querySelector('.mat-mdc-slide-toggle-ripple')!;
expect(rippleElement.classList).toContain('mat-mdc-focus-indicator');
}));

it('should be able to hide the icon', fakeAsync(() => {
expect(slideToggleElement.querySelector('.mdc-switch__icons')).toBeTruthy();

testComponent.hideIcon = true;
fixture.detectChanges();

expect(slideToggleElement.querySelector('.mdc-switch__icons')).toBeFalsy();
}));
});

describe('custom template', () => {
Expand Down Expand Up @@ -798,6 +807,7 @@ describe('MDC-based MatSlideToggle with forms', () => {
[tabIndex]="slideTabindex"
[labelPosition]="labelPosition"
[disableRipple]="disableRipple"
[hideIcon]="hideIcon"
(toggleChange)="onSlideToggleChange()"
(dragChange)="onSlideDragChange()"
(change)="onSlideChange($event)"
Expand All @@ -822,6 +832,7 @@ class SlideToggleBasic {
toggleTriggered: number = 0;
dragTriggered: number = 0;
direction: Direction = 'ltr';
hideIcon = false;

onSlideClick: (event?: Event) => void = () => {};
onSlideChange = (event: MatSlideToggleChange) => (this.lastEvent = event);
Expand Down
11 changes: 11 additions & 0 deletions src/material/slide-toggle/slide-toggle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,16 @@ export abstract class _MatSlideToggleBase<T>
this._changeDetectorRef.markForCheck();
}

/** Whether to hide the icon inside of the slide toggle. */
@Input()
get hideIcon(): boolean {
return this._hideIcon;
}
set hideIcon(value: BooleanInput) {
this._hideIcon = coerceBooleanProperty(value);
}
private _hideIcon = false;

/** An event will be dispatched each time the slide-toggle changes its value. */
@Output() readonly change: EventEmitter<T> = new EventEmitter<T>();

Expand Down Expand Up @@ -174,6 +184,7 @@ export abstract class _MatSlideToggleBase<T>
this.color = this.defaultColor = defaults.color || 'accent';
this._noopAnimations = animationMode === 'NoopAnimations';
this.id = this._uniqueId = `${idPrefix}${++nextUniqueId}`;
this._hideIcon = defaults.hideIcon ?? false;
}

ngAfterContentInit() {
Expand Down
5 changes: 4 additions & 1 deletion tools/public_api_guard/material/slide-toggle.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ export abstract class _MatSlideToggleBase<T> extends _MatSlideToggleMixinBase im
_focused: boolean;
// (undocumented)
protected _focusMonitor: FocusMonitor;
get hideIcon(): boolean;
set hideIcon(value: BooleanInput);
id: string;
get inputId(): string;
labelPosition: 'before' | 'after';
Expand All @@ -102,7 +104,7 @@ export abstract class _MatSlideToggleBase<T> extends _MatSlideToggleMixinBase im
protected _uniqueId: string;
writeValue(value: any): void;
// (undocumented)
static ɵdir: i0.ɵɵDirectiveDeclaration<_MatSlideToggleBase<any>, never, never, { "name": { "alias": "name"; "required": false; }; "id": { "alias": "id"; "required": false; }; "labelPosition": { "alias": "labelPosition"; "required": false; }; "ariaLabel": { "alias": "aria-label"; "required": false; }; "ariaLabelledby": { "alias": "aria-labelledby"; "required": false; }; "ariaDescribedby": { "alias": "aria-describedby"; "required": false; }; "required": { "alias": "required"; "required": false; }; "checked": { "alias": "checked"; "required": false; }; }, { "change": "change"; "toggleChange": "toggleChange"; }, never, never, false, never>;
static ɵdir: i0.ɵɵDirectiveDeclaration<_MatSlideToggleBase<any>, never, never, { "name": { "alias": "name"; "required": false; }; "id": { "alias": "id"; "required": false; }; "labelPosition": { "alias": "labelPosition"; "required": false; }; "ariaLabel": { "alias": "aria-label"; "required": false; }; "ariaLabelledby": { "alias": "aria-labelledby"; "required": false; }; "ariaDescribedby": { "alias": "aria-describedby"; "required": false; }; "required": { "alias": "required"; "required": false; }; "checked": { "alias": "checked"; "required": false; }; "hideIcon": { "alias": "hideIcon"; "required": false; }; }, { "change": "change"; "toggleChange": "toggleChange"; }, never, never, false, never>;
// (undocumented)
static ɵfac: i0.ɵɵFactoryDeclaration<_MatSlideToggleBase<any>, never>;
}
Expand All @@ -120,6 +122,7 @@ export class MatSlideToggleChange {
export interface MatSlideToggleDefaultOptions {
color?: ThemePalette;
disableToggleValue?: boolean;
hideIcon?: boolean;
}

// @public (undocumented)
Expand Down

0 comments on commit df30433

Please sign in to comment.