import Button from "../layout/Button"; import Stack from "../layout/Stack"; import { File, FileArchive, FilePlus, FilePlus2, 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, { FileInputToBase64FunctionReturn, } from "../utils/form/fileInputToBase64"; import { twMerge } from "tailwind-merge"; import fileInputToBase64 from "../utils/form/fileInputToBase64"; import Row from "../layout/Row"; type ImageUploadProps = DetailedHTMLProps< React.HTMLAttributes, HTMLDivElement > & { onChangeHandler?: ( imgData: FileInputToBase64FunctionReturn | 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; allowedRegex?: RegExp; externalSetFile?: React.Dispatch< React.SetStateAction >; }; /** * @note use the `onChangeHandler` prop to grab the parsed base64 image object */ export default function FileUpload({ onChangeHandler, fileInputProps, placeHolderWrapper, previewImageWrapperProps, previewImageProps, label, disablePreview, allowedRegex, externalSetFile, ...props }: ImageUploadProps) { const [file, setFile] = React.useState< FileInputToBase64FunctionReturn | undefined >(undefined); const inputRef = React.useRef(); return ( { const inputFile = e.target.files?.[0]; if (!inputFile) return; fileInputToBase64({ inputFile, allowedRegex }).then( (res) => { setFile(res); externalSetFile?.(res); onChangeHandler?.(res); fileInputProps?.onChange?.(e); } ); }} ref={inputRef as any} /> {file ? ( {disablePreview ? ( Image Uploaded! ) : file.fileType?.match(/image/i) ? ( ) : ( {file.file?.name || file.fileName} {file.fileType} )} ) : ( { inputRef.current?.click(); placeHolderWrapper?.onClick?.(e); }} {...placeHolderWrapper} >
{label || "Click to Upload File"}
)}
); }