An open source design system that's fully customizable and agent ready
TypeScript
fix(hooks): cycle useTreeFocus typeahead on repeated letters (#4844)
alex-js-ltd5576940284c3
diff --git a/.changeset/tree-typeahead-cycling.md b/.changeset/tree-typeahead-cycling.mdnew file mode 100644index 0000000000..63a254c3d0--- /dev/null+++ b/.changeset/tree-typeahead-cycling.md@@ -0,0 +1,6 @@+---+'@astryxdesign/core': patch+---++[fix] TreeList typeahead now cycles through same-letter matches instead of stalling, and searches from the top when no treeitem is focused.+@alex-js-ltddiff --git a/packages/core/src/hooks/useTreeFocus.test.tsx b/packages/core/src/hooks/useTreeFocus.test.tsxindex e5182fb711..7d607633b3 100644--- a/packages/core/src/hooks/useTreeFocus.test.tsx+++ b/packages/core/src/hooks/useTreeFocus.test.tsx@@ -261,4 +261,54 @@ describe('useTreeFocus activation + typeahead', () => {fireEvent.keyDown(tree, {key: 'c'});expect(screen.getByTestId('c')).toHaveFocus();});++ it('typeahead cycles through same-letter matches on repeated presses', () => {+ const nodes: Node[] = [+ {id: 'apple', label: 'Apple', level: 1},+ {id: 'apricot', label: 'Apricot', level: 1},+ {id: 'avocado', label: 'Avocado', level: 1},+ ];+ render(<Tree collapsed={nodes} />);+ const tree = screen.getByRole('tree');+ screen.getByTestId('apple').focus();++ fireEvent.keyDown(tree, {key: 'a'});+ expect(screen.getByTestId('apricot')).toHaveFocus();++ // A repeat must cycle, not extend the query to "aa" (as useTypeahead does).+ fireEvent.keyDown(tree, {key: 'a'});+ expect(screen.getByTestId('avocado')).toHaveFocus();+ });++ it('typeahead searches from the top when no treeitem has focus', () => {+ const nodes: Node[] = [+ {id: 'apple', label: 'Apple', level: 1},+ {id: 'banana', label: 'Banana', level: 1},+ {id: 'avocado', label: 'Avocado', level: 1},+ ];+ render(<Tree collapsed={nodes} />);+ const tree = screen.getByRole('tree');++ // No .focus() call: with no current item, the first match must win.+ fireEvent.keyDown(tree, {key: 'a'});+ expect(screen.getByTestId('apple')).toHaveFocus();+ });++ it('typeahead keeps focus on an item that still matches the refined buffer', () => {+ const nodes: Node[] = [+ {id: 'apple', label: 'Apple', level: 1},+ {id: 'banana', label: 'Banana', level: 1},+ {id: 'apricot', label: 'Apricot', level: 1},+ ];+ render(<Tree collapsed={nodes} />);+ const tree = screen.getByRole('tree');+ screen.getByTestId('apple').focus();++ fireEvent.keyDown(tree, {key: 'a'});+ expect(screen.getByTestId('apricot')).toHaveFocus();++ // "ap" refines the search; Apricot still matches, so focus holds.+ fireEvent.keyDown(tree, {key: 'p'});+ expect(screen.getByTestId('apricot')).toHaveFocus();+ });});diff --git a/packages/core/src/hooks/useTreeFocus.ts b/packages/core/src/hooks/useTreeFocus.tsindex b233676e09..1fd44116a2 100644--- a/packages/core/src/hooks/useTreeFocus.ts+++ b/packages/core/src/hooks/useTreeFocus.ts@@ -377,14 +377,25 @@ export function useTreeFocus<T extends HTMLElement = HTMLElement>(if (state.timer != null) {clearTimeout(state.timer);}- state.buffer += e.key.toLowerCase();+ // "aa" would match nothing; a repeat means "next match" (useTypeahead).+ const char = e.key.toLowerCase();+ const isRepeatSameChar =+ state.buffer.length > 0 &&+ state.buffer.split('').every(c => c === char);+ state.buffer = isRepeatSameChar ? char : state.buffer + char;state.timer = setTimeout(() => {typeaheadRef.current.buffer = '';}, typeaheadResetMs) as unknown as number;const query = state.buffer;- const start = currentIndex < 0 ? 0 : currentIndex;- const ordered = [...items.slice(start + 1), ...items.slice(0, start + 1)];+ // A longer query is refining, so the current item may still be the match.+ const hasCurrent = currentIndex >= 0;+ const start = hasCurrent ? currentIndex : 0;+ const offset = hasCurrent && query.length === 1 ? 1 : 0;+ const ordered = [+ ...items.slice(start + offset),+ ...items.slice(0, start + offset),+ ];const match = ordered.find(item =>!itemDisabled(item) &&