Skip to content

Commit 842896b

Browse files
crisbetotinayuangao
authored andcommitted
fix(list-key-manager): exception when no initial active item (#3431)
* fix(list-key-manager): exception when no initial active item Fixes an exception that is thrown when the user presses a key on ListKeyManager that doesn't have a default active item. The problem was due to the fact that we have a check for `null` a little bit down, that handles cases like this, however the active index is `undefined` by default. Fixes #3317. * fix: it it
1 parent b135b64 commit 842896b

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

src/lib/core/a11y/list-key-manager.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,16 @@ describe('Key managers', () => {
240240
expect(TAB_EVENT.defaultPrevented).toBe(false);
241241
});
242242

243+
it('should activate the first item when pressing down on a clean key manager', () => {
244+
keyManager = new ListKeyManager<FakeFocusable>(itemList);
245+
246+
expect(keyManager.activeItemIndex).toBeNull('Expected active index to default to null.');
247+
248+
keyManager.onKeydown(DOWN_ARROW_EVENT);
249+
250+
expect(keyManager.activeItemIndex).toBe(0, 'Expected first item to become active.');
251+
});
252+
243253
});
244254

245255
describe('programmatic focus', () => {

src/lib/core/a11y/list-key-manager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export interface CanDisable {
1616
* of items, it will set the active item correctly when arrow events occur.
1717
*/
1818
export class ListKeyManager<T extends CanDisable> {
19-
private _activeItemIndex: number;
19+
private _activeItemIndex: number = null;
2020
private _activeItem: T;
2121
private _tabOut: Subject<any> = new Subject();
2222
private _wrap: boolean = false;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ describe('MdTabBody', () => {
169169
}));
170170
});
171171

172-
it('it should toggle the canBeAnimated flag', () => {
172+
it('should toggle the canBeAnimated flag', () => {
173173
let fixture: ComponentFixture<SimpleTabBodyApp>;
174174
let tabBody: MdTabBody;
175175

0 commit comments

Comments
 (0)