Skip to content

Commit cd159f5

Browse files
3rfmmalerba
authored andcommitted
feat(radio): add aria-describedby passthrough to radio button input (#9741)
Adds a simple "aria-describedby" input in the manner of "aria-labelledby." The ability to set "aria-describedby" is essential for accessible radio buttons, as the attribute allows further description of a radio button to be read after mentioning the button's label and whether or not it is checked.
1 parent c2e2744 commit cd159f5

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

src/lib/radio/radio.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
[required]="required"
2424
[attr.aria-label]="ariaLabel"
2525
[attr.aria-labelledby]="ariaLabelledby"
26+
[attr.aria-describedby]="ariaDescribedby"
2627
(change)="_onInputChange($event)"
2728
(click)="_onInputClick($event)">
2829

src/lib/radio/radio.spec.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,23 @@ describe('MatRadio', () => {
627627
expect(fruitRadioNativeInputs[0].getAttribute('aria-labelledby')).toBe('uvw');
628628
});
629629

630+
it('should add aria-describedby attribute to the underlying input element if defined', () => {
631+
expect(fruitRadioNativeInputs[0].getAttribute('aria-describedby')).toBe('abc');
632+
});
633+
634+
it('should not add aria-describedby attribute if not defined', () => {
635+
expect(fruitRadioNativeInputs[1].hasAttribute('aria-describedby')).toBeFalsy();
636+
});
637+
638+
it('should change aria-describedby attribute if property is changed at runtime', () => {
639+
expect(fruitRadioNativeInputs[0].getAttribute('aria-describedby')).toBe('abc');
640+
641+
testComponent.ariaDescribedby = 'uvw';
642+
fixture.detectChanges();
643+
644+
expect(fruitRadioNativeInputs[0].getAttribute('aria-describedby')).toBe('uvw');
645+
});
646+
630647
it('should focus on underlying input element when focus() is called', () => {
631648
for (let i = 0; i < fruitRadioInstances.length; i++) {
632649
expect(document.activeElement).not.toBe(fruitRadioNativeInputs[i]);
@@ -745,10 +762,12 @@ class RadiosInsideRadioGroup {
745762
<mat-radio-button name="weather" value="cool">Autumn</mat-radio-button>
746763
747764
<span id="xyz">Baby Banana</span>
765+
<span id="abc">A smaller banana</span>
748766
<mat-radio-button name="fruit"
749767
value="banana"
750768
[aria-label]="ariaLabel"
751-
[aria-labelledby]="ariaLabelledby">
769+
[aria-labelledby]="ariaLabelledby"
770+
[aria-describedby]="ariaDescribedby">
752771
</mat-radio-button>
753772
<mat-radio-button name="fruit" value="raspberry">Raspberry</mat-radio-button>
754773
<mat-radio-button id="nameless" value="no-name">No name</mat-radio-button>
@@ -757,6 +776,7 @@ class RadiosInsideRadioGroup {
757776
class StandaloneRadioButtons {
758777
ariaLabel: string = 'Banana';
759778
ariaLabelledby: string = 'xyz';
779+
ariaDescribedby: string = 'abc';
760780
}
761781

762782

src/lib/radio/radio.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,9 @@ export class MatRadioButton extends _MatRadioButtonMixinBase
366366
/** The 'aria-labelledby' attribute takes precedence as the element's text alternative. */
367367
@Input('aria-labelledby') ariaLabelledby: string;
368368

369+
/** The 'aria-describedby' attribute is read after the element's label and field type. */
370+
@Input('aria-describedby') ariaDescribedby: string;
371+
369372
/** Whether this radio button is checked. */
370373
@Input()
371374
get checked(): boolean { return this._checked; }

0 commit comments

Comments
 (0)