Skip to content

Commit

Permalink
feat(radio): add aria-describedby passthrough to radio button input (#…
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
3rf authored and mmalerba committed Feb 8, 2018
1 parent c2e2744 commit cd159f5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/lib/radio/radio.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
[required]="required"
[attr.aria-label]="ariaLabel"
[attr.aria-labelledby]="ariaLabelledby"
[attr.aria-describedby]="ariaDescribedby"
(change)="_onInputChange($event)"
(click)="_onInputClick($event)">

Expand Down
22 changes: 21 additions & 1 deletion src/lib/radio/radio.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,23 @@ describe('MatRadio', () => {
expect(fruitRadioNativeInputs[0].getAttribute('aria-labelledby')).toBe('uvw');
});

it('should add aria-describedby attribute to the underlying input element if defined', () => {
expect(fruitRadioNativeInputs[0].getAttribute('aria-describedby')).toBe('abc');
});

it('should not add aria-describedby attribute if not defined', () => {
expect(fruitRadioNativeInputs[1].hasAttribute('aria-describedby')).toBeFalsy();
});

it('should change aria-describedby attribute if property is changed at runtime', () => {
expect(fruitRadioNativeInputs[0].getAttribute('aria-describedby')).toBe('abc');

testComponent.ariaDescribedby = 'uvw';
fixture.detectChanges();

expect(fruitRadioNativeInputs[0].getAttribute('aria-describedby')).toBe('uvw');
});

it('should focus on underlying input element when focus() is called', () => {
for (let i = 0; i < fruitRadioInstances.length; i++) {
expect(document.activeElement).not.toBe(fruitRadioNativeInputs[i]);
Expand Down Expand Up @@ -745,10 +762,12 @@ class RadiosInsideRadioGroup {
<mat-radio-button name="weather" value="cool">Autumn</mat-radio-button>
<span id="xyz">Baby Banana</span>
<span id="abc">A smaller banana</span>
<mat-radio-button name="fruit"
value="banana"
[aria-label]="ariaLabel"
[aria-labelledby]="ariaLabelledby">
[aria-labelledby]="ariaLabelledby"
[aria-describedby]="ariaDescribedby">
</mat-radio-button>
<mat-radio-button name="fruit" value="raspberry">Raspberry</mat-radio-button>
<mat-radio-button id="nameless" value="no-name">No name</mat-radio-button>
Expand All @@ -757,6 +776,7 @@ class RadiosInsideRadioGroup {
class StandaloneRadioButtons {
ariaLabel: string = 'Banana';
ariaLabelledby: string = 'xyz';
ariaDescribedby: string = 'abc';
}


Expand Down
3 changes: 3 additions & 0 deletions src/lib/radio/radio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,9 @@ export class MatRadioButton extends _MatRadioButtonMixinBase
/** The 'aria-labelledby' attribute takes precedence as the element's text alternative. */
@Input('aria-labelledby') ariaLabelledby: string;

/** The 'aria-describedby' attribute is read after the element's label and field type. */
@Input('aria-describedby') ariaDescribedby: string;

/** Whether this radio button is checked. */
@Input()
get checked(): boolean { return this._checked; }
Expand Down

0 comments on commit cd159f5

Please sign in to comment.