import React, { ChangeEvent, useEffect, useState } from 'react'; type ImageInputProps = { label: string; id: string; name: string; onImageSelect: (image: string) => void; initialImage?: string; // Optional prop to set the initial image }; const ImageInput: React.FC = ({ label, id, name, onImageSelect, initialImage, }) => { const [imagePreview, setImagePreview] = useState( initialImage || null, ); useEffect(() => { if (initialImage) { setImagePreview(initialImage); } }, [initialImage]); const handleImageChange = (e: ChangeEvent) => { const file = e.target.files?.[0]; if (file) { const reader = new FileReader(); reader.onloadend = () => { const base64String = reader.result as string; setImagePreview(base64String); onImageSelect(base64String); }; reader.readAsDataURL(file); } }; return (
{imagePreview ? (
Image Preview
) : null}
); }; export default ImageInput;