import { forwardRef, Ref, RefObject, useEffect, useState } from "react"; import { useTranslation } from "react-i18next"; import InputText from "./InputText"; import UploadDocument from "../uploaders/UploadDocument"; import clsx from "clsx"; import { sanitizeIconSvg } from "~/utils/html-sanitizer"; import { XIcon } from "~/components/ui/icons/vendor"; export interface RefInputImage { input: RefObject | RefObject; } type WithDefaultValue = { defaultValue: string | undefined }; type WithValueAndSetValue = { value: string | undefined; setValue: React.Dispatch> }; type Props = (WithDefaultValue | WithValueAndSetValue) & { name?: string; title?: string; disabled?: boolean; size?: "small" | "medium" | "large"; square?: boolean; required?: boolean; hint?: React.ReactNode; accept?: string; placeholder?: string; opened?: boolean; description?: string; className?: string; }; const InputImage = (props: Props, ref: Ref) => { const value = "value" in props ? props.value : undefined; const setValue = "setValue" in props ? props.setValue : undefined; const defaultValue = "defaultValue" in props ? props.defaultValue : undefined; const size = props.size ?? "large"; const square = props.square ?? false; const { t } = useTranslation(); const [showUploadImage, setShowUploadImage] = useState((props.opened && (!value || defaultValue)) ?? false); const [image, setImage] = useState(value || defaultValue || ""); useEffect(() => { if (setValue && value !== image) { setValue(image); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [image]); const imageClassName = clsx( "mx-auto", square ? "object-cover" : "object-contain", size === "small" && "h-8", size === "medium" && "h-16", size === "large" && "h-32", square && clsx(size === "small" && "w-8", size === "medium" && "w-16", size === "large" && "w-32") ); return (
{/* {!showUploadImage && ( */}
{!image ? ( ) : ( )}
} /> {/* )} */} {showUploadImage && ( { setImage(e); setShowUploadImage(false); }} /> )} {!showUploadImage && image && (
{/* */} {image.startsWith(" ) : ( {props.title} )}
)} ); }; export default forwardRef(InputImage);