UNPKG

choerodon-ui

Version:

An enterprise-class UI design language and React-based implementation

447 lines (374 loc) 13.5 kB
import _extends from "@babel/runtime/helpers/extends"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; import _objectSpread from "@babel/runtime/helpers/objectSpread2"; import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray"; import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _inherits from "@babel/runtime/helpers/inherits"; import _createSuper from "@babel/runtime/helpers/createSuper"; import React, { Component } from 'react'; import classNames from 'classnames'; import uniqBy from 'lodash/uniqBy'; import LocaleReceiver from '../locale-provider/LocaleReceiver'; import defaultLocale from '../locale-provider/default'; import UploadList from './UploadList'; import { fileToObject, genPercentAdd, getFileItem, removeFileItem, T } from './utils'; import RcUpload from '../rc-components/upload'; import ConfigContext from '../config-provider/ConfigContext'; var Upload = /*#__PURE__*/function (_Component) { _inherits(Upload, _Component); var _super = _createSuper(Upload); function Upload(props) { var _this; _classCallCheck(this, Upload); _this = _super.call(this, props); _this.onStart = function (file) { var fileList = _this.state.fileList; var nextFileList = _toConsumableArray(fileList); var targetItem = fileToObject(file); targetItem.status = 'uploading'; nextFileList.push(targetItem); _this.onChange({ file: targetItem, fileList: nextFileList }); // fix ie progress if (!window.FormData) { _this.autoUpdateProgress(0, targetItem); } var onStart = _this.props.onStart; if (onStart) { onStart(file); } }; _this.onSuccess = function (response, file) { _this.clearProgressTimer(); try { if (typeof response === 'string') { response = JSON.parse(response); } } catch (e) { /* do nothing */ } var fileList = _this.state.fileList; var targetItem = getFileItem(file, fileList); // removed if (targetItem) { targetItem.status = 'done'; targetItem.response = response; _this.onChange({ file: _objectSpread({}, targetItem), fileList: fileList }); } var onSuccess = _this.props.onSuccess; if (onSuccess) { onSuccess(response, file); } }; _this.onProgress = function (e, file) { var fileList = _this.state.fileList; var targetItem = getFileItem(file, fileList); // removed if (targetItem) { targetItem.percent = e.percent; _this.onChange({ event: e, file: _objectSpread({}, targetItem), fileList: fileList }); } var onProgress = _this.props.onProgress; if (onProgress) { onProgress(e, file); } }; _this.onError = function (error, response, file) { _this.clearProgressTimer(); var fileList = _this.state.fileList; var targetItem = getFileItem(file, fileList); // removed if (!targetItem) { return; } targetItem.error = error; targetItem.response = response; targetItem.status = 'error'; _this.onChange({ file: _objectSpread({}, targetItem), fileList: fileList }); var onError = _this.props.onError; if (onError) { onError(error, response, file); } }; _this.handleManualRemove = function (file) { _this.upload.abort(file); file.status = 'removed'; // eslint-disable-line _this.handleRemove(file); }; /** * 拖拽触发回调 * @param uploadFiles 拖拽后文件列表 */ _this.onDragEnd = function (uploadFiles) { var onDragEnd = _this.props.onDragEnd; if (onDragEnd) { var result = onDragEnd(uploadFiles); if (result !== false) { _this.setState({ fileList: uploadFiles }); } else { return false; } } _this.setState({ fileList: uploadFiles }); }; _this.onChange = function (info) { if (!('fileList' in _this.props)) { _this.setState({ fileList: info.fileList }); } var onChange = _this.props.onChange; if (onChange) { onChange(info); } }; _this.onFileDrop = function (e) { _this.setState({ dragState: e.type }); }; _this.beforeUpload = function (file, uploadFiles) { var beforeUpload = _this.props.beforeUpload; if (beforeUpload) { var result = beforeUpload(file, uploadFiles); if (result === false) { var fileList = _this.state.fileList; _this.onChange({ file: file, fileList: uniqBy(fileList.concat(uploadFiles.map(fileToObject)), function (item) { return item.uid; }) }); return false; } if (result && result.then) { return result; } } return true; }; _this.saveUpload = function (node) { _this.upload = node; }; _this.renderUploadList = function (uploadLocale) { var _this$props = _this.props, showUploadList = _this$props.showUploadList, listType = _this$props.listType, onPreview = _this$props.onPreview, onReUpload = _this$props.onReUpload, downloadPropsIntercept = _this$props.downloadPropsIntercept, locale = _this$props.locale, previewFile = _this$props.previewFile, dragUploadList = _this$props.dragUploadList, showFileSize = _this$props.showFileSize, renderIcon = _this$props.renderIcon, tooltipPrefixCls = _this$props.tooltipPrefixCls, popconfirmProps = _this$props.popconfirmProps; var prefixCls = _this.getPrefixCls(); var fileList = _this.state.fileList; var showRemoveIcon = showUploadList.showRemoveIcon, removePopConfirmTitle = showUploadList.removePopConfirmTitle, showPreviewIcon = showUploadList.showPreviewIcon, showDownloadIcon = showUploadList.showDownloadIcon, showReUploadIcon = showUploadList.showReUploadIcon, reUploadText = showUploadList.reUploadText, reUploadPopConfirmTitle = showUploadList.reUploadPopConfirmTitle, getCustomFilenameTitle = showUploadList.getCustomFilenameTitle; return /*#__PURE__*/React.createElement(UploadList, { prefixCls: prefixCls, listType: listType, items: fileList, onPreview: onPreview, dragUploadList: dragUploadList, onDragEnd: _this.onDragEnd, previewFile: previewFile, onRemove: _this.handleManualRemove, showRemoveIcon: showRemoveIcon, showPreviewIcon: showPreviewIcon, showDownloadIcon: showDownloadIcon, removePopConfirmTitle: removePopConfirmTitle, showReUploadIcon: showReUploadIcon, reUploadText: reUploadText, reUploadPopConfirmTitle: reUploadPopConfirmTitle, onReUpload: onReUpload, getCustomFilenameTitle: getCustomFilenameTitle, locale: _objectSpread(_objectSpread({}, uploadLocale), locale), downloadPropsIntercept: downloadPropsIntercept, showFileSize: showFileSize, renderIcon: renderIcon, tooltipPrefixCls: tooltipPrefixCls, popconfirmProps: popconfirmProps }); }; _this.state = { fileList: props.fileList || props.defaultFileList || [], dragState: 'drop' }; return _this; } _createClass(Upload, [{ key: "componentWillUnmount", value: function componentWillUnmount() { this.clearProgressTimer(); } }, { key: "autoUpdateProgress", value: function autoUpdateProgress(_, file) { var _this2 = this; var getPercent = genPercentAdd(); var curPercent = 0; this.clearProgressTimer(); this.progressTimer = setInterval(function () { curPercent = getPercent(curPercent); _this2.onProgress({ percent: curPercent * 100 }, file); }, 200); } }, { key: "handleRemove", value: function handleRemove(file) { var _this3 = this; var onRemove = this.props.onRemove; Promise.resolve(typeof onRemove === 'function' ? onRemove(file) : onRemove).then(function (ret) { // Prevent removing file if (ret === false) { return; } var fileList = _this3.state.fileList; var removedFileList = removeFileItem(file, fileList); if (removedFileList) { _this3.onChange({ file: file, fileList: removedFileList }); } }); } }, { key: "componentWillReceiveProps", value: function componentWillReceiveProps(nextProps) { if ('fileList' in nextProps) { this.setState({ fileList: nextProps.fileList || [] }); } } }, { key: "clearProgressTimer", value: function clearProgressTimer() { clearInterval(this.progressTimer); } }, { key: "getPrefixCls", value: function getPrefixCls() { var customizePrefixCls = this.props.prefixCls; var getPrefixCls = this.context.getPrefixCls; return getPrefixCls('upload', customizePrefixCls); } }, { key: "render", value: function render() { var _classNames2; var _this$props2 = this.props, className = _this$props2.className, showUploadList = _this$props2.showUploadList, listType = _this$props2.listType, type = _this$props2.type, disabled = _this$props2.disabled, children = _this$props2.children, dragUploadList = _this$props2.dragUploadList, overwriteDefaultEvent = _this$props2.overwriteDefaultEvent; var _this$state = this.state, fileList = _this$state.fileList, dragState = _this$state.dragState; var prefixCls = this.getPrefixCls(); var rcUploadProps = _objectSpread(_objectSpread(_objectSpread({}, overwriteDefaultEvent ? undefined : this.props), {}, { onStart: this.onStart, onError: this.onError, onProgress: this.onProgress, onSuccess: this.onSuccess }, overwriteDefaultEvent ? this.props : undefined), {}, { beforeUpload: this.beforeUpload, prefixCls: prefixCls }); delete rcUploadProps.className; var uploadList = showUploadList ? /*#__PURE__*/React.createElement(LocaleReceiver, { componentName: "Upload", defaultLocale: defaultLocale.Upload }, this.renderUploadList) : null; if (type === 'drag') { var _classNames; var dragCls = classNames(prefixCls, (_classNames = {}, _defineProperty(_classNames, "".concat(prefixCls, "-drag"), true), _defineProperty(_classNames, "".concat(prefixCls, "-drag-uploading"), fileList.some(function (file) { return file.status === 'uploading'; })), _defineProperty(_classNames, "".concat(prefixCls, "-drag-hover"), dragState === 'dragover'), _defineProperty(_classNames, "".concat(prefixCls, "-disabled"), disabled), _classNames)); return /*#__PURE__*/React.createElement("span", { className: className }, /*#__PURE__*/React.createElement("div", { className: dragCls, onDrop: this.onFileDrop, onDragOver: this.onFileDrop, onDragLeave: this.onFileDrop }, /*#__PURE__*/React.createElement(RcUpload, _extends({}, rcUploadProps, { ref: this.saveUpload, className: "".concat(prefixCls, "-btn") }), /*#__PURE__*/React.createElement("div", { className: "".concat(prefixCls, "-drag-container") }, children))), uploadList); } 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)); var uploadButton = /*#__PURE__*/React.createElement("div", { className: uploadButtonCls, style: { display: children ? '' : 'none' } }, /*#__PURE__*/React.createElement(RcUpload, _extends({}, rcUploadProps, { ref: this.saveUpload }))); if (listType === 'picture-card') { return /*#__PURE__*/React.createElement("span", { className: className }, uploadList, uploadButton); } return /*#__PURE__*/React.createElement("span", { className: className }, uploadButton, uploadList); } }], [{ key: "contextType", get: function get() { return ConfigContext; } }]); return Upload; }(Component); export { Upload as default }; Upload.displayName = 'Upload'; Upload.defaultProps = { type: 'select', multiple: false, action: '', data: {}, accept: '', beforeUpload: T, showUploadList: true, listType: 'text', className: '', disabled: false, supportServerRender: true, showFileSize: false }; //# sourceMappingURL=Upload.js.map