Skip to content

Commit

Permalink
fix(core): focus first element in data-list (#9891)
Browse files Browse the repository at this point in the history
  • Loading branch information
splincode authored Dec 3, 2024
1 parent 15f2a46 commit ab7dfde
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 12 deletions.
6 changes: 4 additions & 2 deletions projects/cdk/directives/focus-trap/focus-trap.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,11 @@ export class TuiFocusTrap implements OnDestroy {
}

protected onFocusIn(node: Node): void {
if (!tuiContainsOrAfter(this.el, node)) {
const firstElementChild = this.el.firstElementChild;

if (!tuiContainsOrAfter(this.el, node) && firstElementChild) {
tuiGetClosestFocusable({
initial: this.el,
initial: firstElementChild,
root: this.el,
})?.focus();
}
Expand Down
4 changes: 2 additions & 2 deletions projects/cdk/utils/focus/get-closest-focusable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ export function tuiGetClosestFocusable({

treeWalker.currentNode = initial;

while (previous ? treeWalker.previousNode() : treeWalker.nextNode()) {
do {
if (tuiIsHTMLElement(treeWalker.currentNode)) {
initial = treeWalker.currentNode;
}

if (tuiIsHTMLElement(initial) && check(initial)) {
return initial;
}
}
} while (previous ? treeWalker.previousNode() : treeWalker.nextNode());

return null;
}
23 changes: 18 additions & 5 deletions projects/demo-playwright/tests/core/data-list/data-list.pw.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,37 +36,50 @@ test.describe('DataList', () => {
const example = documentationPagePO.getExample('#submenu');

await example.scrollIntoViewIfNeeded();
await example.locator('button').click();
await documentationPagePO.prepareBeforeScreenshot();

await example.locator('button').click();
await page.keyboard.down('ArrowDown');
await page.keyboard.down('ArrowDown');

await expect(page).toHaveScreenshot('03-1-data-list.png');

await page.waitForTimeout(100);
await page.keyboard.down('Enter');
await page.waitForTimeout(100);

await expect(page).toHaveScreenshot('03-1-data-list.png');
await expect(page).toHaveScreenshot('03-2-data-list.png');

await page.keyboard.down('ArrowRight');

await expect(page).toHaveScreenshot('03-3-data-list.png');

await page.keyboard.down('ArrowDown');
await page.keyboard.down('ArrowDown');
await page.keyboard.down('ArrowDown');

await expect(page).toHaveScreenshot('03-4-data-list.png');

await page.waitForTimeout(100);
await page.keyboard.down('Enter');
await page.waitForTimeout(100);

await expect(page).toHaveScreenshot('03-2-data-list.png');
await expect(page).toHaveScreenshot('03-5-data-list.png');

await page.keyboard.down('ArrowRight');

await expect(page).toHaveScreenshot('03-6-data-list.png');

await page.keyboard.down('ArrowDown');
await page.keyboard.down('ArrowDown');

await expect(page).toHaveScreenshot('03-3-data-list.png');
await expect(page).toHaveScreenshot('03-7-data-list.png');

await page.waitForTimeout(100);
await page.keyboard.down('Enter');
await page.waitForTimeout(100);

await expect(page).toHaveScreenshot('03-4-data-list.png');
await expect(page).toHaveScreenshot('03-8-data-list.png');
});

test('Form control', async ({page}) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,6 @@ export class TuiDataListDropdownManager implements AfterViewInit {
root: content,
});

if (item) {
item.focus();
}
item?.focus();
}
}

0 comments on commit ab7dfde

Please sign in to comment.