Skip to content

Commit 3e3ef2a

Browse files
josephperrottandrewseguin
authored andcommitted
Disable tabs using changes to response to events rather than pointer-events: none (#7364)
1 parent 004e0fe commit 3e3ef2a

File tree

5 files changed

+27
-10
lines changed

5 files changed

+27
-10
lines changed

src/lib/tabs/_tabs-common.scss

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ $mat-tab-animation-duration: 500ms !default;
2424

2525
&.mat-tab-disabled {
2626
cursor: default;
27-
pointer-events: none;
2827
}
2928
}
3029

src/lib/tabs/tab-group.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66
<div class="mat-tab-label" role="tab" matTabLabelWrapper mat-ripple
77
*ngFor="let tab of _tabs; let i = index"
88
[id]="_getTabLabelId(i)"
9-
[tabIndex]="selectedIndex == i ? 0 : -1"
9+
[attr.tabIndex]="_getTabIndex(tab, i)"
1010
[attr.aria-controls]="_getTabContentId(i)"
1111
[attr.aria-selected]="selectedIndex == i"
1212
[class.mat-tab-label-active]="selectedIndex == i"
1313
[disabled]="tab.disabled"
14-
[matRippleDisabled]="disableRipple"
15-
(click)="tabHeader.focusIndex = selectedIndex = i">
14+
[matRippleDisabled]="tab.disabled || disableRipple"
15+
(click)="_handleClick(tab, tabHeader, i)">
1616

17-
<!-- If there is a label template, use it. -->
17+
<!-- If there is a label template, use it. -->
1818
<ng-template [ngIf]="tab.templateLabel">
1919
<ng-template [cdkPortalHost]="tab.templateLabel"></ng-template>
2020
</ng-template>

src/lib/tabs/tab-group.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
import {coerceBooleanProperty} from '@angular/cdk/coercion';
2727
import {Subscription} from 'rxjs/Subscription';
2828
import {MatTab} from './tab';
29+
import {MatTabHeader} from './tab-header';
2930
import {merge} from 'rxjs/observable/merge';
3031
import {
3132
CanColor,
@@ -278,4 +279,19 @@ export class MatTabGroup extends _MatTabGroupMixinBase implements AfterContentIn
278279
this._tabBodyWrapperHeight = this._tabBodyWrapper.nativeElement.clientHeight;
279280
this._renderer.setStyle(this._tabBodyWrapper.nativeElement, 'height', '');
280281
}
282+
283+
/** Handle click events, setting new selected index if appropriate. */
284+
_handleClick(tab: MatTab, tabHeader: MatTabHeader, idx: number) {
285+
if (!tab.disabled) {
286+
this.selectedIndex = tabHeader.focusIndex = idx;
287+
}
288+
}
289+
290+
/** Retrieves the tabindex for the tab. */
291+
_getTabIndex(tab: MatTab, idx: number): number | null {
292+
if (tab.disabled) {
293+
return null;
294+
}
295+
return this.selectedIndex === idx ? 0 : -1;
296+
}
281297
}

src/lib/tabs/tab-nav-bar/tab-nav-bar.spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,9 @@ describe('MatTabNavBar', () => {
119119
fixture.componentInstance.disabled = true;
120120
fixture.detectChanges();
121121

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

src/lib/tabs/tab-nav-bar/tab-nav-bar.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ export const _MatTabLinkMixinBase = mixinDisabled(MatTabLinkBase);
183183
host: {
184184
'class': 'mat-tab-link',
185185
'[attr.aria-disabled]': 'disabled.toString()',
186-
'[attr.tabindex]': 'tabIndex',
186+
'[attr.tabIndex]': 'tabIndex',
187187
'[class.mat-tab-disabled]': 'disabled',
188188
'[class.mat-tab-label-active]': 'active',
189189
}
@@ -209,16 +209,16 @@ export class MatTabLink extends _MatTabLinkMixinBase implements OnDestroy, CanDi
209209
}
210210

211211
/** Whether ripples should be disabled or not. */
212-
get disableRipple(): boolean { return this._disableRipple; }
212+
get disableRipple(): boolean { return this.disabled || this._disableRipple; }
213213
set disableRipple(value: boolean) {
214214
this._disableRipple = value;
215215
this._tabLinkRipple.disabled = this.disableRipple;
216216
this._tabLinkRipple._updateRippleRenderer();
217217
}
218218

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

224224
constructor(private _tabNavBar: MatTabNav,

0 commit comments

Comments
 (0)