gbs-add-block 0.0.6 → 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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gbs-add-block",
3
- "version": "0.0.6",
3
+ "version": "0.0.8",
4
4
  "description": "React Component Library",
5
5
  "files": [
6
6
  "index.js",
@@ -0,0 +1,42 @@
1
+ /**
2
+ * Copyright (c) Grampro Business Services and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ import React from "react";
9
+
10
+ const AestheticProcessingAnimation = () => {
11
+ return (
12
+ <div className="w-full max-w-md mx-auto p-2">
13
+ <div className="w-full h-1 bg-gray-200 rounded-full overflow-hidden">
14
+ <div className="h-full w-1/3 bg-black rounded-full animate-processing"></div>
15
+ </div>
16
+ <p className="text-xs mt-2 text-center">
17
+ Uploading in Progress, Please Wait...
18
+ </p>
19
+ </div>
20
+ );
21
+ };
22
+
23
+ const AestheticProcessingAnimationWithStyles = () => (
24
+ <>
25
+ <style jsx global>{`
26
+ @keyframes processing {
27
+ 0% {
28
+ transform: translateX(-100%);
29
+ }
30
+ 100% {
31
+ transform: translateX(300%);
32
+ }
33
+ }
34
+ .animate-processing {
35
+ animation: processing 2s ease-in-out infinite;
36
+ }
37
+ `}</style>
38
+ <AestheticProcessingAnimation />
39
+ </>
40
+ );
41
+
42
+ export default AestheticProcessingAnimationWithStyles;
@@ -1,47 +1,121 @@
1
- import React, { useState, useRef } from "react";
2
- import { upload, x } from "@/src/component-lib/icon/iconPaths";
3
- import Icon from "@/src/component-lib/icon/Icon";
1
+ /**
2
+ * Copyright (c) Grampro Business Services and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ import React, { useState, useRef, useEffect } from "react";
9
+ import { upload, x } from "../../icon/iconPaths";
10
+ import Icon from "../../icon/Icon";
4
11
  import { GetFileIcon } from "./uploaderIcon";
12
+ import AestheticProcessingAnimationWithStyles from "./ProgressAnimation";
5
13
 
6
- export const FileUploader = ({ showImagePreview = false }: any) => {
14
+ export const FileUploader = ({
15
+ showImagePreview = false,
16
+ multiple = true,
17
+ onChange,
18
+ startsUpload = false,
19
+ selectedFiles = [],
20
+ }: any) => {
7
21
  const [files, setFiles] = useState<any[]>([]);
8
22
  const [previewUrls, setPreviewUrls] = useState<{ [key: string]: string }>({});
23
+ const [isDragging, setIsDragging] = useState(false);
9
24
  const fileInputRef = useRef<any>(null);
25
+ const dropZoneRef = useRef<any>(null);
26
+
27
+ // For Binding values on edit
28
+ useEffect(() => {
29
+ if (selectedFiles && selectedFiles.length > 0) {
30
+ setFiles(multiple ? selectedFiles : [selectedFiles[0]]);
31
+ }
32
+ }, [selectedFiles, multiple]);
10
33
 
11
- // adds files to file array
12
- const handleFileChange = (e: any) => {
13
- const selectedFiles = Array.from(e.target.files);
14
- const newPreviewUrls = { ...previewUrls };
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);
15
43
 
16
- selectedFiles.forEach((file: any) => {
44
+ // Clear previous preview URLs if not multiple
45
+ if (!multiple) {
46
+ setPreviewUrls({});
47
+ }
48
+
49
+ newFiles.forEach((file) => {
17
50
  if (showImagePreview && file.type.startsWith("image/")) {
18
51
  const reader = new FileReader();
19
52
  reader.onload = () => {
20
- newPreviewUrls[file.name] = reader.result as string;
21
- setPreviewUrls(newPreviewUrls);
53
+ setPreviewUrls((prev) => ({
54
+ ...prev,
55
+ [file.name]: reader.result as string,
56
+ }));
22
57
  };
23
58
  reader.readAsDataURL(file);
24
59
  }
25
60
  });
26
61
 
27
- setFiles((prevFiles: any) => [...prevFiles, ...selectedFiles]);
62
+ if (onChange) onChange(updatedFiles);
28
63
  };
29
64
 
30
- // remove file from the file array
31
- const removeFile = (index: any) => {
32
- const fileToRemove = files[index];
33
- setFiles((prevFiles) => prevFiles.filter((_, i) => i !== index));
65
+ // Removes the file from file array
66
+ const removeFile = (index: number) => {
67
+ const updatedFiles = files.filter((_, i) => i !== index);
68
+ setFiles(updatedFiles);
34
69
  setPreviewUrls((prevPreviews) => {
35
- const { [fileToRemove.name]: _, ...rest } = prevPreviews;
70
+ const { [files[index].name]: _, ...rest } = prevPreviews;
36
71
  return rest;
37
72
  });
73
+
74
+ if (onChange) onChange(updatedFiles);
75
+ };
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);
38
88
  };
39
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
+
40
105
  return (
41
106
  <div className="w-full max-w-md mx-auto">
42
107
  <div
43
- className="flex flex-col items-center border-2 border-dashed border-gray-300 rounded-lg p-4 text-center cursor-pointer hover:border-gray-400 transition-colors"
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
+ }`}
44
114
  onClick={() => fileInputRef.current.click()}
115
+ onDragEnter={handleDragEnter}
116
+ onDragOver={handleDragOver}
117
+ onDragLeave={handleDragLeave}
118
+ onDrop={handleDrop}
45
119
  >
46
120
  <Icon
47
121
  dimensions={{ width: "36", height: "36" }}
@@ -51,16 +125,22 @@ export const FileUploader = ({ showImagePreview = false }: any) => {
51
125
  <p className="mt-2 text-sm text-gray-500">
52
126
  Click to upload or drag and drop
53
127
  </p>
54
- <p className="text-xs text-gray-400">Supports multiple file uploads</p>
128
+ <p className="text-xs text-gray-400">
129
+ {multiple
130
+ ? "Supports multiple file uploads"
131
+ : "Single file upload only"}
132
+ </p>
55
133
  </div>
56
134
  <input
57
135
  type="file"
58
136
  ref={fileInputRef}
59
137
  className="hidden"
60
- onChange={handleFileChange}
61
- multiple
138
+ onChange={(e) => handleFileChange(Array.from(e.target.files || []))}
139
+ multiple={multiple}
62
140
  />
63
141
 
142
+ {startsUpload && <AestheticProcessingAnimationWithStyles />}
143
+
64
144
  {files.length > 0 && (
65
145
  <div className="mt-4 space-y-2">
66
146
  {files.map((file: any, index) => (
@@ -1,11 +1,16 @@
1
+ /**
2
+ * Copyright (c) Grampro Business Services and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
1
8
  import {
2
9
  fileimage,
3
10
  filespreadsheet,
4
11
  filetext,
5
12
  film,
6
13
  music,
7
- upload,
8
- x,
9
14
  } from "../../icon/iconPaths";
10
15
  import Icon from "../../icon/Icon";
11
16