Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

feat(design): add custom item support and variant handling for DropdownMenu (#7053)

白熱e739af6f22ff
diff --git a/packages/design/src/components/dropdown-menu/DropdownMenu.tsx b/packages/design/src/components/dropdown-menu/DropdownMenu.tsx
index 6b5f6d40c9..d5e3597a19 100644
--- a/packages/design/src/components/dropdown-menu/DropdownMenu.tsx
+++ b/packages/design/src/components/dropdown-menu/DropdownMenu.tsx
@@ -36,6 +36,7 @@ interface IDropdownMenuNormalItem {
className?: string;
children: ReactNode;
disabled?: boolean;
+ variant?: 'default' | 'destructive';
onSelect?: (item: DropdownMenuType) => void;
}
@@ -78,7 +79,13 @@ interface IDropdownMenuCheckItem {
onSelect?: (item: string) => void;
}
-type DropdownMenuType = IDropdownMenuNormalItem | IDropdownMenuNormalSubItem | IDropdownMenuSeparatorItem | IDropdownMenuRadioItem | IDropdownMenuCheckItem;
+interface IDropdownMenuCustomItem {
+ type: 'custom';
+ className?: string;
+ children: ReactNode;
+}
+
+type DropdownMenuType = IDropdownMenuNormalItem | IDropdownMenuNormalSubItem | IDropdownMenuSeparatorItem | IDropdownMenuRadioItem | IDropdownMenuCheckItem | IDropdownMenuCustomItem;
export interface IDropdownMenuProps extends ComponentProps<typeof DropdownMenuContent> {
children: ReactNode;
@@ -118,6 +125,12 @@ export function DropdownMenu(props: IDropdownMenuProps) {
if (type === 'separator') {
return <DropdownMenuSeparator key={index} className={className} />;
+ } else if (type === 'custom') {
+ return (
+ <div key={index} className={className}>
+ {item.children}
+ </div>
+ );
} else if (type === 'radio') {
return (
<DropdownMenuRadioGroup
@@ -170,6 +183,7 @@ export function DropdownMenu(props: IDropdownMenuProps) {
key={index}
className={className}
disabled={item.disabled}
+ variant={item.variant}
onSelect={() => {
item.onSelect?.(item);
}}
diff --git a/packages/design/src/components/dropdown-menu/DropdownMenuPrimitive.tsx b/packages/design/src/components/dropdown-menu/DropdownMenuPrimitive.tsx
index 6beca67095..5d5eb4361c 100644
--- a/packages/design/src/components/dropdown-menu/DropdownMenuPrimitive.tsx
+++ b/packages/design/src/components/dropdown-menu/DropdownMenuPrimitive.tsx
@@ -202,9 +202,14 @@ function DropdownMenuItem({
`
univer-relative univer-flex univer-cursor-default univer-select-none univer-items-center univer-gap-2
univer-rounded univer-px-2 univer-py-1.5 univer-text-sm univer-outline-none univer-transition-colors
- data-[disabled]:univer-pointer-events-none data-[disabled]:univer-opacity-50
+ data-[disabled]:univer-pointer-events-none
+ data-[variant=destructive]:univer-text-red-600
+ data-[disabled]:univer-opacity-50
focus:univer-bg-gray-100
+ data-[variant=destructive]:focus:univer-bg-red-50
+ dark:data-[variant=destructive]:!univer-text-red-400
dark:focus:!univer-bg-gray-600
+ dark:data-[variant=destructive]:focus:!univer-bg-red-900
[&>svg]:univer-size-4 [&>svg]:univer-shrink-0
`,
className
diff --git a/packages/design/src/components/dropdown-menu/__tests__/index.spec.tsx b/packages/design/src/components/dropdown-menu/__tests__/index.spec.tsx
index 1a64168964..4a0f1bef2c 100644
--- a/packages/design/src/components/dropdown-menu/__tests__/index.spec.tsx
+++ b/packages/design/src/components/dropdown-menu/__tests__/index.spec.tsx
@@ -25,10 +25,10 @@ describe('DropdownMenu', () => {
it('should render with normal items', () => {
const items = [
{ type: 'item' as const, children: 'Item 1' },
- { type: 'item' as const, children: 'Item 2', disabled: true },
+ { type: 'item' as const, children: 'Item 2', disabled: true, variant: 'destructive' as const },
];
const { container } = render(
- <DropdownMenu items={items}>
+ <DropdownMenu open items={items}>
<button type="button">Trigger</button>
</DropdownMenu>
);
@@ -36,6 +36,7 @@ describe('DropdownMenu', () => {
expect(trigger).toBeTruthy();
expect(trigger).toHaveAttribute('aria-haspopup', 'menu');
expect(trigger).toHaveAttribute('type', 'button');
+ expect(document.querySelector('[data-variant="destructive"]')).toHaveTextContent('Item 2');
});
it('should render with separator', () => {
@@ -121,6 +122,25 @@ describe('DropdownMenu', () => {
expect(trigger).toHaveAttribute('aria-haspopup', 'menu');
});
+ it('should render custom content without wrapping it as a menu item', () => {
+ const items = [
+ {
+ type: 'custom' as const,
+ children: <input aria-label="Insert count" defaultValue="1" />,
+ },
+ ];
+
+ const { getByLabelText } = render(
+ <DropdownMenu open items={items}>
+ <button type="button">Trigger</button>
+ </DropdownMenu>
+ );
+
+ const input = getByLabelText('Insert count');
+ expect(input).toBeInTheDocument();
+ expect(input.closest('[role="menuitem"]')).toBeNull();
+ });
+
it('should invoke onSelect callbacks for item/checkbox/radio', () => {
const onItemSelect = vi.fn();
const onCheckboxSelect = vi.fn();
diff --git a/packages/design/src/components/tooltip/Tooltip.tsx b/packages/design/src/components/tooltip/Tooltip.tsx
index 94c05b9cd4..725b38e23b 100644
--- a/packages/design/src/components/tooltip/Tooltip.tsx
+++ b/packages/design/src/components/tooltip/Tooltip.tsx
@@ -226,7 +226,7 @@ export function Tooltip(props: ITooltipProps) {
const triggerElement = asChild
? (
- <span {...commonProps} className="univer-inline-block univer-max-w-full univer-truncate">
+ <span {...commonProps} className="univer-inline-flex univer-max-w-full univer-items-center univer-truncate">
{children}
</span>
)