Skip to content

fix(tabs): disable without using pointer-events #7364

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
Oct 9, 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
1 change: 0 additions & 1 deletion src/lib/tabs/_tabs-common.scss
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ $mat-tab-animation-duration: 500ms !default;

&.mat-tab-disabled {
cursor: default;
pointer-events: none;
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/lib/tabs/tab-group.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
<div class="mat-tab-label" role="tab" matTabLabelWrapper mat-ripple
*ngFor="let tab of _tabs; let i = index"
[id]="_getTabLabelId(i)"
[tabIndex]="selectedIndex == i ? 0 : -1"
[attr.tabIndex]="_getTabIndex(tab, i)"
[attr.aria-controls]="_getTabContentId(i)"
[attr.aria-selected]="selectedIndex == i"
[class.mat-tab-label-active]="selectedIndex == i"
[disabled]="tab.disabled"
[matRippleDisabled]="disableRipple"
(click)="tabHeader.focusIndex = selectedIndex = i">
[matRippleDisabled]="tab.disabled || disableRipple"
(click)="_handleClick(tab, tabHeader, i)">

<!-- If there is a label template, use it. -->
<!-- If there is a label template, use it. -->
<ng-template [ngIf]="tab.templateLabel">
<ng-template [cdkPortalHost]="tab.templateLabel"></ng-template>
</ng-template>
Expand Down
16 changes: 16 additions & 0 deletions src/lib/tabs/tab-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
import {coerceBooleanProperty} from '@angular/cdk/coercion';
import {Subscription} from 'rxjs/Subscription';
import {MatTab} from './tab';
import {MatTabHeader} from './tab-header';
import {merge} from 'rxjs/observable/merge';
import {
CanColor,
Expand Down Expand Up @@ -290,4 +291,19 @@ export class MatTabGroup extends _MatTabGroupMixinBase implements AfterContentIn
this._tabBodyWrapperHeight = this._tabBodyWrapper.nativeElement.clientHeight;
this._renderer.setStyle(this._tabBodyWrapper.nativeElement, 'height', '');
}

/** Handle click events, setting new selected index if appropriate. */
_handleClick(tab: MatTab, tabHeader: MatTabHeader, idx: number) {
if (!tab.disabled) {
this.selectedIndex = tabHeader.focusIndex = idx;
}
}

/** Retrieves the tabindex for the tab. */
_getTabIndex(tab: MatTab, idx: number): number | null {
if (tab.disabled) {
return null;
}
return this.selectedIndex === idx ? 0 : -1;
}
}
4 changes: 3 additions & 1 deletion src/lib/tabs/tab-nav-bar/tab-nav-bar.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ describe('MatTabNavBar', () => {
fixture.componentInstance.disabled = true;
fixture.detectChanges();

expect(tabLinkElements.every(tabLink => tabLink.tabIndex === -1))
expect(tabLinkElements.every(tabLink => {
return tabLink.getAttribute('tabIndex') === null;
}))
.toBe(true, 'Expected element to no longer be keyboard focusable if disabled.');
});

Expand Down
8 changes: 4 additions & 4 deletions src/lib/tabs/tab-nav-bar/tab-nav-bar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ export const _MatTabLinkMixinBase = mixinDisabled(MatTabLinkBase);
host: {
'class': 'mat-tab-link',
'[attr.aria-disabled]': 'disabled.toString()',
'[attr.tabindex]': 'tabIndex',
'[attr.tabIndex]': 'tabIndex',
'[class.mat-tab-disabled]': 'disabled',
'[class.mat-tab-label-active]': 'active',
}
Expand All @@ -209,16 +209,16 @@ export class MatTabLink extends _MatTabLinkMixinBase implements OnDestroy, CanDi
}

/** Whether ripples should be disabled or not. */
get disableRipple(): boolean { return this._disableRipple; }
get disableRipple(): boolean { return this.disabled || this._disableRipple; }
set disableRipple(value: boolean) {
this._disableRipple = value;
this._tabLinkRipple.disabled = this.disableRipple;
this._tabLinkRipple._updateRippleRenderer();
}

/** @docs-private */
get tabIndex(): number {
return this.disabled ? -1 : 0;
get tabIndex(): number | null {
return this.disabled ? null : 0;
}

constructor(private _tabNavBar: MatTabNav,
Expand Down