misc_pterodactyl-panel/resources/scripts/components/elements/dropdown/DropdownItem.tsx

69 lines
2.8 KiB
TypeScript
Raw Normal View History

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';
interface Props {
2023-01-25 18:14:39 +00:00
children: ReactNode | ((opts: { active: boolean; disabled: boolean }) => JSX.Element);
danger?: boolean;
disabled?: boolean;
className?: string;
icon?: JSX.Element;
2023-01-25 18:14:39 +00:00
onClick?: (e: MouseEvent) => void;
}
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) => {
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>
)}
2023-01-25 18:14:39 +00:00
</>
)}
</Menu.Item>
);
2022-11-25 20:25:03 +00:00
},
);
2023-01-25 18:14:39 +00:00
export { DropdownItem };