Skip to content

feat(button-toggle): Add aria-label and aria-labelledby to button toggle #5919

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/lib/button-toggle/button-toggle.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
[checked]="checked"
[disabled]="disabled || null"
[name]="name"
[attr.aria-label]="ariaLabel"
[attr.aria-labelledby]="ariaLabelledby"
(change)="_onInputChange($event)"
(click)="_onInputClick($event)">

Expand Down
57 changes: 56 additions & 1 deletion src/lib/button-toggle/button-toggle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ describe('MdButtonToggle without forms', () => {
ButtonTogglesInsideButtonToggleGroupMultiple,
ButtonToggleGroupWithInitialValue,
StandaloneButtonToggle,
ButtonToggleWithAriaLabel,
ButtonToggleWithAriaLabelledby,
],
});

Expand Down Expand Up @@ -592,8 +594,49 @@ describe('MdButtonToggle without forms', () => {
});

});
});

describe('with provided aria-label ', () => {
let checkboxDebugElement: DebugElement;
let checkboxNativeElement: HTMLElement;
let inputElement: HTMLInputElement;

it('should use the provided aria-label', () => {
let fixture = TestBed.createComponent(ButtonToggleWithAriaLabel);
checkboxDebugElement = fixture.debugElement.query(By.directive(MdButtonToggle));
checkboxNativeElement = checkboxDebugElement.nativeElement;
inputElement = checkboxNativeElement.querySelector('input') as HTMLInputElement;

fixture.detectChanges();
expect(inputElement.getAttribute('aria-label')).toBe('Super effective');
});
});

describe('with provided aria-labelledby ', () => {
let checkboxDebugElement: DebugElement;
let checkboxNativeElement: HTMLElement;
let inputElement: HTMLInputElement;

it('should use the provided aria-labelledby', () => {
let fixture = TestBed.createComponent(ButtonToggleWithAriaLabelledby);
checkboxDebugElement = fixture.debugElement.query(By.directive(MdButtonToggle));
checkboxNativeElement = checkboxDebugElement.nativeElement;
inputElement = checkboxNativeElement.querySelector('input') as HTMLInputElement;

fixture.detectChanges();
expect(inputElement.getAttribute('aria-labelledby')).toBe('some-id');
});

it('should not assign aria-labelledby if none is provided', () => {
let fixture = TestBed.createComponent(StandaloneButtonToggle);
checkboxDebugElement = fixture.debugElement.query(By.directive(MdButtonToggle));
checkboxNativeElement = checkboxDebugElement.nativeElement;
inputElement = checkboxNativeElement.querySelector('input') as HTMLInputElement;

fixture.detectChanges();
expect(inputElement.getAttribute('aria-labelledby')).toBe(null);
});
});
});

@Component({
template: `
Expand Down Expand Up @@ -674,3 +717,15 @@ class ButtonToggleGroupWithInitialValue {
class ButtonToggleGroupWithFormControl {
control = new FormControl();
}

/** Simple test component with an aria-label set. */
@Component({
template: `<md-button-toggle aria-label="Super effective"></md-button-toggle>`
})
class ButtonToggleWithAriaLabel { }

/** Simple test component with an aria-label set. */
@Component({
template: `<md-button-toggle aria-labelledby="some-id"></md-button-toggle>`
})
class ButtonToggleWithAriaLabelledby {}
11 changes: 11 additions & 0 deletions src/lib/button-toggle/button-toggle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,17 @@ export class MdButtonToggleGroupMultiple extends _MdButtonToggleGroupMixinBase
}
})
export class MdButtonToggle implements OnInit, OnDestroy {
/**
* Attached to the aria-label attribute of the host element. In most cases, arial-labelledby will
* take precedence so this may be omitted.
*/
@Input('aria-label') ariaLabel: string = '';

/**
* Users can specify the `aria-labelledby` attribute which will be forwarded to the input element
*/
@Input('aria-labelledby') ariaLabelledby: string | null = null;

/** Whether or not this button toggle is checked. */
private _checked: boolean = false;

Expand Down