An open source design system that's fully customizable and agent ready
TypeScript
fix(treelist): make parent rows keyboard-expandable (#3344)
Cindy Zhang474ba5b51780
diff --git a/.changeset/treelist-keyboard-expand.md b/.changeset/treelist-keyboard-expand.mdnew file mode 100644index 0000000000..2b02ac5a37--- /dev/null+++ b/.changeset/treelist-keyboard-expand.md@@ -0,0 +1,6 @@+---+'@astryxdesign/core': patch+---++[fix] TreeList: parent rows can now be expanded and collapsed from the keyboard. Any item with children renders a real focusable toggle button with `aria-expanded`, so expansion no longer requires a mouse — previously items without an `onClick`/`href` had no focusable control at all (#3343).+@cixzhangdiff --git a/packages/core/src/TreeList/TreeList.test.tsx b/packages/core/src/TreeList/TreeList.test.tsxindex 9174d7a4f5..9c0527d420 100644--- a/packages/core/src/TreeList/TreeList.test.tsx+++ b/packages/core/src/TreeList/TreeList.test.tsx@@ -160,6 +160,26 @@ describe('TreeList', () => {expect(item).not.toHaveAttribute('aria-expanded');});+ it('renders a keyboard-focusable toggle button for parents without onClick/href', () => {+ render(<TreeList items={nestedItems} />);+ const toggle = screen.getByRole('button', {name: 'Toggle children'});+ expect(toggle).toBeInTheDocument();+ expect(toggle).toHaveAttribute('aria-expanded', 'false');+ });++ it('expands a parent from the keyboard via the toggle button', async () => {+ const user = userEvent.setup();+ render(<TreeList items={nestedItems} />);+ // Collapsed: children are not rendered.+ expect(screen.queryByText('Child 1')).not.toBeInTheDocument();+ const toggle = screen.getByRole('button', {name: 'Toggle children'});+ toggle.focus();+ expect(toggle).toHaveFocus();+ await user.keyboard('{Enter}');+ expect(screen.getByText('Child 1')).toBeInTheDocument();+ expect(toggle).toHaveAttribute('aria-expanded', 'true');+ });+it('renders group role for nested children', () => {render(<TreeList items={nestedItemsExpanded} />);const groups = document.querySelectorAll('[role="group"]');@@ -210,18 +230,14 @@ describe('TreeList', () => {it('fires onClick when invisible button is clicked', async () => {const user = userEvent.setup();const onClick = vi.fn();- const items: TreeListItemData[] = [- {id: 'a', label: 'Clickable', onClick},- ];+ const items: TreeListItemData[] = [{id: 'a', label: 'Clickable', onClick}];render(<TreeList items={items} />);await user.click(screen.getByRole('button'));expect(onClick).toHaveBeenCalledTimes(1);});it('renders an invisible anchor when href is provided', () => {- const items: TreeListItemData[] = [- {id: 'a', label: 'Link', href: '/docs'},- ];+ const items: TreeListItemData[] = [{id: 'a', label: 'Link', href: '/docs'}];const {container} = render(<TreeList items={items} />);const anchor = container.querySelector('a');expect(anchor).toBeInTheDocument();diff --git a/packages/core/src/TreeList/TreeListItem.tsx b/packages/core/src/TreeList/TreeListItem.tsxindex c26f39474b..4b8da4cc89 100644--- a/packages/core/src/TreeList/TreeListItem.tsx+++ b/packages/core/src/TreeList/TreeListItem.tsx@@ -342,18 +342,23 @@ export function TreeListItem({);const chevron = hasChildren ? (- handleClick != null && onClick != null ? (- // Separate toggle button when item has its own onClick+ handleToggle != null ? (+ // Real toggle button whenever expand/collapse is supported, so the row+ // can be expanded from the keyboard even when the item has no onClick/href+ // (row-level onClick is the only click path in that case, but there is no+ // focusable element to receive Enter/Space). The row's handleClick ignores+ // clicks originating inside a <button>, so this never double-toggles.<buttontype="button"aria-expanded={isExpanded}aria-label="Toggle children"+ disabled={isDisabled}onClick={handleToggle}{...stylex.props(styles.chevronButton)}>{chevronIcon}</button>) : (- // Non-interactive chevron when clicking the row toggles+ // Non-interactive chevron only when toggling is not wired up at all<span {...stylex.props(styles.chevronContainer)}>{chevronIcon}</span>)) : null;