Skip to content

Commit eabe2cb

Browse files
tinayuangaokara
authored andcommitted
fix(button-toggle): add aria-label for button-toggle (#5919)
1 parent 2c7ba93 commit eabe2cb

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed

src/lib/button-toggle/button-toggle.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
[checked]="checked"
66
[disabled]="disabled || null"
77
[name]="name"
8+
[attr.aria-label]="ariaLabel"
9+
[attr.aria-labelledby]="ariaLabelledby"
810
(change)="_onInputChange($event)"
911
(click)="_onInputClick($event)">
1012

src/lib/button-toggle/button-toggle.spec.ts

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,8 @@ describe('MdButtonToggle without forms', () => {
218218
ButtonTogglesInsideButtonToggleGroupMultiple,
219219
ButtonToggleGroupWithInitialValue,
220220
StandaloneButtonToggle,
221+
ButtonToggleWithAriaLabel,
222+
ButtonToggleWithAriaLabelledby,
221223
],
222224
});
223225

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

594596
});
595-
});
596597

598+
describe('with provided aria-label ', () => {
599+
let checkboxDebugElement: DebugElement;
600+
let checkboxNativeElement: HTMLElement;
601+
let inputElement: HTMLInputElement;
602+
603+
it('should use the provided aria-label', () => {
604+
let fixture = TestBed.createComponent(ButtonToggleWithAriaLabel);
605+
checkboxDebugElement = fixture.debugElement.query(By.directive(MdButtonToggle));
606+
checkboxNativeElement = checkboxDebugElement.nativeElement;
607+
inputElement = checkboxNativeElement.querySelector('input') as HTMLInputElement;
608+
609+
fixture.detectChanges();
610+
expect(inputElement.getAttribute('aria-label')).toBe('Super effective');
611+
});
612+
});
613+
614+
describe('with provided aria-labelledby ', () => {
615+
let checkboxDebugElement: DebugElement;
616+
let checkboxNativeElement: HTMLElement;
617+
let inputElement: HTMLInputElement;
618+
619+
it('should use the provided aria-labelledby', () => {
620+
let fixture = TestBed.createComponent(ButtonToggleWithAriaLabelledby);
621+
checkboxDebugElement = fixture.debugElement.query(By.directive(MdButtonToggle));
622+
checkboxNativeElement = checkboxDebugElement.nativeElement;
623+
inputElement = checkboxNativeElement.querySelector('input') as HTMLInputElement;
624+
625+
fixture.detectChanges();
626+
expect(inputElement.getAttribute('aria-labelledby')).toBe('some-id');
627+
});
628+
629+
it('should not assign aria-labelledby if none is provided', () => {
630+
let fixture = TestBed.createComponent(StandaloneButtonToggle);
631+
checkboxDebugElement = fixture.debugElement.query(By.directive(MdButtonToggle));
632+
checkboxNativeElement = checkboxDebugElement.nativeElement;
633+
inputElement = checkboxNativeElement.querySelector('input') as HTMLInputElement;
634+
635+
fixture.detectChanges();
636+
expect(inputElement.getAttribute('aria-labelledby')).toBe(null);
637+
});
638+
});
639+
});
597640

598641
@Component({
599642
template: `
@@ -674,3 +717,15 @@ class ButtonToggleGroupWithInitialValue {
674717
class ButtonToggleGroupWithFormControl {
675718
control = new FormControl();
676719
}
720+
721+
/** Simple test component with an aria-label set. */
722+
@Component({
723+
template: `<md-button-toggle aria-label="Super effective"></md-button-toggle>`
724+
})
725+
class ButtonToggleWithAriaLabel { }
726+
727+
/** Simple test component with an aria-label set. */
728+
@Component({
729+
template: `<md-button-toggle aria-labelledby="some-id"></md-button-toggle>`
730+
})
731+
class ButtonToggleWithAriaLabelledby {}

src/lib/button-toggle/button-toggle.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,17 @@ export class MdButtonToggleGroupMultiple extends _MdButtonToggleGroupMixinBase
277277
}
278278
})
279279
export class MdButtonToggle implements OnInit, OnDestroy {
280+
/**
281+
* Attached to the aria-label attribute of the host element. In most cases, arial-labelledby will
282+
* take precedence so this may be omitted.
283+
*/
284+
@Input('aria-label') ariaLabel: string = '';
285+
286+
/**
287+
* Users can specify the `aria-labelledby` attribute which will be forwarded to the input element
288+
*/
289+
@Input('aria-labelledby') ariaLabelledby: string | null = null;
290+
280291
/** Whether or not this button toggle is checked. */
281292
private _checked: boolean = false;
282293

0 commit comments

Comments
 (0)