Skip to content

fix(tabs): don't prevent default space/enter action if active tab doesn't change #19207

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
May 8, 2020
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
13 changes: 13 additions & 0 deletions src/material-experimental/mdc-tabs/tab-header.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,19 @@ describe('MDC-based MatTabHeader', () => {
expect(spaceEvent.defaultPrevented).toBe(true);
});

it('should not prevent the default space/enter action if the current is selected', () => {
appComponent.tabHeader.focusIndex = appComponent.tabHeader.selectedIndex = 0;
fixture.detectChanges();

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

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

it('should move focus to the first tab when pressing HOME', () => {
appComponent.tabHeader.focusIndex = 3;
fixture.detectChanges();
Expand Down
6 changes: 4 additions & 2 deletions src/material/tabs/paginated-tab-header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,10 @@ export abstract class MatPaginatedTabHeader implements AfterContentChecked, Afte
break;
case ENTER:
case SPACE:
this.selectFocusedIndex.emit(this.focusIndex);
this._itemSelected(event);
if (this.focusIndex !== this.selectedIndex) {
this.selectFocusedIndex.emit(this.focusIndex);
this._itemSelected(event);
}
break;
default:
this._keyManager.onKeydown(event);
Expand Down
13 changes: 13 additions & 0 deletions src/material/tabs/tab-header.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,19 @@ describe('MatTabHeader', () => {
expect(spaceEvent.defaultPrevented).toBe(true);
});

it('should not prevent the default space/enter action if the current is selected', () => {
appComponent.tabHeader.focusIndex = appComponent.tabHeader.selectedIndex = 0;
fixture.detectChanges();

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

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

it('should move focus to the first tab when pressing HOME', () => {
appComponent.tabHeader.focusIndex = 3;
fixture.detectChanges();
Expand Down