@txdfe/at
Version:
一个设计体系组件库
207 lines (170 loc) • 8.46 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 from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import zhCN from '../locale/zh-cn.js';
import { func, obj } from '../util';
import Base from './base';
import List from './list';
import Upload from './upload';
/**
* Upload.Card
* @description 继承 Upload 的 API,除非特别说明
*/
var Card = /*#__PURE__*/function (_Base) {
_inherits(Card, _Base);
var _super = _createSuper(Card);
function Card(props) {
var _this;
_classCallCheck(this, Card);
_this = _super.call(this, props);
_defineProperty(_assertThisInitialized(_this), "onProgress", function (value) {
_this.setState({
value: value
});
});
_defineProperty(_assertThisInitialized(_this), "onChange", function (value, file) {
if (!('value' in _this.props)) {
_this.setState({
value: value
});
}
_this.props.onChange(value, file);
});
var _value;
/* istanbul ignore else */
if ('value' in props) {
_value = props.value;
} else {
_value = props.defaultValue;
}
_this.state = {
value: typeof _value === 'undefined' ?
/* istanbul ignore next */
[] : _value,
uploaderRef: _this.uploaderRef
};
return _this;
}
/* eslint react/no-did-mount-set-state: [0] */
_createClass(Card, [{
key: "componentDidMount",
value: function componentDidMount() {
this.setState({
uploaderRef: this.uploaderRef
});
}
}, {
key: "UNSAFE_componentWillReceiveProps",
value: function UNSAFE_componentWillReceiveProps(nextProps) {
/* istanbul ignore if */
if ('value' in nextProps) {
this.setState({
value: typeof nextProps.value === 'undefined' ? [] : nextProps.value
});
}
}
}, {
key: "isUploading",
value: function isUploading() {
return this.state.uploaderRef.isUploading();
}
}, {
key: "saveRef",
value: function saveRef(ref) {
this.saveUploaderRef(ref);
}
}, {
key: "render",
value: function render() {
var _classNames,
_this2 = this;
var _this$props = this.props,
action = _this$props.action,
disabled = _this$props.disabled,
prefix = _this$props.prefix,
locale = _this$props.locale,
className = _this$props.className,
style = _this$props.style,
limit = _this$props.limit,
onPreview = _this$props.onPreview,
onRemove = _this$props.onRemove,
onCancel = _this$props.onCancel,
timeout = _this$props.timeout;
var isExceedLimit = this.state.value.length >= limit;
var uploadButtonCls = classNames((_classNames = {}, _defineProperty(_classNames, "".concat(prefix, "upload-list-item"), true), _defineProperty(_classNames, "".concat(prefix, "hidden"), isExceedLimit), _classNames));
var children = this.props.children || locale.card.addPhoto;
var onRemoveFunc = disabled ? func.prevent : onRemove;
var othersForList = obj.pickOthers(Card.propTypes, this.props);
var othersForUpload = obj.pickOthers(List.propTypes, othersForList);
return /*#__PURE__*/React.createElement(List, _extends({
className: className,
style: style,
listType: "card",
closable: true,
locale: locale,
value: this.state.value,
onRemove: onRemoveFunc,
onCancel: onCancel,
onPreview: onPreview,
uploader: this.state.uploaderRef
}, othersForList), /*#__PURE__*/React.createElement(Upload, _extends({}, othersForUpload, {
shape: "card",
disabled: disabled,
action: action,
timeout: timeout,
value: this.state.value,
onProgress: this.onProgress,
onChange: this.onChange,
ref: function ref(_ref) {
return _this2.saveRef(_ref);
},
className: uploadButtonCls
}), children));
}
}]);
return Card;
}(Base);
_defineProperty(Card, "displayName", 'Card');
_defineProperty(Card, "propTypes", {
prefix: PropTypes.string,
locale: PropTypes.object,
children: PropTypes.object,
value: PropTypes.oneOfType([PropTypes.array, PropTypes.object]),
defaultValue: PropTypes.oneOfType([PropTypes.array, PropTypes.object]),
/**
* 点击图片回调
*/
onPreview: PropTypes.func,
/**
* 改变时候的回调
*/
onChange: PropTypes.func,
/**
* 点击移除的回调
*/
onRemove: PropTypes.func,
/**
* 取消上传的回调
*/
onCancel: PropTypes.func
});
_defineProperty(Card, "defaultProps", {
prefix: 'next-',
locale: zhCN.Upload,
onChange: func.noop,
onPreview: func.noop
});
export default Card;