Skip to content

Commit fdfec2f

Browse files
crisbetojelbourn
authored andcommitted
fix(a11y): list key manager not emitting change event if new item is added to active index (#19666)
Currently we emit the `ListKeyManager.change` event if the active index changes, but the problem is that the list could be swapped out, resulting in a new item at the same index. These changes switch to using the item reference to determine whether we need to emit the event. Fixes #19661.
1 parent c23de0e commit fdfec2f

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/cdk/a11y/key-manager/list-key-manager.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,18 @@ describe('Key managers', () => {
143143
subscription.unsubscribe();
144144
});
145145

146+
it('should emit if the active item changed, but not the active index', () => {
147+
const spy = jasmine.createSpy('change spy');
148+
const subscription = keyManager.change.subscribe(spy);
149+
150+
keyManager.setActiveItem(0);
151+
itemList.items.unshift(new FakeFocusable('zero'));
152+
keyManager.setActiveItem(0);
153+
154+
expect(spy).toHaveBeenCalledTimes(1);
155+
subscription.unsubscribe();
156+
});
157+
146158
it('should activate the first item when pressing down on a clean key manager', () => {
147159
keyManager = new ListKeyManager<FakeFocusable>(itemList);
148160

src/cdk/a11y/key-manager/list-key-manager.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,11 @@ export class ListKeyManager<T extends ListKeyManagerOption> {
187187
setActiveItem(item: T): void;
188188

189189
setActiveItem(item: any): void {
190-
const previousIndex = this._activeItemIndex;
190+
const previousActiveItem = this._activeItem;
191191

192192
this.updateActiveItem(item);
193193

194-
if (this._activeItemIndex !== previousIndex) {
194+
if (this._activeItem !== previousActiveItem) {
195195
this.change.next(this._activeItemIndex);
196196
}
197197
}

0 commit comments

Comments
 (0)