An open source design system that's fully customizable and agent ready
TypeScript
refactor(menu): source DropdownMenuItemData from DropdownMenuItem's props (#4809)
Cindy Zhangfc1c3f3194aa
diff --git a/.changeset/menu-item-data-from-item-props.md b/.changeset/menu-item-data-from-item-props.mdnew file mode 100644index 0000000000..606c788a70--- /dev/null+++ b/.changeset/menu-item-data-from-item-props.md@@ -0,0 +1,6 @@+---+'@astryxdesign/core': patch+---++[chore] `DropdownMenuItemData` — the shape of one entry in a `DropdownMenu` / `ContextMenu` / `MoreMenu` `items` array — is now sourced from `DropdownMenuItemProps` (`Pick`) instead of restating `icon`, `onClick`, `isDisabled`, and `variant` by hand, and `renderDropdownItems` forwards the whole item to `DropdownMenuItem` rather than copying it field by field. The data and compound APIs describe the same item, so they can no longer drift — exposing another item prop to the data API is now one key in the `Pick`. The type is structurally identical to before (`label` is still narrowed to `string`, since the renderer keys rows by it) and rendering is unchanged.+@cixzhangdiff --git a/packages/core/src/DropdownMenu/DropdownMenu.tsx b/packages/core/src/DropdownMenu/DropdownMenu.tsxindex ced3259787..bbc55070c1 100644--- a/packages/core/src/DropdownMenu/DropdownMenu.tsx+++ b/packages/core/src/DropdownMenu/DropdownMenu.tsx@@ -35,9 +35,9 @@ import * as stylex from '@stylexjs/stylex';import {usePopover} from '../Popover/usePopover';import {Button, type ButtonProps} from '../Button';import {Icon} from '../Icon';-import type {IconType} from '../Icon';import {renderDropdownItems} from './renderDropdownItems';+import type {DropdownMenuItemProps} from './DropdownMenuItem';import {MENU_ITEM_ROLES,MENU_ITEM_SELECTOR,@@ -99,16 +99,23 @@ const styles = stylex.create({// Types// =============================================================================-export interface DropdownMenuItemData {- label: string;- onClick?: () => void;- isDisabled?: boolean;- icon?: ReactNode | IconType;+/**+ * Data-mode shape for one menu row.+ *+ * The item fields are sourced from `DropdownMenuItemProps` — data mode renders+ * through `DropdownMenuItem`, so the two APIs describe the same thing and must+ * not drift. Only the fields listed here are part of the data API; add a key to+ * the `Pick` to expose more of the item's props to `items`.+ */+export interface DropdownMenuItemData extends Pick<+ DropdownMenuItemProps,+ 'icon' | 'onClick' | 'isDisabled' | 'variant'+> {/**- * Visual variant for a leaf item. `'destructive'` renders it in the error- * color for dangerous actions (e.g. Delete). @default 'default'+ * Primary label text. Narrowed to `string` from the item's `ReactNode`:+ * data mode derives each row's React key from the label.*/- variant?: 'default' | 'destructive';+ label: string;/*** Nested submenu entries. When present, this row becomes a submenu (a* flyout revealing `items`) instead of a leaf action — no separate itemdiff --git a/packages/core/src/DropdownMenu/renderDropdownItems.tsx b/packages/core/src/DropdownMenu/renderDropdownItems.tsxindex 0edbe1a4bd..68dec4464b 100644--- a/packages/core/src/DropdownMenu/renderDropdownItems.tsx+++ b/packages/core/src/DropdownMenu/renderDropdownItems.tsx@@ -6,7 +6,7 @@* @position Utility; used by DropdownMenu to unify data-driven and compound paths*/-import type {ReactNode} from 'react';+import type {ReactElement, ReactNode} from 'react';import * as stylex from '@stylexjs/stylex';import {Divider} from '../Divider';import {DropdownMenuItem} from './DropdownMenuItem';@@ -47,6 +47,20 @@ function getSectionKey(section: DropdownMenuSection, index: number): string {return `section-${section.title ?? index}`;}+/**+ * Renders one leaf row as a `DropdownMenuItem`.+ *+ * `items` is stripped because it selects the submenu shape rather than being an+ * item prop; every other field of `DropdownMenuItemData` is a+ * `DropdownMenuItem` prop by construction (the type is `Pick`ed from+ * `DropdownMenuItemProps`), so the data path forwards them wholesale and can't+ * silently drop a field the data API advertises.+ */+function renderLeafItem(item: DropdownMenuItemData): ReactElement {+ const {items: _submenuItems, ...itemProps} = item;+ return <DropdownMenuItem key={getItemKey(item)} {...itemProps} />;+}+/*** Converts data-driven items into DropdownMenuItem components,* so both modes share the same rendering and keyboard navigation path.@@ -81,16 +95,7 @@ export function renderDropdownItems(items: DropdownMenuOption[]): ReactNode {{option.title}</div>)}- {option.items.map(item => (- <DropdownMenuItem- key={getItemKey(item)}- icon={item.icon}- label={item.label}- onClick={item.onClick}- isDisabled={item.isDisabled}- variant={item.variant}- />- ))}+ {option.items.map(renderLeafItem)}</div>,);} else if (!('type' in option)) {@@ -110,16 +115,7 @@ export function renderDropdownItems(items: DropdownMenuOption[]): ReactNode {</DropdownMenuSubMenu>,);} else {- elements.push(- <DropdownMenuItem- key={getItemKey(option)}- icon={option.icon}- label={option.label}- onClick={option.onClick}- isDisabled={option.isDisabled}- variant={option.variant}- />,- );+ elements.push(renderLeafItem(option));}}}