From a5498e81d19c5ff9f445b5119fb90fa0658d04ed Mon Sep 17 00:00:00 2001 From: Bogdan Chadkin Date: Wed, 8 Jul 2026 22:41:05 +0400 Subject: [PATCH] Skip disabled focus group items --- focus-group.js | 8 ++++++-- test/focus-group.test.ts | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/focus-group.js b/focus-group.js index 868608b..6990ba8 100644 --- a/focus-group.js +++ b/focus-group.js @@ -32,6 +32,10 @@ function getItems(target, group) { function filterNested(allItems, group) { return Array.from(allItems).filter(item => { + if (item.disabled || item.getAttribute('aria-disabled') === 'true') { + return false + } + let current = item.parentElement while (current && current !== group) { if (current.getAttribute('aria-hidden') === 'true') { @@ -80,7 +84,7 @@ export function focusGroupKeyUX(options) { let allItems = getItems(event.target, group) let items = filterNested(allItems, group) - let index = Array.from(items).indexOf(event.target) + let index = items.indexOf(event.target) let nextKey = 'ArrowDown' let prevKey = 'ArrowUp' @@ -114,7 +118,7 @@ export function focusGroupKeyUX(options) { } lastTyped = event.timeStamp - let found = Array.from(allItems).find(item => { + let found = items.find(item => { return item.textContent ?.trim() ?.toLowerCase() diff --git a/test/focus-group.test.ts b/test/focus-group.test.ts index b96148d..6dbbb21 100644 --- a/test/focus-group.test.ts +++ b/test/focus-group.test.ts @@ -204,6 +204,46 @@ describe('focus-group', () => { equal(window.document.activeElement, items[3]) }) + test('skips disabled items', async () => { + let window = new JSDOM().window + startKeyUX(window, [ + hotkeyKeyUX(), + focusGroupKeyUX({ + searchDelayMs: 100 + }) + ]) + + window.document.body.innerHTML = + '' + + let items = window.document.querySelectorAll('button') + items[0]!.focus() + + press(window, 'ArrowDown') + equal(window.document.activeElement, items[3]) + + press(window, 'ArrowUp') + equal(window.document.activeElement, items[0]) + + press(window, 'End') + equal(window.document.activeElement, items[3]) + + press(window, 'Home') + equal(window.document.activeElement, items[0]) + + press(window, 'ArrowUp') + equal(window.document.activeElement, items[3]) + + await setTimeout(150) + press(window, 'b') + equal(window.document.activeElement, items[3]) + }) + test('supports RTL locales', () => { let window = new JSDOM().window startKeyUX(window, [focusGroupKeyUX()])