Skip to content

Commit bf66d57

Browse files
crisbetovivian-hu-zz
authored andcommitted
fix(tabs): disabled tab link not preventing router navigation (#10358)
Fixes users being able to navigate to a new route by clicking on a disabled `mat-tab-link`. Fixes #10354.
1 parent eb95e61 commit bf66d57

File tree

3 files changed

+12
-20
lines changed

3 files changed

+12
-20
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@
2424
flex-basis: 0;
2525
flex-grow: 1;
2626
}
27+
28+
&.mat-tab-disabled {
29+
// We use `pointer-events` to make the element unclickable when it's disabled, rather than
30+
// preventing the default action through JS, because we can't prevent the action reliably
31+
// due to other directives potentially registering their events earlier. This shouldn't cause
32+
// the user to click through, because we always have a `.mat-tab-links` behind the link.
33+
pointer-events: none;
34+
}
2735
}
2836

2937

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

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {async, ComponentFixture, fakeAsync, TestBed, tick} from '@angular/core/testing';
22
import {Component, ViewChild, ViewChildren, QueryList} from '@angular/core';
33
import {By} from '@angular/platform-browser';
4-
import {dispatchFakeEvent, dispatchMouseEvent, createMouseEvent} from '@angular/cdk/testing';
4+
import {dispatchFakeEvent, dispatchMouseEvent} from '@angular/cdk/testing';
55
import {Direction, Directionality} from '@angular/cdk/bidi';
66
import {Subject} from 'rxjs';
77
import {MatTabLink, MatTabNav, MatTabsModule} from '../index';
@@ -150,21 +150,15 @@ describe('MatTabNavBar', () => {
150150
.toBe(true, 'Expected element to no longer be keyboard focusable if disabled.');
151151
});
152152

153-
it('should prevent default action for clicks if links are disabled', () => {
153+
it('should make disabled links unclickable', () => {
154154
const tabLinkElement = fixture.debugElement.query(By.css('a')).nativeElement;
155155

156-
const mouseEvent = createMouseEvent('click');
157-
spyOn(mouseEvent, 'preventDefault');
158-
tabLinkElement.dispatchEvent(mouseEvent);
159-
expect(mouseEvent.preventDefault).not.toHaveBeenCalled();
156+
expect(getComputedStyle(tabLinkElement).pointerEvents).not.toBe('none');
160157

161158
fixture.componentInstance.disabled = true;
162159
fixture.detectChanges();
163160

164-
const mouseEventWhileDisabled = createMouseEvent('click');
165-
spyOn(mouseEventWhileDisabled, 'preventDefault');
166-
tabLinkElement.dispatchEvent(mouseEventWhileDisabled);
167-
expect(mouseEventWhileDisabled.preventDefault).toHaveBeenCalled();
161+
expect(getComputedStyle(tabLinkElement).pointerEvents).toBe('none');
168162
});
169163

170164
it('should show ripples for tab links', () => {

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

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,6 @@ export const _MatTabLinkMixinBase:
180180
'[attr.tabIndex]': 'tabIndex',
181181
'[class.mat-tab-disabled]': 'disabled',
182182
'[class.mat-tab-label-active]': 'active',
183-
'(click)': '_handleClick($event)'
184183
}
185184
})
186185
export class MatTabLink extends _MatTabLinkMixinBase
@@ -259,13 +258,4 @@ export class MatTabLink extends _MatTabLinkMixinBase
259258
this._focusMonitor.stopMonitoring(this._elementRef);
260259
}
261260
}
262-
263-
/**
264-
* Handles the click event, preventing default navigation if the tab link is disabled.
265-
*/
266-
_handleClick(event: MouseEvent) {
267-
if (this.disabled) {
268-
event.preventDefault();
269-
}
270-
}
271261
}

0 commit comments

Comments
 (0)