UNPKG

acud

Version:

acg react 组件库

223 lines • 10.1 kB
import _extends from "@babel/runtime/helpers/esm/extends"; import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray"; /* * upload 上传 * author zhousheng */ import * as React from 'react'; import CSSMotion from 'rc-motion'; import classNames from 'classnames'; import EyeOutlined from "acud-icon/es/icons/OutlinedEye"; import DeleteOutlined from "acud-icon/es/icons/OutlinedDelete"; import OutlinedClose from "acud-icon/es/icons/OutlinedClose"; import DownloadOutlined from "acud-icon/es/icons/OutlinedCloudDownload"; import OutlinedButtonUpload from "acud-icon/es/icons/OutlinedButtonUpload"; import Tooltip from '../../tooltip'; import Progress from '../../progress'; import { ConfigContext } from '../../config-provider'; var ListItem = /*#__PURE__*/React.forwardRef(function (_ref, ref) { var prefixCls = _ref.prefixCls, className = _ref.className, style = _ref.style, locale = _ref.locale, listType = _ref.listType, file = _ref.file, items = _ref.items, progressProps = _ref.progress, iconRender = _ref.iconRender, actionIconRender = _ref.actionIconRender, itemRender = _ref.itemRender, isImgUrl = _ref.isImgUrl, _ref$motionDeadline = _ref.motionDeadline, motionDeadline = _ref$motionDeadline === void 0 ? 2000 : _ref$motionDeadline, showPreviewIcon = _ref.showPreviewIcon, showRemoveIcon = _ref.showRemoveIcon, showDownloadIcon = _ref.showDownloadIcon, internalShowUploadIcon = _ref.showUploadIcon, customRemoveIcon = _ref.removeIcon, customUploadIcon = _ref.uploadIcon, customDownloadIcon = _ref.downloadIcon, onPreview = _ref.onPreview, onDownload = _ref.onDownload, onClose = _ref.onClose, onUpload = _ref.onUpload; var _a, _b; var status = file.status; var _React$useState = React.useState(status), _React$useState2 = _slicedToArray(_React$useState, 2), mergedStatus = _React$useState2[0], setMergedStatus = _React$useState2[1]; React.useEffect(function () { if (status !== 'removed') { setMergedStatus(status); } }, [status]); // Delay to show the progress bar var _React$useState3 = React.useState(false), _React$useState4 = _slicedToArray(_React$useState3, 2), showProgress = _React$useState4[0], setShowProgress = _React$useState4[1]; // 外部传入的file 无法重传,根据originFileObj来判断是本地选中的 var showUploadIcon = React.useMemo(function () { return !!(file === null || file === void 0 ? void 0 : file.originFileObj) && internalShowUploadIcon; }, [file, internalShowUploadIcon]); React.useEffect(function () { var timer = setTimeout(function () { setShowProgress(true); }, 300); return function () { window.clearTimeout(timer); }; }, []); // This is used for legacy span make scrollHeight the wrong value. // We will force these to be `display: block` with non `picture-card` var spanClassName = "".concat(prefixCls, "-span"); var iconNode = iconRender(file); var icon = /*#__PURE__*/React.createElement("div", { className: "".concat(prefixCls, "-text-icon") }, iconNode); if (listType === 'picture' || listType === 'picture-card') { if (mergedStatus === 'uploading' || !file.thumbUrl && !file.url) { var uploadingClassName = classNames(_defineProperty(_defineProperty({}, "".concat(prefixCls, "-list-item-thumbnail"), true), "".concat(prefixCls, "-list-item-file"), mergedStatus !== 'uploading')); icon = /*#__PURE__*/React.createElement("div", { className: uploadingClassName }, iconNode); } else { var thumbnail = (isImgUrl === null || isImgUrl === void 0 ? void 0 : isImgUrl(file)) ? /*#__PURE__*/React.createElement("img", { src: file.thumbUrl || file.url, alt: file.name, className: "".concat(prefixCls, "-list-item-image") }) : iconNode; var aClassName = classNames(_defineProperty(_defineProperty({}, "".concat(prefixCls, "-list-item-thumbnail"), true), "".concat(prefixCls, "-list-item-file"), isImgUrl && !isImgUrl(file))); icon = /*#__PURE__*/React.createElement("a", { className: aClassName, onClick: function onClick(e) { return onPreview(file, e); }, href: file.url || file.thumbUrl, target: "_blank", rel: "noopener noreferrer" }, thumbnail); } } var infoUploadingClass = classNames(_defineProperty(_defineProperty(_defineProperty({}, "".concat(prefixCls, "-list-item"), true), "".concat(prefixCls, "-list-item-").concat(mergedStatus), true), "".concat(prefixCls, "-list-item-list-type-").concat(listType), true)); var linkProps = typeof file.linkProps === 'string' ? JSON.parse(file.linkProps) : file.linkProps; var removeIcon = showRemoveIcon ? actionIconRender((typeof customRemoveIcon === 'function' ? customRemoveIcon(file) : customRemoveIcon) || (listType === 'text' ? /*#__PURE__*/React.createElement(OutlinedClose, { width: 16 }) : /*#__PURE__*/React.createElement(DeleteOutlined, { width: 16 })), function () { return onClose(file); }, prefixCls, locale.removeFile) : null; var downloadIcon = showDownloadIcon ? actionIconRender((typeof customDownloadIcon === 'function' ? customDownloadIcon(file) : customDownloadIcon) || /*#__PURE__*/React.createElement(DownloadOutlined, null), function () { return onDownload(file); }, prefixCls, locale.downloadFile) : null; // 下载icon var downloadOrDelete = listType !== 'picture-card' && /*#__PURE__*/React.createElement("span", { key: "download-delete", className: classNames("".concat(prefixCls, "-list-item-card-actions"), { picture: listType === 'picture' }) }, downloadIcon, removeIcon); var listItemNameClass = classNames("".concat(prefixCls, "-list-item-name")); var preview = file.url ? [/*#__PURE__*/React.createElement("a", _extends({ key: "view", target: "_blank", rel: "noopener noreferrer", className: listItemNameClass, title: file.name }, linkProps, { href: file.url, onClick: function onClick(e) { return onPreview(file, e); } }), file.name), downloadOrDelete] : [/*#__PURE__*/React.createElement("span", { key: "view", className: listItemNameClass, onClick: function onClick(e) { return onPreview(file, e); }, title: file.name }, file.name), downloadOrDelete]; var previewStyle = { pointerEvents: 'none', opacity: 0.5 }; // 预览icon var previewIcon = showPreviewIcon ? /*#__PURE__*/React.createElement("a", { href: file.url || file.thumbUrl, target: "_blank", rel: "noopener noreferrer", style: file.url || file.thumbUrl ? undefined : previewStyle, onClick: function onClick(e) { return onPreview(file, e); }, title: locale.previewFile }, /*#__PURE__*/React.createElement(EyeOutlined, null)) : null; // 上传icon var uploadIcon = showUploadIcon ? actionIconRender((typeof customUploadIcon === 'function' ? customUploadIcon(file) : customUploadIcon) || /*#__PURE__*/React.createElement(OutlinedButtonUpload, null), function () { return onUpload(file); }, prefixCls, locale.uploadFile) : null; var actions = listType === 'picture-card' && mergedStatus !== 'uploading' && /*#__PURE__*/React.createElement("span", { className: "".concat(prefixCls, "-list-item-actions") }, mergedStatus === 'done' && previewIcon, mergedStatus === 'done' && downloadIcon, ['error', 'done'].includes(mergedStatus) && uploadIcon, removeIcon); var message; if (file.response && typeof file.response === 'string') { message = file.response; } else { message = ((_a = file.error) === null || _a === void 0 ? void 0 : _a.statusText) || ((_b = file.error) === null || _b === void 0 ? void 0 : _b.message) || locale.uploadError; } var iconAndPreview = /*#__PURE__*/React.createElement("span", { className: spanClassName }, icon, preview); var _React$useContext = React.useContext(ConfigContext), getPrefixCls = _React$useContext.getPrefixCls; var rootPrefixCls = getPrefixCls(); var dom = /*#__PURE__*/React.createElement("div", { className: infoUploadingClass }, /*#__PURE__*/React.createElement("div", { className: "".concat(prefixCls, "-list-item-info") }, iconAndPreview), actions, showProgress && /*#__PURE__*/React.createElement(CSSMotion, { motionName: "".concat(rootPrefixCls, "-fade"), visible: mergedStatus === 'uploading' // rc-motion 锁了版本2.4.3,但是新版本(2.7.3)已经fix,所以采用key // https://github.com/react-component/motion/pull/24 , key: mergedStatus, motionDeadline: motionDeadline }, function (_ref2) { var motionClassName = _ref2.className; // show loading icon if upload progress listener is disabled var loadingProgress = 'percent' in file ? /*#__PURE__*/React.createElement(Progress, _extends({ trailColor: "#DADBDD", strokeColor: "#2468F2", infoType: "none", type: "line", percent: file.percent, strokeWidth: listType === 'text' ? 2 : 4 }, progressProps)) : null; return /*#__PURE__*/React.createElement("div", { className: classNames("".concat(prefixCls, "-list-item-progress"), motionClassName) }, loadingProgress); })); var listContainerNameClass = classNames("".concat(prefixCls, "-list-").concat(listType, "-container"), className); var item = mergedStatus === 'error' ? /*#__PURE__*/React.createElement(Tooltip, { title: message, overlayClassName: "error-tooltip", getPopupContainer: function getPopupContainer(node) { return node.parentNode; } }, dom) : dom; return /*#__PURE__*/React.createElement("div", { className: listContainerNameClass, style: style, ref: ref }, itemRender ? itemRender(item, file, items, { download: onDownload.bind(null, file), preview: onPreview.bind(null, file), upload: onUpload.bind(null, file), remove: onClose.bind(null, file) }) : item); }); export default ListItem;