An open source design system that's fully customizable and agent ready
TypeScript
fix(DropdownMenu): a data item with nested items inside a section renders its submenu (#8858)
Cindy Zhang1d462afd37a2
diff --git a/.changeset/dropdown-section-submenu.md b/.changeset/dropdown-section-submenu.mdnew file mode 100644index 0000000000..2f8e37b718--- /dev/null+++ b/.changeset/dropdown-section-submenu.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[fix] `DropdownMenu`, `MoreMenu` and `ContextMenu` render a submenu for a data item with nested `items` inside a `{type: 'section'}`, as they do at the top level. Before, such an item rendered as a plain row with no flyout and no `aria-haspopup`, although its type allows nested `items` there; the bottom sheet on a phone already showed it as a submenu.++@cixzhangdiff --git a/packages/core/src/ContextMenu/ContextMenu.test.tsx b/packages/core/src/ContextMenu/ContextMenu.test.tsxindex d3d00b8417..e4a7e98c61 100644--- a/packages/core/src/ContextMenu/ContextMenu.test.tsx+++ b/packages/core/src/ContextMenu/ContextMenu.test.tsx@@ -1320,6 +1320,41 @@ describe('ContextMenu keyboard access for menuitemradio/menuitemcheckbox (#3829)});});+describe('ContextMenu data-driven submenu inside a section', () => {+ it('renders a submenu row for an item with nested items inside a section', () => {+ render(+ <ContextMenu+ items={[+ {+ type: 'section',+ title: 'Organize',+ items: [+ {label: 'Rename', onClick: () => {}},+ {+ label: 'Move to',+ items: [+ {label: 'Folder A', onClick: () => {}},+ {label: 'Folder B', onClick: () => {}},+ ],+ },+ ],+ },+ ]}>+ <div>Right-click me</div>+ </ContextMenu>,+ );+ fireEvent.contextMenu(screen.getByText('Right-click me'));+ const trigger = screen.getByRole('menuitem', {+ name: /Move to/,+ hidden: true,+ });+ expect(trigger).toHaveAttribute('aria-haspopup', 'menu');+ expect(+ screen.getByRole('menuitem', {name: 'Folder A', hidden: true}),+ ).toBeInTheDocument();+ });+});+describe('ContextMenu press model', () => {const touch = {pointerType: 'touch', pointerId: 1};const item = (name: string) =>diff --git a/packages/core/src/DropdownMenu/DropdownMenuSubMenu.test.tsx b/packages/core/src/DropdownMenu/DropdownMenuSubMenu.test.tsxindex 271fad83ff..e3b322bedf 100644--- a/packages/core/src/DropdownMenu/DropdownMenuSubMenu.test.tsx+++ b/packages/core/src/DropdownMenu/DropdownMenuSubMenu.test.tsx@@ -647,6 +647,40 @@ describe('DropdownMenu data-driven submenus', () => {expect(onMove).toHaveBeenCalledWith('b');});+ it('renders a submenu for an item with nested items inside a section', async () => {+ const user = userEvent.setup();+ const onWork = vi.fn();+ render(+ <DropdownMenu+ button={{label: 'Share'}}+ items={[+ {+ type: 'section',+ title: 'Send',+ items: [+ {label: 'Copy link', onClick: () => {}},+ {+ label: 'Email',+ items: [+ {label: 'Work', onClick: onWork},+ {label: 'Personal', onClick: () => {}},+ ],+ },+ ],+ },+ ]}+ />,+ );+ await user.click(screen.getByRole('button', {name: /Share/}));+ const trigger = screen.getByRole('menuitem', {name: /Email/, hidden: true});+ expect(trigger).toHaveAttribute('aria-haspopup', 'menu');+ await user.click(trigger);+ await user.click(+ await screen.findByRole('menuitem', {name: 'Work', hidden: true}),+ );+ expect(onWork).toHaveBeenCalled();+ });+it('keyboard-reaches an item positioned after a submenu row', async () => {// Regression: the submenu flyout renders inline inside the root menu, so// the root's item query swept in the (hidden) flyout items. Arrow nav thendiff --git a/packages/core/src/DropdownMenu/renderDropdownItems.tsx b/packages/core/src/DropdownMenu/renderDropdownItems.tsxindex 6075996993..c56f3f36c3 100644--- a/packages/core/src/DropdownMenu/renderDropdownItems.tsx+++ b/packages/core/src/DropdownMenu/renderDropdownItems.tsx@@ -68,6 +68,28 @@ function renderLeafItem(return <DropdownMenuItem key={getItemKey(item, index)} {...itemProps} />;}+/**+ * A data item that declares nested `items` becomes a submenu (data-mode+ * parity with the compound DropdownMenuSubMenu API), at the top level and+ * inside a section alike; otherwise it's a leaf. renderDropdownItems owns the+ * recursion and hands the rendered children to DropdownMenuSubMenu, so+ * DropdownMenuSubMenu never imports this module back (no import cycle).+ */+function renderItem(item: DropdownMenuItemData, index: number): ReactElement {+ if (item.items && item.items.length > 0) {+ return (+ <DropdownMenuSubMenu+ key={getItemKey(item, index)}+ icon={item.icon}+ label={item.label}+ isDisabled={item.isDisabled}>+ {renderDropdownItems(item.items)}+ </DropdownMenuSubMenu>+ );+ }+ return renderLeafItem(item, index);+}+/*** Converts data-driven items into DropdownMenuItem components,* so both modes share the same rendering and keyboard navigation path.@@ -96,28 +118,11 @@ export function renderDropdownItems(items: DropdownMenuOption[]): ReactNode {{option.title}</div>)}- {option.items.map(renderLeafItem)}+ {option.items.map(renderItem)}</div>,);} else if (!('type' in option)) {- // A plain item that declares nested `items` becomes a submenu (data-mode- // parity with the compound DropdownMenuSubMenu API); otherwise it's a- // leaf. renderDropdownItems owns the recursion and hands the rendered- // children to DropdownMenuSubMenu — so DropdownMenuSubMenu never imports- // this module back (no import cycle).- if (option.items && option.items.length > 0) {- elements.push(- <DropdownMenuSubMenu- key={getItemKey(option, i)}- icon={option.icon}- label={option.label}- isDisabled={option.isDisabled}>- {renderDropdownItems(option.items)}- </DropdownMenuSubMenu>,- );- } else {- elements.push(renderLeafItem(option, i));- }+ elements.push(renderItem(option, i));}}