UNPKG

@txdfe/at

Version:

一个设计体系组件库

247 lines (201 loc) 10.9 kB
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; } /* eslint-disable valid-jsdoc */ import React from 'react'; import ReactDOM from 'react-dom'; import PropTypes from 'prop-types'; import classNames from 'classnames'; import Icon from '../icon'; import nextLocale from '../locale/zh-cn'; import { obj } from '../util'; var pickOthers = 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.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 = classNames((_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 = classNames(_defineProperty({}, "".concat(prefix, "card-head"), true)); var cardBody = classNames((_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 = classNames((_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.createElement("div", { className: "".concat(prefix, "card-extra") }, extra) : null; var headmain = classNames((_classNames5 = {}, _defineProperty(_classNames5, "".concat(prefix, "card-head-main"), true), _defineProperty(_classNames5, "".concat(prefix, "card-head-main-box-tab"), isBox && isTab), _classNames5)); return /*#__PURE__*/React.createElement("div", _extends({}, obj.pickAttrs(others), { className: cardCls }), title ? /*#__PURE__*/React.createElement("div", { className: headCls }, /*#__PURE__*/React.createElement("div", { className: headmain }, /*#__PURE__*/React.createElement("div", { className: "".concat(prefix, "card-title") }, title, !isBox && subTitle ? /*#__PURE__*/React.createElement("span", { className: "".concat(prefix, "card-subtitle") }, subTitle) : null, isBox && subTitle ? /*#__PURE__*/React.createElement("div", { className: subTitleBox }, subTitle) : null), headExtra)) : null, /*#__PURE__*/React.createElement("div", { className: cardBody }, /*#__PURE__*/React.createElement("div", { className: "".concat(prefix, "card-content"), ref: this._contentRefHandler }, children), needMore ? /*#__PURE__*/React.createElement("div", { className: "".concat(prefix, "card-footer"), ref: this.saveFooter, onClick: this.handleToggle }, expand ? locale.fold : locale.expand, /*#__PURE__*/React.createElement(Icon, { type: "chevron-down", size: "xs", className: expand ? 'expand' : '' })) : null)); } }]); return Card; }(React.Component); _defineProperty(Card, "displayName", 'Card'); _defineProperty(Card, "propTypes", { prefix: PropTypes.string, rtl: PropTypes.bool, /** * 卡片的标题 */ title: PropTypes.node, /** * 卡片的副标题 */ subTitle: PropTypes.node, /** * 是否展示头部的分隔线 */ showHeadDivider: PropTypes.bool, /** * 内容区域的固定高度 */ contentHeight: PropTypes.oneOfType([PropTypes.string, PropTypes.number]), /** * 标题区域的用户自定义内容 */ /** * 是否结合 tab 的使用类型 */ isTab: PropTypes.bool, /** * 是否是Box */ isBox: PropTypes.bool, extra: PropTypes.node, className: PropTypes.string, children: PropTypes.node, locale: PropTypes.object }); _defineProperty(Card, "defaultProps", { prefix: 'next-', contentHeight: 120, showHeadDivider: true, locale: nextLocale.Card }); export { Card as default };