Skip to content

Commit

Permalink
fix(material/tabs): nav bar not navigating on enter presses
Browse files Browse the repository at this point in the history
Fixes that the tab nav bar wasn't navigating when pressing the enter key.

Fixes angular#27843.
  • Loading branch information
crisbeto committed Sep 28, 2023
1 parent cc3428e commit c9e154c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
13 changes: 13 additions & 0 deletions src/material/tabs/tab-nav-bar/tab-nav-bar.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,19 @@ describe('MDC-based MatTabNavBar', () => {
expect(tabLinks[1].classList.contains('mdc-tab--active')).toBe(true);
});

it('should activate a link when enter is pressed', () => {
const fixture = TestBed.createComponent(SimpleTabNavBarTestApp);
fixture.detectChanges();

const tabLinks = fixture.nativeElement.querySelectorAll('.mat-mdc-tab-link');
expect(tabLinks[1].classList.contains('mdc-tab--active')).toBe(false);

dispatchKeyboardEvent(tabLinks[1], 'keydown', ENTER);
fixture.detectChanges();

expect(tabLinks[1].classList.contains('mdc-tab--active')).toBe(true);
});

describe('ripples', () => {
let fixture: ComponentFixture<SimpleTabNavBarTestApp>;

Expand Down
10 changes: 6 additions & 4 deletions src/material/tabs/tab-nav-bar/tab-nav-bar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,10 +384,12 @@ export class MatTabLink
}

_handleKeydown(event: KeyboardEvent) {
if (this.disabled && (event.keyCode === SPACE || event.keyCode === ENTER)) {
event.preventDefault();
} else if (this._tabNavBar.tabPanel && event.keyCode === SPACE) {
this.elementRef.nativeElement.click();
if (event.keyCode === SPACE || event.keyCode === ENTER) {
if (this.disabled) {
event.preventDefault();
} else if (this._tabNavBar.tabPanel) {
this.elementRef.nativeElement.click();
}
}
}

Expand Down

0 comments on commit c9e154c

Please sign in to comment.