Skip to content

feat(material/slide-toggle): add aria-describedby input #23104

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
[attr.aria-checked]="checked.toString()"
[attr.aria-label]="ariaLabel"
[attr.aria-labelledby]="ariaLabelledby"
[attr.aria-describedby]="ariaDescribedby"
(change)="_onChangeEvent($event)"
(click)="_onInputClick($event)">
</div>
Expand Down
14 changes: 14 additions & 0 deletions src/material-experimental/mdc-slide-toggle/slide-toggle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,18 @@ describe('MDC-based MatSlideToggle without forms', () => {
expect(inputElement.hasAttribute('aria-labelledby')).toBeFalsy();
});

it('should forward the aria-describedby attribute to the input', () => {
testComponent.slideAriaDescribedBy = 'some-element';
fixture.detectChanges();

expect(inputElement.getAttribute('aria-describedby')).toBe('some-element');

testComponent.slideAriaDescribedBy = null;
fixture.detectChanges();

expect(inputElement.hasAttribute('aria-describedby')).toBe(false);
});

it('should set the `for` attribute to the id of the input element', () => {
expect(labelElement.getAttribute('for')).toBeTruthy();
expect(inputElement.getAttribute('id')).toBeTruthy();
Expand Down Expand Up @@ -810,6 +822,7 @@ describe('MDC-based MatSlideToggle with forms', () => {
[name]="slideName"
[aria-label]="slideLabel"
[aria-labelledby]="slideLabelledBy"
[aria-describedby]="slideAriaDescribedBy"
[tabIndex]="slideTabindex"
[labelPosition]="labelPosition"
[disableRipple]="disableRipple"
Expand All @@ -830,6 +843,7 @@ class SlideToggleBasic {
slideName: string | null;
slideLabel: string | null;
slideLabelledBy: string | null;
slideAriaDescribedBy: string | null;
slideTabindex: number;
lastEvent: MatSlideToggleChange;
labelPosition: string;
Expand Down
3 changes: 3 additions & 0 deletions src/material-experimental/mdc-slide-toggle/slide-toggle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ export class MatSlideToggle implements ControlValueAccessor, AfterViewInit, OnDe
/** Used to set the aria-labelledby attribute on the underlying input element. */
@Input('aria-labelledby') ariaLabelledby: string | null = null;

/** Used to set the aria-describedby attribute on the underlying input element. */
@Input('aria-describedby') ariaDescribedby: string;

/** Whether the slide-toggle is required. */
@Input()
get required(): boolean { return this._required; }
Expand Down
1 change: 1 addition & 0 deletions src/material/slide-toggle/slide-toggle.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
[attr.aria-checked]="checked.toString()"
[attr.aria-label]="ariaLabel"
[attr.aria-labelledby]="ariaLabelledby"
[attr.aria-describedby]="ariaDescribedby"
(change)="_onChangeEvent($event)"
(click)="_onInputClick($event)">

Expand Down
14 changes: 14 additions & 0 deletions src/material/slide-toggle/slide-toggle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,18 @@ describe('MatSlideToggle without forms', () => {
expect(inputElement.hasAttribute('aria-labelledby')).toBeFalsy();
});

it('should forward the aria-describedby attribute to the input', () => {
testComponent.slideAriaDescribedBy = 'some-element';
fixture.detectChanges();

expect(inputElement.getAttribute('aria-describedby')).toBe('some-element');

testComponent.slideAriaDescribedBy = null;
fixture.detectChanges();

expect(inputElement.hasAttribute('aria-describedby')).toBe(false);
});

it('should set the `for` attribute to the id of the input element', () => {
expect(labelElement.getAttribute('for')).toBeTruthy();
expect(inputElement.getAttribute('id')).toBeTruthy();
Expand Down Expand Up @@ -886,6 +898,7 @@ describe('MatSlideToggle with forms', () => {
[name]="slideName"
[aria-label]="slideLabel"
[aria-labelledby]="slideLabelledBy"
[aria-describedby]="slideAriaDescribedBy"
[tabIndex]="slideTabindex"
[labelPosition]="labelPosition"
[disableRipple]="disableRipple"
Expand All @@ -906,6 +919,7 @@ class SlideToggleBasic {
slideName: string | null;
slideLabel: string | null;
slideLabelledBy: string | null;
slideAriaDescribedBy: string | null;
slideTabindex: number;
lastEvent: MatSlideToggleChange;
labelPosition: string;
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 @@ -127,6 +127,9 @@ export class MatSlideToggle extends _MatSlideToggleBase implements OnDestroy, Af
/** Used to set the aria-labelledby attribute on the underlying input element. */
@Input('aria-labelledby') ariaLabelledby: string | null = null;

/** Used to set the aria-describedby attribute on the underlying input element. */
@Input('aria-describedby') ariaDescribedby: string;

/** Whether the slide-toggle is required. */
@Input()
get required(): boolean { return this._required; }
Expand Down
3 changes: 2 additions & 1 deletion tools/public_api_guard/material/slide-toggle.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export declare class MatSlideToggle extends _MatSlideToggleBase implements OnDes
_noopAnimations: boolean;
_thumbBarEl: ElementRef;
_thumbEl: ElementRef;
ariaDescribedby: string;
ariaLabel: string | null;
ariaLabelledby: string | null;
readonly change: EventEmitter<MatSlideToggleChange>;
Expand Down Expand Up @@ -45,7 +46,7 @@ export declare class MatSlideToggle extends _MatSlideToggleBase implements OnDes
static ngAcceptInputType_disabled: BooleanInput;
static ngAcceptInputType_required: BooleanInput;
static ngAcceptInputType_tabIndex: NumberInput;
static ɵcmp: i0.ɵɵComponentDeclaration<MatSlideToggle, "mat-slide-toggle", ["matSlideToggle"], { "disabled": "disabled"; "disableRipple": "disableRipple"; "color": "color"; "tabIndex": "tabIndex"; "name": "name"; "id": "id"; "labelPosition": "labelPosition"; "ariaLabel": "aria-label"; "ariaLabelledby": "aria-labelledby"; "required": "required"; "checked": "checked"; }, { "change": "change"; "toggleChange": "toggleChange"; }, never, ["*"]>;
static ɵcmp: i0.ɵɵComponentDeclaration<MatSlideToggle, "mat-slide-toggle", ["matSlideToggle"], { "disabled": "disabled"; "disableRipple": "disableRipple"; "color": "color"; "tabIndex": "tabIndex"; "name": "name"; "id": "id"; "labelPosition": "labelPosition"; "ariaLabel": "aria-label"; "ariaLabelledby": "aria-labelledby"; "ariaDescribedby": "aria-describedby"; "required": "required"; "checked": "checked"; }, { "change": "change"; "toggleChange": "toggleChange"; }, never, ["*"]>;
static ɵfac: i0.ɵɵFactoryDeclaration<MatSlideToggle, [null, null, null, { attribute: "tabindex"; }, null, { optional: true; }]>;
}

Expand Down