Skip to content

Commit b5f4caf

Browse files
crisbetoandrewseguin
authored andcommitted
fix(tabs): allow for tabs to be selected using the space key (#6426)
* Allows for the user to select a tab using the space key, in addition to enter. This seems to be consistent with the M1 implementation of tabs. * Prevents the default action for space and enter in order to prevent the page from scrolling by accident. Fixes #6406.
1 parent 1df79e9 commit b5f4caf

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

src/lib/tabs/tab-header.spec.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
import {Component, ViewChild, ViewContainerRef} from '@angular/core';
55
import {CommonModule} from '@angular/common';
66
import {By} from '@angular/platform-browser';
7-
import {ENTER, LEFT_ARROW, RIGHT_ARROW} from '@angular/cdk/keycodes';
7+
import {ENTER, LEFT_ARROW, RIGHT_ARROW, SPACE} from '@angular/cdk/keycodes';
88
import {PortalModule} from '@angular/cdk/portal';
99
import {ViewportRuler} from '@angular/cdk/overlay';
1010
import {Direction, Directionality} from '@angular/cdk/bidi';
@@ -120,14 +120,22 @@ describe('MdTabHeader', () => {
120120

121121
// Select the focused index 2
122122
expect(appComponent.selectedIndex).toBe(0);
123-
dispatchKeyboardEvent(tabListContainer, 'keydown', ENTER);
123+
const enterEvent = dispatchKeyboardEvent(tabListContainer, 'keydown', ENTER);
124124
fixture.detectChanges();
125125
expect(appComponent.selectedIndex).toBe(2);
126+
expect(enterEvent.defaultPrevented).toBe(true);
126127

127128
// Move focus right to 0
128129
dispatchKeyboardEvent(tabListContainer, 'keydown', LEFT_ARROW);
129130
fixture.detectChanges();
130131
expect(appComponent.mdTabHeader.focusIndex).toBe(0);
132+
133+
// Select the focused 0 using space.
134+
expect(appComponent.selectedIndex).toBe(2);
135+
const spaceEvent = dispatchKeyboardEvent(tabListContainer, 'keydown', SPACE);
136+
fixture.detectChanges();
137+
expect(appComponent.selectedIndex).toBe(0);
138+
expect(spaceEvent.defaultPrevented).toBe(true);
131139
});
132140
});
133141

src/lib/tabs/tab-header.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import {
2525
ChangeDetectorRef,
2626
} from '@angular/core';
2727
import {Directionality, Direction} from '@angular/cdk/bidi';
28-
import {RIGHT_ARROW, LEFT_ARROW, ENTER} from '@angular/cdk/keycodes';
28+
import {RIGHT_ARROW, LEFT_ARROW, ENTER, SPACE} from '@angular/cdk/keycodes';
2929
import {auditTime, startWith} from '@angular/cdk/rxjs';
3030
import {Subscription} from 'rxjs/Subscription';
3131
import {of as observableOf} from 'rxjs/observable/of';
@@ -172,7 +172,9 @@ export class MdTabHeader extends _MdTabHeaderMixinBase
172172
this._focusPreviousTab();
173173
break;
174174
case ENTER:
175+
case SPACE:
175176
this.selectFocusedIndex.emit(this.focusIndex);
177+
event.preventDefault();
176178
break;
177179
}
178180
}

0 commit comments

Comments
 (0)