Skip to content

Commit

Permalink
fix(typewriter): initial focus list
Browse files Browse the repository at this point in the history
- allow empty focus lists

Signed-off-by: Cory Rylan <cory@coryrylan.com>
  • Loading branch information
coryrylan committed Aug 20, 2023
1 parent 90ee488 commit 368b294
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
27 changes: 26 additions & 1 deletion packages/typewriter/src/internals/utils/focus.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { html, LitElement } from 'lit';
import { customElement } from 'lit/decorators/custom-element.js';
import { createFixture, removeFixture } from '@blueprintui/test';
import { focusElement, focusable, simpleFocusable } from './focus.js';
import { focusElement, focusable, initializeKeyListItems, simpleFocusable } from './focus.js';

describe('isFocusable', () => {
let fixture: HTMLElement;
Expand Down Expand Up @@ -127,3 +127,28 @@ describe('simpleFocusable', () => {
expect(elements.filter(i => i === false).length).toBe(13);
});
});

describe('initializeKeyListItems', () => {
let fixture: HTMLElement;

beforeEach(async () => {
fixture = await createFixture(html`
<div></div>
<div></div>
<div></div>
`);
});

afterEach(() => {
removeFixture(fixture);
});

it('should mark simpleFocusable elements as true', () => {
const elements = Array.from(fixture.querySelectorAll<HTMLElement>('*'));
initializeKeyListItems(elements);

expect(elements[0].tabIndex).toBe(0);
expect(elements[1].tabIndex).toBe(-1);
expect(elements[2].tabIndex).toBe(-1);
});
});
6 changes: 4 additions & 2 deletions packages/typewriter/src/internals/utils/focus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ export function setActiveKeyListItem(items: NodeListOf<HTMLElement> | HTMLElemen
}

export function initializeKeyListItems(items: NodeListOf<HTMLElement> | HTMLElement[]) {
items.forEach(i => (i.tabIndex = -1));
items[0].tabIndex = 0;
if (items.length) {
items.forEach(i => (i.tabIndex = -1));
items[0].tabIndex = 0;
}
}

0 comments on commit 368b294

Please sign in to comment.