Skip to content

Commit 7834261

Browse files
committed
fix(tabs): disabled tab link not preventing router navigation
Fixes users being able to navigate to a new route by clicking on a disabled `mat-tab-link`. Fixes #10354.
1 parent 1a60a7a commit 7834261

File tree

4 files changed

+13
-21
lines changed

4 files changed

+13
-21
lines changed

src/demo-app/tabs/tabs-demo.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ <h1>Tab Nav Bar</h1>
1414
[active]="rla.isActive">
1515
{{tabLink.label}}
1616
</a>
17-
<a mat-tab-link disabled>Disabled Link</a>
17+
<a routerLink="foggy-tab" mat-tab-link disabled>Disabled Link</a>
1818
</nav>
1919
<router-outlet></router-outlet>
2020
</div>

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

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

2836

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';
@@ -125,21 +125,15 @@ describe('MatTabNavBar', () => {
125125
.toBe(true, 'Expected element to no longer be keyboard focusable if disabled.');
126126
});
127127

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

131-
const mouseEvent = createMouseEvent('click');
132-
spyOn(mouseEvent, 'preventDefault');
133-
tabLinkElement.dispatchEvent(mouseEvent);
134-
expect(mouseEvent.preventDefault).not.toHaveBeenCalled();
131+
expect(getComputedStyle(tabLinkElement).pointerEvents).not.toBe('none');
135132

136133
fixture.componentInstance.disabled = true;
137134
fixture.detectChanges();
138135

139-
const mouseEventWhileDisabled = createMouseEvent('click');
140-
spyOn(mouseEventWhileDisabled, 'preventDefault');
141-
tabLinkElement.dispatchEvent(mouseEventWhileDisabled);
142-
expect(mouseEventWhileDisabled.preventDefault).toHaveBeenCalled();
136+
expect(getComputedStyle(tabLinkElement).pointerEvents).toBe('none');
143137
});
144138

145139
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
@@ -193,7 +193,6 @@ export const _MatTabLinkMixinBase =
193193
'[attr.tabIndex]': 'tabIndex',
194194
'[class.mat-tab-disabled]': 'disabled',
195195
'[class.mat-tab-label-active]': 'active',
196-
'(click)': '_handleClick($event)'
197196
}
198197
})
199198
export class MatTabLink extends _MatTabLinkMixinBase
@@ -254,13 +253,4 @@ export class MatTabLink extends _MatTabLinkMixinBase
254253
ngOnDestroy() {
255254
this._tabLinkRipple._removeTriggerEvents();
256255
}
257-
258-
/**
259-
* Handles the click event, preventing default navigation if the tab link is disabled.
260-
*/
261-
_handleClick(event: MouseEvent) {
262-
if (this.disabled) {
263-
event.preventDefault();
264-
}
265-
}
266256
}

0 commit comments

Comments
 (0)