|
|
@ -1,13 +1,42 @@ |
|
|
|
|
|
|
|
|
|
|
|
import React from "react"; |
|
|
|
import React from "react"; |
|
|
|
|
|
|
|
import { useState, useEffect } from "react"; |
|
|
|
|
|
|
|
|
|
|
|
const FileInput = ({ |
|
|
|
const FileInput = ({ |
|
|
|
title |
|
|
|
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 ( |
|
|
|
return ( |
|
|
|
<div > |
|
|
|
<div > |
|
|
|
|
|
|
|
|
|
|
|
<input type='file' className="hidden" id="file"/> |
|
|
|
<input type='file' id="file" onFocus={() => setFocus('border-red52')} onchange={(e) => onSelectFile(e)}/> |
|
|
|
<label for="file" className="flex flex-col items-center border-dotted border-2 border-gray-300 rounded-lg p-4 cursor-pointer"> |
|
|
|
<label for="file" className={`flex flex-col text-center items-center border-dotted border-2 ${focus} rounded-lg p-4 cursor-pointer`} > |
|
|
|
<div> |
|
|
|
<div> |
|
|
|
<svg width="40" height="40" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"> |
|
|
|
<svg width="40" height="40" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"> |
|
|
|
<path d="M9 22H7C3 22 2 21 2 17V7C2 3 3 2 7 2H8.5C10 2 10.33 2.44001 10.9 3.20001L12.4 5.20001C12.78 5.70001 13 6 14 6H17C21 6 22 7 22 11V13" stroke="rgba(110,132,159,0.3)" stroke-width="1.5" stroke-miterlimit="10" stroke-linecap="round" stroke-linejoin="round" /> |
|
|
|
<path d="M9 22H7C3 22 2 21 2 17V7C2 3 3 2 7 2H8.5C10 2 10.33 2.44001 10.9 3.20001L12.4 5.20001C12.78 5.70001 13 6 14 6H17C21 6 22 7 22 11V13" stroke="rgba(110,132,159,0.3)" stroke-width="1.5" stroke-miterlimit="10" stroke-linecap="round" stroke-linejoin="round" /> |
|
|
@ -17,6 +46,7 @@ const FileInput = ({ |
|
|
|
<h1 className="text-xl font-bold text-gray-400">{title}</h1> |
|
|
|
<h1 className="text-xl font-bold text-gray-400">{title}</h1> |
|
|
|
<p className="text-sm leading-7 text-gray-400">فایل خود را بکشید و اینجا رها کنید</p> |
|
|
|
<p className="text-sm leading-7 text-gray-400">فایل خود را بکشید و اینجا رها کنید</p> |
|
|
|
<p className="bg-gray-200 p-2 rounded text-sm mt-1 text-gray-500">انتخاب فایل</p> |
|
|
|
<p className="bg-gray-200 p-2 rounded text-sm mt-1 text-gray-500">انتخاب فایل</p> |
|
|
|
|
|
|
|
{selectedFile && <img src={preview} /> } |
|
|
|
</label> |
|
|
|
</label> |
|
|
|
</div> |
|
|
|
</div> |
|
|
|
) |
|
|
|
) |
|
|
|