UNPKG

acud

Version:

acg react 组件库

265 lines • 9.84 kB
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray"; import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray"; import _extends from "@babel/runtime/helpers/esm/extends"; /* * upload 上传 * author zhousheng */ import * as React from 'react'; import CSSMotion, { CSSMotionList } from 'rc-motion'; import classNames from 'classnames'; import LoadingOutlined from "acud-icon/es/icons/OutlinedLoading"; import PaperClipOutlined from "acud-icon/es/icons/OutlinedFileDoc"; import OutlinedFileImage from "acud-icon/es/icons/OutlinedFileImage"; import OutlinedFileVideo from "acud-icon/es/icons/OutlinedFileVideo"; import OutlinedFileMusic from "acud-icon/es/icons/OutlinedFileMusic"; import OutlinedFileZip from "acud-icon/es/icons/OutlinedFileZip"; import OutlinedPhoto from "acud-icon/es/icons/OutlinedPhoto"; import FileTwoTone from "acud-icon/es/icons/OutlinedFile"; import { cloneElement, isValidElement } from '../../_util/reactNode'; import { previewImage, isImageUrl, getFiletypeByName } from '../utils'; import collapseMotion from '../../_util/motion'; import { ConfigContext } from '../../config-provider'; import Button from '../../button'; import useForceUpdate from '../../_util/hooks/useForceUpdate'; import ListItem from './ListItem'; var listItemMotion = _extends({}, collapseMotion); delete listItemMotion.onAppearEnd; delete listItemMotion.onEnterEnd; delete listItemMotion.onLeaveEnd; var renderIcon = function renderIcon(fileName) { var type = getFiletypeByName(fileName); var Icon = PaperClipOutlined; switch (type) { case 'image': Icon = OutlinedFileImage; break; case 'video': Icon = OutlinedFileVideo; break; case 'audio': Icon = OutlinedFileMusic; break; case 'zip': Icon = OutlinedFileZip; break; default: break; } return /*#__PURE__*/React.createElement(Icon, { width: 16 }); }; var InternalUploadList = function InternalUploadList(_ref, ref) { var listType = _ref.listType, previewFile = _ref.previewFile, onPreview = _ref.onPreview, onDownload = _ref.onDownload, onRemove = _ref.onRemove, onUpload = _ref.onUpload, locale = _ref.locale, iconRender = _ref.iconRender, isImgUrl = _ref.isImageUrl, customizePrefixCls = _ref.prefixCls, _ref$items = _ref.items, items = _ref$items === void 0 ? [] : _ref$items, showPreviewIcon = _ref.showPreviewIcon, showUploadIcon = _ref.showUploadIcon, showRemoveIcon = _ref.showRemoveIcon, showDownloadIcon = _ref.showDownloadIcon, removeIcon = _ref.removeIcon, downloadIcon = _ref.downloadIcon, uploadIcon = _ref.uploadIcon, progress = _ref.progress, _ref$motionDeadline = _ref.motionDeadline, motionDeadline = _ref$motionDeadline === void 0 ? 2000 : _ref$motionDeadline, appendAction = _ref.appendAction, itemRender = _ref.itemRender; var forceUpdate = useForceUpdate(); var _React$useState = React.useState(false), _React$useState2 = _slicedToArray(_React$useState, 2), motionAppear = _React$useState2[0], setMotionAppear = _React$useState2[1]; var _React$useContext = React.useContext(ConfigContext), getPrefixCls = _React$useContext.getPrefixCls, direction = _React$useContext.direction; // ============================= Render ============================= var prefixCls = getPrefixCls('upload', customizePrefixCls); // ============================= Effect ============================= React.useEffect(function () { if (listType !== 'picture' && listType !== 'picture-card') { return; } (items || []).forEach(function (file) { if (typeof document === 'undefined' || typeof window === 'undefined' || !window.FileReader || !window.File || !(file.originFileObj instanceof File || file.originFileObj instanceof Blob) || file.thumbUrl !== undefined) { return; } file.thumbUrl = ''; if (previewFile) { previewFile(file.originFileObj).then(function (previewDataUrl) { // Need append '' to avoid dead loop file.thumbUrl = previewDataUrl || ''; forceUpdate(); }); } }); }, [listType, items, previewFile]); React.useEffect(function () { setMotionAppear(true); }, []); // ============================= Events ============================= var onInternalPreview = function onInternalPreview(file, e) { if (!onPreview) { return; } e === null || e === void 0 ? void 0 : e.preventDefault(); return onPreview(file); }; var onInternalDownload = function onInternalDownload(file) { if (typeof onDownload === 'function') { onDownload(file); } else if (file.url) { window.open(file.url); } }; var onInternalClose = function onInternalClose(file) { onRemove === null || onRemove === void 0 ? void 0 : onRemove(file); }; var onInternalUpload = function onInternalUpload(file) { onUpload === null || onUpload === void 0 ? void 0 : onUpload(file); }; var internalIconRender = function internalIconRender(file) { if (iconRender) { return iconRender(file, listType); } var isLoading = file.status === 'uploading'; var fileIcon = isImgUrl && isImgUrl(file) ? /*#__PURE__*/React.createElement(OutlinedPhoto, { width: 24 }) : /*#__PURE__*/React.createElement(FileTwoTone, { width: 24 }); var icon = renderIcon(file.name || file.fileName); if (listType === 'picture') { icon = isLoading ? /*#__PURE__*/React.createElement(LoadingOutlined, { animation: "spin" }) : fileIcon; } else if (listType === 'picture-card') { icon = isLoading ? "".concat(locale.uploading, "...") : fileIcon; } return icon; }; var actionIconRender = function actionIconRender(customIcon, callback, prefixCls, title) { var btnProps = { type: 'text', size: 'small', title: title, onClick: function onClick(e) { callback(); if (isValidElement(customIcon) && customIcon.props.onClick) { customIcon.props.onClick(e); } }, className: "".concat(prefixCls, "-list-item-card-actions-btn") }; if (isValidElement(customIcon)) { var btnIcon = cloneElement(customIcon, _extends(_extends({}, customIcon.props), { onClick: function onClick() {} })); return /*#__PURE__*/React.createElement(Button, _extends({}, btnProps, { icon: btnIcon })); } return /*#__PURE__*/React.createElement(Button, btnProps, /*#__PURE__*/React.createElement("span", null, customIcon)); }; // ============================== Ref =============================== // Test needs React.useImperativeHandle(ref, function () { return { handlePreview: onInternalPreview, handleDownload: onInternalDownload }; }); // >>> Motion config var motionKeyList = _toConsumableArray(items.map(function (file) { return { key: file.uid, file: file }; })); var animationDirection = listType === 'picture-card' ? 'animate-inline' : 'animate'; // const transitionName = list.length === 0 ? '' : `${prefixCls}-${animationDirection}`; var motionConfig = { motionDeadline: motionDeadline, motionName: "".concat(prefixCls, "-").concat(animationDirection), keys: motionKeyList, motionAppear: motionAppear }; if (listType !== 'picture-card') { motionConfig = _extends(_extends({}, listItemMotion), motionConfig); } var listClassNames = classNames(_defineProperty(_defineProperty(_defineProperty({}, "".concat(prefixCls, "-list"), true), "".concat(prefixCls, "-list-").concat(listType), true), "".concat(prefixCls, "-list-rtl"), direction === 'rtl')); return /*#__PURE__*/React.createElement("div", { className: listClassNames }, appendAction && /*#__PURE__*/React.createElement(CSSMotion, motionConfig, function (_ref2) { var motionClassName = _ref2.className, motionStyle = _ref2.style; return cloneElement(appendAction, function (oriProps) { return { className: classNames(oriProps.className, motionClassName), style: _extends(_extends({}, motionStyle), oriProps.style) }; }); }), /*#__PURE__*/React.createElement(CSSMotionList, _extends({}, motionConfig, { component: false }), function (_ref3) { var key = _ref3.key, file = _ref3.file, motionClassName = _ref3.className, motionStyle = _ref3.style; return /*#__PURE__*/React.createElement(ListItem, { key: key, locale: locale, prefixCls: prefixCls, className: motionClassName, style: motionStyle, file: file, items: items, progress: progress, listType: listType, isImgUrl: isImgUrl, showPreviewIcon: showPreviewIcon, showUploadIcon: showUploadIcon, showRemoveIcon: showRemoveIcon, showDownloadIcon: showDownloadIcon, removeIcon: removeIcon, downloadIcon: downloadIcon, uploadIcon: uploadIcon, iconRender: internalIconRender, actionIconRender: actionIconRender, itemRender: itemRender, onPreview: onInternalPreview, onDownload: onInternalDownload, onClose: onInternalClose, onUpload: onInternalUpload, motionDeadline: motionDeadline }); })); }; var UploadList = /*#__PURE__*/React.forwardRef(InternalUploadList); UploadList.displayName = 'UploadList'; UploadList.defaultProps = { listType: 'text', progress: { // strokeWidth: 4, // showInfo: false }, showUploadIcon: true, motionDeadline: 2000, showRemoveIcon: true, showDownloadIcon: false, showPreviewIcon: true, previewFile: previewImage, isImageUrl: isImageUrl }; export default UploadList;