import React, { useEffect, useState } from "react"; import { LoadingOutlined, PlusOutlined } from "@ant-design/icons"; import { message, Upload } from "antd"; import type { GetProp, UploadProps } from "antd"; import useFormField from "../../../Hooks/useFormField"; type FileType = Parameters>[0]; const DropFile = ({ name, label, onChange, isDisabled, placeholder, className, props, no_label, label_icon, }: any) => { const { formik, t, isError } = useFormField(name, props); let FormikName = formik?.values[name]; const FormikValue = typeof FormikName === "string" ? FormikName : FormikName instanceof File ? URL.createObjectURL(FormikName) : ""; const [imageUrl, setImageUrl] = useState(FormikValue ?? ""); useEffect(() => { setImageUrl(FormikName); }, [FormikName]); const getBase64 = (img: FileType, callback: (url: string) => void) => { const reader = new FileReader(); reader.addEventListener("load", () => callback(reader.result as string)); reader.readAsDataURL(img); }; const handleChange: UploadProps["onChange"] = (info) => { if (info.file.status === "done") { getBase64(info.file.originFileObj as FileType, (url) => { setImageUrl(url); }); } formik.setFieldValue(name, info.file.originFileObj); }; const customRequest = async ({ onSuccess }: any) => { onSuccess(); }; const uploadButton = ( ); return (
{imageUrl ? ( ) : ( uploadButton )}
); }; export default DropFile;