import { cn } from '#utils'; import { DropdownButton } from '../DropdownButton/DropdownButton.tsx'; import { DropdownMenu } from '../DropdownMenu/DropdownMenu.tsx'; import type { DropdownMenuContentProps } from '../DropdownMenu/DropdownMenuContent.tsx'; type ActionDropdownOptions = readonly string[] | { [key: string]: string }; type ActionDropdownOptionKey = T extends readonly string[] ? T[number] : T extends { [key: string]: string } ? Extract : never; export type ActionDropdownProps = { [key: `data-${string}`]: unknown; align?: DropdownMenuContentProps['align']; className?: string; contentClassName?: string; disabled?: boolean; /** Callback function invoked when user clicks an option */ onSelection: (option: ActionDropdownOptionKey) => void; /** Either a list of options for the dropdown, or an object with options mapped to custom labels */ options: T; /** The text content for the dropdown toggle */ title: string; triggerClassName?: string; widthFull?: boolean; }; // eslint-disable-next-line react/function-component-definition export function ActionDropdown({ align = 'start', className, contentClassName, disabled, onSelection, options, title, triggerClassName, widthFull, ...props }: ActionDropdownProps) { const optionKeys: readonly string[] = options instanceof Array ? options : Object.keys(options); return (
{title} {optionKeys.map((option) => ( { onSelection(option as ActionDropdownOptionKey); }} > {Array.isArray(options) ? option : (options[option as keyof T] as string)} ))}
); }