gbs-add-block 0.0.9 → 0.0.11
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/index.js +17 -1
- package/package.json +1 -1
- package/source/components/uploader/Uploader.tsx +49 -26
package/index.js
CHANGED
|
@@ -17,7 +17,7 @@ const COMPONENTS = [
|
|
|
17
17
|
"Modal",
|
|
18
18
|
"Spinner",
|
|
19
19
|
"Toast",
|
|
20
|
-
"Uploader"
|
|
20
|
+
"Uploader",
|
|
21
21
|
];
|
|
22
22
|
const SOURCE_PATH = path.join(__dirname, "source", "components");
|
|
23
23
|
const DEST_PATH = path.join(process.cwd(), "src", "component-lib");
|
|
@@ -30,7 +30,17 @@ const rl = readline.createInterface({
|
|
|
30
30
|
let isFirstCopy = true;
|
|
31
31
|
|
|
32
32
|
function listComponents() {
|
|
33
|
+
console.log(
|
|
34
|
+
` _____ ____ _____
|
|
35
|
+
/ ____| | \\ / ___|
|
|
36
|
+
| | __ | |_) | | (___
|
|
37
|
+
| | |_ | | < \\ __ \\
|
|
38
|
+
| |__| | | |_) | ____) |
|
|
39
|
+
\\_____| |____/ |_____/ `
|
|
40
|
+
);
|
|
41
|
+
console.log(" ");
|
|
33
42
|
console.log("Available components:");
|
|
43
|
+
console.log(" ");
|
|
34
44
|
COMPONENTS.forEach((component, index) => {
|
|
35
45
|
console.log(`${index + 1}. ${component}`);
|
|
36
46
|
});
|
|
@@ -60,7 +70,13 @@ function copyComponent(component) {
|
|
|
60
70
|
}
|
|
61
71
|
|
|
62
72
|
fs.copySync(componentSrc, componentDest, { overwrite: true });
|
|
73
|
+
console.log("");
|
|
63
74
|
console.log(`Component ${component} copied successfully to ${componentDest}`);
|
|
75
|
+
console.log("");
|
|
76
|
+
console.log(
|
|
77
|
+
"For Props and Usage Guides Visit : https://blackmax-designs.gitbook.io/building-block-v2.0"
|
|
78
|
+
);
|
|
79
|
+
console.log("");
|
|
64
80
|
|
|
65
81
|
if (isFirstCopy) {
|
|
66
82
|
copyCommonFiles();
|
package/package.json
CHANGED
|
@@ -19,11 +19,14 @@ export const FileUploader = ({
|
|
|
19
19
|
selectedFiles = [],
|
|
20
20
|
accept,
|
|
21
21
|
fileCount,
|
|
22
|
+
disabled = false,
|
|
23
|
+
inputFileSize, // Max file size in MB
|
|
22
24
|
}: any) => {
|
|
23
25
|
const [files, setFiles] = useState<any[]>([]);
|
|
24
26
|
const [previewUrls, setPreviewUrls] = useState<{ [key: string]: string }>({});
|
|
25
27
|
const [isDragging, setIsDragging] = useState(false);
|
|
26
28
|
const [fileCountError, setFileCountError] = useState(false);
|
|
29
|
+
const [fileSizeErrors, setFileSizeErrors] = useState<string[]>([]); // Array for file size errors
|
|
27
30
|
const fileInputRef = useRef<any>(null);
|
|
28
31
|
const dropZoneRef = useRef<any>(null);
|
|
29
32
|
|
|
@@ -34,13 +37,6 @@ export const FileUploader = ({
|
|
|
34
37
|
}
|
|
35
38
|
}, [selectedFiles, multiple]);
|
|
36
39
|
|
|
37
|
-
// Shows Error when filecount is higher
|
|
38
|
-
useEffect(() => {
|
|
39
|
-
if (files.length > fileCount) {
|
|
40
|
-
setFileCountError(true);
|
|
41
|
-
}
|
|
42
|
-
}, [files, fileCount]);
|
|
43
|
-
|
|
44
40
|
// Adds files to file array
|
|
45
41
|
const handleFileChange = (newFiles: File[]) => {
|
|
46
42
|
let updatedFiles: File[] = multiple
|
|
@@ -55,27 +51,42 @@ export const FileUploader = ({
|
|
|
55
51
|
setFileCountError(false);
|
|
56
52
|
}
|
|
57
53
|
|
|
58
|
-
|
|
54
|
+
// File size validation
|
|
55
|
+
const validFiles: File[] = [];
|
|
56
|
+
const newErrors: string[] = [];
|
|
57
|
+
|
|
58
|
+
updatedFiles.forEach((file) => {
|
|
59
|
+
const fileSizeInMB = file.size / 1024 / 1024; // Convert bytes to MB
|
|
60
|
+
if (inputFileSize && fileSizeInMB > inputFileSize) {
|
|
61
|
+
newErrors.push(
|
|
62
|
+
`File ${file.name} exceeds the size limit of ${inputFileSize} MB`
|
|
63
|
+
);
|
|
64
|
+
} else {
|
|
65
|
+
validFiles.push(file);
|
|
66
|
+
}
|
|
67
|
+
});
|
|
59
68
|
|
|
60
|
-
//
|
|
61
|
-
if (
|
|
69
|
+
// Update state with valid files and set errors for oversized files
|
|
70
|
+
if (validFiles.length > 0) {
|
|
71
|
+
setFiles(validFiles);
|
|
62
72
|
setPreviewUrls({});
|
|
73
|
+
validFiles.forEach((file) => {
|
|
74
|
+
if (showImagePreview && file.type.startsWith("image/")) {
|
|
75
|
+
const reader = new FileReader();
|
|
76
|
+
reader.onload = () => {
|
|
77
|
+
setPreviewUrls((prev) => ({
|
|
78
|
+
...prev,
|
|
79
|
+
[file.name]: reader.result as string,
|
|
80
|
+
}));
|
|
81
|
+
};
|
|
82
|
+
reader.readAsDataURL(file);
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
if (onChange) onChange(validFiles);
|
|
63
86
|
}
|
|
64
87
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
const reader = new FileReader();
|
|
68
|
-
reader.onload = () => {
|
|
69
|
-
setPreviewUrls((prev) => ({
|
|
70
|
-
...prev,
|
|
71
|
-
[file.name]: reader.result as string,
|
|
72
|
-
}));
|
|
73
|
-
};
|
|
74
|
-
reader.readAsDataURL(file);
|
|
75
|
-
}
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
if (onChange) onChange(updatedFiles);
|
|
88
|
+
// Set file size errors
|
|
89
|
+
setFileSizeErrors(newErrors);
|
|
79
90
|
};
|
|
80
91
|
|
|
81
92
|
// Removes the file from file array
|
|
@@ -90,7 +101,7 @@ export const FileUploader = ({
|
|
|
90
101
|
if (onChange) onChange(updatedFiles);
|
|
91
102
|
};
|
|
92
103
|
|
|
93
|
-
//
|
|
104
|
+
// Drag and drop handling
|
|
94
105
|
const handleDragEnter = (e: React.DragEvent<HTMLDivElement>) => {
|
|
95
106
|
e.preventDefault();
|
|
96
107
|
e.stopPropagation();
|
|
@@ -116,7 +127,6 @@ export const FileUploader = ({
|
|
|
116
127
|
const droppedFiles = Array.from(e.dataTransfer.files);
|
|
117
128
|
handleFileChange(multiple ? droppedFiles : [droppedFiles[0]]);
|
|
118
129
|
};
|
|
119
|
-
// Code to handle Drag and drop functionalities Ends here ***
|
|
120
130
|
|
|
121
131
|
return (
|
|
122
132
|
<div className="w-full max-w-md mx-auto">
|
|
@@ -146,6 +156,17 @@ export const FileUploader = ({
|
|
|
146
156
|
? "Supports multiple file uploads"
|
|
147
157
|
: "Single file upload only"}
|
|
148
158
|
</p>
|
|
159
|
+
|
|
160
|
+
{/* Display file size errors */}
|
|
161
|
+
{fileSizeErrors.length > 0 && (
|
|
162
|
+
<div className="mt-2 space-y-1">
|
|
163
|
+
{fileSizeErrors.map((error, index) => (
|
|
164
|
+
<span key={index} className="text-xs text-red-500">
|
|
165
|
+
{error}
|
|
166
|
+
</span>
|
|
167
|
+
))}
|
|
168
|
+
</div>
|
|
169
|
+
)}
|
|
149
170
|
</div>
|
|
150
171
|
<input
|
|
151
172
|
type="file"
|
|
@@ -154,8 +175,10 @@ export const FileUploader = ({
|
|
|
154
175
|
onChange={(e) => handleFileChange(Array.from(e.target.files || []))}
|
|
155
176
|
multiple={multiple}
|
|
156
177
|
accept={accept}
|
|
178
|
+
disabled={disabled}
|
|
157
179
|
/>
|
|
158
180
|
|
|
181
|
+
{/* Display file count error */}
|
|
159
182
|
{fileCountError && (
|
|
160
183
|
<span className="text-xs text-red-500">
|
|
161
184
|
Maximum file count is set to {fileCount}
|