import Button from "@/components/lib/layout/Button"; import Stack from "@/components/lib/layout/Stack"; import { ImagePlus, X } from "lucide-react"; import React, { DetailedHTMLProps } from "react"; import Card from "@/components/lib/elements/Card"; import Span from "@/components/lib/layout/Span"; import Center from "@/components/lib/layout/Center"; import imageInputToBase64, { ImageInputToBase64FunctionReturn, } from "../utils/form/imageInputToBase64"; import { twMerge } from "tailwind-merge"; type ImageUploadProps = { onChange?: (imgData: ImageInputToBase64FunctionReturn | undefined) => any; fileInputProps?: DetailedHTMLProps< React.InputHTMLAttributes, HTMLInputElement >; wrapperProps?: DetailedHTMLProps< React.HTMLAttributes, HTMLDivElement >; placeHolderWrapper?: DetailedHTMLProps< React.HTMLAttributes, HTMLDivElement >; previewImageWrapperProps?: DetailedHTMLProps< React.HTMLAttributes, HTMLDivElement >; previewImageProps?: DetailedHTMLProps< React.ImgHTMLAttributes, HTMLImageElement >; }; export default function ImageUpload({ onChange, fileInputProps, wrapperProps, placeHolderWrapper, previewImageWrapperProps, previewImageProps, }: ImageUploadProps) { const [src, setSrc] = React.useState(undefined); const inputRef = React.useRef(); return ( { imageInputToBase64({ imageInput: e.target }).then((res) => { setSrc(res.imageBase64Full); onChange?.(res); fileInputProps?.onChange?.(e); }); }} ref={inputRef as any} /> {src ? ( ) : ( { inputRef.current?.click(); placeHolderWrapper?.onClick?.(e); }} {...placeHolderWrapper} >
Click to Upload Image
)}
); }