diff --git a/src/components/fileInput/index.js b/src/components/fileInput/index.js index 6b370b2..e9f778d 100644 --- a/src/components/fileInput/index.js +++ b/src/components/fileInput/index.js @@ -1,13 +1,42 @@ + import React from "react"; +import { useState, useEffect } from "react"; const FileInput = ({ title }) => { + + const [focus, setFocus] = useState('border-gray-300'); + const [selectedFile, setSelectedFile] = useState() + const [preview, setPreview] = useState(); + + useEffect(() => { + if (!selectedFile) { + setPreview(undefined) + return + } + + const objectUrl = URL.createObjectURL(selectedFile) + setPreview(objectUrl) + + // free memory when ever this component is unmounted + return () => URL.revokeObjectURL(objectUrl) + }, [selectedFile]) + + const onSelectFile = e => { + if (!e.target.files || e.target.files.length === 0) { + setSelectedFile(undefined) + return + } + + // I've kept this example simple by using the first image instead of multiple + setSelectedFile(e.target.files[0]) + } return (