Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/aria/listbox/listbox.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,23 @@ describe('Listbox', () => {
expect(listboxInstance.value()).toEqual([]);
});
});

describe('item mutations and focus stability', () => {
it('should recover focus by shifting to the default state if the active option is removed', async () => {
setupListbox({focusMode: 'activedescendant'});
click(2);
expect(listboxElement.getAttribute('aria-activedescendant')).toBe(optionElements[2].id);

const testComponent = fixture.componentInstance as ListboxExample;
const updatedOptions = testComponent.options().filter(o => o.value !== 2);
testComponent.options.set(updatedOptions);
fixture.detectChanges();
await waitForMicrotasks();
defineTestVariables(fixture);

expect(listboxElement.getAttribute('aria-activedescendant')).toBe(optionElements[0].id);
});
});
});

interface TestOption {
Expand Down
3 changes: 2 additions & 1 deletion src/aria/listbox/listbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,9 @@ export class Listbox<V> implements OnDestroy {
const items = inputs.items();
const activeItem = untracked(() => inputs.activeItem());

if (!items.some(i => i === activeItem) && activeItem) {
if (activeItem && !items.some(i => i === activeItem)) {
this._pattern.listBehavior.unfocus();
this._pattern.setDefaultState();
}
},
});
Expand Down
19 changes: 19 additions & 0 deletions src/aria/tree/tree.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1556,6 +1556,25 @@ describe('Tree', () => {
});
}
});

describe('item mutations and focus stability', () => {
it('should recover focus by shifting to the default state if the active item is removed', async () => {
setupTestTree();
updateTree({focusMode: 'activedescendant'});

const vegetablesEl = getTreeItemElementByValue('vegetables')!;
click(vegetablesEl);
expect(getFocusedTreeItemValue()).toBe('vegetables');

const updatedNodes = testComponent.nodes().filter(n => n.value !== 'vegetables');
testComponent.nodes.set(updatedNodes);
fixture.detectChanges();
await waitForMicrotasks();
defineTestVariables();

expect(getFocusedTreeItemValue()).toBe('fruits');
});
});
});

interface TestTreeNode<V = string> {
Expand Down
3 changes: 2 additions & 1 deletion src/aria/tree/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,9 @@ export class Tree<V> implements OnDestroy {
const items = inputs.items();
const activeItem = untracked(() => inputs.activeItem());

if (!items.some(i => i === activeItem) && activeItem) {
if (activeItem && !items.some(i => i === activeItem)) {
this._pattern.treeBehavior.unfocus();
this._pattern.setDefaultState();
}
},
});
Expand Down
Loading