gbs-add-block 0.0.9 → 0.0.10

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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gbs-add-block",
3
- "version": "0.0.9",
3
+ "version": "0.0.10",
4
4
  "description": "React Component Library",
5
5
  "files": [
6
6
  "index.js",
@@ -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
- setFiles(updatedFiles);
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
- // Clear previous preview URLs if not multiple
61
- if (!multiple) {
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
- newFiles.forEach((file) => {
66
- if (showImagePreview && file.type.startsWith("image/")) {
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
- // *** Code to handle Drag and drop functionalities Starts here
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}