Upload.js
11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
"use strict";
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var React = _interopRequireWildcard(require("react"));
var _rcUpload = _interopRequireDefault(require("rc-upload"));
var _classnames = _interopRequireDefault(require("classnames"));
var _Dragger = _interopRequireDefault(require("./Dragger"));
var _UploadList = _interopRequireDefault(require("./UploadList"));
var _utils = require("./utils");
var _LocaleReceiver = _interopRequireDefault(require("../locale-provider/LocaleReceiver"));
var _default2 = _interopRequireDefault(require("../locale/default"));
var _configProvider = require("../config-provider");
var _devWarning = _interopRequireDefault(require("../_util/devWarning"));
var _useForceUpdate = _interopRequireDefault(require("../_util/hooks/useForceUpdate"));
var _useFreshState3 = _interopRequireDefault(require("./useFreshState"));
var InternalUpload = function InternalUpload(props, ref) {
var _classNames2;
var fileListProp = props.fileList,
defaultFileList = props.defaultFileList,
onRemove = props.onRemove,
showUploadList = props.showUploadList,
listType = props.listType,
onPreview = props.onPreview,
onDownload = props.onDownload,
previewFile = props.previewFile,
disabled = props.disabled,
propLocale = props.locale,
iconRender = props.iconRender,
isImageUrl = props.isImageUrl,
progress = props.progress,
customizePrefixCls = props.prefixCls,
className = props.className,
type = props.type,
children = props.children,
style = props.style,
itemRender = props.itemRender;
var _React$useState = React.useState('drop'),
_React$useState2 = (0, _slicedToArray2["default"])(_React$useState, 2),
dragState = _React$useState2[0],
setDragState = _React$useState2[1];
var forceUpdate = (0, _useForceUpdate["default"])(); // Refresh always use fresh data
var _useFreshState = (0, _useFreshState3["default"])(fileListProp || defaultFileList || [], fileListProp),
_useFreshState2 = (0, _slicedToArray2["default"])(_useFreshState, 2),
getFileList = _useFreshState2[0],
setFileList = _useFreshState2[1];
var upload = React.useRef();
React.useEffect(function () {
(0, _devWarning["default"])('fileList' in props || !('value' in props), 'Upload', '`value` is not a valid prop, do you mean `fileList`?');
}, []);
var onChange = function onChange(info) {
setFileList(info.fileList);
var onChangeProp = props.onChange;
if (onChangeProp) {
onChangeProp((0, _extends2["default"])((0, _extends2["default"])({}, info), {
fileList: (0, _toConsumableArray2["default"])(info.fileList)
}));
}
};
var onStart = function onStart(file) {
var targetItem = (0, _utils.fileToObject)(file);
targetItem.status = 'uploading';
var nextFileList = getFileList().concat();
var fileIndex = nextFileList.findIndex(function (_ref) {
var uid = _ref.uid;
return uid === targetItem.uid;
});
if (fileIndex === -1) {
nextFileList.push(targetItem);
} else {
nextFileList[fileIndex] = targetItem;
}
onChange({
file: targetItem,
fileList: nextFileList
});
};
var onSuccess = function onSuccess(response, file, xhr) {
try {
if (typeof response === 'string') {
response = JSON.parse(response);
}
} catch (e) {
/* do nothing */
}
var targetItem = (0, _utils.getFileItem)(file, getFileList()); // removed
if (!targetItem) {
return;
}
targetItem.status = 'done';
targetItem.response = response;
targetItem.xhr = xhr;
onChange({
file: (0, _extends2["default"])({}, targetItem),
fileList: getFileList().concat()
});
};
var onProgress = function onProgress(e, file) {
var targetItem = (0, _utils.getFileItem)(file, getFileList()); // removed
if (!targetItem) {
return;
}
targetItem.percent = e.percent;
onChange({
event: e,
file: (0, _extends2["default"])({}, targetItem),
fileList: getFileList().concat()
});
};
var onError = function onError(error, response, file) {
var targetItem = (0, _utils.getFileItem)(file, getFileList()); // removed
if (!targetItem) {
return;
}
targetItem.error = error;
targetItem.response = response;
targetItem.status = 'error';
onChange({
file: (0, _extends2["default"])({}, targetItem),
fileList: getFileList().concat()
});
};
var handleRemove = function handleRemove(file) {
Promise.resolve(typeof onRemove === 'function' ? onRemove(file) : onRemove).then(function (ret) {
// Prevent removing file
if (ret === false) {
return;
}
var removedFileList = (0, _utils.removeFileItem)(file, getFileList());
if (removedFileList) {
file.status = 'removed';
if (upload.current) {
upload.current.abort(file);
}
onChange({
file: file,
fileList: removedFileList
});
}
});
};
var onFileDrop = function onFileDrop(e) {
setDragState(e.type);
};
var beforeUpload = function beforeUpload(file, fileListArgs) {
var beforeUploadProp = props.beforeUpload;
if (!beforeUploadProp) {
return true;
}
var result = beforeUploadProp(file, fileListArgs);
if (result === false) {
// Get unique file list
var uniqueList = [];
getFileList().concat(fileListArgs.map(_utils.fileToObject)).forEach(function (f) {
if (uniqueList.every(function (uf) {
return uf.uid !== f.uid;
})) {
uniqueList.push(f);
}
});
onChange({
file: file,
fileList: uniqueList
});
return false;
}
if (result && result.then) {
return result;
}
return true;
}; // Test needs
React.useImperativeHandle(ref, function () {
return {
onStart: onStart,
onSuccess: onSuccess,
onProgress: onProgress,
onError: onError,
fileList: getFileList(),
upload: upload.current,
forceUpdate: forceUpdate
};
});
var _React$useContext = React.useContext(_configProvider.ConfigContext),
getPrefixCls = _React$useContext.getPrefixCls,
direction = _React$useContext.direction;
var prefixCls = getPrefixCls('upload', customizePrefixCls);
var rcUploadProps = (0, _extends2["default"])((0, _extends2["default"])({
onStart: onStart,
onError: onError,
onProgress: onProgress,
onSuccess: onSuccess
}, props), {
prefixCls: prefixCls,
beforeUpload: beforeUpload
});
delete rcUploadProps.className;
delete rcUploadProps.style; // Remove id to avoid open by label when trigger is hidden
// !children: https://github.com/ant-design/ant-design/issues/14298
// disabled: https://github.com/ant-design/ant-design/issues/16478
// https://github.com/ant-design/ant-design/issues/24197
if (!children || disabled) {
delete rcUploadProps.id;
}
var renderUploadList = function renderUploadList(button) {
return showUploadList ? /*#__PURE__*/React.createElement(_LocaleReceiver["default"], {
componentName: "Upload",
defaultLocale: _default2["default"].Upload
}, function (locale) {
var _ref2 = typeof showUploadList === 'boolean' ? {} : showUploadList,
showRemoveIcon = _ref2.showRemoveIcon,
showPreviewIcon = _ref2.showPreviewIcon,
showDownloadIcon = _ref2.showDownloadIcon,
removeIcon = _ref2.removeIcon,
downloadIcon = _ref2.downloadIcon;
return /*#__PURE__*/React.createElement(_UploadList["default"], {
listType: listType,
items: getFileList(true),
previewFile: previewFile,
onPreview: onPreview,
onDownload: onDownload,
onRemove: handleRemove,
showRemoveIcon: !disabled && showRemoveIcon,
showPreviewIcon: showPreviewIcon,
showDownloadIcon: showDownloadIcon,
removeIcon: removeIcon,
downloadIcon: downloadIcon,
iconRender: iconRender,
locale: (0, _extends2["default"])((0, _extends2["default"])({}, locale), propLocale),
isImageUrl: isImageUrl,
progress: progress,
appendAction: button,
itemRender: itemRender
});
}) : button;
};
if (type === 'drag') {
var _classNames;
var dragCls = (0, _classnames["default"])(prefixCls, (_classNames = {}, (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-drag"), true), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-drag-uploading"), getFileList().some(function (file) {
return file.status === 'uploading';
})), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-drag-hover"), dragState === 'dragover'), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-disabled"), disabled), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-rtl"), direction === 'rtl'), _classNames), className);
return /*#__PURE__*/React.createElement("span", null, /*#__PURE__*/React.createElement("div", {
className: dragCls,
onDrop: onFileDrop,
onDragOver: onFileDrop,
onDragLeave: onFileDrop,
style: style
}, /*#__PURE__*/React.createElement(_rcUpload["default"], (0, _extends2["default"])({}, rcUploadProps, {
ref: upload,
className: "".concat(prefixCls, "-btn")
}), /*#__PURE__*/React.createElement("div", {
className: "".concat(prefixCls, "-drag-container")
}, children))), renderUploadList());
}
var uploadButtonCls = (0, _classnames["default"])(prefixCls, (_classNames2 = {}, (0, _defineProperty2["default"])(_classNames2, "".concat(prefixCls, "-select"), true), (0, _defineProperty2["default"])(_classNames2, "".concat(prefixCls, "-select-").concat(listType), true), (0, _defineProperty2["default"])(_classNames2, "".concat(prefixCls, "-disabled"), disabled), (0, _defineProperty2["default"])(_classNames2, "".concat(prefixCls, "-rtl"), direction === 'rtl'), _classNames2));
var uploadButton = /*#__PURE__*/React.createElement("div", {
className: uploadButtonCls,
style: children ? undefined : {
display: 'none'
}
}, /*#__PURE__*/React.createElement(_rcUpload["default"], (0, _extends2["default"])({}, rcUploadProps, {
ref: upload
})));
if (listType === 'picture-card') {
return /*#__PURE__*/React.createElement("span", {
className: (0, _classnames["default"])("".concat(prefixCls, "-picture-card-wrapper"), className)
}, renderUploadList(uploadButton));
}
return /*#__PURE__*/React.createElement("span", {
className: className
}, uploadButton, renderUploadList());
};
var Upload = /*#__PURE__*/React.forwardRef(InternalUpload);
Upload.Dragger = _Dragger["default"];
Upload.displayName = 'Upload';
Upload.defaultProps = {
type: 'select',
multiple: false,
action: '',
data: {},
accept: '',
beforeUpload: _utils.T,
showUploadList: true,
listType: 'text',
className: '',
disabled: false,
supportServerRender: true
};
var _default = Upload;
exports["default"] = _default;