Skip to content

Commit 8ccc49b

Browse files
MartinMajelbourn
authored andcommitted
fix(radio): support aria-label(ledby) on md-radio (#586) (#596)
1 parent b24d321 commit 8ccc49b

File tree

5 files changed

+68
-2
lines changed

5 files changed

+68
-2
lines changed

src/components/radio/README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,16 @@ The `md-radio-group` component has no button initially selected.
4747
## `<md-radio-button>`
4848
### Properties
4949

50-
| Name | Type | Description |
50+
| Name (attribute) | Type | Description |
5151
| --- | --- | --- |
5252
| `id` | `string` | The unique ID of this radio button. |
5353
| `name` | `string` | Group name, defaults to parent radio group if present. |
5454
| `value` | `any` | The value of this radio button. |
5555
| `checked` | `boolean` | Whether the radio is checked. |
5656
| `disabled` | `boolean` | Whether the radio is disabled. |
57+
| `aria-label` | `string` | Used to set the `aria-label` attribute of the underlying input element. |
58+
| `aria-labelledby` | `string` | Used to set the `aria-labelledby` attribute of the underlying input element.
59+
If provided, this attribute takes precedence as the element's text alternative. |
5760

5861
When checked, an event is emitted from the `change` EventEmitter property.
5962

src/components/radio/radio.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
[checked]="checked"
1414
[disabled]="disabled"
1515
[name]="name"
16+
[attr.aria-label]="ariaLabel"
17+
[attr.aria-labelledby]="ariaLabelledby"
1618
(change)="onInputChange($event)"
1719
(focus)="onInputFocus()"
1820
(blur)="onInputBlur()" />

src/components/radio/radio.spec.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,10 +345,14 @@ describe('MdRadio', () => {
345345
let radioDebugElements: DebugElement[];
346346
let seasonRadioInstances: MdRadioButton[];
347347
let weatherRadioInstances: MdRadioButton[];
348+
let fruitRadioInstances: MdRadioButton[];
349+
let fruitRadioNativeInputs: HTMLElement[];
348350
let testComponent: StandaloneRadioButtons;
349351

350352
beforeEach(async(() => {
351353
builder.createAsync(StandaloneRadioButtons).then(f => {
354+
let fruitRadioNativeElements: HTMLElement[];
355+
352356
fixture = f;
353357
fixture.detectChanges();
354358

@@ -361,6 +365,18 @@ describe('MdRadio', () => {
361365
weatherRadioInstances = radioDebugElements
362366
.filter(debugEl => debugEl.componentInstance.name == 'weather')
363367
.map(debugEl => debugEl.componentInstance);
368+
fruitRadioInstances = radioDebugElements
369+
.filter(debugEl => debugEl.componentInstance.name == 'fruit')
370+
.map(debugEl => debugEl.componentInstance);
371+
372+
fruitRadioNativeElements = radioDebugElements
373+
.filter(debugEl => debugEl.componentInstance.name == 'fruit')
374+
.map(debugEl => debugEl.nativeElement);
375+
376+
fruitRadioNativeInputs = [];
377+
for (let element of fruitRadioNativeElements) {
378+
fruitRadioNativeInputs.push(<HTMLElement> element.querySelector('input'));
379+
}
364380
});
365381
}));
366382

@@ -393,6 +409,40 @@ describe('MdRadio', () => {
393409
expect(weatherRadioInstances[1].checked).toBe(false);
394410
expect(weatherRadioInstances[2].checked).toBe(true);
395411
});
412+
413+
it('should add aria-label attribute to the underlying input element if defined', () => {
414+
expect(fruitRadioNativeInputs[0].getAttribute('aria-label')).toBe('Banana');
415+
});
416+
417+
it('should not add aria-label attribute if not defined', () => {
418+
expect(fruitRadioNativeInputs[1].hasAttribute('aria-label')).toBeFalsy();
419+
});
420+
421+
it('should change aria-label attribute if property is changed at runtime', () => {
422+
expect(fruitRadioNativeInputs[0].getAttribute('aria-label')).toBe('Banana');
423+
424+
fruitRadioInstances[0].ariaLabel = 'Pineapple';
425+
fixture.detectChanges();
426+
427+
expect(fruitRadioNativeInputs[0].getAttribute('aria-label')).toBe('Pineapple');
428+
});
429+
430+
it('should add aria-labelledby attribute to the underlying input element if defined', () => {
431+
expect(fruitRadioNativeInputs[0].getAttribute('aria-labelledby')).toBe('xyz');
432+
});
433+
434+
it('should not add aria-labelledby attribute if not defined', () => {
435+
expect(fruitRadioNativeInputs[1].hasAttribute('aria-labelledby')).toBeFalsy();
436+
});
437+
438+
it('should change aria-labelledby attribute if property is changed at runtime', () => {
439+
expect(fruitRadioNativeInputs[0].getAttribute('aria-labelledby')).toBe('xyz');
440+
441+
fruitRadioInstances[0].ariaLabelledby = 'uvw';
442+
fixture.detectChanges();
443+
444+
expect(fruitRadioNativeInputs[0].getAttribute('aria-labelledby')).toBe('uvw');
445+
});
396446
});
397447
});
398448

@@ -423,6 +473,11 @@ class RadiosInsideRadioGroup {
423473
<md-radio-button name="weather" value="warm">Spring</md-radio-button>
424474
<md-radio-button name="weather" value="hot">Summer</md-radio-button>
425475
<md-radio-button name="weather" value="cool">Autumn</md-radio-button>
476+
477+
<span id="xyz">Baby Banana<span>
478+
<md-radio-button name="fruit" value="banana" aria-label="Banana" aria-labelledby="xyz">
479+
</md-radio-button>
480+
<md-radio-button name="fruit" value="raspberry">Raspberry</md-radio-button>
426481
`
427482
})
428483
class StandaloneRadioButtons { }

src/components/radio/radio.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,12 @@ export class MdRadioButton implements OnInit {
254254
@Input()
255255
name: string;
256256

257+
/** Used to set the 'aria-label' attribute on the underlying input element. */
258+
@Input('aria-label') ariaLabel: string;
259+
260+
/** The 'aria-labelledby' attribute takes precedence as the element's text alternative. */
261+
@Input('aria-labelledby') ariaLabelledby: string;
262+
257263
/** Whether this radio is disabled. */
258264
private _disabled: boolean;
259265

src/demo-app/radio/radio-demo.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@
1111
md-radio-button {
1212
margin: 8px;
1313
}
14-
}
14+
}

0 commit comments

Comments
 (0)