Skip to content

fix(tabs): unable to set aria-label or aria-labelledby on tab #11898

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
Jun 26, 2018
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/tabs/tab-group.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
[attr.aria-setsize]="_tabs.length"
[attr.aria-controls]="_getTabContentId(i)"
[attr.aria-selected]="selectedIndex == i"
[attr.aria-label]="tab.ariaLabel || null"
[attr.aria-labelledby]="(!tab.ariaLabel && tab.ariaLabelledby) ? tab.ariaLabelledby : null"
[class.mat-tab-label-active]="selectedIndex == i"
[disabled]="tab.disabled"
[matRippleDisabled]="tab.disabled || disableRipple"
Expand Down
54 changes: 54 additions & 0 deletions src/lib/tabs/tab-group.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ describe('MatTabGroup', () => {
DisabledTabsTestApp,
TabGroupWithSimpleApi,
TemplateTabs,
TabGroupWithAriaInputs,
],
});

Expand Down Expand Up @@ -233,6 +234,46 @@ describe('MatTabGroup', () => {

});

describe('aria labelling', () => {
let fixture: ComponentFixture<TabGroupWithAriaInputs>;
let tab: HTMLElement;

beforeEach(fakeAsync(() => {
fixture = TestBed.createComponent(TabGroupWithAriaInputs);
fixture.detectChanges();
tick();
tab = fixture.nativeElement.querySelector('.mat-tab-label');
}));

it('should not set aria-label or aria-labelledby attributes if they are not passed in', () => {
expect(tab.hasAttribute('aria-label')).toBe(false);
expect(tab.hasAttribute('aria-labelledby')).toBe(false);
});

it('should set the aria-label attribute', () => {
fixture.componentInstance.ariaLabel = 'Fruit';
fixture.detectChanges();

expect(tab.getAttribute('aria-label')).toBe('Fruit');
});

it('should set the aria-labelledby attribute', () => {
fixture.componentInstance.ariaLabelledby = 'fruit-label';
fixture.detectChanges();

expect(tab.getAttribute('aria-labelledby')).toBe('fruit-label');
});

it('should not be able to set both an aria-label and aria-labelledby', () => {
fixture.componentInstance.ariaLabel = 'Fruit';
fixture.componentInstance.ariaLabelledby = 'fruit-label';
fixture.detectChanges();

expect(tab.getAttribute('aria-label')).toBe('Fruit');
expect(tab.hasAttribute('aria-labelledby')).toBe(false);
});
});

describe('disable tabs', () => {
let fixture: ComponentFixture<DisabledTabsTestApp>;
beforeEach(() => {
Expand Down Expand Up @@ -661,3 +702,16 @@ class NestedTabs {}
`,
})
class TemplateTabs {}


@Component({
template: `
<mat-tab-group>
<mat-tab [aria-label]="ariaLabel" [aria-labelledby]="ariaLabelledby"></mat-tab>
</mat-tab-group>
`
})
class TabGroupWithAriaInputs {
ariaLabel: string;
ariaLabelledby: string;
}
13 changes: 11 additions & 2 deletions src/lib/tabs/tab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,19 @@ export class MatTab extends _MatTabMixinBase implements OnInit, CanDisable, OnCh
/** Template inside the MatTab view that contains an `<ng-content>`. */
@ViewChild(TemplateRef) _implicitContent: TemplateRef<any>;

/** The plain text label for the tab, used when there is no template label. */
/** Plain text label for the tab, used when there is no template label. */
@Input('label') textLabel: string = '';

/** The portal that will be the hosted content of the tab */
/** Aria label for the tab. */
@Input('aria-label') ariaLabel: string;

/**
* Reference to the element that the tab is labelled by.
* Will be cleared if `aria-label` is set at the same time.
*/
@Input('aria-labelledby') ariaLabelledby: string;

/** Portal that will be the hosted content of the tab */
private _contentPortal: TemplatePortal | null = null;

/** @docs-private */
Expand Down