From f160dc18411e110c1b45fdb4ba93d9940724bf2c Mon Sep 17 00:00:00 2001 From: Cassandra Choi Date: Tue, 19 Dec 2023 20:03:57 +0000 Subject: [PATCH] fix(cdk/a11y): allows disabled items to receive initial focus --- src/cdk/a11y/key-manager/tree-key-manager.spec.ts | 14 ++------------ src/cdk/a11y/key-manager/tree-key-manager.ts | 6 +----- 2 files changed, 3 insertions(+), 17 deletions(-) diff --git a/src/cdk/a11y/key-manager/tree-key-manager.spec.ts b/src/cdk/a11y/key-manager/tree-key-manager.spec.ts index 77a89d78e139..338aead9c0e4 100644 --- a/src/cdk/a11y/key-manager/tree-key-manager.spec.ts +++ b/src/cdk/a11y/key-manager/tree-key-manager.spec.ts @@ -182,18 +182,8 @@ describe('TreeKeyManager', () => { itemList.notifyOnChanges(); }); - it('does not initialize with the first item activated', () => { - expect(keyManager.getActiveItemIndex()).withContext('active item index').toBe(-1); - }); - - it('if an item is subsequently enabled, activates it', () => { - itemList.reset([ - new itemParam.constructor('Bilbo', true), - new itemParam.constructor('Frodo', false), - new itemParam.constructor('Pippin', true), - ]); - itemList.notifyOnChanges(); - expect(keyManager.getActiveItemIndex()).withContext('active item index').toBe(1); + it('initializes with the first item activated', () => { + expect(keyManager.getActiveItemIndex()).withContext('active item index').toBe(0); }); }); diff --git a/src/cdk/a11y/key-manager/tree-key-manager.ts b/src/cdk/a11y/key-manager/tree-key-manager.ts index 81323e40ca35..c3d1fd683e0f 100644 --- a/src/cdk/a11y/key-manager/tree-key-manager.ts +++ b/src/cdk/a11y/key-manager/tree-key-manager.ts @@ -73,7 +73,7 @@ export class TreeKeyManager implements TreeKeyMana return; } - let focusIndex = -1; + let focusIndex = 0; for (let i = 0; i < this._items.length; i++) { if (!this._skipPredicateFn(this._items[i]) && !this._isItemDisabled(this._items[i])) { focusIndex = i; @@ -81,10 +81,6 @@ export class TreeKeyManager implements TreeKeyMana } } - if (focusIndex === -1) { - return; - } - this.focusItem(focusIndex); this._hasInitialFocused = true; }