Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion goldens/material/slide-toggle/index.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export class MatSlideToggle implements OnDestroy, AfterContentInit, OnChanges, C
_getAriaLabelledBy(): string | null;
_handleClick(): void;
hideIcon: boolean;
hideLabel: boolean;
id: string;
get inputId(): string;
_labelId: string;
Expand All @@ -66,6 +67,8 @@ export class MatSlideToggle implements OnDestroy, AfterContentInit, OnChanges, C
// (undocumented)
static ngAcceptInputType_hideIcon: unknown;
// (undocumented)
static ngAcceptInputType_hideLabel: unknown;
// (undocumented)
static ngAcceptInputType_required: unknown;
// (undocumented)
static ngAcceptInputType_tabIndex: unknown;
Expand All @@ -88,7 +91,7 @@ export class MatSlideToggle implements OnDestroy, AfterContentInit, OnChanges, C
validate(control: AbstractControl<boolean>): ValidationErrors | null;
writeValue(value: any): void;
// (undocumented)
static ɵcmp: i0.ɵɵComponentDeclaration<MatSlideToggle, "mat-slide-toggle", ["matSlideToggle"], { "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; }; "color": { "alias": "color"; "required": false; }; "disabled": { "alias": "disabled"; "required": false; }; "disableRipple": { "alias": "disableRipple"; "required": false; }; "tabIndex": { "alias": "tabIndex"; "required": false; }; "checked": { "alias": "checked"; "required": false; }; "hideIcon": { "alias": "hideIcon"; "required": false; }; "disabledInteractive": { "alias": "disabledInteractive"; "required": false; }; }, { "change": "change"; "toggleChange": "toggleChange"; }, never, ["*"], true, never>;
static ɵcmp: i0.ɵɵComponentDeclaration<MatSlideToggle, "mat-slide-toggle", ["matSlideToggle"], { "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; }; "color": { "alias": "color"; "required": false; }; "disabled": { "alias": "disabled"; "required": false; }; "disableRipple": { "alias": "disableRipple"; "required": false; }; "tabIndex": { "alias": "tabIndex"; "required": false; }; "checked": { "alias": "checked"; "required": false; }; "hideIcon": { "alias": "hideIcon"; "required": false; }; "hideLabel": { "alias": "hideLabel"; "required": false; }; "disabledInteractive": { "alias": "disabledInteractive"; "required": false; }; }, { "change": "change"; "toggleChange": "toggleChange"; }, never, ["*"], true, never>;
// (undocumented)
static ɵfac: i0.ɵɵFactoryDeclaration<MatSlideToggle, never>;
}
Expand Down
4 changes: 4 additions & 0 deletions src/dev-app/slide-toggle/slide-toggle-demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
<mat-slide-toggle labelPosition="before" [disabled]="firstToggle">Disable Bound</mat-slide-toggle>
<mat-slide-toggle labelPosition="before" hideIcon [(ngModel)]="firstToggle">No icon</mat-slide-toggle>

<p>With no label.</p>

<mat-slide-toggle aria-label="Toggle only" hideLabel color="primary" [(ngModel)]="firstToggle" />

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

<form #form="ngForm" (ngSubmit)="onFormSubmit()">
Expand Down
16 changes: 9 additions & 7 deletions src/material/slide-toggle/slide-toggle.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,13 @@
</span>
</button>

<!--
Clicking on the label will trigger another click event from the button.
Stop propagation here so other listeners further up in the DOM don't execute twice.
-->
<label class="mdc-label" [for]="buttonId" [attr.id]="_labelId" (click)="$event.stopPropagation()">
<ng-content></ng-content>
</label>
@if (!hideLabel) {
<!--
Clicking on the label will trigger another click event from the button.
Stop propagation here so other listeners further up in the DOM don't execute twice.
-->
<label class="mdc-label" [for]="buttonId" [attr.id]="_labelId" (click)="$event.stopPropagation()">
<ng-content></ng-content>
</label>
}
</div>
2 changes: 2 additions & 0 deletions src/material/slide-toggle/slide-toggle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,8 @@ describe('MatSlideToggle with forms', () => {

it('should update checked state on click if control is checked initially', fakeAsync(() => {
fixture = TestBed.createComponent(SlideToggleWithModel);
fixture.detectChanges();

slideToggle = fixture.debugElement.query(By.directive(MatSlideToggle))!.componentInstance;
labelElement = fixture.debugElement.query(By.css('label'))!.nativeElement;

Expand Down
3 changes: 3 additions & 0 deletions src/material/slide-toggle/slide-toggle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ export class MatSlideToggle
/** Whether to hide the icon inside of the slide toggle. */
@Input({transform: booleanAttribute}) hideIcon: boolean;

/** Whether to hide label for the slide toggle. */
@Input({transform: booleanAttribute}) hideLabel: boolean;

/** Whether the slide toggle should remain interactive when it is disabled. */
@Input({transform: booleanAttribute}) disabledInteractive: boolean;

Expand Down
Loading