Skip to content

Commit

Permalink
fix(tabs): don't prevent default space/enter action if action tab doe…
Browse files Browse the repository at this point in the history
…sn't change (#19207)

Switches to only preventing the default enter/space action if we did something with the event, otherwise we risk interfering with other user events.

Fixes #19190.
  • Loading branch information
crisbeto authored and jelbourn committed May 8, 2020
1 parent 49be570 commit 7a4128b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
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

0 comments on commit 7a4128b

Please sign in to comment.