@txdfe/at
Version:
一个设计体系组件库
263 lines (206 loc) • 11.7 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); }
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _react = _interopRequireDefault(require("react"));
var _reactDom = _interopRequireDefault(require("react-dom"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _classnames = _interopRequireDefault(require("classnames"));
var _icon = _interopRequireDefault(require("../icon"));
var _zhCn = _interopRequireDefault(require("../locale/zh-cn"));
var _util = require("../util");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": 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; }
var pickOthers = _util.obj.pickOthers;
/** Card */
var Card = /*#__PURE__*/function (_React$Component) {
_inherits(Card, _React$Component);
var _super = _createSuper(Card);
function Card(props, context) {
var _this;
_classCallCheck(this, Card);
_this = _super.call(this, props, context);
_defineProperty(_assertThisInitialized(_this), "handleToggle", function () {
_this.setState(function (prevState) {
return {
expand: !prevState.expand
};
});
});
_defineProperty(_assertThisInitialized(_this), "_contentRefHandler", function (ref) {
_this.content = ref;
});
_defineProperty(_assertThisInitialized(_this), "saveFooter", function (ref) {
_this.footer = ref;
});
_this.state = {
needMore: false,
expand: false
};
return _this;
}
_createClass(Card, [{
key: "componentDidMount",
value: function componentDidMount() {
this._setNeedMore();
this._setContentHeight();
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate() {
this._setContentHeight();
}
}, {
key: "_setNeedMore",
value: // 是否展示 More 按钮
function _setNeedMore() {
var contentHeight = this.props.contentHeight;
var childrenHeight = this._getNodeChildrenHeight(this.content);
this.setState({
needMore: contentHeight !== 'auto' && childrenHeight > contentHeight
});
} // 设置 Body 的高度
}, {
key: "_setContentHeight",
value: function _setContentHeight() {
if (this.state.expand) {
var childrenHeight = this._getNodeChildrenHeight(this.content);
this.content.style.height = "".concat(childrenHeight, "px"); // get the real height
} else {
var el = _reactDom["default"].findDOMNode(this.footer);
var height = this.props.contentHeight;
if (el) {
height -= el.getBoundingClientRect().height;
}
this.content.style.height = "".concat(height, "px");
}
}
}, {
key: "_getNodeChildrenHeight",
value: function _getNodeChildrenHeight(node) {
if (!node) {
return 0;
}
var contentChildNodes = node.childNodes;
var length = contentChildNodes.length;
if (!length) {
return 0;
}
var lastNode = contentChildNodes[length - 1];
return lastNode.offsetTop + lastNode.offsetHeight;
}
}, {
key: "render",
value: function render() {
var _classNames, _classNames3, _classNames4, _classNames5;
var _this$props = this.props,
prefix = _this$props.prefix,
className = _this$props.className,
title = _this$props.title,
subTitle = _this$props.subTitle,
isTab = _this$props.isTab,
isBox = _this$props.isBox,
extra = _this$props.extra,
showHeadDivider = _this$props.showHeadDivider,
children = _this$props.children,
locale = _this$props.locale,
rtl = _this$props.rtl;
var _this$state = this.state,
needMore = _this$state.needMore,
expand = _this$state.expand;
var cardCls = (0, _classnames["default"])((_classNames = {}, _defineProperty(_classNames, "".concat(prefix, "card"), true), _defineProperty(_classNames, "".concat(prefix, "card-show-divider"), showHeadDivider && !isTab), _defineProperty(_classNames, "".concat(prefix, "card-hide-divider"), !showHeadDivider), _classNames), className);
var headCls = (0, _classnames["default"])(_defineProperty({}, "".concat(prefix, "card-head"), true));
var cardBody = (0, _classnames["default"])((_classNames3 = {}, _defineProperty(_classNames3, "".concat(prefix, "card-body-padding"), true), _defineProperty(_classNames3, "".concat(prefix, "card-body"), !needMore), _defineProperty(_classNames3, "".concat(prefix, "card-body-top"), !title && !isTab), _defineProperty(_classNames3, "".concat(prefix, "card-body-tab"), isTab), _classNames3));
var subTitleBox = (0, _classnames["default"])((_classNames4 = {}, _defineProperty(_classNames4, "".concat(prefix, "card-subtitle"), true), _defineProperty(_classNames4, "".concat(prefix, "card-subtitle-box"), isBox), _classNames4));
var others = pickOthers(Object.keys(Card.propTypes), this.props);
others.dir = rtl ? 'rtl' : undefined;
var headExtra = extra ? /*#__PURE__*/_react["default"].createElement("div", {
className: "".concat(prefix, "card-extra")
}, extra) : null;
var headmain = (0, _classnames["default"])((_classNames5 = {}, _defineProperty(_classNames5, "".concat(prefix, "card-head-main"), true), _defineProperty(_classNames5, "".concat(prefix, "card-head-main-box-tab"), isBox && isTab), _classNames5));
return /*#__PURE__*/_react["default"].createElement("div", _extends({}, _util.obj.pickAttrs(others), {
className: cardCls
}), title ? /*#__PURE__*/_react["default"].createElement("div", {
className: headCls
}, /*#__PURE__*/_react["default"].createElement("div", {
className: headmain
}, /*#__PURE__*/_react["default"].createElement("div", {
className: "".concat(prefix, "card-title")
}, title, !isBox && subTitle ? /*#__PURE__*/_react["default"].createElement("span", {
className: "".concat(prefix, "card-subtitle")
}, subTitle) : null, isBox && subTitle ? /*#__PURE__*/_react["default"].createElement("div", {
className: subTitleBox
}, subTitle) : null), headExtra)) : null, /*#__PURE__*/_react["default"].createElement("div", {
className: cardBody
}, /*#__PURE__*/_react["default"].createElement("div", {
className: "".concat(prefix, "card-content"),
ref: this._contentRefHandler
}, children), needMore ? /*#__PURE__*/_react["default"].createElement("div", {
className: "".concat(prefix, "card-footer"),
ref: this.saveFooter,
onClick: this.handleToggle
}, expand ? locale.fold : locale.expand, /*#__PURE__*/_react["default"].createElement(_icon["default"], {
type: "chevron-down",
size: "xs",
className: expand ? 'expand' : ''
})) : null));
}
}]);
return Card;
}(_react["default"].Component);
exports["default"] = Card;
_defineProperty(Card, "displayName", 'Card');
_defineProperty(Card, "propTypes", {
prefix: _propTypes["default"].string,
rtl: _propTypes["default"].bool,
/**
* 卡片的标题
*/
title: _propTypes["default"].node,
/**
* 卡片的副标题
*/
subTitle: _propTypes["default"].node,
/**
* 是否展示头部的分隔线
*/
showHeadDivider: _propTypes["default"].bool,
/**
* 内容区域的固定高度
*/
contentHeight: _propTypes["default"].oneOfType([_propTypes["default"].string, _propTypes["default"].number]),
/**
* 标题区域的用户自定义内容
*/
/**
* 是否结合 tab 的使用类型
*/
isTab: _propTypes["default"].bool,
/**
* 是否是Box
*/
isBox: _propTypes["default"].bool,
extra: _propTypes["default"].node,
className: _propTypes["default"].string,
children: _propTypes["default"].node,
locale: _propTypes["default"].object
});
_defineProperty(Card, "defaultProps", {
prefix: 'next-',
contentHeight: 120,
showHeadDivider: true,
locale: _zhCn["default"].Card
});