Skip to content

Commit 237e030

Browse files
crisbetojelbourn
authored andcommitted
fix(radio): clear aria attributes from host node (#16938)
If the `aria-*` attributes are set on the host node via static bindings, we can end up with both the host and the input having the same `aria-label` which might be read out by a screen reader. These changes clear the attributes from the host. Fixes #16913.
1 parent 8d12902 commit 237e030

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/material/radio/radio.spec.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ describe('MatRadio', () => {
2222
InterleavedRadioGroup,
2323
TranscludingWrapper,
2424
RadioButtonWithPredefinedTabindex,
25+
RadioButtonWithPredefinedAriaAttributes,
2526
]
2627
});
2728

@@ -778,6 +779,18 @@ describe('MatRadio', () => {
778779
expect(radioButtonEl.getAttribute('tabindex')).toBe('-1');
779780
});
780781

782+
it('should remove the aria attributes from the host element', () => {
783+
const predefinedFixture = TestBed.createComponent(RadioButtonWithPredefinedAriaAttributes);
784+
predefinedFixture.detectChanges();
785+
786+
const radioButtonEl =
787+
predefinedFixture.debugElement.query(By.css('.mat-radio-button'))!.nativeElement;
788+
789+
expect(radioButtonEl.hasAttribute('aria-label')).toBe(false);
790+
expect(radioButtonEl.hasAttribute('aria-describedby')).toBe(false);
791+
expect(radioButtonEl.hasAttribute('aria-labelledby')).toBe(false);
792+
});
793+
781794
});
782795

783796
describe('group interspersed with other tags', () => {
@@ -991,3 +1004,13 @@ class DefaultRadioButton {}
9911004
template: `<mat-radio-button color="warn"></mat-radio-button>`
9921005
})
9931006
class RadioButtonWithColorBinding {}
1007+
1008+
1009+
@Component({
1010+
template: `
1011+
<mat-radio-button
1012+
aria-label="Radio button"
1013+
aria-describedby="something"
1014+
aria-labelledby="something-else"></mat-radio-button>`
1015+
})
1016+
class RadioButtonWithPredefinedAriaAttributes {}

src/material/radio/radio.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,9 @@ const _MatRadioButtonMixinBase:
346346
// Needs to be -1 so the `focus` event still fires.
347347
'[attr.tabindex]': '-1',
348348
'[attr.id]': 'id',
349+
'[attr.aria-label]': 'null',
350+
'[attr.aria-labelledby]': 'null',
351+
'[attr.aria-describedby]': 'null',
349352
// Note: under normal conditions focus shouldn't land on this element, however it may be
350353
// programmatically set, for example inside of a focus trap, in this case we want to forward
351354
// the focus to the native element.

0 commit comments

Comments
 (0)