UNPKG

choerodon-ui

Version:

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

530 lines (447 loc) 16.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 { __decorate } from "tslib"; import React, { Component } from 'react'; import classNames from 'classnames'; import uniqBy from 'lodash/uniqBy'; import isUndefined from 'lodash/isUndefined'; import autobind from '../../pro/es/_util/autobind'; 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'; var filterItem = getFileItem(file, nextFileList); if (!filterItem) { nextFileList.push(targetItem); } else { filterItem.status = 'uploading'; } _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.slice() }); } 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) { var onChange = _this.props.onChange; if (!('fileList' in _this.props) || !onChange) { _this.setState({ fileList: info.fileList }); } if (onChange) { onChange(info); } }; _this.onFileDrop = function (e) { _this.setState({ dragState: e.type }); }; _this.beforeUpload = function (file, uploadFiles) { var _this$props = _this.props, multiple = _this$props.multiple, beforeUpload = _this$props.beforeUpload; if (!multiple) { var nowFileList = _this.state.fileList; if (nowFileList.length !== 1) { nowFileList.forEach(function (eachFile) { // 错误态的重新上传,不用执行删除操作,此时 uid 相同 if (eachFile.uid !== file.uid) { _this.handleManualRemove(eachFile); } }); } _this.onChange({ file: file, fileList: uploadFiles }); } if (beforeUpload) { var result = beforeUpload(file, uploadFiles); var rejectCall = function rejectCall() { var fileList = _this.state.fileList; _this.onChange({ file: file, fileList: uniqBy(fileList.concat(uploadFiles.map(fileToObject)), function (item) { return item.uid; }) }); }; if (result === false) { rejectCall(); return false; } if (result && result.then) { return result.then(function (re) { if (re === false) { rejectCall(); } return re; }); } } return true; }; _this.saveUpload = function (node) { _this.upload = node; }; _this.getUpload = function () { return _this.upload; }; _this.setReplaceReuploadItem = function (file) { _this.setState({ originReuploadItem: file }); }; _this.renderUploadList = function (uploadLocale) { var getConfig = _this.context.getConfig; var _this$props2 = _this.props, showUploadList = _this$props2.showUploadList, listType = _this$props2.listType, onPreview = _this$props2.onPreview, _this$props2$onReUplo = _this$props2.onReUpload, onReUpload = _this$props2$onReUplo === void 0 ? _this.defaultReUpload : _this$props2$onReUplo, downloadPropsIntercept = _this$props2.downloadPropsIntercept, locale = _this$props2.locale, previewFile = _this$props2.previewFile, dragUploadList = _this$props2.dragUploadList, showFileSize = _this$props2.showFileSize, renderIcon = _this$props2.renderIcon, tooltipPrefixCls = _this$props2.tooltipPrefixCls, popconfirmProps = _this$props2.popconfirmProps; var prefixCls = _this.getPrefixCls(); var fileList = _this.state.fileList; var showRemoveIcon = showUploadList.showRemoveIcon, removePopConfirmTitle = showUploadList.removePopConfirmTitle, showPreviewIcon = showUploadList.showPreviewIcon, showDownloadIcon = showUploadList.showDownloadIcon, _showUploadList$showR = showUploadList.showReUploadIcon, showReUploadIcon = _showUploadList$showR === void 0 ? getConfig('uploadShowReUploadIcon') : _showUploadList$showR, reUploadText = showUploadList.reUploadText, reUploadPopConfirmTitle = showUploadList.reUploadPopConfirmTitle, getCustomFilenameTitle = showUploadList.getCustomFilenameTitle; var defaultShowPreviewIcon; var defaultShowDownloadIcon; if (['text', 'picture'].includes(listType)) { defaultShowPreviewIcon = isUndefined(showPreviewIcon) ? false : showPreviewIcon; defaultShowDownloadIcon = isUndefined(showDownloadIcon) ? false : showDownloadIcon; } else if (listType === 'picture-card') { defaultShowPreviewIcon = isUndefined(showPreviewIcon) ? true : showPreviewIcon; defaultShowDownloadIcon = isUndefined(showDownloadIcon) ? true : showDownloadIcon; } 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: defaultShowPreviewIcon, showDownloadIcon: defaultShowDownloadIcon, 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, getUploadRef: _this.getUpload, setReplaceReuploadItem: _this.setReplaceReuploadItem }); }; _this.state = { fileList: props.fileList || props.defaultFileList || [], dragState: 'drop', originReuploadItem: null }; 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: "defaultReUpload", value: function defaultReUpload(file) { if (this.upload && this.upload.uploader) { this.upload.uploader.upload(file, [file]); } } }, { 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$props3 = this.props, className = _this$props3.className, showUploadList = _this$props3.showUploadList, listType = _this$props3.listType, type = _this$props3.type, disabled = _this$props3.disabled, children = _this$props3.children, dragUploadList = _this$props3.dragUploadList, overwriteDefaultEvent = _this$props3.overwriteDefaultEvent, beforeUploadFiles = _this$props3.beforeUploadFiles, _this$props3$onReUplo = _this$props3.onReUpload, onReUpload = _this$props3$onReUplo === void 0 ? this.defaultReUpload : _this$props3$onReUplo; var _this$state = this.state, fileList = _this$state.fileList, dragState = _this$state.dragState, originReuploadItem = _this$state.originReuploadItem; 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, onReUpload: onReUpload }, overwriteDefaultEvent ? this.props : undefined), {}, { beforeUpload: this.beforeUpload, beforeUploadFiles: beforeUploadFiles, prefixCls: prefixCls, fileList: fileList, originReuploadItem: originReuploadItem, setReplaceReuploadItem: this.setReplaceReuploadItem }); 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: true, action: '', data: {}, accept: '', beforeUpload: T, showUploadList: true, listType: 'text', className: '', disabled: false, supportServerRender: true, showFileSize: false }; __decorate([autobind], Upload.prototype, "defaultReUpload", null); //# sourceMappingURL=Upload.js.map