import { ButtonHTMLAttributes, Fragment, useState } from "react"; import { Menu, Transition } from "@headlessui/react"; import { IconDefinition, faChevronDown, } from "@fortawesome/pro-solid-svg-icons"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { Button, ButtonProps, TransitionDialogue } from "~/components"; import { twMerge } from "tailwind-merge"; type UnionedButtonProps = { label?: string } & ButtonProps; interface ButtonDropdownProps extends ButtonHTMLAttributes { label: string; icon?: IconDefinition; align?: "left" | "right"; showSelected?: boolean; options: Array<{ label: string; className?: string; icon?: IconDefinition; selected?: boolean; description?: string; onClick?: () => void; disabled?: boolean; divider?: boolean; onDialogOpen?: () => void; dialogProps?: { title: string; content: React.ReactNode; className?: string; confirmButtonProps?: UnionedButtonProps; closeButtonProps?: UnionedButtonProps; showClose?: boolean; onClose?: () => void; }; }>; } export const ButtonDropdown = ({ className, label, options, icon, align = "left", showSelected, }: ButtonDropdownProps) => { const [isOpen, setIsOpen] = useState(false); const [selectedOptionIndex, setSelectedOptionIndex] = useState( null, ); const closeModal = () => { setIsOpen(false); options[selectedOptionIndex!].dialogProps?.onClose?.(); }; const handleOptionClick = (index: number) => { const option = options[index]; if (option.onClick) { option.onClick(); } if (option.onDialogOpen) { option.onDialogOpen(); } if (option.dialogProps) { setSelectedOptionIndex(index); setIsOpen(true); } }; const currentDialogProps = selectedOptionIndex !== null ? options[selectedOptionIndex].dialogProps : null; return ( {icon ? : null} {label} {options.map((option, index) => ( {option.divider && ( )} {({ active }) => ( handleOptionClick(index)} > {option.icon && ( )} {option.label} {option.description && option.description} {showSelected && ( <> {option.selected ? ( ) : ( )} > )} )} ))} {currentDialogProps && ( {currentDialogProps.content} {currentDialogProps.showClose !== false && currentDialogProps.closeButtonProps && ( {currentDialogProps.closeButtonProps.label} )} {currentDialogProps.confirmButtonProps && ( { if (currentDialogProps.confirmButtonProps?.onClick) { currentDialogProps.confirmButtonProps?.onClick(e); } closeModal(); }} > {currentDialogProps.confirmButtonProps.label || "Confirm"} )} )} ); };