Skip to content

Commit bf5bcc5

Browse files
fix(c): dropdown asChild
1 parent 064c4a0 commit bf5bcc5

File tree

2 files changed

+48
-43
lines changed

2 files changed

+48
-43
lines changed

src/components/dropdown/Dropdown.tsx

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -82,55 +82,52 @@ export const Dropdown: React.FC<DropdownProps> = ({
8282
);
8383
};
8484

85-
interface DropdownItemProps {
85+
export interface DropdownItemProps {
8686
children: React.ReactNode;
87-
href?: string;
88-
onClick?: () => void;
87+
asChild?: boolean;
8988
className?: string;
89+
onClick?: () => void;
9090
}
9191

9292
export const DropdownItem = React.forwardRef<HTMLDivElement, DropdownItemProps>(
93-
({ children, href, onClick, className }, ref) => {
94-
const content = (
95-
<div
96-
ref={ref}
97-
className={cn(
98-
"w-full px-4 py-2 text-sm",
99-
"text-gray-700 dark:text-gray-200",
100-
className
101-
)}
102-
>
103-
{children}
104-
</div>
105-
);
106-
93+
({ children, asChild = false, className, onClick }, ref) => {
10794
return (
10895
<Menu.Item>
109-
{({ active }) =>
110-
href ? (
111-
<a
112-
href={href}
113-
onClick={onClick}
114-
className={cn(
115-
"block w-full",
116-
active && "bg-gray-100 dark:bg-gray-700"
117-
)}
118-
>
119-
{content}
120-
</a>
121-
) : (
96+
{({ active }) => {
97+
if (asChild) {
98+
const child = React.Children.only(children) as React.ReactElement;
99+
return React.cloneElement(child, {
100+
className: cn(
101+
"block w-full px-4 py-2 text-sm",
102+
"text-gray-700 dark:text-gray-200",
103+
active && "bg-gray-100 dark:bg-gray-700",
104+
child.props.className,
105+
className
106+
),
107+
onClick: (e: React.MouseEvent) => {
108+
child.props.onClick?.(e);
109+
onClick?.();
110+
},
111+
ref
112+
});
113+
}
114+
115+
return (
122116
<button
123117
type="button"
118+
ref={ref as any}
124119
onClick={onClick}
125120
className={cn(
126-
"block w-full text-left",
127-
active && "bg-gray-100 dark:bg-gray-700"
121+
"block w-full px-4 py-2 text-sm text-left",
122+
"text-gray-700 dark:text-gray-200",
123+
active && "bg-gray-100 dark:bg-gray-700",
124+
className
128125
)}
129126
>
130-
{content}
127+
{children}
131128
</button>
132-
)
133-
}
129+
);
130+
}}
134131
</Menu.Item>
135132
);
136133
}

src/docs/pages/components/DropdownPage.tsx

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -108,23 +108,31 @@ const MyComponent = () => (
108108
title="With Links"
109109
description="Dropdown items as links"
110110
code={`<Dropdown trigger={<Button>Navigation</Button>}>
111-
<DropdownItem href="https://google.com">
112-
Google
111+
<DropdownItem asChild>
112+
<a href="https://google.com" target="_blank" rel="noopener noreferrer">
113+
Google
114+
</a>
113115
</DropdownItem>
114-
<DropdownItem href="/settings">
115-
Settings
116+
<DropdownItem asChild>
117+
<a href="/settings">
118+
Settings
119+
</a>
116120
</DropdownItem>
117121
<DropdownItem onClick={() => alert('Clicked!')}>
118122
Action Button
119123
</DropdownItem>
120124
</Dropdown>`}
121125
>
122126
<Dropdown trigger={<Button>Navigation</Button>}>
123-
<DropdownItem href="https://google.com">
124-
Google
127+
<DropdownItem asChild>
128+
<a href="https://google.com" target="_blank" rel="noopener noreferrer">
129+
Google
130+
</a>
125131
</DropdownItem>
126-
<DropdownItem href="/settings">
127-
Settings
132+
<DropdownItem asChild>
133+
<a href="/settings">
134+
Settings
135+
</a>
128136
</DropdownItem>
129137
<DropdownItem onClick={() => alert('Clicked!')}>
130138
Action Button

0 commit comments

Comments
 (0)