choerodon-ui
Version:
An enterprise-class UI design language and React-based implementation
426 lines (372 loc) • 14.7 kB
JavaScript
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 _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
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 { DragDropContext, Droppable, Draggable } from 'react-beautiful-dnd';
import Icon from '../icon';
import Tooltip from '../tooltip';
import Progress from '../progress';
import Animate from '../animate';
import { ProgressType } from '../progress/enum';
import { previewImage, getFileType, getFileSizeStr, isImageUrl } 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;
};
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 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 _this$props2 = this.props,
listType = _this$props2.listType,
items = _this$props2.items,
previewFile = _this$props2.previewFile;
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 || '';
_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,
locale = _this$props3.locale,
dragUploadList = _this$props3.dragUploadList,
showFileSize = _this$props3.showFileSize;
var getPrefixCls = this.context.getPrefixCls;
var prefixCls = getPrefixCls('upload', customizePrefixCls);
var list = items.map(function (file, index) {
var _classNames;
var progress;
var icon = file.status === 'uploading' ? React.createElement(Progress, {
key: 'loading',
type: ProgressType.loading,
size: Size.small
}) : React.createElement(FileuploadIcon, {
className: "".concat(prefixCls, "-icon-file")
});
if (file.status !== 'uploading') {
var filetype = getFileType(file.name);
switch (filetype) {
case 'compressedfile':
icon = React.createElement(CompressedfileIcon, {
className: "".concat(prefixCls, "-icon-file")
});
break;
case 'doc':
icon = React.createElement(DocIcon, {
className: "".concat(prefixCls, "-icon-file")
});
break;
case 'image':
icon = React.createElement(ImageIcon, {
className: "".concat(prefixCls, "-icon-file")
});
break;
case 'pdf':
icon = React.createElement(PdfIcon, {
className: "".concat(prefixCls, "-icon-file")
});
break;
case 'xls':
icon = React.createElement(XlsIcon, {
className: "".concat(prefixCls, "-icon-file")
});
break;
default:
break;
}
}
if (listType === 'picture' || listType === 'picture-card') {
if (listType === 'picture-card' && file.status === 'uploading') {
icon = React.createElement("div", {
className: "".concat(prefixCls, "-list-item-uploading-text")
}, locale.uploading);
} else if (listType === 'picture' && file.status === 'uploading') {
icon = React.createElement(Progress, {
key: 'loading',
type: ProgressType.loading,
size: Size.small
});
} else if (!file.thumbUrl && !file.url) {
icon = React.createElement(Icon, {
className: "".concat(prefixCls, "-list-item-thumbnail"),
type: "picture"
});
} else {
var thumbnail = isImageUrl(file) ? React.createElement("img", {
src: file.thumbUrl || file.url,
alt: file.name
}) : React.createElement(Icon, {
type: "file",
style: {
fontSize: 48,
color: 'rgba(0,0,0,0.5)'
}
});
icon = 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 (file.status === 'uploading') {
var progressAttr = _this3.props.progressAttr; // show loading icon if upload progress listener is disabled
var loadingProgress = 'percent' in file ? React.createElement(Progress, _extends({
type: ProgressType.line
}, progressAttr, {
percent: file.percent
})) : null;
progress = 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 ? 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) : 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 = showPreviewIcon ? 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
}, React.createElement(Icon, {
type: "visibility"
})) : null;
var removeIcon = showRemoveIcon ? React.createElement(Icon, {
type: "delete",
title: locale.removeFile,
onClick: function onClick() {
return _this3.handleClose(file);
}
}) : null;
var removeIconCross = showRemoveIcon ? React.createElement(Icon, {
type: "close",
title: locale.removeFile,
onClick: function onClick() {
return _this3.handleClose(file);
}
}) : null;
var actions = listType === 'picture-card' && file.status !== 'uploading' ? React.createElement("span", {
className: "".concat(prefixCls, "-list-item-actions")
}, previewIcon, removeIcon) : removeIconCross;
var message;
if (file.response && typeof file.response === 'string') {
message = file.response;
} else {
message = file.error && file.error.statusText || locale.uploadError;
}
var filesizeStr = getFileSizeStr(file.size);
var fileSize = showFileSize && filesizeStr !== '' && listType === 'text' ? React.createElement("span", {
className: "".concat(prefixCls, "-list-item-info-filesize")
}, filesizeStr) : null;
var listItemInfo = React.createElement("div", {
className: "".concat(prefixCls, "-list-item-info")
}, React.createElement("span", {
className: "".concat(prefixCls, "-list-item-span")
}, icon, preview, fileSize, actions), React.createElement(Animate, {
transitionName: "fade",
component: ""
}, progress));
var listItemInfoTooltip = file.status === 'error' ? React.createElement(Tooltip, {
title: message
}, listItemInfo) : listItemInfo;
if (dragUploadList) {
return React.createElement(Draggable, {
key: file.uid,
draggableId: String(file.uid),
index: index
}, function (provided) {
return React.createElement("div", _extends({
className: infoUploadingClass,
key: file.uid,
ref: provided.innerRef
}, provided.draggableProps, provided.dragHandleProps), listItemInfoTooltip);
});
}
return React.createElement("div", {
className: infoUploadingClass,
key: file.uid
}, listItemInfoTooltip);
});
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 React.createElement(DragDropContext, {
onDragEnd: this.onDragEnd
}, React.createElement(Droppable, {
droppableId: "droppable",
direction: "horizontal"
}, function (provided, snapshot) {
return 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 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
};
//# sourceMappingURL=UploadList.js.map