UNPKG

12.4 kBJavaScriptView Raw
1import _extends from "@babel/runtime/helpers/extends";
2import _defineProperty from "@babel/runtime/helpers/defineProperty";
3import _objectSpread from "@babel/runtime/helpers/objectSpread2";
4import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
5import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
6import _createClass from "@babel/runtime/helpers/createClass";
7import _inherits from "@babel/runtime/helpers/inherits";
8import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
9import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
10
11function _createSuper(Derived) {
12 function isNativeReflectConstruct() {
13 if (typeof Reflect === "undefined" || !Reflect.construct) return false;
14 if (Reflect.construct.sham) return false;
15 if (typeof Proxy === "function") return true;
16
17 try {
18 Date.prototype.toString.call(Reflect.construct(Date, [], function () {}));
19 return true;
20 } catch (e) {
21 return false;
22 }
23 }
24
25 return function () {
26 var Super = _getPrototypeOf(Derived),
27 result;
28
29 if (isNativeReflectConstruct()) {
30 var NewTarget = _getPrototypeOf(this).constructor;
31
32 result = Reflect.construct(Super, arguments, NewTarget);
33 } else {
34 result = Super.apply(this, arguments);
35 }
36
37 return _possibleConstructorReturn(this, result);
38 };
39}
40
41import React, { Component } from 'react';
42import classNames from 'classnames';
43import uniqBy from 'lodash/uniqBy';
44import LocaleReceiver from '../locale-provider/LocaleReceiver';
45import defaultLocale from '../locale-provider/default';
46import UploadList from './UploadList';
47import { fileToObject, genPercentAdd, getFileItem, removeFileItem, T } from './utils';
48import RcUpload from '../rc-components/upload';
49import { getPrefixCls } from '../configure';
50
51var Upload =
52/*#__PURE__*/
53function (_Component) {
54 _inherits(Upload, _Component);
55
56 var _super = _createSuper(Upload);
57
58 function Upload(props) {
59 var _this;
60
61 _classCallCheck(this, Upload);
62
63 _this = _super.call(this, props);
64
65 _this.onStart = function (file) {
66 var fileList = _this.state.fileList;
67
68 var nextFileList = _toConsumableArray(fileList);
69
70 var targetItem = fileToObject(file);
71 targetItem.status = 'uploading';
72 nextFileList.push(targetItem);
73
74 _this.onChange({
75 file: targetItem,
76 fileList: nextFileList
77 }); // fix ie progress
78
79
80 if (!window.FormData) {
81 _this.autoUpdateProgress(0, targetItem);
82 }
83
84 var onStart = _this.props.onStart;
85
86 if (onStart) {
87 onStart(file);
88 }
89 };
90
91 _this.onSuccess = function (response, file) {
92 _this.clearProgressTimer();
93
94 try {
95 if (typeof response === 'string') {
96 response = JSON.parse(response);
97 }
98 } catch (e) {
99 /* do nothing */
100 }
101
102 var fileList = _this.state.fileList;
103 var targetItem = getFileItem(file, fileList); // removed
104
105 if (targetItem) {
106 targetItem.status = 'done';
107 targetItem.response = response;
108
109 _this.onChange({
110 file: _objectSpread({}, targetItem),
111 fileList: fileList
112 });
113 }
114
115 var onSuccess = _this.props.onSuccess;
116
117 if (onSuccess) {
118 onSuccess(response, file);
119 }
120 };
121
122 _this.onProgress = function (e, file) {
123 var fileList = _this.state.fileList;
124 var targetItem = getFileItem(file, fileList); // removed
125
126 if (targetItem) {
127 targetItem.percent = e.percent;
128
129 _this.onChange({
130 event: e,
131 file: _objectSpread({}, targetItem),
132 fileList: fileList
133 });
134 }
135
136 var onProgress = _this.props.onProgress;
137
138 if (onProgress) {
139 onProgress(e, file);
140 }
141 };
142
143 _this.onError = function (error, response, file) {
144 _this.clearProgressTimer();
145
146 var fileList = _this.state.fileList;
147 var targetItem = getFileItem(file, fileList); // removed
148
149 if (!targetItem) {
150 return;
151 }
152
153 targetItem.error = error;
154 targetItem.response = response;
155 targetItem.status = 'error';
156
157 _this.onChange({
158 file: _objectSpread({}, targetItem),
159 fileList: fileList
160 });
161
162 var onError = _this.props.onError;
163
164 if (onError) {
165 onError(error, response, file);
166 }
167 };
168
169 _this.handleManualRemove = function (file) {
170 _this.upload.abort(file);
171
172 file.status = 'removed'; // eslint-disable-line
173
174 _this.handleRemove(file);
175 };
176 /**
177 * 拖拽触发回调
178 * @param uploadFiles 拖拽后文件列表
179 */
180
181
182 _this.onDragEnd = function (uploadFiles) {
183 var onDragEnd = _this.props.onDragEnd;
184
185 if (onDragEnd) {
186 var result = onDragEnd(uploadFiles);
187
188 if (result !== false) {
189 _this.setState({
190 fileList: uploadFiles
191 });
192 } else {
193 return false;
194 }
195 }
196
197 _this.setState({
198 fileList: uploadFiles
199 });
200 };
201
202 _this.onChange = function (info) {
203 if (!('fileList' in _this.props)) {
204 _this.setState({
205 fileList: info.fileList
206 });
207 }
208
209 var onChange = _this.props.onChange;
210
211 if (onChange) {
212 onChange(info);
213 }
214 };
215
216 _this.onFileDrop = function (e) {
217 _this.setState({
218 dragState: e.type
219 });
220 };
221
222 _this.beforeUpload = function (file, uploadFiles) {
223 var beforeUpload = _this.props.beforeUpload;
224
225 if (beforeUpload) {
226 var result = beforeUpload(file, uploadFiles);
227
228 if (result === false) {
229 var fileList = _this.state.fileList;
230
231 _this.onChange({
232 file: file,
233 fileList: uniqBy(uploadFiles.concat(fileList), function (item) {
234 return item.uid;
235 })
236 });
237
238 return false;
239 }
240
241 if (result && result.then) {
242 return result;
243 }
244 }
245
246 return true;
247 };
248
249 _this.saveUpload = function (node) {
250 _this.upload = node;
251 };
252
253 _this.renderUploadList = function (uploadLocale) {
254 var _this$props = _this.props,
255 showUploadList = _this$props.showUploadList,
256 listType = _this$props.listType,
257 onPreview = _this$props.onPreview,
258 locale = _this$props.locale,
259 previewFile = _this$props.previewFile,
260 dragUploadList = _this$props.dragUploadList;
261 var fileList = _this.state.fileList;
262 var showRemoveIcon = showUploadList.showRemoveIcon,
263 showPreviewIcon = showUploadList.showPreviewIcon;
264 return React.createElement(UploadList, {
265 listType: listType,
266 items: fileList,
267 onPreview: onPreview,
268 dragUploadList: dragUploadList,
269 onDragEnd: _this.onDragEnd,
270 previewFile: previewFile,
271 onRemove: _this.handleManualRemove,
272 showRemoveIcon: showRemoveIcon,
273 showPreviewIcon: showPreviewIcon,
274 locale: _objectSpread({}, uploadLocale, {}, locale)
275 });
276 };
277
278 _this.state = {
279 fileList: props.fileList || props.defaultFileList || [],
280 dragState: 'drop'
281 };
282 return _this;
283 }
284
285 _createClass(Upload, [{
286 key: "componentWillUnmount",
287 value: function componentWillUnmount() {
288 this.clearProgressTimer();
289 }
290 }, {
291 key: "autoUpdateProgress",
292 value: function autoUpdateProgress(_, file) {
293 var _this2 = this;
294
295 var getPercent = genPercentAdd();
296 var curPercent = 0;
297 this.clearProgressTimer();
298 this.progressTimer = setInterval(function () {
299 curPercent = getPercent(curPercent);
300
301 _this2.onProgress({
302 percent: curPercent
303 }, file);
304 }, 200);
305 }
306 }, {
307 key: "handleRemove",
308 value: function handleRemove(file) {
309 var _this3 = this;
310
311 var onRemove = this.props.onRemove;
312 Promise.resolve(typeof onRemove === 'function' ? onRemove(file) : onRemove).then(function (ret) {
313 // Prevent removing file
314 if (ret === false) {
315 return;
316 }
317
318 var fileList = _this3.state.fileList;
319 var removedFileList = removeFileItem(file, fileList);
320
321 if (removedFileList) {
322 _this3.onChange({
323 file: file,
324 fileList: removedFileList
325 });
326 }
327 });
328 }
329 }, {
330 key: "componentWillReceiveProps",
331 value: function componentWillReceiveProps(nextProps) {
332 if ('fileList' in nextProps) {
333 this.setState({
334 fileList: nextProps.fileList || []
335 });
336 }
337 }
338 }, {
339 key: "clearProgressTimer",
340 value: function clearProgressTimer() {
341 clearInterval(this.progressTimer);
342 }
343 }, {
344 key: "render",
345 value: function render() {
346 var _classNames2;
347
348 var _this$props2 = this.props,
349 customizePrefixCls = _this$props2.prefixCls,
350 className = _this$props2.className,
351 showUploadList = _this$props2.showUploadList,
352 listType = _this$props2.listType,
353 type = _this$props2.type,
354 disabled = _this$props2.disabled,
355 children = _this$props2.children,
356 dragUploadList = _this$props2.dragUploadList;
357 var _this$state = this.state,
358 fileList = _this$state.fileList,
359 dragState = _this$state.dragState;
360 var prefixCls = getPrefixCls('upload', customizePrefixCls);
361
362 var rcUploadProps = _objectSpread({}, this.props, {
363 onStart: this.onStart,
364 onError: this.onError,
365 onProgress: this.onProgress,
366 onSuccess: this.onSuccess,
367 beforeUpload: this.beforeUpload,
368 prefixCls: prefixCls
369 });
370
371 delete rcUploadProps.className;
372 var uploadList = showUploadList ? React.createElement(LocaleReceiver, {
373 componentName: "Upload",
374 defaultLocale: defaultLocale.Upload
375 }, this.renderUploadList) : null;
376
377 if (type === 'drag') {
378 var _classNames;
379
380 var dragCls = classNames(prefixCls, (_classNames = {}, _defineProperty(_classNames, "".concat(prefixCls, "-drag"), true), _defineProperty(_classNames, "".concat(prefixCls, "-drag-uploading"), fileList.some(function (file) {
381 return file.status === 'uploading';
382 })), _defineProperty(_classNames, "".concat(prefixCls, "-drag-hover"), dragState === 'dragover'), _defineProperty(_classNames, "".concat(prefixCls, "-disabled"), disabled), _classNames));
383 return React.createElement("span", {
384 className: className
385 }, React.createElement("div", {
386 className: dragCls,
387 onDrop: this.onFileDrop,
388 onDragOver: this.onFileDrop,
389 onDragLeave: this.onFileDrop
390 }, React.createElement(RcUpload, _extends({}, rcUploadProps, {
391 ref: this.saveUpload,
392 className: "".concat(prefixCls, "-btn")
393 }), React.createElement("div", {
394 className: "".concat(prefixCls, "-drag-container")
395 }, children))), uploadList);
396 }
397
398 var uploadButtonCls = classNames(prefixCls, (_classNames2 = {}, _defineProperty(_classNames2, "".concat(prefixCls, "-select"), true), _defineProperty(_classNames2, "".concat(prefixCls, "-select-").concat(listType), true), _defineProperty(_classNames2, "".concat(prefixCls, "-disabled"), disabled), _defineProperty(_classNames2, "".concat(prefixCls, "-drag-btn"), dragUploadList), _classNames2));
399 var uploadButton = React.createElement("div", {
400 className: uploadButtonCls,
401 style: {
402 display: children ? '' : 'none'
403 }
404 }, React.createElement(RcUpload, _extends({}, rcUploadProps, {
405 ref: this.saveUpload
406 })));
407
408 if (listType === 'picture-card') {
409 return React.createElement("span", {
410 className: className
411 }, uploadList, uploadButton);
412 }
413
414 return React.createElement("span", {
415 className: className
416 }, uploadButton, uploadList);
417 }
418 }]);
419
420 return Upload;
421}(Component);
422
423export { Upload as default };
424Upload.displayName = 'Upload';
425Upload.defaultProps = {
426 type: 'select',
427 multiple: false,
428 action: '',
429 data: {},
430 accept: '',
431 beforeUpload: T,
432 showUploadList: true,
433 listType: 'text',
434 className: '',
435 disabled: false,
436 supportServerRender: true
437};
438//# sourceMappingURL=Upload.js.map