From 12b6db7f40f485cf1e5121660bab420ec61a2f2e Mon Sep 17 00:00:00 2001 From: splincode Date: Tue, 3 Dec 2024 11:39:52 +0300 Subject: [PATCH] fix(core): focus first element in data-list --- .../cdk/utils/focus/get-closest-focusable.ts | 4 ++-- .../tests/core/data-list/data-list.pw.spec.ts | 23 +++++++++++++++---- .../data-list-dropdown-manager.directive.ts | 4 +--- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/projects/cdk/utils/focus/get-closest-focusable.ts b/projects/cdk/utils/focus/get-closest-focusable.ts index cb10b2a24463..db960badd785 100644 --- a/projects/cdk/utils/focus/get-closest-focusable.ts +++ b/projects/cdk/utils/focus/get-closest-focusable.ts @@ -53,7 +53,7 @@ export function tuiGetClosestFocusable({ treeWalker.currentNode = initial; - while (previous ? treeWalker.previousNode() : treeWalker.nextNode()) { + do { if (tuiIsHTMLElement(treeWalker.currentNode)) { initial = treeWalker.currentNode; } @@ -61,7 +61,7 @@ export function tuiGetClosestFocusable({ if (tuiIsHTMLElement(initial) && check(initial)) { return initial; } - } + } while (previous ? treeWalker.previousNode() : treeWalker.nextNode()); return null; } diff --git a/projects/demo-playwright/tests/core/data-list/data-list.pw.spec.ts b/projects/demo-playwright/tests/core/data-list/data-list.pw.spec.ts index f2816d552fa9..8a6401ad9834 100644 --- a/projects/demo-playwright/tests/core/data-list/data-list.pw.spec.ts +++ b/projects/demo-playwright/tests/core/data-list/data-list.pw.spec.ts @@ -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}) => { diff --git a/projects/kit/directives/data-list-dropdown-manager/data-list-dropdown-manager.directive.ts b/projects/kit/directives/data-list-dropdown-manager/data-list-dropdown-manager.directive.ts index f43b62f4bebe..183be4d03693 100644 --- a/projects/kit/directives/data-list-dropdown-manager/data-list-dropdown-manager.directive.ts +++ b/projects/kit/directives/data-list-dropdown-manager/data-list-dropdown-manager.directive.ts @@ -163,8 +163,6 @@ export class TuiDataListDropdownManager implements AfterViewInit { root: content, }); - if (item) { - item.focus(); - } + item?.focus(); } }