choerodon-ui
Version:
An enterprise-class UI design language and React-based implementation
467 lines (411 loc) • 17.7 kB
JavaScript
import _objectSpread from "@babel/runtime/helpers/objectSpread2";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _extends from "@babel/runtime/helpers/extends";
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 _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import React, { Component, createElement } from 'react';
import classNames from 'classnames';
import { DragDropContext, Draggable, Droppable } from 'react-beautiful-dnd';
import isFunction from 'lodash/isFunction';
import Icon from '../icon';
import Tooltip from '../tooltip';
import Progress from '../progress';
import Animate from '../animate';
import PopConfirm from '../popconfirm';
import { ProgressType } from '../progress/enum';
import { getFileSizeStr, getFileType, isImageUrl, previewImage } from './utils';
import CompressedfileIcon from './icon-svg/compressedfileIcon';
import DocIcon from './icon-svg/docIcon';
import FileuploadIcon from './icon-svg/fileuploadIcon';
import ImageIcon from './icon-svg/imageIcon';
import PdfIcon from './icon-svg/pdfIcon';
import XlsIcon from './icon-svg/xlsIcon';
import { Size } from '../_util/enum';
import ConfigContext from '../config-provider/ConfigContext'; // a little function to help us with reordering the result
var reorder = function reorder(list, startIndex, endIndex) {
var result = Array.from(list);
var _result$splice = result.splice(startIndex, 1),
_result$splice2 = _slicedToArray(_result$splice, 1),
removed = _result$splice2[0];
result.splice(endIndex, 0, removed);
return result;
};
function defaultRenderIcon(file, listType, prefixCls) {
if (listType === 'picture' || listType === 'picture-card') {
return /*#__PURE__*/React.createElement(Icon, {
type: "insert_drive_file",
className: "".concat(prefixCls, "-list-item-icon")
});
}
if (file.status === 'uploading') {
return /*#__PURE__*/React.createElement(Progress, {
key: 'loading',
type: ProgressType.loading,
size: Size.small
});
}
var filetype = getFileType(file.name);
switch (filetype) {
case 'compressedfile':
return /*#__PURE__*/React.createElement(CompressedfileIcon, {
className: "".concat(prefixCls, "-icon-file")
});
case 'doc':
return /*#__PURE__*/React.createElement(DocIcon, {
className: "".concat(prefixCls, "-icon-file")
});
case 'image':
return /*#__PURE__*/React.createElement(ImageIcon, {
className: "".concat(prefixCls, "-icon-file")
});
case 'pdf':
return /*#__PURE__*/React.createElement(PdfIcon, {
className: "".concat(prefixCls, "-icon-file")
});
case 'xls':
return /*#__PURE__*/React.createElement(XlsIcon, {
className: "".concat(prefixCls, "-icon-file")
});
default:
return /*#__PURE__*/React.createElement(FileuploadIcon, {
className: "".concat(prefixCls, "-icon-file")
});
}
}
var UploadList = /*#__PURE__*/function (_Component) {
_inherits(UploadList, _Component);
var _super = _createSuper(UploadList);
function UploadList() {
var _this;
_classCallCheck(this, UploadList);
_this = _super.apply(this, arguments);
_this.handleClose = function (file) {
var onRemove = _this.props.onRemove;
if (onRemove) {
onRemove(file);
}
};
_this.handlePreview = function (file, e) {
var onPreview = _this.props.onPreview;
if (!onPreview) {
return;
}
e.preventDefault();
return onPreview(file);
};
/**
* @param {UploadFile} file
* @param {React.SyntheticEvent<HTMLElement>} e
*/
_this.handleReUpload = function (file, e) {
var onReUpload = _this.props.onReUpload;
if (!onReUpload) {
return;
}
e.preventDefault();
return onReUpload(file);
};
/**
* 拖拽事件
* @param result
*/
_this.onDragEnd = function (result) {
var _this$props = _this.props,
items = _this$props.items,
onDragEnd = _this$props.onDragEnd; // dropped outside the list
if (!result.destination) {
return;
}
var dragItems = reorder(items, result.source.index, result.destination.index);
onDragEnd(dragItems);
};
return _this;
}
_createClass(UploadList, [{
key: "componentDidUpdate",
value: function componentDidUpdate() {
var _this2 = this;
var listType = this.props.listType;
if (listType !== 'picture' && listType !== 'picture-card') {
return;
}
var _this$props2 = this.props,
items = _this$props2.items,
previewFile = _this$props2.previewFile;
(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 || '';
_this2.forceUpdate();
});
}
});
}
}, {
key: "render",
value: function render() {
var _this3 = this,
_classNames2;
var _this$props3 = this.props,
customizePrefixCls = _this$props3.prefixCls,
_this$props3$items = _this$props3.items,
items = _this$props3$items === void 0 ? [] : _this$props3$items,
listType = _this$props3.listType,
showPreviewIcon = _this$props3.showPreviewIcon,
showRemoveIcon = _this$props3.showRemoveIcon,
showDownloadIcon = _this$props3.showDownloadIcon,
showReUploadIcon = _this$props3.showReUploadIcon,
removePopConfirmTitle = _this$props3.removePopConfirmTitle,
reUploadText = _this$props3.reUploadText,
reUploadPopConfirmTitle = _this$props3.reUploadPopConfirmTitle,
locale = _this$props3.locale,
dragUploadList = _this$props3.dragUploadList,
showFileSize = _this$props3.showFileSize,
getCustomFilenameTitle = _this$props3.getCustomFilenameTitle,
downloadPropsIntercept = _this$props3.downloadPropsIntercept,
tooltipPrefixCls = _this$props3.tooltipPrefixCls,
popconfirmProps = _this$props3.popconfirmProps,
_this$props3$renderIc = _this$props3.renderIcon,
renderIcon = _this$props3$renderIc === void 0 ? defaultRenderIcon : _this$props3$renderIc;
var getPrefixCls = this.context.getPrefixCls;
var prefixCls = getPrefixCls('upload', customizePrefixCls);
var list = items.map(function (file, index) {
var _classNames;
var progress;
var icon = renderIcon(file, listType, prefixCls);
var stat = {
isImg: isImageUrl(file),
isUploading: file.status === 'uploading',
isError: file.status === 'error',
isPictureCard: listType === 'picture-card'
};
if (listType === 'picture' || stat.isPictureCard) {
if (stat.isPictureCard && stat.isUploading) {
icon = /*#__PURE__*/React.createElement("div", {
className: "".concat(prefixCls, "-list-item-uploading-text")
}, locale.uploading);
} else if (listType === 'picture' && stat.isUploading) {
icon = /*#__PURE__*/React.createElement(Progress, {
key: 'loading',
type: ProgressType.loading,
size: Size.small
});
} else if (!file.thumbUrl && !file.url) {
icon = /*#__PURE__*/React.createElement(Icon, {
className: "".concat(prefixCls, "-list-item-thumbnail"),
type: "picture"
});
} else {
var thumbnail = stat.isImg ? /*#__PURE__*/React.createElement("img", {
src: file.thumbUrl || file.url,
alt: file.name
}) : icon;
icon = /*#__PURE__*/React.createElement("a", {
className: "".concat(prefixCls, "-list-item-thumbnail"),
onClick: function onClick(e) {
return _this3.handlePreview(file, e);
},
href: file.url || file.thumbUrl,
target: "_blank",
rel: "noopener noreferrer"
}, thumbnail);
}
}
if (stat.isUploading) {
var progressAttr = _this3.props.progressAttr; // show loading icon if upload progress listener is disabled
var loadingProgress = 'percent' in file ? /*#__PURE__*/React.createElement(Progress, _extends({
type: ProgressType.line
}, progressAttr, {
percent: file.percent
})) : null;
progress = /*#__PURE__*/React.createElement("div", {
className: "".concat(prefixCls, "-list-item-progress"),
key: "progress"
}, loadingProgress);
}
var infoUploadingClass = classNames((_classNames = {}, _defineProperty(_classNames, "".concat(prefixCls, "-list-item"), true), _defineProperty(_classNames, "".concat(prefixCls, "-list-item-").concat(file.status), true), _classNames));
var preview = file.url ? /*#__PURE__*/React.createElement("a", _extends({}, file.linkProps, {
href: file.url,
target: "_blank",
rel: "noopener noreferrer",
className: "".concat(prefixCls, "-list-item-name"),
onClick: function onClick(e) {
return _this3.handlePreview(file, e);
},
title: file.name
}), file.name) : /*#__PURE__*/React.createElement("span", {
className: "".concat(prefixCls, "-list-item-name"),
onClick: function onClick(e) {
return _this3.handlePreview(file, e);
},
title: file.name
}, file.name);
var style = file.url || file.thumbUrl ? undefined : {
pointerEvents: 'none',
opacity: 0.5
};
var previewIcon = isFunction(showPreviewIcon) ? showPreviewIcon(file) : stat.isImg && showPreviewIcon ? /*#__PURE__*/React.createElement("a", {
href: file.url || file.thumbUrl,
target: "_blank",
rel: "noopener noreferrer",
style: style,
onClick: function onClick(e) {
return _this3.handlePreview(file, e);
},
title: locale.previewFile
}, /*#__PURE__*/React.createElement(Icon, {
type: "visibility"
})) : null;
var reUploadIcon = isFunction(showReUploadIcon) ? showReUploadIcon(file) : showReUploadIcon ? /*#__PURE__*/React.createElement(PopConfirm, _extends({}, popconfirmProps, {
title: reUploadPopConfirmTitle || locale.confirmReUpload,
onConfirm: function onConfirm(e) {
_this3.handleReUpload(file, e);
}
}), /*#__PURE__*/React.createElement(Icon, {
type: "file_upload",
title: reUploadText
})) : null;
var removeIcon = showRemoveIcon ? /*#__PURE__*/React.createElement(PopConfirm, _extends({}, popconfirmProps, {
title: removePopConfirmTitle || locale.confirmRemove,
onConfirm: function onConfirm() {
_this3.handleClose(file);
}
}), /*#__PURE__*/React.createElement(Icon, {
type: stat.isPictureCard ? 'delete' : 'close',
className: stat.isPictureCard ? "".concat(prefixCls, "-list-item-action-remove") : undefined,
title: locale.removeFile
})) : null;
var downloadLinkProps = _objectSpread(_objectSpread({}, file.linkProps), {}, {
rel: 'noopener noreferrer'
});
if (stat.isError) {
downloadLinkProps.style = {
color: '#f5222d'
};
}
if (!(stat.isError || stat.isUploading)) {
downloadLinkProps.href = file.url || file.thumbUrl;
downloadLinkProps.target = '_blank';
if (downloadLinkProps.href) {
downloadLinkProps.filename = file.name;
}
}
var downloadIcon = !stat.isError && !stat.isUploading && (isFunction(showDownloadIcon) ? showDownloadIcon(file) : showDownloadIcon) && /*#__PURE__*/React.createElement("a", _extends({}, downloadPropsIntercept(downloadLinkProps)), /*#__PURE__*/React.createElement(Icon, {
type: "get_app"
}));
var actions = stat.isPictureCard && !stat.isUploading ? /*#__PURE__*/React.createElement("span", {
className: "".concat(prefixCls, "-list-item-actions")
}, stat.isError || previewIcon, downloadIcon, reUploadIcon, removeIcon) : removeIcon;
var message = file.response && typeof file.response === 'string' ? file.response : file.error && file.error.statusText || locale.uploadError;
var fileName = stat.isPictureCard && !stat.isUploading ? /*#__PURE__*/React.createElement(Tooltip, {
prefixCls: tooltipPrefixCls,
title: getCustomFilenameTitle ? getCustomFilenameTitle(file) : file.name,
placement: "bottom"
}, /*#__PURE__*/createElement(downloadLinkProps.href ? 'a' : 'span', _objectSpread({
className: "".concat(prefixCls, "-list-item-file-name")
}, downloadPropsIntercept(downloadLinkProps)), file.name)) : null;
var filesizeStr = getFileSizeStr(file.size);
var fileSize = showFileSize && filesizeStr !== '' && listType === 'text' ? /*#__PURE__*/React.createElement("span", {
className: "".concat(prefixCls, "-list-item-info-filesize")
}, filesizeStr) : null;
var iconAndPreview = stat.isPictureCard ? /*#__PURE__*/React.createElement("span", {
className: "".concat(prefixCls, "-list-item-picture-card")
}, icon) : /*#__PURE__*/React.createElement("span", {
className: "".concat(prefixCls, "-list-item-text")
}, icon, preview);
var iconAndPreviewTooltip = stat.isError ? /*#__PURE__*/React.createElement(Tooltip, {
prefixCls: tooltipPrefixCls,
title: message
}, iconAndPreview) : iconAndPreview;
var listItemInfo = /*#__PURE__*/React.createElement("div", {
className: "".concat(prefixCls, "-list-item-info")
}, /*#__PURE__*/React.createElement("span", {
className: "".concat(prefixCls, "-list-item-span")
}, iconAndPreviewTooltip, fileName, fileSize, actions), /*#__PURE__*/React.createElement(Animate, {
transitionName: "fade",
component: ""
}, progress));
if (dragUploadList) {
return /*#__PURE__*/React.createElement(Draggable, {
key: file.uid,
draggableId: String(file.uid),
index: index
}, function (provided) {
return /*#__PURE__*/React.createElement("div", _extends({
className: infoUploadingClass,
key: file.uid,
ref: provided.innerRef
}, provided.draggableProps, provided.dragHandleProps), listItemInfo);
});
}
return /*#__PURE__*/React.createElement("div", {
className: infoUploadingClass,
key: file.uid
}, listItemInfo);
});
var listClassNames = classNames((_classNames2 = {}, _defineProperty(_classNames2, "".concat(prefixCls, "-list"), true), _defineProperty(_classNames2, "".concat(prefixCls, "-list-").concat(listType), true), _defineProperty(_classNames2, "".concat(prefixCls, "-list-drag"), dragUploadList), _classNames2));
var animationDirection = listType === 'picture-card' ? 'animate-inline' : 'animate';
if (dragUploadList) {
return /*#__PURE__*/React.createElement(DragDropContext, {
onDragEnd: this.onDragEnd
}, /*#__PURE__*/React.createElement(Droppable, {
droppableId: "droppable",
direction: "horizontal"
}, function (provided, snapshot) {
return /*#__PURE__*/React.createElement("div", _extends({
ref: provided.innerRef,
style: {
background: snapshot.isDraggingOver ? '#f2f9f4' : '',
border: snapshot.isDraggingOver ? '2px dashed #1ab16f' : '',
display: 'inline-flex',
maxWidth: '100%',
flexWrap: 'wrap',
overflow: 'auto'
}
}, provided.droppableProps, {
className: listClassNames
}), list, provided.placeholder);
}));
}
return /*#__PURE__*/React.createElement(Animate, {
transitionName: "".concat(prefixCls, "-").concat(animationDirection),
component: "div",
className: listClassNames
}, list);
}
}], [{
key: "contextType",
get: function get() {
return ConfigContext;
}
}]);
return UploadList;
}(Component);
export { UploadList as default };
UploadList.displayName = 'UploadList';
UploadList.defaultProps = {
listType: 'text',
progressAttr: {
strokeWidth: 2,
showInfo: false
},
previewFile: previewImage,
showRemoveIcon: true,
showPreviewIcon: true,
dragUploadList: false,
showFileSize: false,
showDownloadIcon: true,
showReUploadIcon: false,
downloadPropsIntercept: function downloadPropsIntercept(o) {
return o;
}
};
//# sourceMappingURL=UploadList.js.map