-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile-uploader.tsx
204 lines (193 loc) · 7.92 KB
/
file-uploader.tsx
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import {UploaderIcon} from "@/assets/icons/uploader";
import {Icon} from "@iconify/react/dist/iconify";
import {Button, Card, CardBody, Image, Tooltip} from "@nextui-org/react";
import {useState} from "react";
import ImageUploading, {ErrorsType, ImageListType} from "react-images-uploading";
interface IFileUploaderProps {
onChange: (imageList: ImageListType) => void;
initialImages?: ImageListType;
}
function RFileUpload({onChange, initialImages = []}: IFileUploaderProps) {
const [loading, setLoading] = useState(false);
const [errors, setErrors] = useState<string[]>([]);
const maxNumber = 69;
const maxFileSize = 5 * 1024 * 1024; // 5MB
const onChangeImage = (imageList: ImageListType) => {
setLoading(false);
onChange(imageList);
};
const onError = (errors: ErrorsType, files?: ImageListType) => {
setLoading(false);
setErrors(errors ? (Object.values(errors).flat().filter(Boolean) as unknown as string[]) : []);
};
return (
<div>
<ImageUploading
multiple
value={initialImages}
onChange={onChangeImage}
maxNumber={maxNumber}
dataURLKey="data_url"
onError={onError}
maxFileSize={maxFileSize}
acceptType={["jpg", "webp", "svg", "jpeg", "pdf", "gif", "png"]}
inputProps={{
accept: "image/*",
}}
allowNonImageType={false}
>
{({
imageList,
onImageUpload,
onImageRemoveAll,
onImageUpdate,
onImageRemove,
isDragging,
dragProps,
}) => (
<div>
{loading && <div>Loading...</div>}
{errors.length > 0 && (
<div>
{errors.map((error, index) => (
<div key={index}>{error}</div>
))}
</div>
)}
<Card className="w-fit p-3 text-dark dark:text-light" shadow="sm">
<CardBody
className={`border dark:border-light/40 h-full rounded-lg`}
style={isDragging ? {color: "red", borderColor: "red"} : undefined}
onClick={onImageUpload}
{...dragProps}
>
<div className="flex justify-center gap-3 items-center font-medium text-center">
{imageList.length === 1 &&
imageList.map((image, index) => (
<div className="flex gap-1 items-center">
<Image
radius="sm"
src={image.data_url}
alt=""
className="w-16 h-10 object-cover border dark:border-light/40"
/>{" "}
<h3 className="font-medium text-dark dark:text-light">
{Object.entries((image as any)?.file?.name)?.length > 16
? `${image?.file?.name.slice(0, 16)}...`
: image?.file?.name.slice(0, 16)}
</h3>
</div>
))}
{imageList.length !== 1 && (
<h3 className="text-dark dark:text-light">Drag and drop your files or</h3>
)}
<div className="flex gap-1 items-center">
<Button
size="sm"
type="button"
onClick={onImageUpload}
variant="flat"
radius="sm"
>
<UploaderIcon className="w-6 h-6" />
Upload
</Button>
{imageList.length > 0 && (
<Tooltip color="danger" content="Remove all" showArrow={true}>
<Button
size="sm"
color="danger"
radius="sm"
isIconOnly
onClick={onImageRemoveAll}
>
<Icon className="w-5 h-5" icon="solar:trash-bin-minimalistic-broken" />
</Button>
</Tooltip>
)}
</div>
</div>
</CardBody>
{imageList.length > 1 && (
<div
className="space-y-1"
style={imageList.length > 0 ? {marginTop: "10px"} : undefined}
>
{imageList.map((image, index) => (
<div key={index} className="border dark:border-light/40 rounded-lg p-1">
<div className="flex items-center justify-between gap-3">
<div className="flex items-center justify-between">
<div className="flex gap-1 items-center">
<Image
radius="sm"
src={image.data_url}
alt=""
className="w-16 h-10 object-cover border dark:border-light/40"
/>{" "}
<h3 className="font-medium text-dark dark:text-light">
{Object.entries((image as any)?.file?.name)?.length > 16
? `${image?.file?.name.slice(0, 16)}...`
: image?.file?.name.slice(0, 16)}
</h3>
</div>
</div>
<div className="space-y-1">
<div className="flex gap-1 items-center">
<div className="text-dark dark:text-light">
{Math.round((image.file?.size || 0) / 1024) > 1024
? `${Math.round((image.file?.size || 0) / (1024 * 1024))} MB`
: `${Math.round((image.file?.size || 0) / 1024)} KB`}
</div>
<div className="space-x-1">
<Tooltip content="Update" color="primary" showArrow={true}>
<Button
variant="flat"
color="primary"
size="sm"
isIconOnly
onClick={() => onImageUpdate(index)}
>
<Icon className="w-5 h-5" icon="solar:restart-broken" />
</Button>
</Tooltip>
<Tooltip content="Remove" color="danger" showArrow={true}>
<Button
variant="flat"
color="danger"
size="sm"
isIconOnly
onClick={() => onImageRemove(index)}
>
<Icon
className="w-5 h-5"
icon="solar:trash-bin-minimalistic-broken"
/>
</Button>
</Tooltip>
</div>
</div>
</div>
</div>
</div>
))}
</div>
)}
</Card>
</div>
)}
</ImageUploading>
</div>
);
}
export default RFileUpload;
{
/*
const [images, setImages] = useState<any>([]);
const handleImageChange = (imageList: any) => {
// Handle the updated image list
setImages(imageList);
console.log("Updated images:", imageList);
};
<RFileUpload onChange={handleImageChange} initialImages={images} />
*/
}