-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathDropzone.jsx
More file actions
148 lines (134 loc) · 3.94 KB
/
Dropzone.jsx
File metadata and controls
148 lines (134 loc) · 3.94 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
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
import { useCallback } from 'react';
import { useFormContext } from 'react-hook-form';
import { useDropzone } from 'react-dropzone';
import { HiX } from 'react-icons/hi';
import ImageLightbox from './ImageLightbox';
import { classNames } from '@/lib/helper';
const FilePreview = ({ file, deleteFile }) => {
const imagesType = ['image/png', 'image/jpg', 'image/jpeg'];
return imagesType.includes(file.type) ? (
<div key={file.name} className='relative aspect-w-3 aspect-h-2'>
<ImageLightbox
src={URL.createObjectURL(file)}
alt={file.name}
className='object-cover rounded-lg shadow-lg'
/>
<button
onClick={(e) => deleteFile(e, file)}
className='absolute top-0 right-0 flex p-2 leading-none'
>
<HiX size={24} className='text-red-500 cursor-pointer' />
</button>
</div>
) : (
<div key={file.name} className='flex px-3 py-2 rounded-lg shadow-lg'>
<button
onClick={(e) => deleteFile(e, file)}
className='flex mr-2 leading-none'
>
<HiX size={24} className='text-red-500 cursor-pointer' />
</button>
{file.name}
</div>
);
};
export default function DragnDropInput({
accept,
id,
label,
helperText = '',
maxFiles = 1,
validation,
}) {
const {
register,
setValue,
setError,
clearErrors,
watch,
formState: { errors },
} = useFormContext();
const files = watch(id);
const onDrop = useCallback(
(acceptedFiles, rejectedFiles) => {
if (rejectedFiles && rejectedFiles.length > 0) {
setValue(id, []);
setError(id, {
type: 'manual',
message: rejectedFiles && rejectedFiles[0].errors[0].message,
});
} else {
setValue(id, acceptedFiles, { shouldValidate: true });
clearErrors(id);
}
},
[id, setValue, setError, clearErrors]
);
const deleteFile = (e, file) => {
e.preventDefault();
const newFiles = [...files];
newFiles.splice(newFiles.indexOf(file), 1);
if (newFiles.length > 0) {
setValue(id, newFiles);
} else {
setValue(id, []);
}
};
const { getRootProps, getInputProps } = useDropzone({
onDrop,
accept,
maxFiles,
});
return (
<>
<label className='block text-sm font-normal text-gray-700' htmlFor={id}>
{label}
</label>
{files?.length >= maxFiles ? (
<div className='grid grid-cols-1 gap-2 mt-1'>
{files.map((file) => (
<FilePreview key={file} file={file} deleteFile={deleteFile} />
))}
</div>
) : (
<>
<div className='mt-1' {...getRootProps()}>
<input {...register(id, validation)} id={id} {...getInputProps()} />
<div
className={classNames(
'w-full p-2 bg-gray-100 border border-gray-300 border-dashed rounded cursor-pointer',
errors[id]
? 'focus:ring-red-500 border-red-500 focus:border-red-500'
: 'focus:ring-dark-400 focus:border-dark-400'
)}
>
<p className='my-20 text-center text-gray-500'>
Drag 'n' drop some files here, or click to select
files
</p>
{!!files?.length && (
<div className='grid grid-cols-1 gap-1 mt-2'>
{files.map((file) => (
<FilePreview
key={file}
file={file}
deleteFile={deleteFile}
/>
))}
</div>
)}
</div>
</div>
<div className='mt-1'>
{helperText !== '' && (
<p className='text-xs text-gray-500'>{helperText}</p>
)}
{errors[id] && (
<p className='text-sm text-red-500'>{errors[id].message}</p>
)}
</div>
</>
)}
</>
);
}