Skip to content
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

fix(material/tabs): prevent default keyboard actions on disabled links #27274

Merged
merged 1 commit into from Jun 10, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 15 additions & 26 deletions src/material/tabs/tab-nav-bar/tab-nav-bar.spec.ts
@@ -1,4 +1,4 @@
import {SPACE} from '@angular/cdk/keycodes';
import {ENTER, SPACE} from '@angular/cdk/keycodes';
import {waitForAsync, ComponentFixture, fakeAsync, TestBed, tick} from '@angular/core/testing';
import {Component, QueryList, ViewChild, ViewChildren} from '@angular/core';
import {MAT_RIPPLE_GLOBAL_OPTIONS, RippleGlobalOptions} from '@angular/material/core';
Expand Down Expand Up @@ -73,31 +73,6 @@ describe('MDC-based MatTabNavBar', () => {
expect(tabLinkElements[1].classList.contains('mdc-tab--active')).toBeTruthy();
});

it('should add the disabled class if disabled', () => {
const tabLinkElements = fixture.debugElement
Copy link
Member Author

Choose a reason for hiding this comment

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

This functionality was covered in another test so I deleted this one.

.queryAll(By.css('a'))
.map(tabLinkDebugEl => tabLinkDebugEl.nativeElement);

expect(
tabLinkElements.every(tabLinkEl => {
return !tabLinkEl.classList.contains('mat-mdc-tab-disabled');
}),
)
.withContext('Expected every tab link to not have the disabled class initially')
.toBe(true);

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

expect(
tabLinkElements.every(tabLinkEl => {
return tabLinkEl.classList.contains('mat-mdc-tab-disabled');
}),
)
.withContext('Expected every tab link to have the disabled class if set through binding')
.toBe(true);
});

it('should update aria-disabled if disabled', () => {
const tabLinkElements = fixture.debugElement
.queryAll(By.css('a'))
Expand Down Expand Up @@ -143,6 +118,20 @@ describe('MDC-based MatTabNavBar', () => {
expect(tabLinkElement.classList).toContain('mat-mdc-tab-disabled');
});

it('should prevent default keyboard actions on disabled links', () => {
const link = fixture.debugElement.query(By.css('a')).nativeElement;
fixture.componentInstance.disabled = true;
fixture.detectChanges();

const spaceEvent = dispatchKeyboardEvent(link, 'keydown', SPACE);
fixture.detectChanges();
expect(spaceEvent.defaultPrevented).toBe(true);

const enterEvent = dispatchKeyboardEvent(link, 'keydown', ENTER);
fixture.detectChanges();
expect(enterEvent.defaultPrevented).toBe(true);
});

it('should re-align the ink bar when the direction changes', fakeAsync(() => {
const inkBar = fixture.componentInstance.tabNavBar._inkBar;

Expand Down
6 changes: 4 additions & 2 deletions src/material/tabs/tab-nav-bar/tab-nav-bar.ts
Expand Up @@ -48,7 +48,7 @@ import {MatInkBar, MatInkBarItem, mixinInkBarItem} from '../ink-bar';
import {BooleanInput, coerceBooleanProperty, NumberInput} from '@angular/cdk/coercion';
import {BehaviorSubject, Subject} from 'rxjs';
import {startWith, takeUntil} from 'rxjs/operators';
import {SPACE} from '@angular/cdk/keycodes';
import {ENTER, SPACE} from '@angular/cdk/keycodes';
import {MAT_TABS_CONFIG, MatTabsConfig} from '../tab-config';
import {MatPaginatedTabHeader, MatPaginatedTabHeaderItem} from '../paginated-tab-header';

Expand Down Expand Up @@ -261,7 +261,9 @@ export class _MatTabLinkBase
}

_handleKeydown(event: KeyboardEvent) {
if (this._tabNavBar.tabPanel && event.keyCode === SPACE) {
if (this.disabled && (event.keyCode === SPACE || event.keyCode === ENTER)) {
event.preventDefault();
} else if (this._tabNavBar.tabPanel && event.keyCode === SPACE) {
Copy link
Contributor

Choose a reason for hiding this comment

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

we can switch to focused tab when we press Enter key too? Shouldn't it be like:

else if (this._tabNavBar.tabPanel && (event.keyCode === SPACE || event.keyCode === ENTER))

Copy link
Member Author

Choose a reason for hiding this comment

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

The enter condition is in there since some browsers do clicks both on enter and space, IIRC.

Copy link
Contributor

@naaajii naaajii Jun 10, 2023

Choose a reason for hiding this comment

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

got it, ig. I tried to navigate while tab being focused & pressing enter but it didn't navigate me, although it worked when I used spacebar only. Chrome 113.0.5672.92, Firefox 113.0.1 nor Chrome 104.0.5112.101.

this.elementRef.nativeElement.click();
}
}
Expand Down