allaw-ui 3.0.6 → 3.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.
|
@@ -17,6 +17,9 @@ export interface FileUploaderProps {
|
|
|
17
17
|
onFileRemove?: () => void;
|
|
18
18
|
uploadProgress?: number;
|
|
19
19
|
isLoading?: boolean;
|
|
20
|
+
autoManageProgress?: boolean;
|
|
21
|
+
progressSteps?: number;
|
|
22
|
+
progressInterval?: number;
|
|
20
23
|
errorMessage?: string | null;
|
|
21
24
|
buttonLabel?: string;
|
|
22
25
|
acceptedLabel?: string;
|
|
@@ -34,21 +34,47 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
34
34
|
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
35
35
|
}
|
|
36
36
|
};
|
|
37
|
-
import React, { useState, useRef } from "react";
|
|
37
|
+
import React, { useState, useRef, useEffect } from "react";
|
|
38
38
|
import Image from "next/image";
|
|
39
39
|
import styles from "./FileUploader.module.css";
|
|
40
40
|
import ImageCropperModal from "./ImageCropperModal";
|
|
41
41
|
import IconButton from "../../atoms/buttons/IconButton";
|
|
42
42
|
import TertiaryButton from "../../atoms/buttons/TertiaryButton";
|
|
43
43
|
var FileUploader = function (_a) {
|
|
44
|
-
var acceptedExtensions = _a.acceptedExtensions, maxFileSizeMB = _a.maxFileSizeMB, _b = _a.enableDragAndDrop, enableDragAndDrop = _b === void 0 ? true : _b, _c = _a.enableCropping, enableCropping = _c === void 0 ? false : _c, _d = _a.cropShape, cropShape = _d === void 0 ? "square" : _d, iconUrl = _a.iconUrl, descriptionParts = _a.descriptionParts, onFileRead = _a.onFileRead, onFileRemove = _a.onFileRemove, _e = _a.uploadProgress, uploadProgress = _e === void 0 ? 0 : _e, _f = _a.isLoading, isLoading = _f === void 0 ? false : _f, _g = _a.
|
|
45
|
-
var
|
|
46
|
-
var
|
|
47
|
-
var
|
|
48
|
-
var
|
|
49
|
-
var
|
|
50
|
-
var
|
|
44
|
+
var acceptedExtensions = _a.acceptedExtensions, maxFileSizeMB = _a.maxFileSizeMB, _b = _a.enableDragAndDrop, enableDragAndDrop = _b === void 0 ? true : _b, _c = _a.enableCropping, enableCropping = _c === void 0 ? false : _c, _d = _a.cropShape, cropShape = _d === void 0 ? "square" : _d, iconUrl = _a.iconUrl, descriptionParts = _a.descriptionParts, onFileRead = _a.onFileRead, onFileRemove = _a.onFileRemove, _e = _a.uploadProgress, uploadProgress = _e === void 0 ? 0 : _e, _f = _a.isLoading, isLoading = _f === void 0 ? false : _f, _g = _a.autoManageProgress, autoManageProgress = _g === void 0 ? false : _g, _h = _a.progressSteps, progressSteps = _h === void 0 ? 10 : _h, _j = _a.progressInterval, progressInterval = _j === void 0 ? 500 : _j, _k = _a.errorMessage, errorMessage = _k === void 0 ? null : _k, _l = _a.buttonLabel, buttonLabel = _l === void 0 ? "Choisir un fichier" : _l, _m = _a.acceptedLabel, acceptedLabel = _m === void 0 ? "Format accepté :" : _m, _o = _a.maxSizeLabel, maxSizeLabel = _o === void 0 ? "Taille maximale :" : _o, fileName = _a.fileName, fileSize = _a.fileSize, _p = _a.filePresentationLabel, filePresentationLabel = _p === void 0 ? "Voici votre fichier." : _p;
|
|
45
|
+
var _q = useState(null), selectedFile = _q[0], setSelectedFile = _q[1];
|
|
46
|
+
var _r = useState(null), fileContent = _r[0], setFileContent = _r[1];
|
|
47
|
+
var _s = useState(false), isHovering = _s[0], setIsHovering = _s[1];
|
|
48
|
+
var _t = useState(false), showCropper = _t[0], setShowCropper = _t[1];
|
|
49
|
+
var _u = useState(null), previewUrl = _u[0], setPreviewUrl = _u[1];
|
|
50
|
+
var _v = useState(null), cropMetadata = _v[0], setCropMetadata = _v[1];
|
|
51
|
+
var _w = useState(0), internalProgress = _w[0], setInternalProgress = _w[1];
|
|
52
|
+
var progressIntervalRef = useRef(null);
|
|
51
53
|
var fileInputRef = useRef(null);
|
|
54
|
+
useEffect(function () {
|
|
55
|
+
return function () {
|
|
56
|
+
stopProgressSimulation();
|
|
57
|
+
};
|
|
58
|
+
}, []);
|
|
59
|
+
var startProgressSimulation = function () {
|
|
60
|
+
stopProgressSimulation();
|
|
61
|
+
setInternalProgress(1);
|
|
62
|
+
progressIntervalRef.current = setInterval(function () {
|
|
63
|
+
setInternalProgress(function (prev) {
|
|
64
|
+
if (prev >= progressSteps) {
|
|
65
|
+
stopProgressSimulation();
|
|
66
|
+
return progressSteps;
|
|
67
|
+
}
|
|
68
|
+
return prev + 1;
|
|
69
|
+
});
|
|
70
|
+
}, progressInterval);
|
|
71
|
+
};
|
|
72
|
+
var stopProgressSimulation = function () {
|
|
73
|
+
if (progressIntervalRef.current) {
|
|
74
|
+
clearInterval(progressIntervalRef.current);
|
|
75
|
+
progressIntervalRef.current = null;
|
|
76
|
+
}
|
|
77
|
+
};
|
|
52
78
|
var resetFileInput = function () {
|
|
53
79
|
if (fileInputRef.current) {
|
|
54
80
|
fileInputRef.current.value = "";
|
|
@@ -56,10 +82,8 @@ var FileUploader = function (_a) {
|
|
|
56
82
|
};
|
|
57
83
|
var validateFile = function (file) {
|
|
58
84
|
var _a;
|
|
59
|
-
// Vérifier l'extension
|
|
60
85
|
var fileExtension = ".".concat((_a = file.name.split(".").pop()) === null || _a === void 0 ? void 0 : _a.toLowerCase());
|
|
61
86
|
var isValidExtension = acceptedExtensions.includes(fileExtension);
|
|
62
|
-
// Vérifier la taille
|
|
63
87
|
var isValidSize = file.size <= maxFileSizeMB * 1024 * 1024;
|
|
64
88
|
return isValidExtension && isValidSize;
|
|
65
89
|
};
|
|
@@ -116,11 +140,9 @@ var FileUploader = function (_a) {
|
|
|
116
140
|
case 2:
|
|
117
141
|
content = _a.sent();
|
|
118
142
|
setFileContent(content);
|
|
119
|
-
// Créer un aperçu si c'est une image
|
|
120
143
|
if (isImageFile(file)) {
|
|
121
144
|
url = URL.createObjectURL(file);
|
|
122
145
|
setPreviewUrl(url);
|
|
123
|
-
// Si le cadrage n'est pas activé, on utilise une métadonnée par défaut
|
|
124
146
|
if (!enableCropping) {
|
|
125
147
|
setCropMetadata({
|
|
126
148
|
zoom: 1,
|
|
@@ -134,13 +156,14 @@ var FileUploader = function (_a) {
|
|
|
134
156
|
setPreviewUrl(null);
|
|
135
157
|
setCropMetadata(null);
|
|
136
158
|
}
|
|
137
|
-
|
|
159
|
+
if (autoManageProgress && !(enableCropping && isImageFile(file))) {
|
|
160
|
+
startProgressSimulation();
|
|
161
|
+
}
|
|
138
162
|
if (enableCropping && isImageFile(file)) {
|
|
139
163
|
setSelectedFile(file);
|
|
140
164
|
setShowCropper(true);
|
|
141
165
|
}
|
|
142
166
|
else {
|
|
143
|
-
// Sinon, traiter normalement
|
|
144
167
|
setSelectedFile(file);
|
|
145
168
|
onFileRead === null || onFileRead === void 0 ? void 0 : onFileRead(file, content);
|
|
146
169
|
}
|
|
@@ -167,6 +190,9 @@ var FileUploader = function (_a) {
|
|
|
167
190
|
var handleCropConfirm = function (cropMetadata) {
|
|
168
191
|
setShowCropper(false);
|
|
169
192
|
setCropMetadata(cropMetadata);
|
|
193
|
+
if (autoManageProgress) {
|
|
194
|
+
startProgressSimulation();
|
|
195
|
+
}
|
|
170
196
|
if (selectedFile && fileContent) {
|
|
171
197
|
onFileRead === null || onFileRead === void 0 ? void 0 : onFileRead(selectedFile, fileContent, cropMetadata);
|
|
172
198
|
}
|
|
@@ -175,6 +201,10 @@ var FileUploader = function (_a) {
|
|
|
175
201
|
if (previewUrl) {
|
|
176
202
|
URL.revokeObjectURL(previewUrl);
|
|
177
203
|
}
|
|
204
|
+
if (autoManageProgress) {
|
|
205
|
+
stopProgressSimulation();
|
|
206
|
+
setInternalProgress(0);
|
|
207
|
+
}
|
|
178
208
|
setSelectedFile(null);
|
|
179
209
|
setFileContent(null);
|
|
180
210
|
setPreviewUrl(null);
|
|
@@ -197,6 +227,12 @@ var FileUploader = function (_a) {
|
|
|
197
227
|
return "".concat((size / (1024 * 1024)).toFixed(2), " Mo");
|
|
198
228
|
}
|
|
199
229
|
};
|
|
230
|
+
var displayProgress = autoManageProgress
|
|
231
|
+
? internalProgress
|
|
232
|
+
: uploadProgress;
|
|
233
|
+
var isLoadingState = autoManageProgress
|
|
234
|
+
? internalProgress > 0 && internalProgress < progressSteps
|
|
235
|
+
: isLoading;
|
|
200
236
|
var displayFileSize = fileSize !== undefined ? fileSize : selectedFile ? selectedFile.size : 0;
|
|
201
237
|
var displayFileName = fileName || (selectedFile ? selectedFile.name : "");
|
|
202
238
|
var isSelectedImage = selectedFile && isImageFile(selectedFile);
|
|
@@ -206,7 +242,6 @@ var FileUploader = function (_a) {
|
|
|
206
242
|
? styles.preview_container_banner
|
|
207
243
|
: ""), onClick: function (e) {
|
|
208
244
|
var _a;
|
|
209
|
-
// Ne pas déclencher si on clique sur le conteneur d'image
|
|
210
245
|
if (!e.target.closest(".".concat(styles.preview_image_container))) {
|
|
211
246
|
(_a = fileInputRef.current) === null || _a === void 0 ? void 0 : _a.click();
|
|
212
247
|
}
|
|
@@ -261,7 +296,7 @@ var FileUploader = function (_a) {
|
|
|
261
296
|
errorMessage && (React.createElement("div", { className: styles.uploaded_file_container },
|
|
262
297
|
React.createElement("div", { className: styles.uploaded_file_error },
|
|
263
298
|
React.createElement("span", { className: styles.error_message }, errorMessage)))),
|
|
264
|
-
(
|
|
299
|
+
(isLoadingState || selectedFile || fileName) && (React.createElement("div", { className: styles.uploaded_file_container },
|
|
265
300
|
React.createElement("div", { className: styles.uploaded_file_content },
|
|
266
301
|
React.createElement("div", { className: styles.uploaded_file_icon }, iconUrl ? (React.createElement(Image, { src: iconUrl, alt: "File icon", className: styles.file_icon, width: 20, height: 20 })) : (React.createElement("i", { className: "allaw-icon-file" }))),
|
|
267
302
|
React.createElement("div", { className: styles.uploaded_file_name_size },
|
|
@@ -273,8 +308,11 @@ var FileUploader = function (_a) {
|
|
|
273
308
|
selectedFile && (React.createElement("div", { className: styles.uploaded_file_delete_container },
|
|
274
309
|
React.createElement("div", { className: styles.uploaded_file_delete, onClick: handleFileDelete },
|
|
275
310
|
React.createElement("i", { className: "allaw-icon-close" }))))),
|
|
276
|
-
|
|
277
|
-
React.createElement("div", { className: styles.
|
|
311
|
+
displayProgress > 0 && (React.createElement("div", { className: styles.uploaded_file_progress_bar },
|
|
312
|
+
React.createElement("div", { className: styles.progress_bar_container },
|
|
313
|
+
React.createElement("div", { className: styles.progress_bar_fill, style: {
|
|
314
|
+
width: "".concat((displayProgress / progressSteps) * 100, "%"),
|
|
315
|
+
} })))))),
|
|
278
316
|
showCropper && selectedFile && (React.createElement(ImageCropperModal, { file: selectedFile, shape: cropShape, onCancel: handleCropCancel, onConfirm: handleCropConfirm }))));
|
|
279
317
|
};
|
|
280
318
|
export default FileUploader;
|
|
@@ -223,12 +223,13 @@
|
|
|
223
223
|
}
|
|
224
224
|
|
|
225
225
|
.uploaded_file_progress_bar {
|
|
226
|
-
display: flex;
|
|
227
|
-
flex-direction: row;
|
|
228
|
-
align-items: center;
|
|
229
|
-
justify-content: center;
|
|
230
|
-
width: 100
|
|
231
|
-
height: 40px;
|
|
226
|
+
display: flex !important;
|
|
227
|
+
flex-direction: row !important;
|
|
228
|
+
align-items: center !important;
|
|
229
|
+
justify-content: center !important;
|
|
230
|
+
width: 100% !important;
|
|
231
|
+
height: 40px !important;
|
|
232
|
+
margin-top: 8px !important;
|
|
232
233
|
}
|
|
233
234
|
|
|
234
235
|
.progress_bar_container {
|
|
@@ -237,12 +238,14 @@
|
|
|
237
238
|
background-color: #e6edf5;
|
|
238
239
|
border-radius: 2px;
|
|
239
240
|
overflow: hidden;
|
|
241
|
+
display: block !important;
|
|
240
242
|
}
|
|
241
243
|
|
|
242
244
|
.progress_bar_fill {
|
|
243
245
|
height: 100%;
|
|
244
246
|
background-color: #25beeb;
|
|
245
247
|
transition: width 0.3s ease-in-out;
|
|
248
|
+
display: block !important;
|
|
246
249
|
}
|
|
247
250
|
|
|
248
251
|
.drag_over {
|