@txdfe/at
Version:
一个设计体系组件库
525 lines (448 loc) • 20.5 kB
JavaScript
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import ConfigProvider from '../config-provider';
import Progress from '../progress';
import Icon from '../icon';
import Button from '../button';
import { func, obj, KEYCODE } from '../util';
import zhCN from '../locale/zh-cn.js';
import { previewFile } from './util';
import transform from './transform';
var List = /*#__PURE__*/function (_Component) {
_inherits(List, _Component);
var _super = _createSuper(List);
function List() {
var _this;
_classCallCheck(this, List);
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
_this = _super.call.apply(_super, [this].concat(args));
_defineProperty(_assertThisInitialized(_this), "handleClose", function (file) {
var _this$props = _this.props,
onRemove = _this$props.onRemove,
uploader = _this$props.uploader;
var remove = onRemove(file);
func.promiseCall(remove, function () {
uploader && uploader.removeFile(file);
});
});
_defineProperty(_assertThisInitialized(_this), "handleCancel", function (file) {
var _this$props2 = _this.props,
onCancel = _this$props2.onCancel,
uploader = _this$props2.uploader;
var cancel = onCancel(file);
func.promiseCall(cancel, function () {
uploader && uploader.abort(file);
});
});
_defineProperty(_assertThisInitialized(_this), "onImageError", function (file, obj) {
obj.onerror = null;
_this.props.onImageError(obj, file);
});
_defineProperty(_assertThisInitialized(_this), "onPreview", function (file, e) {
_this.props.onPreview(file, e);
});
return _this;
}
_createClass(List, [{
key: "componentDidUpdate",
value: function componentDidUpdate() {
var _this2 = this;
var _this$props3 = this.props,
listType = _this$props3.listType,
useDataURL = _this$props3.useDataURL,
value = _this$props3.value;
if (listType !== 'image' && listType !== 'card') {
return;
}
useDataURL && value.forEach(function (file) {
if (typeof document === 'undefined' || typeof window === 'undefined' || !window.FileReader || !window.File || !(file.originFileObj instanceof File) || file.imgURL !== undefined) {
return;
}
file.imgURL = '';
previewFile(file.originFileObj, function (previewDataUrl) {
file.imgURL = previewDataUrl;
_this2.forceUpdate();
});
});
}
}, {
key: "getInfo",
value: function getInfo(file) {
var _classNames;
var prefixCls = "".concat(this.props.prefix, "upload");
var downloadURL = file.downloadURL || file.url;
var imgURL = file.imgURL || file.url;
var size = this.sizeCaculator(file.size);
var itemCls = classNames((_classNames = {}, _defineProperty(_classNames, "".concat(prefixCls, "-list-item"), true), _defineProperty(_classNames, "".concat(prefixCls, "-list-item-").concat(file.state), file.state), _defineProperty(_classNames, "".concat(prefixCls, "-list-item-error-with-msg"), file.state === 'error' && file.errorMsg), _classNames));
var alt = file.name || file.alt;
return {
prefixCls: prefixCls,
downloadURL: downloadURL,
imgURL: imgURL,
size: size,
itemCls: itemCls,
alt: alt
};
} // transfer size from number to xx K/ XxxM / xxG
}, {
key: "sizeCaculator",
value: function sizeCaculator(size) {
var fileSize = parseFloat(size, 10); // fileSize为浮点数 用 < 0.000001 替代 === 0
if (isNaN(fileSize) || fileSize < 0.0000001) {
return 0;
}
var SIZE_SUFFIX = ['B', 'K', 'M', 'G', 'T', 'P'];
var suffixIndex = 0; // 在Mac上实验发现 取1024造成显示的大小和实际大小不一致
// 因为单位制不同 见 https://superuser.com/questions/938234/size-of-files-in-windows-os-its-kb-or-kb
var BIT_NUMBER_SYSTEM = 1024;
while (fileSize >= BIT_NUMBER_SYSTEM && suffixIndex < SIZE_SUFFIX.length) {
suffixIndex++;
fileSize /= BIT_NUMBER_SYSTEM;
}
var suffix = SIZE_SUFFIX[suffixIndex];
fileSize = fileSize.toFixed(2);
return "".concat(fileSize).concat(suffix);
}
}, {
key: "getTextList",
value: function getTextList(file) {
var _this3 = this;
var _this$props4 = this.props,
locale = _this$props4.locale,
extraRender = _this$props4.extraRender,
progressProps = _this$props4.progressProps,
rtl = _this$props4.rtl;
var _this$getInfo = this.getInfo(file),
prefixCls = _this$getInfo.prefixCls,
downloadURL = _this$getInfo.downloadURL,
size = _this$getInfo.size,
itemCls = _this$getInfo.itemCls;
var onClick = function onClick() {
return file.state === 'uploading' ? _this3.handleCancel(file) : _this3.handleClose(file);
};
var onKeyDown = function onKeyDown(e) {
if (e.keyCode === KEYCODE.ENTER) {
onClick();
}
};
return /*#__PURE__*/React.createElement("div", {
className: itemCls,
key: file.uid || file.name
}, /*#__PURE__*/React.createElement("div", {
className: "".concat(prefixCls, "-list-item-name-wrap")
}, /*#__PURE__*/React.createElement("a", {
href: downloadURL,
target: "_blank",
rel: "noopener noreferrer",
style: {
pointerEvents: downloadURL ? '' : 'none'
},
className: "".concat(prefixCls, "-list-item-name")
}, /*#__PURE__*/React.createElement("span", null, file.name), !!size && /*#__PURE__*/React.createElement("span", {
className: "".concat(prefixCls, "-list-item-size"),
dir: rtl ? 'rtl' : undefined
}, "(", size, ")"), /*#__PURE__*/React.createElement("span", {
className: "".concat(prefixCls, "-extra")
}, extraRender(file)))), file.state === 'uploading' ? /*#__PURE__*/React.createElement("div", {
className: "".concat(prefixCls, "-list-item-progress")
}, /*#__PURE__*/React.createElement(Progress, _extends({
size: "medium",
percent: file.percent,
textRender: func.noop,
rtl: rtl
}, progressProps))) : null, file.state === 'error' && file.errorMsg ? /*#__PURE__*/React.createElement("div", {
className: "".concat(prefixCls, "-list-item-error-msg")
}, file.errorMsg) : null, this.props.closable ? /*#__PURE__*/React.createElement(Button.Icon, {
type: "secondary",
color: "gray",
name: "remove",
size: "medium",
role: "button",
"aria-label": locale.upload["delete"],
tabIndex: "0",
onClick: onClick,
onKeyDown: onKeyDown
}) : null);
}
}, {
key: "getImageList",
value: function getImageList(file) {
var _this4 = this;
var _this$props5 = this.props,
extraRender = _this$props5.extraRender,
progressProps = _this$props5.progressProps,
rtl = _this$props5.rtl;
var _this$getInfo2 = this.getInfo(file),
prefixCls = _this$getInfo2.prefixCls,
downloadURL = _this$getInfo2.downloadURL,
imgURL = _this$getInfo2.imgURL,
size = _this$getInfo2.size,
itemCls = _this$getInfo2.itemCls,
alt = _this$getInfo2.alt;
var img = null;
var onClick = function onClick() {
return file.state === 'uploading' ? _this4.handleCancel(file) : _this4.handleClose(file);
};
var onKeyDown = function onKeyDown(e) {
if (e.keyCode === KEYCODE.ENTER) {
onClick();
}
};
if (file.state === 'uploading' || file.state === 'selected' && !imgURL) {
img = /*#__PURE__*/React.createElement(Icon, {
type: "picture"
});
} else if (file.state === 'error') {
img = /*#__PURE__*/React.createElement(Icon, {
type: "emoji-frown"
});
} else {
img = /*#__PURE__*/React.createElement("img", {
src: imgURL,
onError: this.onImageError.bind(this, file),
tabIndex: "0",
alt: alt,
onClick: this.onPreview.bind(this, file)
});
}
return /*#__PURE__*/React.createElement("div", {
className: itemCls,
key: file.uid || file.name
}, /*#__PURE__*/React.createElement("div", {
className: "".concat(prefixCls, "-list-item-thumbnail")
}, img), this.props.closable ? /*#__PURE__*/React.createElement(Icon, {
type: "remove",
size: "medium",
tabIndex: "0",
role: "button",
onClick: onClick,
onKeyDown: onKeyDown
}) : null, /*#__PURE__*/React.createElement("a", {
href: downloadURL,
target: "_blank",
rel: "noopener noreferrer",
style: {
pointerEvents: downloadURL ? '' : 'none'
},
className: "".concat(prefixCls, "-list-item-name")
}, /*#__PURE__*/React.createElement("span", null, file.name), !!size && /*#__PURE__*/React.createElement("span", {
className: "".concat(prefixCls, "-list-item-size"),
dir: rtl ? 'rtl' : undefined
}, "(", size, ")"), /*#__PURE__*/React.createElement("span", {
className: "".concat(prefixCls, "-extra")
}, extraRender(file))), file.state === 'uploading' ? /*#__PURE__*/React.createElement("div", {
className: "".concat(prefixCls, "-list-item-progress")
}, /*#__PURE__*/React.createElement(Progress, _extends({
size: "medium",
percent: file.percent,
textRender: func.noop
}, progressProps))) : null, file.state === 'error' && file.errorMsg ? /*#__PURE__*/React.createElement("div", {
className: "".concat(prefixCls, "-list-item-error-msg")
}, file.errorMsg) : null);
}
}, {
key: "getPictureCardList",
value: function getPictureCardList(file) {
var _this5 = this;
var _this$props6 = this.props,
locale = _this$props6.locale,
progressProps = _this$props6.progressProps;
var _this$getInfo3 = this.getInfo(file),
prefixCls = _this$getInfo3.prefixCls,
downloadURL = _this$getInfo3.downloadURL,
imgURL = _this$getInfo3.imgURL,
itemCls = _this$getInfo3.itemCls,
alt = _this$getInfo3.alt;
var img = null;
if (file.state === 'uploading' || file.state === 'selected' && !imgURL) {
img = /*#__PURE__*/React.createElement("div", {
className: "".concat(prefixCls, "-list-item-handler")
}, /*#__PURE__*/React.createElement(Icon, {
type: "picture"
}), /*#__PURE__*/React.createElement(Button, {
text: true,
onClick: function onClick() {
return _this5.handleCancel(file);
}
}, locale.card.cancel));
} else if (file.state === 'error') {
img = /*#__PURE__*/React.createElement("div", {
className: "".concat(prefixCls, "-list-item-handler")
}, /*#__PURE__*/React.createElement(Icon, {
type: "emoji-frown"
}));
} else {
img = /*#__PURE__*/React.createElement("img", {
src: imgURL,
tabIndex: "0",
alt: alt,
onError: this.onImageError.bind(this, file),
onClick: this.onPreview.bind(this, file)
});
}
var onClose = function onClose() {
return _this5.handleClose(file);
};
var onKeyDownClose = function onKeyDownClose(e) {
if (e.keyCode === KEYCODE.ENTER) {
onClose();
}
}; // 下载功能受同源策略限制,改为预览图标
return /*#__PURE__*/React.createElement("div", {
className: itemCls,
key: file.uid || file.name
}, /*#__PURE__*/React.createElement("div", {
className: "".concat(prefixCls, "-list-item-wrapper")
}, /*#__PURE__*/React.createElement("div", {
className: "".concat(prefixCls, "-list-item-thumbnail")
}, img), file.state === 'uploading' ? /*#__PURE__*/React.createElement("div", {
className: "".concat(prefixCls, "-list-item-progress")
}, /*#__PURE__*/React.createElement(Progress, _extends({
size: "medium",
percent: file.percent,
textRender: func.noop
}, progressProps))) : null, file.state !== 'uploading' ? /*#__PURE__*/React.createElement("span", {
className: "".concat(prefixCls, "-tool ").concat(!this.props.closable ? "".concat(prefixCls, "-noclose") : '')
}, /*#__PURE__*/React.createElement("a", {
href: downloadURL,
target: "_blank",
rel: "noopener noreferrer",
tabIndex: downloadURL ? '0' : '-1',
className: "".concat(prefixCls, "-tool-download-link"),
style: {
pointerEvents: downloadURL ? '' : 'none'
}
}, /*#__PURE__*/React.createElement(Icon, {
type: downloadURL ? 'eye' : '',
"aria-label": locale.card.download,
className: "".concat(prefixCls, "-tool-download-icon")
})), this.props.closable ? /*#__PURE__*/React.createElement("span", {
className: "".concat(prefixCls, "-tool-close")
}, /*#__PURE__*/React.createElement(Icon, {
type: "trashbin",
"aria-label": locale.card["delete"],
tabIndex: "0",
role: "button",
onClick: onClose,
onKeyDown: onKeyDownClose
})) : null) : ''), /*#__PURE__*/React.createElement("span", {
className: "".concat(prefixCls, "-list-item-name")
}, file.name));
}
}, {
key: "render",
value: function render() {
var _this6 = this,
_classNames2;
var _this$props7 = this.props,
listType = _this$props7.listType,
children = _this$props7.children,
prefix = _this$props7.prefix,
rtl = _this$props7.rtl,
className = _this$props7.className;
var prefixCls = "".concat(prefix, "upload");
var list = this.props.value.map(function (file) {
if (listType === 'text') {
return _this6.getTextList(file);
} else if (listType === 'image') {
return _this6.getImageList(file);
} else if (listType === 'card') {
return _this6.getPictureCardList(file);
}
return null;
});
if (rtl && listType === 'card' && Array.isArray(list)) {
list = list.reverse();
}
var listclassNames = classNames((_classNames2 = {}, _defineProperty(_classNames2, "".concat(prefixCls, "-list"), true), _defineProperty(_classNames2, "".concat(prefixCls, "-list-").concat(this.props.listType), true), _classNames2), className);
var others = obj.pickAttrsWith(this.props, 'data-');
return /*#__PURE__*/React.createElement("div", _extends({}, others, {
className: listclassNames,
dir: rtl ? 'rtl' : undefined
}), rtl ? children : list, rtl ? list : children);
}
}]);
return List;
}(Component); // Wrap <List> with <ConfigProvider> to avoid context missing if it is
// referenced by other internal modules.
// https://github.com/alibaba-fusion/next/blob/build/1.13.9/src/upload/upload.jsx#L521
_defineProperty(List, "propTypes", {
prefix: PropTypes.string,
/**
* 多语言
*/
locale: PropTypes.object,
/**
* 文件列表,数据格式请参考 文件对象
*/
listType: PropTypes.oneOf(['text', 'image', 'card']),
/**
* 文件列表
*/
value: PropTypes.array,
closable: PropTypes.bool,
/**
* 删除文件回调(支持Promise)
*/
onRemove: PropTypes.func,
/**
* 取消上传回调(支持Promise)
*/
onCancel: PropTypes.func,
/**
* 头像加载出错回调
*/
onImageError: PropTypes.func,
/**
* listType=card时点击图片回调
*/
onPreview: PropTypes.func,
/**
* 自定义额外渲染
*/
extraRender: PropTypes.func,
/**
* 透传给Progress props
*/
progressProps: PropTypes.object,
children: PropTypes.node,
uploader: PropTypes.any,
/**
* 可选参数,是否本地预览
*/
useDataURL: PropTypes.bool,
rtl: PropTypes.bool
});
_defineProperty(List, "defaultProps", {
prefix: 'next-',
listType: 'text',
value: [],
locale: zhCN.Upload,
closable: false,
onRemove: func.noop,
onCancel: func.noop,
extraRender: func.noop,
onImageError: func.noop,
onPreview: func.noop,
progressProps: {}
});
export default ConfigProvider.config(List, {
componentName: 'Upload',
transform: transform
});