Skip to content

Commit

Permalink
fix(list-key-manager): exception when no initial active item
Browse files Browse the repository at this point in the history
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 angular#3317.
  • Loading branch information
crisbeto committed Mar 4, 2017
1 parent 94adecd commit 1f87cfc
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/lib/core/a11y/list-key-manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<FakeFocusable>(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', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/core/a11y/list-key-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export interface CanDisable {
* of items, it will set the active item correctly when arrow events occur.
*/
export class ListKeyManager<T extends CanDisable> {
private _activeItemIndex: number;
private _activeItemIndex: number = null;
private _activeItem: T;
private _tabOut: Subject<any> = new Subject();
private _wrap: boolean = false;
Expand Down

0 comments on commit 1f87cfc

Please sign in to comment.