Skip to content

fix(tabs): disabled tab link not preventing router navigation #10358

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 4, 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
8 changes: 8 additions & 0 deletions src/lib/tabs/tab-nav-bar/tab-nav-bar.scss
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@
flex-basis: 0;
flex-grow: 1;
}

&.mat-tab-disabled {
// We use `pointer-events` to make the element unclickable when it's disabled, rather than
// preventing the default action through JS, because we can't prevent the action reliably
// due to other directives potentially registering their events earlier. This shouldn't cause
// the user to click through, because we always have a `.mat-tab-links` behind the link.
pointer-events: none;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this prevent tabbing to the element and hitting enter to navigate?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We add tabindex="-1" when the link becomes disabled so you can't tab to it.

}
}


Expand Down
14 changes: 4 additions & 10 deletions src/lib/tabs/tab-nav-bar/tab-nav-bar.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {async, ComponentFixture, fakeAsync, TestBed, tick} from '@angular/core/testing';
import {Component, ViewChild, ViewChildren, QueryList} from '@angular/core';
import {By} from '@angular/platform-browser';
import {dispatchFakeEvent, dispatchMouseEvent, createMouseEvent} from '@angular/cdk/testing';
import {dispatchFakeEvent, dispatchMouseEvent} from '@angular/cdk/testing';
import {Direction, Directionality} from '@angular/cdk/bidi';
import {Subject} from 'rxjs';
import {MatTabLink, MatTabNav, MatTabsModule} from '../index';
Expand Down Expand Up @@ -150,21 +150,15 @@ describe('MatTabNavBar', () => {
.toBe(true, 'Expected element to no longer be keyboard focusable if disabled.');
});

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

const mouseEvent = createMouseEvent('click');
spyOn(mouseEvent, 'preventDefault');
tabLinkElement.dispatchEvent(mouseEvent);
expect(mouseEvent.preventDefault).not.toHaveBeenCalled();
expect(getComputedStyle(tabLinkElement).pointerEvents).not.toBe('none');

fixture.componentInstance.disabled = true;
fixture.detectChanges();

const mouseEventWhileDisabled = createMouseEvent('click');
spyOn(mouseEventWhileDisabled, 'preventDefault');
tabLinkElement.dispatchEvent(mouseEventWhileDisabled);
expect(mouseEventWhileDisabled.preventDefault).toHaveBeenCalled();
expect(getComputedStyle(tabLinkElement).pointerEvents).toBe('none');
});

it('should show ripples for tab links', () => {
Expand Down
10 changes: 0 additions & 10 deletions src/lib/tabs/tab-nav-bar/tab-nav-bar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@ export const _MatTabLinkMixinBase =
'[attr.tabIndex]': 'tabIndex',
'[class.mat-tab-disabled]': 'disabled',
'[class.mat-tab-label-active]': 'active',
'(click)': '_handleClick($event)'
}
})
export class MatTabLink extends _MatTabLinkMixinBase
Expand Down Expand Up @@ -258,13 +257,4 @@ export class MatTabLink extends _MatTabLinkMixinBase
this._focusMonitor.stopMonitoring(this._elementRef.nativeElement);
}
}

/**
* Handles the click event, preventing default navigation if the tab link is disabled.
*/
_handleClick(event: MouseEvent) {
if (this.disabled) {
event.preventDefault();
}
}
}