From 1f87cfcb025675cf4e8c3c21b7ded0b4caea6383 Mon Sep 17 00:00:00 2001 From: crisbeto Date: Sat, 4 Mar 2017 11:06:05 +0100 Subject: [PATCH 1/2] 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. --- src/lib/core/a11y/list-key-manager.spec.ts | 10 ++++++++++ src/lib/core/a11y/list-key-manager.ts | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/lib/core/a11y/list-key-manager.spec.ts b/src/lib/core/a11y/list-key-manager.spec.ts index 2f6bd8657f8e..6300a8c5b370 100644 --- a/src/lib/core/a11y/list-key-manager.spec.ts +++ b/src/lib/core/a11y/list-key-manager.spec.ts @@ -240,6 +240,16 @@ describe('Key managers', () => { expect(TAB_EVENT.defaultPrevented).toBe(false); }); + it('it should activate the first item when pressing down on a clean key manager', () => { + keyManager = new ListKeyManager(itemList); + + expect(keyManager.activeItemIndex).toBeNull('Expected active index to default to null.'); + + keyManager.onKeydown(DOWN_ARROW_EVENT); + + expect(keyManager.activeItemIndex).toBe(0, 'Expected first item to become active.'); + }); + }); describe('programmatic focus', () => { diff --git a/src/lib/core/a11y/list-key-manager.ts b/src/lib/core/a11y/list-key-manager.ts index 308e10a67dca..d54bf1a1521d 100644 --- a/src/lib/core/a11y/list-key-manager.ts +++ b/src/lib/core/a11y/list-key-manager.ts @@ -16,7 +16,7 @@ export interface CanDisable { * of items, it will set the active item correctly when arrow events occur. */ export class ListKeyManager { - private _activeItemIndex: number; + private _activeItemIndex: number = null; private _activeItem: T; private _tabOut: Subject = new Subject(); private _wrap: boolean = false; From af412e525605e2cbac903f8f5ac231545fb71614 Mon Sep 17 00:00:00 2001 From: crisbeto Date: Sun, 5 Mar 2017 00:14:13 +0100 Subject: [PATCH 2/2] fix: it it --- src/lib/core/a11y/list-key-manager.spec.ts | 2 +- src/lib/tabs/tab-body.spec.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/core/a11y/list-key-manager.spec.ts b/src/lib/core/a11y/list-key-manager.spec.ts index 6300a8c5b370..4a64bac10cd1 100644 --- a/src/lib/core/a11y/list-key-manager.spec.ts +++ b/src/lib/core/a11y/list-key-manager.spec.ts @@ -240,7 +240,7 @@ describe('Key managers', () => { expect(TAB_EVENT.defaultPrevented).toBe(false); }); - it('it should activate the first item when pressing down on a clean key manager', () => { + it('should activate the first item when pressing down on a clean key manager', () => { keyManager = new ListKeyManager(itemList); expect(keyManager.activeItemIndex).toBeNull('Expected active index to default to null.'); diff --git a/src/lib/tabs/tab-body.spec.ts b/src/lib/tabs/tab-body.spec.ts index 4ff41baf642a..4d6840097c4a 100644 --- a/src/lib/tabs/tab-body.spec.ts +++ b/src/lib/tabs/tab-body.spec.ts @@ -169,7 +169,7 @@ describe('MdTabBody', () => { })); }); - it('it should toggle the canBeAnimated flag', () => { + it('should toggle the canBeAnimated flag', () => { let fixture: ComponentFixture; let tabBody: MdTabBody;