Skip to content

Commit 8d73869

Browse files
crisbetojelbourn
authored andcommitted
fix(slide-toggle): clear static aria attributes from host nodes (#17085)
Follow-up from #16938. Clears the `aria-*` attributes from the host node so that they're not duplicated with the underlying `input`. (cherry picked from commit fb390fb)
1 parent 7ad30eb commit 8d73869

File tree

4 files changed

+38
-0
lines changed

4 files changed

+38
-0
lines changed

src/material-experimental/mdc-slide-toggle/slide-toggle.spec.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ describe('MatSlideToggle without forms', () => {
1717
SlideToggleWithoutLabel,
1818
SlideToggleProjectedLabel,
1919
TextBindingComponent,
20+
SlideToggleWithStaticAriaAttributes,
2021
]
2122
});
2223

@@ -378,6 +379,15 @@ describe('MatSlideToggle without forms', () => {
378379
expect(testComponent.toggleTriggered).toBe(2, 'Expect toggle twice');
379380
expect(testComponent.dragTriggered).toBe(0);
380381
}));
382+
383+
it('should clear static aria attributes from the host node', () => {
384+
const fixture = TestBed.createComponent(SlideToggleWithStaticAriaAttributes);
385+
fixture.detectChanges();
386+
387+
const host: HTMLElement = fixture.nativeElement.querySelector('mat-slide-toggle');
388+
expect(host.hasAttribute('aria-label')).toBe(false);
389+
expect(host.hasAttribute('aria-labelledby')).toBe(false);
390+
});
381391
});
382392

383393
describe('MatSlideToggle with forms', () => {
@@ -804,3 +814,10 @@ class SlideToggleProjectedLabel {}
804814
class TextBindingComponent {
805815
text: string = 'Some text';
806816
}
817+
818+
@Component({
819+
template: `
820+
<mat-slide-toggle aria-label="Slide toggle" aria-labelledby="something"></mat-slide-toggle>
821+
`
822+
})
823+
class SlideToggleWithStaticAriaAttributes {}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ export class MatSlideToggleChange {
6262
'class': 'mat-mdc-slide-toggle',
6363
'[id]': 'id',
6464
'[attr.tabindex]': 'null',
65+
'[attr.aria-label]': 'null',
66+
'[attr.aria-labelledby]': 'null',
6567
'[class.mat-primary]': 'color == "primary"',
6668
'[class.mat-accent]': 'color == "accent"',
6769
'[class.mat-warn]': 'color == "warn"',

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ describe('MatSlideToggle without forms', () => {
3232
SlideToggleWithoutLabel,
3333
SlideToggleProjectedLabel,
3434
TextBindingComponent,
35+
SlideToggleWithStaticAriaAttributes,
3536
],
3637
providers: [
3738
{provide: HAMMER_GESTURE_CONFIG, useFactory: () => gestureConfig = new TestGestureConfig()},
@@ -767,6 +768,15 @@ describe('MatSlideToggle without forms', () => {
767768
.toContain('mat-slide-toggle-bar-no-side-margin');
768769
});
769770
});
771+
772+
it('should clear static aria attributes from the host node', () => {
773+
const fixture = TestBed.createComponent(SlideToggleWithStaticAriaAttributes);
774+
fixture.detectChanges();
775+
776+
const host: HTMLElement = fixture.nativeElement.querySelector('mat-slide-toggle');
777+
expect(host.hasAttribute('aria-label')).toBe(false);
778+
expect(host.hasAttribute('aria-labelledby')).toBe(false);
779+
});
770780
});
771781

772782
describe('MatSlideToggle with forms', () => {
@@ -1192,3 +1202,10 @@ class SlideToggleProjectedLabel {}
11921202
class TextBindingComponent {
11931203
text: string = 'Some text';
11941204
}
1205+
1206+
@Component({
1207+
template: `
1208+
<mat-slide-toggle aria-label="Slide toggle" aria-labelledby="something"></mat-slide-toggle>
1209+
`
1210+
})
1211+
class SlideToggleWithStaticAriaAttributes {}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ const _MatSlideToggleMixinBase:
8787
'[id]': 'id',
8888
// Needs to be `-1` so it can still receive programmatic focus.
8989
'[attr.tabindex]': 'disabled ? null : -1',
90+
'[attr.aria-label]': 'null',
91+
'[attr.aria-labelledby]': 'null',
9092
'[class.mat-checked]': 'checked',
9193
'[class.mat-disabled]': 'disabled',
9294
'[class.mat-slide-toggle-label-before]': 'labelPosition == "before"',

0 commit comments

Comments
 (0)