import Button from "../layout/Button"; import Stack from "../layout/Stack"; import { ImagePlus, X } from "lucide-react"; import React, { DetailedHTMLProps } from "react"; import Card from "../elements/Card"; import Span from "../layout/Span"; import Center from "../layout/Center"; import imageInputToBase64, { ImageInputToBase64FunctionReturn, } from "../utils/form/imageInputToBase64"; import { twMerge } from "tailwind-merge"; type ImageUploadProps = DetailedHTMLProps< React.HTMLAttributes, HTMLDivElement > & { onChange?: (imgData: ImageInputToBase64FunctionReturn | undefined) => any; fileInputProps?: DetailedHTMLProps< React.InputHTMLAttributes, HTMLInputElement >; placeHolderWrapper?: DetailedHTMLProps< React.HTMLAttributes, HTMLDivElement >; previewImageWrapperProps?: DetailedHTMLProps< React.HTMLAttributes, HTMLDivElement >; previewImageProps?: DetailedHTMLProps< React.ImgHTMLAttributes, HTMLImageElement >; label?: string; disablePreview?: boolean; }; export default function ImageUpload({ onChange, fileInputProps, placeHolderWrapper, previewImageWrapperProps, previewImageProps, label, disablePreview, ...props }: 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 ? ( {disablePreview ? ( Image Uploaded! ) : ( )} ) : ( { inputRef.current?.click(); placeHolderWrapper?.onClick?.(e); }} {...placeHolderWrapper} >
{label || "Click to Upload Image"}
)}
); }