UNPKG

choerodon-ui

Version:

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

439 lines (361 loc) 12.4 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 _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn"; import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf"; function _createSuper(Derived) { function isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } } return function () { var Super = _getPrototypeOf(Derived), result; if (isNativeReflectConstruct()) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } 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 { getPrefixCls } from '../configure'; 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(uploadFiles.concat(fileList), 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, locale = _this$props.locale, previewFile = _this$props.previewFile, dragUploadList = _this$props.dragUploadList; var fileList = _this.state.fileList; var showRemoveIcon = showUploadList.showRemoveIcon, showPreviewIcon = showUploadList.showPreviewIcon; return React.createElement(UploadList, { listType: listType, items: fileList, onPreview: onPreview, dragUploadList: dragUploadList, onDragEnd: _this.onDragEnd, previewFile: previewFile, onRemove: _this.handleManualRemove, showRemoveIcon: showRemoveIcon, showPreviewIcon: showPreviewIcon, locale: _objectSpread({}, uploadLocale, {}, locale) }); }; _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 }, 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: "render", value: function render() { var _classNames2; var _this$props2 = this.props, customizePrefixCls = _this$props2.prefixCls, 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; var _this$state = this.state, fileList = _this$state.fileList, dragState = _this$state.dragState; var prefixCls = getPrefixCls('upload', customizePrefixCls); var rcUploadProps = _objectSpread({}, this.props, { onStart: this.onStart, onError: this.onError, onProgress: this.onProgress, onSuccess: this.onSuccess, beforeUpload: this.beforeUpload, prefixCls: prefixCls }); delete rcUploadProps.className; var uploadList = showUploadList ? 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 React.createElement("span", { className: className }, React.createElement("div", { className: dragCls, onDrop: this.onFileDrop, onDragOver: this.onFileDrop, onDragLeave: this.onFileDrop }, React.createElement(RcUpload, _extends({}, rcUploadProps, { ref: this.saveUpload, className: "".concat(prefixCls, "-btn") }), 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 = React.createElement("div", { className: uploadButtonCls, style: { display: children ? '' : 'none' } }, React.createElement(RcUpload, _extends({}, rcUploadProps, { ref: this.saveUpload }))); if (listType === 'picture-card') { return React.createElement("span", { className: className }, uploadList, uploadButton); } return React.createElement("span", { className: className }, uploadButton, uploadList); } }]); 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 }; //# sourceMappingURL=Upload.js.map