UNPKG

qt-public-ui

Version:

新设计规范下业务组件库

75 lines 3.23 kB
import _extends from "@babel/runtime/helpers/esm/extends"; import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose"; var _excluded = ["title", "expandable", "defaultVisible", "visible", "onVisibleChange", "className", "subTitle", "bordered"]; import React, { useState, useEffect, useRef } from 'react'; import { Card } from 'antd'; import { IconSlim as Icon } from '..'; import classnames from 'classnames'; import Expand from './expand'; import './style'; var getLastElement = function getLastElement(children) { if (Array.isArray(children)) { return children[children.length - 1]; } return children; }; var PageCard = function PageCard(props) { var _getLastElement; var title = props.title, expandable = props.expandable, defaultVisible = props.defaultVisible, visible = props.visible, onVisibleChange = props.onVisibleChange, className = props.className, subTitle = props.subTitle, bordered = props.bordered, cardProps = _objectWithoutPropertiesLoose(props, _excluded); var isControlled = ('visible' in props); var _useState = useState(isControlled ? visible : defaultVisible), expand = _useState[0], setExpand = _useState[1]; var _useState2 = useState(0), height = _useState2[0], setHeight = _useState2[1]; var contentRef = useRef(null); var onExpand = function onExpand() { !isControlled && setExpand(!expand); onVisibleChange && onVisibleChange(!expand); }; useEffect(function () { isControlled && setExpand(props.visible); }, [isControlled, props.visible]); useEffect(function () { var _contentRef$current; !height && setHeight(((_contentRef$current = contentRef.current) === null || _contentRef$current === void 0 ? void 0 : _contentRef$current.offsetHeight) || 0); }, [expand, height]); var cardTitle = (title || expandable) && /*#__PURE__*/React.createElement(React.Fragment, null, typeof title !== 'string' && title, expandable ? /*#__PURE__*/React.createElement("span", { className: "umui-expand-controller", onClick: onExpand }, typeof title === 'string' && title, expand ? /*#__PURE__*/React.createElement(Icon, { type: "umicon-circle-up", className: "umui-expand-icon" }) : /*#__PURE__*/React.createElement(Icon, { type: "umicon-circle-down", className: "umui-expand-icon" })) : typeof title === 'string' && title, subTitle && /*#__PURE__*/React.createElement("span", { className: "umui-page-card-subtitle" }, subTitle)); var hasLastExpand = ((_getLastElement = getLastElement(props.children)) === null || _getLastElement === void 0 ? void 0 : _getLastElement.type) === Expand; return /*#__PURE__*/React.createElement(Card, _extends({ key: title + "-" + subTitle, className: classnames('umui-page-card', className, { 'umui-with-expand-child': hasLastExpand, 'umui-without-border': !bordered }), bordered: bordered, title: cardTitle }, cardProps), /*#__PURE__*/React.createElement("div", { ref: contentRef, style: props.bodyStyle, className: classnames('umui-expand-wrapper', { 'umui-expandless': expandable && !expand }) }, props.children)); }; export default PageCard;