Skip to content

Commit

Permalink
fix(radio): radio buttons not being a tab stop and missing focus indi…
Browse files Browse the repository at this point in the history
…cation

* Fixes the radio buttons not being tabable.
* Adds focus indication to the radio buttons.

Referencing angular#421.
  • Loading branch information
crisbeto committed Nov 4, 2016
1 parent a0d85d8 commit 1e5759a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/lib/radio/radio.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<!-- TODO(jelbourn): render the radio on either side of the content -->
<!-- TODO(mtlin): Evaluate trade-offs of using native radio vs. cost of additional bindings. -->
<label [attr.for]="inputId" class="md-radio-label">
<label [attr.for]="inputId" class="md-radio-label" [class.md-ripple-focused]="_isFocused">
<!-- The actual 'radio' part of the control. -->
<div class="md-radio-container">
<div class="md-radio-outer-circle"></div>
Expand All @@ -17,6 +17,7 @@
[checked]="checked"
[disabled]="disabled"
[name]="name"
[tabIndex]="tabindex"
[attr.aria-label]="ariaLabel"
[attr.aria-labelledby]="ariaLabelledby"
(change)="_onInputChange($event)"
Expand Down
30 changes: 26 additions & 4 deletions src/lib/radio/radio.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ describe('MdRadio', () => {
expect(spies[1]).toHaveBeenCalledTimes(1);
});

it(`should not emit a change event from the radio group when change group value
it(`should not emit a change event from the radio group when change group value
programmatically`, () => {
expect(groupInstance.value).toBeFalsy();

Expand Down Expand Up @@ -235,7 +235,7 @@ describe('MdRadio', () => {
}
}));

it(`should update the group's selected radio to null when unchecking that radio
it(`should update the group's selected radio to null when unchecking that radio
programmatically`, () => {
let changeSpy = jasmine.createSpy('radio-group change listener');
groupInstance.change.subscribe(changeSpy);
Expand Down Expand Up @@ -485,6 +485,28 @@ describe('MdRadio', () => {

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

it('should make the native input element a tab stop', () => {
expect(fruitRadioNativeInputs[0].tabIndex).toBe(0);
});

it('should add a ripple when the native input element is focused', () => {
let hostElement = seasonRadioInstances[0].getHostElement() as HTMLElement;
let input = hostElement.querySelector('input') as HTMLInputElement;
let label = hostElement.querySelector('label') as HTMLLabelElement;

expect(label.classList).not.toContain('md-ripple-focused');

dispatchEvent('focus', input);
fixture.detectChanges();

expect(label.classList).toContain('md-ripple-focused');

dispatchEvent('blur', input);
fixture.detectChanges();

expect(label.classList).not.toContain('md-ripple-focused');
});
});
});

Expand Down Expand Up @@ -514,11 +536,11 @@ class RadiosInsideRadioGroup {
<md-radio-button name="season" value="spring">Spring</md-radio-button>
<md-radio-button name="season" value="summer">Summer</md-radio-button>
<md-radio-button name="season" value="autum">Autumn</md-radio-button>
<md-radio-button name="weather" value="warm">Spring</md-radio-button>
<md-radio-button name="weather" value="hot">Summer</md-radio-button>
<md-radio-button name="weather" value="cool">Autumn</md-radio-button>
<span id="xyz">Baby Banana</span>
<md-radio-button name="fruit" value="banana" aria-label="Banana" aria-labelledby="xyz">
</md-radio-button>
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 @@ -239,6 +239,9 @@ export class MdRadioButton implements OnInit {
@HostBinding('class.md-radio-focused')
_isFocused: boolean;

/** Tabindex for the input element. */
@Input() tabindex: number = 0;

/** Whether this radio is checked. */
private _checked: boolean = false;

Expand Down

0 comments on commit 1e5759a

Please sign in to comment.