2022-06-05 18:56:42 +00:00
|
|
|
import { Menu } from '@headlessui/react';
|
|
|
|
import classNames from 'classnames';
|
2023-01-25 18:14:39 +00:00
|
|
|
import type { MouseEvent, ReactNode, Ref } from 'react';
|
|
|
|
import { forwardRef } from 'react';
|
|
|
|
import type { NavLinkProps } from 'react-router-dom';
|
|
|
|
import { NavLink } from 'react-router-dom';
|
|
|
|
|
|
|
|
import styles from './style.module.css';
|
2022-06-05 18:56:42 +00:00
|
|
|
|
|
|
|
interface Props {
|
2023-01-25 18:14:39 +00:00
|
|
|
children: ReactNode | ((opts: { active: boolean; disabled: boolean }) => JSX.Element);
|
2022-06-05 18:56:42 +00:00
|
|
|
danger?: boolean;
|
|
|
|
disabled?: boolean;
|
|
|
|
className?: string;
|
|
|
|
icon?: JSX.Element;
|
2023-01-25 18:14:39 +00:00
|
|
|
onClick?: (e: MouseEvent) => void;
|
2022-06-05 18:56:42 +00:00
|
|
|
}
|
|
|
|
|
2023-01-25 18:14:39 +00:00
|
|
|
const DropdownItem = forwardRef<HTMLAnchorElement | HTMLButtonElement, Props & Partial<Omit<NavLinkProps, 'children'>>>(
|
|
|
|
({ disabled, danger, className, onClick, children, icon: IconComponent, ...props }, ref) => {
|
2022-06-26 19:13:52 +00:00
|
|
|
return (
|
|
|
|
<Menu.Item disabled={disabled}>
|
|
|
|
{({ disabled, active }) => (
|
2023-01-25 18:14:39 +00:00
|
|
|
<>
|
|
|
|
{'to' in props && props.to !== undefined ? (
|
|
|
|
<NavLink
|
|
|
|
{...props}
|
|
|
|
to={props.to}
|
|
|
|
ref={ref as unknown as Ref<HTMLAnchorElement>}
|
|
|
|
className={classNames(
|
|
|
|
styles.menu_item,
|
|
|
|
{
|
|
|
|
[styles.danger]: danger,
|
|
|
|
[styles.disabled]: disabled,
|
|
|
|
},
|
|
|
|
className,
|
|
|
|
)}
|
|
|
|
onClick={onClick}
|
|
|
|
>
|
|
|
|
{IconComponent}
|
|
|
|
{typeof children === 'function' ? children({ disabled, active }) : children}
|
|
|
|
</NavLink>
|
|
|
|
) : (
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
ref={ref as unknown as Ref<HTMLButtonElement>}
|
|
|
|
className={classNames(
|
|
|
|
styles.menu_item,
|
|
|
|
{
|
|
|
|
[styles.danger]: danger,
|
|
|
|
[styles.disabled]: disabled,
|
|
|
|
},
|
|
|
|
className,
|
|
|
|
)}
|
|
|
|
onClick={onClick}
|
|
|
|
>
|
|
|
|
{IconComponent}
|
|
|
|
{typeof children === 'function' ? children({ disabled, active }) : children}
|
|
|
|
</button>
|
2022-06-26 19:13:52 +00:00
|
|
|
)}
|
2023-01-25 18:14:39 +00:00
|
|
|
</>
|
2022-06-26 19:13:52 +00:00
|
|
|
)}
|
|
|
|
</Menu.Item>
|
|
|
|
);
|
2022-11-25 20:25:03 +00:00
|
|
|
},
|
2022-06-26 19:13:52 +00:00
|
|
|
);
|
2022-06-05 18:56:42 +00:00
|
|
|
|
2023-01-25 18:14:39 +00:00
|
|
|
export { DropdownItem };
|