siam-ui-utils 3.1.0 → 3.1.2
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/dist/CustomBootstrap.js +9 -9
- package/dist/_virtual/_commonjsHelpers.js +7 -7
- package/dist/_virtual/events.js +6 -6
- package/dist/_virtual/events2.js +5 -5
- package/dist/_virtual/index.js +8 -8
- package/dist/_virtual/index2.js +5 -5
- package/dist/assets/img/copiar.png.js +5 -5
- package/dist/assets/img/take-photo.jpg.js +5 -5
- package/dist/constants.js +9 -9
- package/dist/copy-link/index.js +60 -60
- package/dist/copy-link/styles.css +37 -37
- package/dist/custom-input/CustomInputCheckbox.js +37 -37
- package/dist/custom-input/CustomInputCurrency.js +84 -84
- package/dist/custom-input/CustomInputFile.js +47 -47
- package/dist/custom-input/CustomInputRadio.js +68 -68
- package/dist/custom-input/constant.js +5 -5
- package/dist/custom-input/index.js +12 -12
- package/dist/drag-and-dropzone/DropzoneComponent.js +83 -83
- package/dist/drag-and-dropzone/config.js +59 -59
- package/dist/drag-and-dropzone/index.js +39 -39
- package/dist/dropzone/Input.js +77 -77
- package/dist/dropzone/Layout.js +50 -50
- package/dist/dropzone/Preview.js +117 -117
- package/dist/dropzone/SubmitButton.js +34 -34
- package/dist/dropzone/assets/cancel.svg.js +5 -5
- package/dist/dropzone/assets/remove.svg.js +5 -5
- package/dist/dropzone/assets/restart.svg.js +5 -5
- package/dist/dropzone/index.js +514 -514
- package/dist/dropzone/utils.js +93 -93
- package/dist/dropzone-uploader/base/index.js +135 -135
- package/dist/dropzone-uploader/index.js +7 -7
- package/dist/dropzone-uploader/styled/index.js +148 -148
- package/dist/iconos/constants-svg.js +666 -666
- package/dist/iconos/icon-button-svg.js +50 -50
- package/dist/iconos/index.js +24 -24
- package/dist/iconos/styled-icon.js +26 -26
- package/dist/index.js +4 -4
- package/dist/intl-messages/index.js +11 -11
- package/dist/node_modules/@babel/runtime/helpers/esm/assertThisInitialized.js +8 -8
- package/dist/node_modules/@babel/runtime/helpers/esm/extends.js +13 -13
- package/dist/node_modules/@babel/runtime/helpers/esm/inheritsLoose.js +8 -8
- package/dist/node_modules/@babel/runtime/helpers/esm/objectWithoutPropertiesLoose.js +13 -13
- package/dist/node_modules/@babel/runtime/helpers/esm/setPrototypeOf.js +9 -9
- package/dist/node_modules/@neolution-ch/reactstrap/esm/Badge.js +77 -0
- package/dist/node_modules/@neolution-ch/reactstrap/esm/Badge.js.map +1 -0
- package/dist/node_modules/@neolution-ch/reactstrap/esm/Button.js +111 -111
- package/dist/node_modules/@neolution-ch/reactstrap/esm/CloseButton.js +106 -106
- package/dist/node_modules/@neolution-ch/reactstrap/esm/Col.js +125 -125
- package/dist/node_modules/@neolution-ch/reactstrap/esm/FormGroup.js +75 -75
- package/dist/node_modules/@neolution-ch/reactstrap/esm/Input.js +225 -225
- package/dist/node_modules/@neolution-ch/reactstrap/esm/Label.js +122 -122
- package/dist/node_modules/@neolution-ch/reactstrap/esm/Row.js +83 -83
- package/dist/node_modules/@neolution-ch/reactstrap/esm/utils.js +68 -68
- package/dist/node_modules/classnames/index.js +62 -62
- package/dist/node_modules/dom-helpers/esm/addClass.js +10 -10
- package/dist/node_modules/dom-helpers/esm/hasClass.js +8 -8
- package/dist/node_modules/dom-helpers/esm/removeClass.js +16 -16
- package/dist/node_modules/dropzone/dist/dropzone.js +1968 -1968
- package/dist/node_modules/events/events.js +372 -372
- package/dist/node_modules/just-extend/index.esm.js +43 -43
- package/dist/node_modules/react-transition-group/esm/CSSTransition.js +275 -275
- package/dist/node_modules/react-transition-group/esm/Transition.js +441 -441
- package/dist/node_modules/react-transition-group/esm/TransitionGroup.js +146 -146
- package/dist/node_modules/react-transition-group/esm/TransitionGroupContext.js +6 -6
- package/dist/node_modules/react-transition-group/esm/config.js +7 -7
- package/dist/node_modules/react-transition-group/esm/utils/ChildMapping.js +100 -100
- package/dist/node_modules/react-transition-group/esm/utils/PropTypes.js +24 -24
- package/dist/node_modules/react-transition-group/esm/utils/reflow.js +7 -7
- package/dist/react-notifications/Notification.js +73 -73
- package/dist/react-notifications/NotificationContainer.js +59 -59
- package/dist/react-notifications/NotificationManager.js +174 -174
- package/dist/react-notifications/Notifications.js +72 -72
- package/dist/react-notifications/constants.js +26 -26
- package/dist/react-notifications/index.js +11 -11
- package/dist/select/custom-select/constants.js +8 -8
- package/dist/select/custom-select/index.js +89 -89
- package/dist/select/custom-select-input/index.js +12 -12
- package/dist/select/index.js +9 -9
- package/dist/select/multi-select/badge-label-multi-select.js +29 -29
- package/dist/select/multi-select/index.css +36 -36
- package/dist/select/multi-select/index.js +171 -171
- package/dist/select/multi-select/index.js.map +1 -1
- package/dist/select/multi-select/styled-component.js +23 -23
- package/dist/select/multi-select/styled-component.js.map +1 -1
- package/dist/timer/index.js +60 -60
- package/dist/timer/styles.css +58 -58
- package/dist/tomar-foto/index.js +62 -62
- package/dist/view-layout/button-editor.js +14 -14
- package/dist/view-layout/constants.js +11 -11
- package/dist/view-layout/editor-layer.js +28 -28
- package/dist/view-layout/index.js +53 -53
- package/dist/view-layout/slot-wrapper.js +39 -39
- package/dist/view-layout/styles.css +126 -126
- package/dist/where-by-room/index.js +20 -20
- package/dist/where-by-room/room.js +78 -78
- package/package.json +1 -4
package/dist/dropzone/index.js
CHANGED
|
@@ -1,514 +1,514 @@
|
|
|
1
|
-
import { jsx } from "react/jsx-runtime";
|
|
2
|
-
import React from "react";
|
|
3
|
-
import PropTypes from "prop-types";
|
|
4
|
-
import Layout from "./Layout.js";
|
|
5
|
-
import Input from "./Input.js";
|
|
6
|
-
import Preview from "./Preview.js";
|
|
7
|
-
import SubmitButton from "./SubmitButton.js";
|
|
8
|
-
import { getFilesFromEvent, accepts, resolveValue, mergeStyles } from "./utils.js";
|
|
9
|
-
import { defaultClassNames, formatBytes, formatDuration } from "./utils.js";
|
|
10
|
-
class Dropzone extends React.Component {
|
|
11
|
-
constructor(props) {
|
|
12
|
-
super(props);
|
|
13
|
-
this.forceUpdate = () => {
|
|
14
|
-
if (this.mounted) super.forceUpdate();
|
|
15
|
-
};
|
|
16
|
-
this.getFilesFromEvent = () => {
|
|
17
|
-
return this.props.getFilesFromEvent || getFilesFromEvent;
|
|
18
|
-
};
|
|
19
|
-
this.getDataTransferItemsFromEvent = () => {
|
|
20
|
-
return this.props.getDataTransferItemsFromEvent || getFilesFromEvent;
|
|
21
|
-
};
|
|
22
|
-
this.handleDragEnter = async (e) => {
|
|
23
|
-
e.preventDefault();
|
|
24
|
-
e.stopPropagation();
|
|
25
|
-
const dragged = await this.getDataTransferItemsFromEvent()(e);
|
|
26
|
-
this.setState({ active: true, dragged });
|
|
27
|
-
};
|
|
28
|
-
this.handleDragOver = async (e) => {
|
|
29
|
-
e.preventDefault();
|
|
30
|
-
e.stopPropagation();
|
|
31
|
-
clearTimeout(this.dragTimeoutId);
|
|
32
|
-
const dragged = await this.getDataTransferItemsFromEvent()(e);
|
|
33
|
-
this.setState({ active: true, dragged });
|
|
34
|
-
};
|
|
35
|
-
this.handleDragLeave = (e) => {
|
|
36
|
-
e.preventDefault();
|
|
37
|
-
e.stopPropagation();
|
|
38
|
-
this.dragTimeoutId = window.setTimeout(() => this.setState({ active: false, dragged: [] }), 150);
|
|
39
|
-
};
|
|
40
|
-
this.handleDrop = async (e) => {
|
|
41
|
-
e.preventDefault();
|
|
42
|
-
e.stopPropagation();
|
|
43
|
-
this.setState({ active: false, dragged: [] });
|
|
44
|
-
const files = await this.getFilesFromEvent()(e);
|
|
45
|
-
this.handleFiles(files);
|
|
46
|
-
};
|
|
47
|
-
this.handleDropDisabled = (e) => {
|
|
48
|
-
e.preventDefault();
|
|
49
|
-
e.stopPropagation();
|
|
50
|
-
this.setState({ active: false, dragged: [] });
|
|
51
|
-
};
|
|
52
|
-
this.handleChangeStatus = (fileWithMeta) => {
|
|
53
|
-
if (!this.props.onChangeStatus) return;
|
|
54
|
-
const { meta = {} } = this.props.onChangeStatus(fileWithMeta, fileWithMeta.meta.status, this.files) || {};
|
|
55
|
-
if (meta) {
|
|
56
|
-
delete meta.status;
|
|
57
|
-
fileWithMeta.meta = { ...fileWithMeta.meta, ...meta };
|
|
58
|
-
this.forceUpdate();
|
|
59
|
-
}
|
|
60
|
-
};
|
|
61
|
-
this.handleSubmit = (files) => {
|
|
62
|
-
if (this.props.onSubmit) this.props.onSubmit(files, [...this.files]);
|
|
63
|
-
};
|
|
64
|
-
this.handleCancel = (fileWithMeta) => {
|
|
65
|
-
if (fileWithMeta.meta.status !== "uploading") return;
|
|
66
|
-
fileWithMeta.meta.status = "aborted";
|
|
67
|
-
if (fileWithMeta.xhr) fileWithMeta.xhr.abort();
|
|
68
|
-
this.handleChangeStatus(fileWithMeta);
|
|
69
|
-
this.forceUpdate();
|
|
70
|
-
};
|
|
71
|
-
this.handleRemove = (fileWithMeta) => {
|
|
72
|
-
const index = this.files.findIndex((f) => f === fileWithMeta);
|
|
73
|
-
if (index !== -1) {
|
|
74
|
-
URL.revokeObjectURL(fileWithMeta.meta.previewUrl || "");
|
|
75
|
-
fileWithMeta.meta.status = "removed";
|
|
76
|
-
this.handleChangeStatus(fileWithMeta);
|
|
77
|
-
this.files.splice(index, 1);
|
|
78
|
-
this.forceUpdate();
|
|
79
|
-
}
|
|
80
|
-
};
|
|
81
|
-
this.handleRestart = (fileWithMeta) => {
|
|
82
|
-
if (!this.props.getUploadParams) return;
|
|
83
|
-
if (fileWithMeta.meta.status === "ready") fileWithMeta.meta.status = "started";
|
|
84
|
-
else fileWithMeta.meta.status = "restarted";
|
|
85
|
-
this.handleChangeStatus(fileWithMeta);
|
|
86
|
-
fileWithMeta.meta.status = "getting_upload_params";
|
|
87
|
-
fileWithMeta.meta.percent = 0;
|
|
88
|
-
this.handleChangeStatus(fileWithMeta);
|
|
89
|
-
this.forceUpdate();
|
|
90
|
-
this.uploadFile(fileWithMeta);
|
|
91
|
-
};
|
|
92
|
-
this.handleFiles = (files) => {
|
|
93
|
-
files.forEach((f, i) => this.handleFile(f, `${(/* @__PURE__ */ new Date()).getTime()}-${i}`));
|
|
94
|
-
const { current } = this.dropzone;
|
|
95
|
-
if (current) setTimeout(() => current.scroll({ top: current.scrollHeight, behavior: "smooth" }), 150);
|
|
96
|
-
};
|
|
97
|
-
this.handleFile = async (file, id) => {
|
|
98
|
-
const { name, size, type, lastModified } = file;
|
|
99
|
-
const { minSizeBytes, maxSizeBytes, maxFiles, accept, getUploadParams, autoUpload, validate } = this.props;
|
|
100
|
-
const uploadedDate = (/* @__PURE__ */ new Date()).toISOString();
|
|
101
|
-
const lastModifiedDate = lastModified && new Date(lastModified).toISOString();
|
|
102
|
-
const fileWithMeta = {
|
|
103
|
-
file,
|
|
104
|
-
meta: { name, size, type, lastModifiedDate, uploadedDate, percent: 0, id }
|
|
105
|
-
};
|
|
106
|
-
if (file.type !== "application/x-moz-file" && !accepts(file, accept)) {
|
|
107
|
-
fileWithMeta.meta.status = "rejected_file_type";
|
|
108
|
-
this.handleChangeStatus(fileWithMeta);
|
|
109
|
-
return;
|
|
110
|
-
}
|
|
111
|
-
if (this.files.length >= maxFiles) {
|
|
112
|
-
fileWithMeta.meta.status = "rejected_max_files";
|
|
113
|
-
this.handleChangeStatus(fileWithMeta);
|
|
114
|
-
return;
|
|
115
|
-
}
|
|
116
|
-
fileWithMeta.cancel = () => this.handleCancel(fileWithMeta);
|
|
117
|
-
fileWithMeta.remove = () => this.handleRemove(fileWithMeta);
|
|
118
|
-
fileWithMeta.restart = () => this.handleRestart(fileWithMeta);
|
|
119
|
-
fileWithMeta.meta.status = "preparing";
|
|
120
|
-
this.files.push(fileWithMeta);
|
|
121
|
-
this.handleChangeStatus(fileWithMeta);
|
|
122
|
-
this.forceUpdate();
|
|
123
|
-
if (size < minSizeBytes || size > maxSizeBytes) {
|
|
124
|
-
fileWithMeta.meta.status = "error_file_size";
|
|
125
|
-
this.handleChangeStatus(fileWithMeta);
|
|
126
|
-
this.forceUpdate();
|
|
127
|
-
return;
|
|
128
|
-
}
|
|
129
|
-
await this.generatePreview(fileWithMeta);
|
|
130
|
-
if (validate) {
|
|
131
|
-
const error = validate(fileWithMeta);
|
|
132
|
-
if (error) {
|
|
133
|
-
fileWithMeta.meta.status = "error_validation";
|
|
134
|
-
fileWithMeta.meta.validationError = error;
|
|
135
|
-
this.handleChangeStatus(fileWithMeta);
|
|
136
|
-
this.forceUpdate();
|
|
137
|
-
return;
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
if (getUploadParams) {
|
|
141
|
-
if (autoUpload) {
|
|
142
|
-
this.uploadFile(fileWithMeta);
|
|
143
|
-
fileWithMeta.meta.status = "getting_upload_params";
|
|
144
|
-
} else {
|
|
145
|
-
fileWithMeta.meta.status = "ready";
|
|
146
|
-
}
|
|
147
|
-
} else {
|
|
148
|
-
fileWithMeta.meta.status = "done";
|
|
149
|
-
}
|
|
150
|
-
this.handleChangeStatus(fileWithMeta);
|
|
151
|
-
this.forceUpdate();
|
|
152
|
-
};
|
|
153
|
-
this.generatePreview = async (fileWithMeta) => {
|
|
154
|
-
const {
|
|
155
|
-
meta: { type },
|
|
156
|
-
file
|
|
157
|
-
} = fileWithMeta;
|
|
158
|
-
const isImage = type.startsWith("image/");
|
|
159
|
-
const isAudio = type.startsWith("audio/");
|
|
160
|
-
const isVideo = type.startsWith("video/");
|
|
161
|
-
if (!isImage && !isAudio && !isVideo) return;
|
|
162
|
-
const objectUrl = URL.createObjectURL(file);
|
|
163
|
-
const fileCallbackToPromise = (fileObj) => {
|
|
164
|
-
return Promise.race([
|
|
165
|
-
new Promise((resolve) => {
|
|
166
|
-
if (fileObj instanceof HTMLImageElement) fileObj.onload = resolve;
|
|
167
|
-
else fileObj.onloadedmetadata = resolve;
|
|
168
|
-
}),
|
|
169
|
-
new Promise((_, reject) => {
|
|
170
|
-
setTimeout(reject, 1e3);
|
|
171
|
-
})
|
|
172
|
-
]);
|
|
173
|
-
};
|
|
174
|
-
try {
|
|
175
|
-
if (isImage) {
|
|
176
|
-
const img = new Image();
|
|
177
|
-
img.src = objectUrl;
|
|
178
|
-
fileWithMeta.meta.previewUrl = objectUrl;
|
|
179
|
-
await fileCallbackToPromise(img);
|
|
180
|
-
fileWithMeta.meta.width = img.width;
|
|
181
|
-
fileWithMeta.meta.height = img.height;
|
|
182
|
-
}
|
|
183
|
-
if (isAudio) {
|
|
184
|
-
const audio = new Audio();
|
|
185
|
-
audio.src = objectUrl;
|
|
186
|
-
await fileCallbackToPromise(audio);
|
|
187
|
-
fileWithMeta.meta.duration = audio.duration;
|
|
188
|
-
}
|
|
189
|
-
if (isVideo) {
|
|
190
|
-
const video = document.createElement("video");
|
|
191
|
-
video.src = objectUrl;
|
|
192
|
-
await fileCallbackToPromise(video);
|
|
193
|
-
fileWithMeta.meta.duration = video.duration;
|
|
194
|
-
fileWithMeta.meta.videoWidth = video.videoWidth;
|
|
195
|
-
fileWithMeta.meta.videoHeight = video.videoHeight;
|
|
196
|
-
}
|
|
197
|
-
if (!isImage) URL.revokeObjectURL(objectUrl);
|
|
198
|
-
} catch (e) {
|
|
199
|
-
URL.revokeObjectURL(objectUrl);
|
|
200
|
-
}
|
|
201
|
-
this.forceUpdate();
|
|
202
|
-
};
|
|
203
|
-
this.uploadFile = async (fileWithMeta) => {
|
|
204
|
-
const { getUploadParams } = this.props;
|
|
205
|
-
if (!getUploadParams) return;
|
|
206
|
-
let params = null;
|
|
207
|
-
try {
|
|
208
|
-
params = await getUploadParams(fileWithMeta);
|
|
209
|
-
} catch (e) {
|
|
210
|
-
console.error("Error Upload Params", e.stack);
|
|
211
|
-
}
|
|
212
|
-
if (params === null) return;
|
|
213
|
-
const { url, method = "POST", body, fields = {}, headers = {}, meta: extraMeta = {} } = params;
|
|
214
|
-
delete extraMeta.status;
|
|
215
|
-
if (!url) {
|
|
216
|
-
fileWithMeta.meta.status = "error_upload_params";
|
|
217
|
-
this.handleChangeStatus(fileWithMeta);
|
|
218
|
-
this.forceUpdate();
|
|
219
|
-
return;
|
|
220
|
-
}
|
|
221
|
-
const xhr = new XMLHttpRequest();
|
|
222
|
-
const formData = new FormData();
|
|
223
|
-
xhr.open(method, url, true);
|
|
224
|
-
for (const field of Object.keys(fields)) formData.append(field, fields[field]);
|
|
225
|
-
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
|
|
226
|
-
for (const header of Object.keys(headers)) xhr.setRequestHeader(header, headers[header]);
|
|
227
|
-
fileWithMeta.meta = { ...fileWithMeta.meta, ...extraMeta };
|
|
228
|
-
xhr.upload.addEventListener("progress", (e) => {
|
|
229
|
-
fileWithMeta.meta.percent = e.loaded * 100 / e.total || 100;
|
|
230
|
-
this.forceUpdate();
|
|
231
|
-
});
|
|
232
|
-
xhr.addEventListener("readystatechange", () => {
|
|
233
|
-
if (xhr.readyState !== 2 && xhr.readyState !== 4) return;
|
|
234
|
-
if (xhr.status === 0 && fileWithMeta.meta.status !== "aborted") {
|
|
235
|
-
fileWithMeta.meta.status = "exception_upload";
|
|
236
|
-
this.handleChangeStatus(fileWithMeta);
|
|
237
|
-
this.forceUpdate();
|
|
238
|
-
}
|
|
239
|
-
if (xhr.status > 0 && xhr.status < 400) {
|
|
240
|
-
fileWithMeta.meta.percent = 100;
|
|
241
|
-
if (xhr.readyState === 2) fileWithMeta.meta.status = "headers_received";
|
|
242
|
-
if (xhr.readyState === 4) fileWithMeta.meta.status = "done";
|
|
243
|
-
this.handleChangeStatus(fileWithMeta);
|
|
244
|
-
this.forceUpdate();
|
|
245
|
-
}
|
|
246
|
-
if (xhr.status >= 400 && fileWithMeta.meta.status !== "error_upload") {
|
|
247
|
-
fileWithMeta.meta.status = "error_upload";
|
|
248
|
-
this.handleChangeStatus(fileWithMeta);
|
|
249
|
-
this.forceUpdate();
|
|
250
|
-
}
|
|
251
|
-
});
|
|
252
|
-
formData.append("file", fileWithMeta.file);
|
|
253
|
-
if (this.props.timeout) xhr.timeout = this.props.timeout;
|
|
254
|
-
xhr.send(body || formData);
|
|
255
|
-
fileWithMeta.xhr = xhr;
|
|
256
|
-
fileWithMeta.meta.status = "uploading";
|
|
257
|
-
this.handleChangeStatus(fileWithMeta);
|
|
258
|
-
this.forceUpdate();
|
|
259
|
-
};
|
|
260
|
-
this.state = {
|
|
261
|
-
active: false,
|
|
262
|
-
dragged: []
|
|
263
|
-
};
|
|
264
|
-
this.files = [];
|
|
265
|
-
this.mounted = true;
|
|
266
|
-
this.dropzone = React.createRef();
|
|
267
|
-
}
|
|
268
|
-
componentDidMount() {
|
|
269
|
-
if (this.props.initialFiles) this.handleFiles(this.props.initialFiles);
|
|
270
|
-
}
|
|
271
|
-
componentDidUpdate(prevProps) {
|
|
272
|
-
const { initialFiles } = this.props;
|
|
273
|
-
if (prevProps.initialFiles !== initialFiles && initialFiles) this.handleFiles(initialFiles);
|
|
274
|
-
}
|
|
275
|
-
componentWillUnmount() {
|
|
276
|
-
this.mounted = false;
|
|
277
|
-
for (const fileWithMeta of this.files) this.handleCancel(fileWithMeta);
|
|
278
|
-
}
|
|
279
|
-
render() {
|
|
280
|
-
const {
|
|
281
|
-
accept,
|
|
282
|
-
multiple,
|
|
283
|
-
maxFiles,
|
|
284
|
-
minSizeBytes,
|
|
285
|
-
maxSizeBytes,
|
|
286
|
-
onSubmit,
|
|
287
|
-
getUploadParams,
|
|
288
|
-
disabled,
|
|
289
|
-
canCancel,
|
|
290
|
-
canRemove,
|
|
291
|
-
canRestart,
|
|
292
|
-
inputContent,
|
|
293
|
-
inputWithFilesContent,
|
|
294
|
-
submitButtonDisabled,
|
|
295
|
-
submitButtonContent,
|
|
296
|
-
classNames,
|
|
297
|
-
styles,
|
|
298
|
-
addClassNames,
|
|
299
|
-
InputComponent,
|
|
300
|
-
PreviewComponent,
|
|
301
|
-
SubmitButtonComponent,
|
|
302
|
-
LayoutComponent
|
|
303
|
-
} = this.props;
|
|
304
|
-
const { active, dragged } = this.state;
|
|
305
|
-
const reject = dragged.some((file) => file.type !== "application/x-moz-file" && !accepts(file, accept));
|
|
306
|
-
const extra = { active, reject, dragged, accept, multiple, minSizeBytes, maxSizeBytes, maxFiles };
|
|
307
|
-
const files = [...this.files];
|
|
308
|
-
const dropzoneDisabled = resolveValue(disabled, files, extra);
|
|
309
|
-
const {
|
|
310
|
-
classNames: {
|
|
311
|
-
dropzone: dropzoneClassName,
|
|
312
|
-
dropzoneActive: dropzoneActiveClassName,
|
|
313
|
-
dropzoneReject: dropzoneRejectClassName,
|
|
314
|
-
dropzoneDisabled: dropzoneDisabledClassName,
|
|
315
|
-
input: inputClassName,
|
|
316
|
-
inputLabel: inputLabelClassName,
|
|
317
|
-
inputLabelWithFiles: inputLabelWithFilesClassName,
|
|
318
|
-
preview: previewClassName,
|
|
319
|
-
previewImage: previewImageClassName,
|
|
320
|
-
submitButtonContainer: submitButtonContainerClassName,
|
|
321
|
-
submitButton: submitButtonClassName
|
|
322
|
-
},
|
|
323
|
-
styles: {
|
|
324
|
-
dropzone: dropzoneStyle,
|
|
325
|
-
dropzoneActive: dropzoneActiveStyle,
|
|
326
|
-
dropzoneReject: dropzoneRejectStyle,
|
|
327
|
-
dropzoneDisabled: dropzoneDisabledStyle,
|
|
328
|
-
input: inputStyle,
|
|
329
|
-
inputLabel: inputLabelStyle,
|
|
330
|
-
inputLabelWithFiles: inputLabelWithFilesStyle,
|
|
331
|
-
preview: previewStyle,
|
|
332
|
-
previewImage: previewImageStyle,
|
|
333
|
-
submitButtonContainer: submitButtonContainerStyle,
|
|
334
|
-
submitButton: submitButtonStyle
|
|
335
|
-
}
|
|
336
|
-
} = mergeStyles(classNames, styles, addClassNames, files, extra);
|
|
337
|
-
const Input$1 = InputComponent || Input;
|
|
338
|
-
const Preview$1 = PreviewComponent || Preview;
|
|
339
|
-
const SubmitButton$1 = SubmitButtonComponent || SubmitButton;
|
|
340
|
-
const Layout$1 = LayoutComponent || Layout;
|
|
341
|
-
let previews = null;
|
|
342
|
-
if (PreviewComponent !== null) {
|
|
343
|
-
previews = files.map((f) => {
|
|
344
|
-
return (
|
|
345
|
-
//@ts-ignore
|
|
346
|
-
/* @__PURE__ */ jsx(
|
|
347
|
-
Preview$1,
|
|
348
|
-
{
|
|
349
|
-
className: previewClassName,
|
|
350
|
-
imageClassName: previewImageClassName,
|
|
351
|
-
style: previewStyle,
|
|
352
|
-
imageStyle: previewImageStyle,
|
|
353
|
-
fileWithMeta: f,
|
|
354
|
-
meta: { ...f.meta },
|
|
355
|
-
isUpload: Boolean(getUploadParams),
|
|
356
|
-
canCancel: resolveValue(canCancel, files, extra),
|
|
357
|
-
canRemove: resolveValue(canRemove, files, extra),
|
|
358
|
-
canRestart: resolveValue(canRestart, files, extra),
|
|
359
|
-
files,
|
|
360
|
-
extra
|
|
361
|
-
},
|
|
362
|
-
f.meta.id
|
|
363
|
-
)
|
|
364
|
-
);
|
|
365
|
-
});
|
|
366
|
-
}
|
|
367
|
-
const input = InputComponent !== null ? (
|
|
368
|
-
//@ts-ignore
|
|
369
|
-
/* @__PURE__ */ jsx(
|
|
370
|
-
Input$1,
|
|
371
|
-
{
|
|
372
|
-
className: inputClassName,
|
|
373
|
-
labelClassName: inputLabelClassName,
|
|
374
|
-
labelWithFilesClassName: inputLabelWithFilesClassName,
|
|
375
|
-
style: inputStyle,
|
|
376
|
-
labelStyle: inputLabelStyle,
|
|
377
|
-
labelWithFilesStyle: inputLabelWithFilesStyle,
|
|
378
|
-
getFilesFromEvent: this.getFilesFromEvent(),
|
|
379
|
-
accept,
|
|
380
|
-
multiple,
|
|
381
|
-
disabled: dropzoneDisabled,
|
|
382
|
-
content: resolveValue(inputContent, files, extra),
|
|
383
|
-
withFilesContent: resolveValue(inputWithFilesContent, files, extra),
|
|
384
|
-
onFiles: this.handleFiles,
|
|
385
|
-
files,
|
|
386
|
-
extra
|
|
387
|
-
}
|
|
388
|
-
)
|
|
389
|
-
) : null;
|
|
390
|
-
const submitButton = onSubmit && SubmitButtonComponent !== null ? (
|
|
391
|
-
//@ts-ignore
|
|
392
|
-
/* @__PURE__ */ jsx(
|
|
393
|
-
SubmitButton$1,
|
|
394
|
-
{
|
|
395
|
-
className: submitButtonContainerClassName,
|
|
396
|
-
buttonClassName: submitButtonClassName,
|
|
397
|
-
style: submitButtonContainerStyle,
|
|
398
|
-
buttonStyle: submitButtonStyle,
|
|
399
|
-
disabled: resolveValue(submitButtonDisabled, files, extra),
|
|
400
|
-
content: resolveValue(submitButtonContent, files, extra),
|
|
401
|
-
onSubmit: this.handleSubmit,
|
|
402
|
-
files,
|
|
403
|
-
extra
|
|
404
|
-
}
|
|
405
|
-
)
|
|
406
|
-
) : null;
|
|
407
|
-
let className = dropzoneClassName;
|
|
408
|
-
let style = dropzoneStyle;
|
|
409
|
-
if (dropzoneDisabled) {
|
|
410
|
-
className = `${className} ${dropzoneDisabledClassName}`;
|
|
411
|
-
style = { ...style || {}, ...dropzoneDisabledStyle || {} };
|
|
412
|
-
} else if (reject) {
|
|
413
|
-
className = `${className} ${dropzoneRejectClassName}`;
|
|
414
|
-
style = { ...style || {}, ...dropzoneRejectStyle || {} };
|
|
415
|
-
} else if (active) {
|
|
416
|
-
className = `${className} ${dropzoneActiveClassName}`;
|
|
417
|
-
style = { ...style || {}, ...dropzoneActiveStyle || {} };
|
|
418
|
-
}
|
|
419
|
-
return (
|
|
420
|
-
//@ts-ignore
|
|
421
|
-
/* @__PURE__ */ jsx(
|
|
422
|
-
Layout$1,
|
|
423
|
-
{
|
|
424
|
-
input,
|
|
425
|
-
previews,
|
|
426
|
-
submitButton,
|
|
427
|
-
dropzoneProps: {
|
|
428
|
-
ref: this.dropzone,
|
|
429
|
-
className,
|
|
430
|
-
style,
|
|
431
|
-
onDragEnter: this.handleDragEnter,
|
|
432
|
-
onDragOver: this.handleDragOver,
|
|
433
|
-
onDragLeave: this.handleDragLeave,
|
|
434
|
-
onDrop: dropzoneDisabled ? this.handleDropDisabled : this.handleDrop
|
|
435
|
-
},
|
|
436
|
-
files,
|
|
437
|
-
extra: {
|
|
438
|
-
...extra,
|
|
439
|
-
onFiles: this.handleFiles,
|
|
440
|
-
onCancelFile: this.handleCancel,
|
|
441
|
-
onRemoveFile: this.handleRemove,
|
|
442
|
-
onRestartFile: this.handleRestart
|
|
443
|
-
}
|
|
444
|
-
}
|
|
445
|
-
)
|
|
446
|
-
);
|
|
447
|
-
}
|
|
448
|
-
}
|
|
449
|
-
Dropzone.defaultProps = {
|
|
450
|
-
accept: "*",
|
|
451
|
-
multiple: true,
|
|
452
|
-
minSizeBytes: 0,
|
|
453
|
-
maxSizeBytes: Number.MAX_SAFE_INTEGER,
|
|
454
|
-
maxFiles: Number.MAX_SAFE_INTEGER,
|
|
455
|
-
autoUpload: true,
|
|
456
|
-
disabled: false,
|
|
457
|
-
canCancel: true,
|
|
458
|
-
canRemove: true,
|
|
459
|
-
canRestart: true,
|
|
460
|
-
inputContent: "Drag Files or Click to Browse",
|
|
461
|
-
inputWithFilesContent: "Add Files",
|
|
462
|
-
submitButtonDisabled: false,
|
|
463
|
-
submitButtonContent: "Submit",
|
|
464
|
-
classNames: {},
|
|
465
|
-
styles: {},
|
|
466
|
-
addClassNames: {}
|
|
467
|
-
};
|
|
468
|
-
Dropzone.propTypes = {
|
|
469
|
-
onChangeStatus: PropTypes.func,
|
|
470
|
-
getUploadParams: PropTypes.func,
|
|
471
|
-
onSubmit: PropTypes.func,
|
|
472
|
-
getFilesFromEvent: PropTypes.func,
|
|
473
|
-
getDataTransferItemsFromEvent: PropTypes.func,
|
|
474
|
-
accept: PropTypes.string,
|
|
475
|
-
multiple: PropTypes.bool,
|
|
476
|
-
minSizeBytes: PropTypes.number.isRequired,
|
|
477
|
-
maxSizeBytes: PropTypes.number.isRequired,
|
|
478
|
-
maxFiles: PropTypes.number.isRequired,
|
|
479
|
-
validate: PropTypes.func,
|
|
480
|
-
autoUpload: PropTypes.bool,
|
|
481
|
-
timeout: PropTypes.number,
|
|
482
|
-
initialFiles: PropTypes.arrayOf(PropTypes.any),
|
|
483
|
-
/* component customization */
|
|
484
|
-
disabled: PropTypes.oneOfType([PropTypes.bool, PropTypes.func]),
|
|
485
|
-
canCancel: PropTypes.oneOfType([PropTypes.bool, PropTypes.func]),
|
|
486
|
-
canRemove: PropTypes.oneOfType([PropTypes.bool, PropTypes.func]),
|
|
487
|
-
canRestart: PropTypes.oneOfType([PropTypes.bool, PropTypes.func]),
|
|
488
|
-
inputContent: PropTypes.oneOfType([PropTypes.node, PropTypes.func]),
|
|
489
|
-
inputWithFilesContent: PropTypes.oneOfType([PropTypes.node, PropTypes.func]),
|
|
490
|
-
submitButtonDisabled: PropTypes.oneOfType([PropTypes.bool, PropTypes.func]),
|
|
491
|
-
submitButtonContent: PropTypes.oneOfType([PropTypes.node, PropTypes.func]),
|
|
492
|
-
classNames: PropTypes.object.isRequired,
|
|
493
|
-
styles: PropTypes.object.isRequired,
|
|
494
|
-
addClassNames: PropTypes.object.isRequired,
|
|
495
|
-
/* component injection */
|
|
496
|
-
InputComponent: PropTypes.func,
|
|
497
|
-
PreviewComponent: PropTypes.func,
|
|
498
|
-
SubmitButtonComponent: PropTypes.func,
|
|
499
|
-
LayoutComponent: PropTypes.func
|
|
500
|
-
};
|
|
501
|
-
export {
|
|
502
|
-
Dropzone,
|
|
503
|
-
Input,
|
|
504
|
-
Layout,
|
|
505
|
-
Preview,
|
|
506
|
-
SubmitButton,
|
|
507
|
-
accepts,
|
|
508
|
-
Dropzone as default,
|
|
509
|
-
defaultClassNames,
|
|
510
|
-
formatBytes,
|
|
511
|
-
formatDuration,
|
|
512
|
-
getFilesFromEvent
|
|
513
|
-
};
|
|
514
|
-
//# sourceMappingURL=index.js.map
|
|
1
|
+
import { jsx } from "react/jsx-runtime";
|
|
2
|
+
import React from "react";
|
|
3
|
+
import PropTypes from "prop-types";
|
|
4
|
+
import Layout from "./Layout.js";
|
|
5
|
+
import Input from "./Input.js";
|
|
6
|
+
import Preview from "./Preview.js";
|
|
7
|
+
import SubmitButton from "./SubmitButton.js";
|
|
8
|
+
import { getFilesFromEvent, accepts, resolveValue, mergeStyles } from "./utils.js";
|
|
9
|
+
import { defaultClassNames, formatBytes, formatDuration } from "./utils.js";
|
|
10
|
+
class Dropzone extends React.Component {
|
|
11
|
+
constructor(props) {
|
|
12
|
+
super(props);
|
|
13
|
+
this.forceUpdate = () => {
|
|
14
|
+
if (this.mounted) super.forceUpdate();
|
|
15
|
+
};
|
|
16
|
+
this.getFilesFromEvent = () => {
|
|
17
|
+
return this.props.getFilesFromEvent || getFilesFromEvent;
|
|
18
|
+
};
|
|
19
|
+
this.getDataTransferItemsFromEvent = () => {
|
|
20
|
+
return this.props.getDataTransferItemsFromEvent || getFilesFromEvent;
|
|
21
|
+
};
|
|
22
|
+
this.handleDragEnter = async (e) => {
|
|
23
|
+
e.preventDefault();
|
|
24
|
+
e.stopPropagation();
|
|
25
|
+
const dragged = await this.getDataTransferItemsFromEvent()(e);
|
|
26
|
+
this.setState({ active: true, dragged });
|
|
27
|
+
};
|
|
28
|
+
this.handleDragOver = async (e) => {
|
|
29
|
+
e.preventDefault();
|
|
30
|
+
e.stopPropagation();
|
|
31
|
+
clearTimeout(this.dragTimeoutId);
|
|
32
|
+
const dragged = await this.getDataTransferItemsFromEvent()(e);
|
|
33
|
+
this.setState({ active: true, dragged });
|
|
34
|
+
};
|
|
35
|
+
this.handleDragLeave = (e) => {
|
|
36
|
+
e.preventDefault();
|
|
37
|
+
e.stopPropagation();
|
|
38
|
+
this.dragTimeoutId = window.setTimeout(() => this.setState({ active: false, dragged: [] }), 150);
|
|
39
|
+
};
|
|
40
|
+
this.handleDrop = async (e) => {
|
|
41
|
+
e.preventDefault();
|
|
42
|
+
e.stopPropagation();
|
|
43
|
+
this.setState({ active: false, dragged: [] });
|
|
44
|
+
const files = await this.getFilesFromEvent()(e);
|
|
45
|
+
this.handleFiles(files);
|
|
46
|
+
};
|
|
47
|
+
this.handleDropDisabled = (e) => {
|
|
48
|
+
e.preventDefault();
|
|
49
|
+
e.stopPropagation();
|
|
50
|
+
this.setState({ active: false, dragged: [] });
|
|
51
|
+
};
|
|
52
|
+
this.handleChangeStatus = (fileWithMeta) => {
|
|
53
|
+
if (!this.props.onChangeStatus) return;
|
|
54
|
+
const { meta = {} } = this.props.onChangeStatus(fileWithMeta, fileWithMeta.meta.status, this.files) || {};
|
|
55
|
+
if (meta) {
|
|
56
|
+
delete meta.status;
|
|
57
|
+
fileWithMeta.meta = { ...fileWithMeta.meta, ...meta };
|
|
58
|
+
this.forceUpdate();
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
this.handleSubmit = (files) => {
|
|
62
|
+
if (this.props.onSubmit) this.props.onSubmit(files, [...this.files]);
|
|
63
|
+
};
|
|
64
|
+
this.handleCancel = (fileWithMeta) => {
|
|
65
|
+
if (fileWithMeta.meta.status !== "uploading") return;
|
|
66
|
+
fileWithMeta.meta.status = "aborted";
|
|
67
|
+
if (fileWithMeta.xhr) fileWithMeta.xhr.abort();
|
|
68
|
+
this.handleChangeStatus(fileWithMeta);
|
|
69
|
+
this.forceUpdate();
|
|
70
|
+
};
|
|
71
|
+
this.handleRemove = (fileWithMeta) => {
|
|
72
|
+
const index = this.files.findIndex((f) => f === fileWithMeta);
|
|
73
|
+
if (index !== -1) {
|
|
74
|
+
URL.revokeObjectURL(fileWithMeta.meta.previewUrl || "");
|
|
75
|
+
fileWithMeta.meta.status = "removed";
|
|
76
|
+
this.handleChangeStatus(fileWithMeta);
|
|
77
|
+
this.files.splice(index, 1);
|
|
78
|
+
this.forceUpdate();
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
this.handleRestart = (fileWithMeta) => {
|
|
82
|
+
if (!this.props.getUploadParams) return;
|
|
83
|
+
if (fileWithMeta.meta.status === "ready") fileWithMeta.meta.status = "started";
|
|
84
|
+
else fileWithMeta.meta.status = "restarted";
|
|
85
|
+
this.handleChangeStatus(fileWithMeta);
|
|
86
|
+
fileWithMeta.meta.status = "getting_upload_params";
|
|
87
|
+
fileWithMeta.meta.percent = 0;
|
|
88
|
+
this.handleChangeStatus(fileWithMeta);
|
|
89
|
+
this.forceUpdate();
|
|
90
|
+
this.uploadFile(fileWithMeta);
|
|
91
|
+
};
|
|
92
|
+
this.handleFiles = (files) => {
|
|
93
|
+
files.forEach((f, i) => this.handleFile(f, `${(/* @__PURE__ */ new Date()).getTime()}-${i}`));
|
|
94
|
+
const { current } = this.dropzone;
|
|
95
|
+
if (current) setTimeout(() => current.scroll({ top: current.scrollHeight, behavior: "smooth" }), 150);
|
|
96
|
+
};
|
|
97
|
+
this.handleFile = async (file, id) => {
|
|
98
|
+
const { name, size, type, lastModified } = file;
|
|
99
|
+
const { minSizeBytes, maxSizeBytes, maxFiles, accept, getUploadParams, autoUpload, validate } = this.props;
|
|
100
|
+
const uploadedDate = (/* @__PURE__ */ new Date()).toISOString();
|
|
101
|
+
const lastModifiedDate = lastModified && new Date(lastModified).toISOString();
|
|
102
|
+
const fileWithMeta = {
|
|
103
|
+
file,
|
|
104
|
+
meta: { name, size, type, lastModifiedDate, uploadedDate, percent: 0, id }
|
|
105
|
+
};
|
|
106
|
+
if (file.type !== "application/x-moz-file" && !accepts(file, accept)) {
|
|
107
|
+
fileWithMeta.meta.status = "rejected_file_type";
|
|
108
|
+
this.handleChangeStatus(fileWithMeta);
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
if (this.files.length >= maxFiles) {
|
|
112
|
+
fileWithMeta.meta.status = "rejected_max_files";
|
|
113
|
+
this.handleChangeStatus(fileWithMeta);
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
fileWithMeta.cancel = () => this.handleCancel(fileWithMeta);
|
|
117
|
+
fileWithMeta.remove = () => this.handleRemove(fileWithMeta);
|
|
118
|
+
fileWithMeta.restart = () => this.handleRestart(fileWithMeta);
|
|
119
|
+
fileWithMeta.meta.status = "preparing";
|
|
120
|
+
this.files.push(fileWithMeta);
|
|
121
|
+
this.handleChangeStatus(fileWithMeta);
|
|
122
|
+
this.forceUpdate();
|
|
123
|
+
if (size < minSizeBytes || size > maxSizeBytes) {
|
|
124
|
+
fileWithMeta.meta.status = "error_file_size";
|
|
125
|
+
this.handleChangeStatus(fileWithMeta);
|
|
126
|
+
this.forceUpdate();
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
await this.generatePreview(fileWithMeta);
|
|
130
|
+
if (validate) {
|
|
131
|
+
const error = validate(fileWithMeta);
|
|
132
|
+
if (error) {
|
|
133
|
+
fileWithMeta.meta.status = "error_validation";
|
|
134
|
+
fileWithMeta.meta.validationError = error;
|
|
135
|
+
this.handleChangeStatus(fileWithMeta);
|
|
136
|
+
this.forceUpdate();
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
if (getUploadParams) {
|
|
141
|
+
if (autoUpload) {
|
|
142
|
+
this.uploadFile(fileWithMeta);
|
|
143
|
+
fileWithMeta.meta.status = "getting_upload_params";
|
|
144
|
+
} else {
|
|
145
|
+
fileWithMeta.meta.status = "ready";
|
|
146
|
+
}
|
|
147
|
+
} else {
|
|
148
|
+
fileWithMeta.meta.status = "done";
|
|
149
|
+
}
|
|
150
|
+
this.handleChangeStatus(fileWithMeta);
|
|
151
|
+
this.forceUpdate();
|
|
152
|
+
};
|
|
153
|
+
this.generatePreview = async (fileWithMeta) => {
|
|
154
|
+
const {
|
|
155
|
+
meta: { type },
|
|
156
|
+
file
|
|
157
|
+
} = fileWithMeta;
|
|
158
|
+
const isImage = type.startsWith("image/");
|
|
159
|
+
const isAudio = type.startsWith("audio/");
|
|
160
|
+
const isVideo = type.startsWith("video/");
|
|
161
|
+
if (!isImage && !isAudio && !isVideo) return;
|
|
162
|
+
const objectUrl = URL.createObjectURL(file);
|
|
163
|
+
const fileCallbackToPromise = (fileObj) => {
|
|
164
|
+
return Promise.race([
|
|
165
|
+
new Promise((resolve) => {
|
|
166
|
+
if (fileObj instanceof HTMLImageElement) fileObj.onload = resolve;
|
|
167
|
+
else fileObj.onloadedmetadata = resolve;
|
|
168
|
+
}),
|
|
169
|
+
new Promise((_, reject) => {
|
|
170
|
+
setTimeout(reject, 1e3);
|
|
171
|
+
})
|
|
172
|
+
]);
|
|
173
|
+
};
|
|
174
|
+
try {
|
|
175
|
+
if (isImage) {
|
|
176
|
+
const img = new Image();
|
|
177
|
+
img.src = objectUrl;
|
|
178
|
+
fileWithMeta.meta.previewUrl = objectUrl;
|
|
179
|
+
await fileCallbackToPromise(img);
|
|
180
|
+
fileWithMeta.meta.width = img.width;
|
|
181
|
+
fileWithMeta.meta.height = img.height;
|
|
182
|
+
}
|
|
183
|
+
if (isAudio) {
|
|
184
|
+
const audio = new Audio();
|
|
185
|
+
audio.src = objectUrl;
|
|
186
|
+
await fileCallbackToPromise(audio);
|
|
187
|
+
fileWithMeta.meta.duration = audio.duration;
|
|
188
|
+
}
|
|
189
|
+
if (isVideo) {
|
|
190
|
+
const video = document.createElement("video");
|
|
191
|
+
video.src = objectUrl;
|
|
192
|
+
await fileCallbackToPromise(video);
|
|
193
|
+
fileWithMeta.meta.duration = video.duration;
|
|
194
|
+
fileWithMeta.meta.videoWidth = video.videoWidth;
|
|
195
|
+
fileWithMeta.meta.videoHeight = video.videoHeight;
|
|
196
|
+
}
|
|
197
|
+
if (!isImage) URL.revokeObjectURL(objectUrl);
|
|
198
|
+
} catch (e) {
|
|
199
|
+
URL.revokeObjectURL(objectUrl);
|
|
200
|
+
}
|
|
201
|
+
this.forceUpdate();
|
|
202
|
+
};
|
|
203
|
+
this.uploadFile = async (fileWithMeta) => {
|
|
204
|
+
const { getUploadParams } = this.props;
|
|
205
|
+
if (!getUploadParams) return;
|
|
206
|
+
let params = null;
|
|
207
|
+
try {
|
|
208
|
+
params = await getUploadParams(fileWithMeta);
|
|
209
|
+
} catch (e) {
|
|
210
|
+
console.error("Error Upload Params", e.stack);
|
|
211
|
+
}
|
|
212
|
+
if (params === null) return;
|
|
213
|
+
const { url, method = "POST", body, fields = {}, headers = {}, meta: extraMeta = {} } = params;
|
|
214
|
+
delete extraMeta.status;
|
|
215
|
+
if (!url) {
|
|
216
|
+
fileWithMeta.meta.status = "error_upload_params";
|
|
217
|
+
this.handleChangeStatus(fileWithMeta);
|
|
218
|
+
this.forceUpdate();
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
const xhr = new XMLHttpRequest();
|
|
222
|
+
const formData = new FormData();
|
|
223
|
+
xhr.open(method, url, true);
|
|
224
|
+
for (const field of Object.keys(fields)) formData.append(field, fields[field]);
|
|
225
|
+
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
|
|
226
|
+
for (const header of Object.keys(headers)) xhr.setRequestHeader(header, headers[header]);
|
|
227
|
+
fileWithMeta.meta = { ...fileWithMeta.meta, ...extraMeta };
|
|
228
|
+
xhr.upload.addEventListener("progress", (e) => {
|
|
229
|
+
fileWithMeta.meta.percent = e.loaded * 100 / e.total || 100;
|
|
230
|
+
this.forceUpdate();
|
|
231
|
+
});
|
|
232
|
+
xhr.addEventListener("readystatechange", () => {
|
|
233
|
+
if (xhr.readyState !== 2 && xhr.readyState !== 4) return;
|
|
234
|
+
if (xhr.status === 0 && fileWithMeta.meta.status !== "aborted") {
|
|
235
|
+
fileWithMeta.meta.status = "exception_upload";
|
|
236
|
+
this.handleChangeStatus(fileWithMeta);
|
|
237
|
+
this.forceUpdate();
|
|
238
|
+
}
|
|
239
|
+
if (xhr.status > 0 && xhr.status < 400) {
|
|
240
|
+
fileWithMeta.meta.percent = 100;
|
|
241
|
+
if (xhr.readyState === 2) fileWithMeta.meta.status = "headers_received";
|
|
242
|
+
if (xhr.readyState === 4) fileWithMeta.meta.status = "done";
|
|
243
|
+
this.handleChangeStatus(fileWithMeta);
|
|
244
|
+
this.forceUpdate();
|
|
245
|
+
}
|
|
246
|
+
if (xhr.status >= 400 && fileWithMeta.meta.status !== "error_upload") {
|
|
247
|
+
fileWithMeta.meta.status = "error_upload";
|
|
248
|
+
this.handleChangeStatus(fileWithMeta);
|
|
249
|
+
this.forceUpdate();
|
|
250
|
+
}
|
|
251
|
+
});
|
|
252
|
+
formData.append("file", fileWithMeta.file);
|
|
253
|
+
if (this.props.timeout) xhr.timeout = this.props.timeout;
|
|
254
|
+
xhr.send(body || formData);
|
|
255
|
+
fileWithMeta.xhr = xhr;
|
|
256
|
+
fileWithMeta.meta.status = "uploading";
|
|
257
|
+
this.handleChangeStatus(fileWithMeta);
|
|
258
|
+
this.forceUpdate();
|
|
259
|
+
};
|
|
260
|
+
this.state = {
|
|
261
|
+
active: false,
|
|
262
|
+
dragged: []
|
|
263
|
+
};
|
|
264
|
+
this.files = [];
|
|
265
|
+
this.mounted = true;
|
|
266
|
+
this.dropzone = React.createRef();
|
|
267
|
+
}
|
|
268
|
+
componentDidMount() {
|
|
269
|
+
if (this.props.initialFiles) this.handleFiles(this.props.initialFiles);
|
|
270
|
+
}
|
|
271
|
+
componentDidUpdate(prevProps) {
|
|
272
|
+
const { initialFiles } = this.props;
|
|
273
|
+
if (prevProps.initialFiles !== initialFiles && initialFiles) this.handleFiles(initialFiles);
|
|
274
|
+
}
|
|
275
|
+
componentWillUnmount() {
|
|
276
|
+
this.mounted = false;
|
|
277
|
+
for (const fileWithMeta of this.files) this.handleCancel(fileWithMeta);
|
|
278
|
+
}
|
|
279
|
+
render() {
|
|
280
|
+
const {
|
|
281
|
+
accept,
|
|
282
|
+
multiple,
|
|
283
|
+
maxFiles,
|
|
284
|
+
minSizeBytes,
|
|
285
|
+
maxSizeBytes,
|
|
286
|
+
onSubmit,
|
|
287
|
+
getUploadParams,
|
|
288
|
+
disabled,
|
|
289
|
+
canCancel,
|
|
290
|
+
canRemove,
|
|
291
|
+
canRestart,
|
|
292
|
+
inputContent,
|
|
293
|
+
inputWithFilesContent,
|
|
294
|
+
submitButtonDisabled,
|
|
295
|
+
submitButtonContent,
|
|
296
|
+
classNames,
|
|
297
|
+
styles,
|
|
298
|
+
addClassNames,
|
|
299
|
+
InputComponent,
|
|
300
|
+
PreviewComponent,
|
|
301
|
+
SubmitButtonComponent,
|
|
302
|
+
LayoutComponent
|
|
303
|
+
} = this.props;
|
|
304
|
+
const { active, dragged } = this.state;
|
|
305
|
+
const reject = dragged.some((file) => file.type !== "application/x-moz-file" && !accepts(file, accept));
|
|
306
|
+
const extra = { active, reject, dragged, accept, multiple, minSizeBytes, maxSizeBytes, maxFiles };
|
|
307
|
+
const files = [...this.files];
|
|
308
|
+
const dropzoneDisabled = resolveValue(disabled, files, extra);
|
|
309
|
+
const {
|
|
310
|
+
classNames: {
|
|
311
|
+
dropzone: dropzoneClassName,
|
|
312
|
+
dropzoneActive: dropzoneActiveClassName,
|
|
313
|
+
dropzoneReject: dropzoneRejectClassName,
|
|
314
|
+
dropzoneDisabled: dropzoneDisabledClassName,
|
|
315
|
+
input: inputClassName,
|
|
316
|
+
inputLabel: inputLabelClassName,
|
|
317
|
+
inputLabelWithFiles: inputLabelWithFilesClassName,
|
|
318
|
+
preview: previewClassName,
|
|
319
|
+
previewImage: previewImageClassName,
|
|
320
|
+
submitButtonContainer: submitButtonContainerClassName,
|
|
321
|
+
submitButton: submitButtonClassName
|
|
322
|
+
},
|
|
323
|
+
styles: {
|
|
324
|
+
dropzone: dropzoneStyle,
|
|
325
|
+
dropzoneActive: dropzoneActiveStyle,
|
|
326
|
+
dropzoneReject: dropzoneRejectStyle,
|
|
327
|
+
dropzoneDisabled: dropzoneDisabledStyle,
|
|
328
|
+
input: inputStyle,
|
|
329
|
+
inputLabel: inputLabelStyle,
|
|
330
|
+
inputLabelWithFiles: inputLabelWithFilesStyle,
|
|
331
|
+
preview: previewStyle,
|
|
332
|
+
previewImage: previewImageStyle,
|
|
333
|
+
submitButtonContainer: submitButtonContainerStyle,
|
|
334
|
+
submitButton: submitButtonStyle
|
|
335
|
+
}
|
|
336
|
+
} = mergeStyles(classNames, styles, addClassNames, files, extra);
|
|
337
|
+
const Input$1 = InputComponent || Input;
|
|
338
|
+
const Preview$1 = PreviewComponent || Preview;
|
|
339
|
+
const SubmitButton$1 = SubmitButtonComponent || SubmitButton;
|
|
340
|
+
const Layout$1 = LayoutComponent || Layout;
|
|
341
|
+
let previews = null;
|
|
342
|
+
if (PreviewComponent !== null) {
|
|
343
|
+
previews = files.map((f) => {
|
|
344
|
+
return (
|
|
345
|
+
//@ts-ignore
|
|
346
|
+
/* @__PURE__ */ jsx(
|
|
347
|
+
Preview$1,
|
|
348
|
+
{
|
|
349
|
+
className: previewClassName,
|
|
350
|
+
imageClassName: previewImageClassName,
|
|
351
|
+
style: previewStyle,
|
|
352
|
+
imageStyle: previewImageStyle,
|
|
353
|
+
fileWithMeta: f,
|
|
354
|
+
meta: { ...f.meta },
|
|
355
|
+
isUpload: Boolean(getUploadParams),
|
|
356
|
+
canCancel: resolveValue(canCancel, files, extra),
|
|
357
|
+
canRemove: resolveValue(canRemove, files, extra),
|
|
358
|
+
canRestart: resolveValue(canRestart, files, extra),
|
|
359
|
+
files,
|
|
360
|
+
extra
|
|
361
|
+
},
|
|
362
|
+
f.meta.id
|
|
363
|
+
)
|
|
364
|
+
);
|
|
365
|
+
});
|
|
366
|
+
}
|
|
367
|
+
const input = InputComponent !== null ? (
|
|
368
|
+
//@ts-ignore
|
|
369
|
+
/* @__PURE__ */ jsx(
|
|
370
|
+
Input$1,
|
|
371
|
+
{
|
|
372
|
+
className: inputClassName,
|
|
373
|
+
labelClassName: inputLabelClassName,
|
|
374
|
+
labelWithFilesClassName: inputLabelWithFilesClassName,
|
|
375
|
+
style: inputStyle,
|
|
376
|
+
labelStyle: inputLabelStyle,
|
|
377
|
+
labelWithFilesStyle: inputLabelWithFilesStyle,
|
|
378
|
+
getFilesFromEvent: this.getFilesFromEvent(),
|
|
379
|
+
accept,
|
|
380
|
+
multiple,
|
|
381
|
+
disabled: dropzoneDisabled,
|
|
382
|
+
content: resolveValue(inputContent, files, extra),
|
|
383
|
+
withFilesContent: resolveValue(inputWithFilesContent, files, extra),
|
|
384
|
+
onFiles: this.handleFiles,
|
|
385
|
+
files,
|
|
386
|
+
extra
|
|
387
|
+
}
|
|
388
|
+
)
|
|
389
|
+
) : null;
|
|
390
|
+
const submitButton = onSubmit && SubmitButtonComponent !== null ? (
|
|
391
|
+
//@ts-ignore
|
|
392
|
+
/* @__PURE__ */ jsx(
|
|
393
|
+
SubmitButton$1,
|
|
394
|
+
{
|
|
395
|
+
className: submitButtonContainerClassName,
|
|
396
|
+
buttonClassName: submitButtonClassName,
|
|
397
|
+
style: submitButtonContainerStyle,
|
|
398
|
+
buttonStyle: submitButtonStyle,
|
|
399
|
+
disabled: resolveValue(submitButtonDisabled, files, extra),
|
|
400
|
+
content: resolveValue(submitButtonContent, files, extra),
|
|
401
|
+
onSubmit: this.handleSubmit,
|
|
402
|
+
files,
|
|
403
|
+
extra
|
|
404
|
+
}
|
|
405
|
+
)
|
|
406
|
+
) : null;
|
|
407
|
+
let className = dropzoneClassName;
|
|
408
|
+
let style = dropzoneStyle;
|
|
409
|
+
if (dropzoneDisabled) {
|
|
410
|
+
className = `${className} ${dropzoneDisabledClassName}`;
|
|
411
|
+
style = { ...style || {}, ...dropzoneDisabledStyle || {} };
|
|
412
|
+
} else if (reject) {
|
|
413
|
+
className = `${className} ${dropzoneRejectClassName}`;
|
|
414
|
+
style = { ...style || {}, ...dropzoneRejectStyle || {} };
|
|
415
|
+
} else if (active) {
|
|
416
|
+
className = `${className} ${dropzoneActiveClassName}`;
|
|
417
|
+
style = { ...style || {}, ...dropzoneActiveStyle || {} };
|
|
418
|
+
}
|
|
419
|
+
return (
|
|
420
|
+
//@ts-ignore
|
|
421
|
+
/* @__PURE__ */ jsx(
|
|
422
|
+
Layout$1,
|
|
423
|
+
{
|
|
424
|
+
input,
|
|
425
|
+
previews,
|
|
426
|
+
submitButton,
|
|
427
|
+
dropzoneProps: {
|
|
428
|
+
ref: this.dropzone,
|
|
429
|
+
className,
|
|
430
|
+
style,
|
|
431
|
+
onDragEnter: this.handleDragEnter,
|
|
432
|
+
onDragOver: this.handleDragOver,
|
|
433
|
+
onDragLeave: this.handleDragLeave,
|
|
434
|
+
onDrop: dropzoneDisabled ? this.handleDropDisabled : this.handleDrop
|
|
435
|
+
},
|
|
436
|
+
files,
|
|
437
|
+
extra: {
|
|
438
|
+
...extra,
|
|
439
|
+
onFiles: this.handleFiles,
|
|
440
|
+
onCancelFile: this.handleCancel,
|
|
441
|
+
onRemoveFile: this.handleRemove,
|
|
442
|
+
onRestartFile: this.handleRestart
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
)
|
|
446
|
+
);
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
Dropzone.defaultProps = {
|
|
450
|
+
accept: "*",
|
|
451
|
+
multiple: true,
|
|
452
|
+
minSizeBytes: 0,
|
|
453
|
+
maxSizeBytes: Number.MAX_SAFE_INTEGER,
|
|
454
|
+
maxFiles: Number.MAX_SAFE_INTEGER,
|
|
455
|
+
autoUpload: true,
|
|
456
|
+
disabled: false,
|
|
457
|
+
canCancel: true,
|
|
458
|
+
canRemove: true,
|
|
459
|
+
canRestart: true,
|
|
460
|
+
inputContent: "Drag Files or Click to Browse",
|
|
461
|
+
inputWithFilesContent: "Add Files",
|
|
462
|
+
submitButtonDisabled: false,
|
|
463
|
+
submitButtonContent: "Submit",
|
|
464
|
+
classNames: {},
|
|
465
|
+
styles: {},
|
|
466
|
+
addClassNames: {}
|
|
467
|
+
};
|
|
468
|
+
Dropzone.propTypes = {
|
|
469
|
+
onChangeStatus: PropTypes.func,
|
|
470
|
+
getUploadParams: PropTypes.func,
|
|
471
|
+
onSubmit: PropTypes.func,
|
|
472
|
+
getFilesFromEvent: PropTypes.func,
|
|
473
|
+
getDataTransferItemsFromEvent: PropTypes.func,
|
|
474
|
+
accept: PropTypes.string,
|
|
475
|
+
multiple: PropTypes.bool,
|
|
476
|
+
minSizeBytes: PropTypes.number.isRequired,
|
|
477
|
+
maxSizeBytes: PropTypes.number.isRequired,
|
|
478
|
+
maxFiles: PropTypes.number.isRequired,
|
|
479
|
+
validate: PropTypes.func,
|
|
480
|
+
autoUpload: PropTypes.bool,
|
|
481
|
+
timeout: PropTypes.number,
|
|
482
|
+
initialFiles: PropTypes.arrayOf(PropTypes.any),
|
|
483
|
+
/* component customization */
|
|
484
|
+
disabled: PropTypes.oneOfType([PropTypes.bool, PropTypes.func]),
|
|
485
|
+
canCancel: PropTypes.oneOfType([PropTypes.bool, PropTypes.func]),
|
|
486
|
+
canRemove: PropTypes.oneOfType([PropTypes.bool, PropTypes.func]),
|
|
487
|
+
canRestart: PropTypes.oneOfType([PropTypes.bool, PropTypes.func]),
|
|
488
|
+
inputContent: PropTypes.oneOfType([PropTypes.node, PropTypes.func]),
|
|
489
|
+
inputWithFilesContent: PropTypes.oneOfType([PropTypes.node, PropTypes.func]),
|
|
490
|
+
submitButtonDisabled: PropTypes.oneOfType([PropTypes.bool, PropTypes.func]),
|
|
491
|
+
submitButtonContent: PropTypes.oneOfType([PropTypes.node, PropTypes.func]),
|
|
492
|
+
classNames: PropTypes.object.isRequired,
|
|
493
|
+
styles: PropTypes.object.isRequired,
|
|
494
|
+
addClassNames: PropTypes.object.isRequired,
|
|
495
|
+
/* component injection */
|
|
496
|
+
InputComponent: PropTypes.func,
|
|
497
|
+
PreviewComponent: PropTypes.func,
|
|
498
|
+
SubmitButtonComponent: PropTypes.func,
|
|
499
|
+
LayoutComponent: PropTypes.func
|
|
500
|
+
};
|
|
501
|
+
export {
|
|
502
|
+
Dropzone,
|
|
503
|
+
Input,
|
|
504
|
+
Layout,
|
|
505
|
+
Preview,
|
|
506
|
+
SubmitButton,
|
|
507
|
+
accepts,
|
|
508
|
+
Dropzone as default,
|
|
509
|
+
defaultClassNames,
|
|
510
|
+
formatBytes,
|
|
511
|
+
formatDuration,
|
|
512
|
+
getFilesFromEvent
|
|
513
|
+
};
|
|
514
|
+
//# sourceMappingURL=index.js.map
|