import { 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, ButtonPropsI } from "./Button"; import { TransitionDialogue } from "~/components/TransitionDialogue"; type UnionedButtonProps = { label?: string } & ButtonPropsI; interface ButtonDropdownProps { label: string; icon?: IconDefinition; options: Array<{ label: string; 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 = ({ label, options, icon, }: 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 (
{options.map((option, index) => ( {option.divider && (
)} {({ active }) => ( )} ))}
{currentDialogProps && ( {currentDialogProps.content}
{currentDialogProps.showClose !== false && currentDialogProps.closeButtonProps && ( )} {currentDialogProps.confirmButtonProps && ( )}
)}
); };