import { forwardRef, Fragment, Ref, useEffect, useImperativeHandle, useRef, useState } from "react"; import { Dialog, Transition } from "@headlessui/react"; import clsx from "clsx"; import { useApp } from "../../../hooks"; export interface RefConfirmModal { setValue: (value: any) => void; show: (_question: string, _yesTitle?: string, _noTitle?: string, _description?: string, _inputString?: string) => void; } interface Props { destructive?: boolean; inputType?: string; onYes?: (value: any) => void; onNo?: () => void; placeholder?: string; } const ConfirmModal = (props: Props, ref: Ref) => { const { t } = useApp(); const [open, setOpen] = useState(false); const [title, setTitle] = useState(); const [value, setValue] = useState(); const [description, setDescription] = useState(); const [inputString, setInputString] = useState(""); const [yesTitle, setYesTitle] = useState(""); const [noTitle, setNoTitle] = useState(""); const cancelButtonRef = useRef(null); const inputValue = useRef() as React.MutableRefObject; useEffect(() => { setTitle(t("shared.confirm").toString()); // eslint-disable-next-line react-hooks/exhaustive-deps }, []); function no() { setOpen(false); if (props.onNo) { props.onNo(); } } function yes() { setOpen(false); if (props.onYes) { props.onYes(value ?? inputString); } } useImperativeHandle(ref, () => ({ show, setValue })); function show( _question: string, _yesTitle: string = t("shared.confirm").toString(), _noTitle: string = t("shared.back").toString(), _description?: string, _inputString?: string ) { setTitle(_question.toString()); if (_yesTitle) { setYesTitle(_yesTitle); } if (_noTitle) { setNoTitle(_noTitle); } if (_description) { setDescription(_description); } setTimeout(() => { if ((props.inputType === "email" || props.inputType === "string" || props.inputType === "slug") && inputValue.current) { inputValue.current.focus(); inputValue.current.select(); } }, 0); setOpen(true); setInputString(_inputString); } return (
{/* This element is to trick the browser into centering the modal contents. */}
{title}

{description}

{props.inputType === "email" && (
{/*Heroicon name: solid/mail */}
setInputString(e.target.value)} ref={inputValue} type="value" name="email" id="email" className="block w-full rounded-md border-gray-300 pl-10 focus:border-gray-500 focus:ring-gray-500 sm:text-sm" placeholder="your@email.com" />
)} {props.inputType === "string" && (
setInputString(e.target.value)} ref={inputValue} type="text" name="value" id="value" className="block w-full rounded-md border-gray-300 focus:border-gray-500 focus:ring-gray-500 sm:text-sm" placeholder={props.placeholder} />
)} {props.inputType === "slug" && (
setInputString(e.target.value.toLowerCase())} ref={inputValue} type="text" name="value" id="value" className="block w-full rounded-md border-gray-300 focus:border-gray-500 focus:ring-gray-500 sm:text-sm" placeholder={props.placeholder} />
)}
); }; export default forwardRef(ConfirmModal);