-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathImageUploadButton.tsx
More file actions
73 lines (69 loc) · 2.11 KB
/
ImageUploadButton.tsx
File metadata and controls
73 lines (69 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { useRef } from 'react';
export default function ImageUploadButton({
label,
currentUrl,
onUpload,
shape = 'rounded-lg',
}: {
label: string;
currentUrl: string;
onUpload: (url: string) => void;
shape?: string;
}) {
const inputRef = useRef<HTMLInputElement>(null);
const handleFile = (e: React.ChangeEvent<HTMLInputElement>) => {
const file = e.target.files?.[0];
if (!file) return;
const reader = new FileReader();
reader.onload = () => onUpload(reader.result as string);
reader.readAsDataURL(file);
};
return (
<div className="flex items-center gap-3">
<div
className={`w-14 h-14 ${shape} overflow-hidden bg-emerald-100 border-2 border-dashed border-emerald-400 flex items-center justify-center shrink-0`}
onClick={() => inputRef.current?.click()}
style={{ cursor: 'pointer' }}
>
{currentUrl ? (
<img
src={currentUrl}
alt={label}
className="w-full h-full object-cover"
/>
) : (
<svg
viewBox="0 0 24 24"
fill="none"
className="w-6 h-6 text-emerald-400"
stroke="currentColor"
strokeWidth="1.5"
>
<path
d="M4 16l4-4a3 3 0 014 0l4 4M14 12l2-2a3 3 0 014 0l2 2M8 21h8a2 2 0 002-2V5a2 2 0 00-2-2H8a2 2 0 00-2 2v14a2 2 0 002 2z"
strokeLinecap="round"
strokeLinejoin="round"
/>
</svg>
)}
</div>
<div className="flex flex-col gap-1">
<span className="text-xs font-medium text-slate-600">{label}</span>
<button
type="button"
onClick={() => inputRef.current?.click()}
className="text-xs px-3 py-1.5 bg-emerald-50 hover:bg-emerald-100 text-emerald-600 rounded-lg border border-emerald-200 transition font-medium w-fit"
>
{currentUrl ? 'Change image' : 'Upload image'}
</button>
</div>
<input
ref={inputRef}
type="file"
accept="image/*"
className="hidden"
onChange={handleFile}
/>
</div>
);
}