gbs-add-block 0.0.7 → 0.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -20,55 +20,102 @@ export const FileUploader = ({
|
|
|
20
20
|
}: any) => {
|
|
21
21
|
const [files, setFiles] = useState<any[]>([]);
|
|
22
22
|
const [previewUrls, setPreviewUrls] = useState<{ [key: string]: string }>({});
|
|
23
|
+
const [isDragging, setIsDragging] = useState(false);
|
|
23
24
|
const fileInputRef = useRef<any>(null);
|
|
25
|
+
const dropZoneRef = useRef<any>(null);
|
|
24
26
|
|
|
27
|
+
// For Binding values on edit
|
|
25
28
|
useEffect(() => {
|
|
26
29
|
if (selectedFiles && selectedFiles.length > 0) {
|
|
27
|
-
setFiles(selectedFiles);
|
|
30
|
+
setFiles(multiple ? selectedFiles : [selectedFiles[0]]);
|
|
28
31
|
}
|
|
29
|
-
}, [selectedFiles]);
|
|
32
|
+
}, [selectedFiles, multiple]);
|
|
30
33
|
|
|
31
|
-
//
|
|
32
|
-
const handleFileChange = (
|
|
33
|
-
|
|
34
|
-
|
|
34
|
+
// Adds files to file array
|
|
35
|
+
const handleFileChange = (newFiles: File[]) => {
|
|
36
|
+
let updatedFiles: File[];
|
|
37
|
+
if (multiple) {
|
|
38
|
+
updatedFiles = [...files, ...newFiles];
|
|
39
|
+
} else {
|
|
40
|
+
updatedFiles = [newFiles[0]];
|
|
41
|
+
}
|
|
42
|
+
setFiles(updatedFiles);
|
|
35
43
|
|
|
36
|
-
|
|
44
|
+
// Clear previous preview URLs if not multiple
|
|
45
|
+
if (!multiple) {
|
|
46
|
+
setPreviewUrls({});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
newFiles.forEach((file) => {
|
|
37
50
|
if (showImagePreview && file.type.startsWith("image/")) {
|
|
38
51
|
const reader = new FileReader();
|
|
39
52
|
reader.onload = () => {
|
|
40
|
-
|
|
41
|
-
|
|
53
|
+
setPreviewUrls((prev) => ({
|
|
54
|
+
...prev,
|
|
55
|
+
[file.name]: reader.result as string,
|
|
56
|
+
}));
|
|
42
57
|
};
|
|
43
58
|
reader.readAsDataURL(file);
|
|
44
59
|
}
|
|
45
60
|
});
|
|
46
61
|
|
|
47
|
-
const updatedFiles = [...files, ...selectedFiles];
|
|
48
|
-
setFiles(updatedFiles);
|
|
49
|
-
|
|
50
62
|
if (onChange) onChange(updatedFiles);
|
|
51
63
|
};
|
|
52
64
|
|
|
53
|
-
//
|
|
54
|
-
const removeFile = (index:
|
|
55
|
-
const fileToRemove = files[index];
|
|
65
|
+
// Removes the file from file array
|
|
66
|
+
const removeFile = (index: number) => {
|
|
56
67
|
const updatedFiles = files.filter((_, i) => i !== index);
|
|
57
|
-
|
|
58
68
|
setFiles(updatedFiles);
|
|
59
69
|
setPreviewUrls((prevPreviews) => {
|
|
60
|
-
const { [
|
|
70
|
+
const { [files[index].name]: _, ...rest } = prevPreviews;
|
|
61
71
|
return rest;
|
|
62
72
|
});
|
|
63
73
|
|
|
64
74
|
if (onChange) onChange(updatedFiles);
|
|
65
75
|
};
|
|
66
76
|
|
|
77
|
+
// *** Code to handle Drag and drop functionalities Starts here
|
|
78
|
+
const handleDragEnter = (e: React.DragEvent<HTMLDivElement>) => {
|
|
79
|
+
e.preventDefault();
|
|
80
|
+
e.stopPropagation();
|
|
81
|
+
setIsDragging(true);
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
const handleDragLeave = (e: React.DragEvent<HTMLDivElement>) => {
|
|
85
|
+
e.preventDefault();
|
|
86
|
+
e.stopPropagation();
|
|
87
|
+
setIsDragging(false);
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
const handleDragOver = (e: React.DragEvent<HTMLDivElement>) => {
|
|
91
|
+
e.preventDefault();
|
|
92
|
+
e.stopPropagation();
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
const handleDrop = (e: React.DragEvent<HTMLDivElement>) => {
|
|
96
|
+
e.preventDefault();
|
|
97
|
+
e.stopPropagation();
|
|
98
|
+
setIsDragging(false);
|
|
99
|
+
|
|
100
|
+
const droppedFiles = Array.from(e.dataTransfer.files);
|
|
101
|
+
handleFileChange(multiple ? droppedFiles : [droppedFiles[0]]);
|
|
102
|
+
};
|
|
103
|
+
// Code to handle Drag and drop functionalities Ends here ***
|
|
104
|
+
|
|
67
105
|
return (
|
|
68
106
|
<div className="w-full max-w-md mx-auto">
|
|
69
107
|
<div
|
|
70
|
-
|
|
108
|
+
ref={dropZoneRef}
|
|
109
|
+
className={`flex flex-col items-center border-2 border-dashed rounded-lg p-4 text-center cursor-pointer transition-colors ${
|
|
110
|
+
isDragging
|
|
111
|
+
? "border-blue-500 bg-blue-50"
|
|
112
|
+
: "border-gray-300 hover:border-gray-400"
|
|
113
|
+
}`}
|
|
71
114
|
onClick={() => fileInputRef.current.click()}
|
|
115
|
+
onDragEnter={handleDragEnter}
|
|
116
|
+
onDragOver={handleDragOver}
|
|
117
|
+
onDragLeave={handleDragLeave}
|
|
118
|
+
onDrop={handleDrop}
|
|
72
119
|
>
|
|
73
120
|
<Icon
|
|
74
121
|
dimensions={{ width: "36", height: "36" }}
|
|
@@ -78,17 +125,17 @@ export const FileUploader = ({
|
|
|
78
125
|
<p className="mt-2 text-sm text-gray-500">
|
|
79
126
|
Click to upload or drag and drop
|
|
80
127
|
</p>
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
Supports multiple file uploads
|
|
84
|
-
|
|
85
|
-
|
|
128
|
+
<p className="text-xs text-gray-400">
|
|
129
|
+
{multiple
|
|
130
|
+
? "Supports multiple file uploads"
|
|
131
|
+
: "Single file upload only"}
|
|
132
|
+
</p>
|
|
86
133
|
</div>
|
|
87
134
|
<input
|
|
88
135
|
type="file"
|
|
89
136
|
ref={fileInputRef}
|
|
90
137
|
className="hidden"
|
|
91
|
-
onChange={handleFileChange}
|
|
138
|
+
onChange={(e) => handleFileChange(Array.from(e.target.files || []))}
|
|
92
139
|
multiple={multiple}
|
|
93
140
|
/>
|
|
94
141
|
|