Card.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. "use client";
  2. var __rest = this && this.__rest || function (s, e) {
  3. var t = {};
  4. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
  5. if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
  6. if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
  7. }
  8. return t;
  9. };
  10. import * as React from 'react';
  11. import classNames from 'classnames';
  12. import omit from "rc-util/es/omit";
  13. import { devUseWarning } from '../_util/warning';
  14. import { ConfigContext } from '../config-provider';
  15. import useSize from '../config-provider/hooks/useSize';
  16. import Skeleton from '../skeleton';
  17. import Tabs from '../tabs';
  18. import Grid from './Grid';
  19. import useStyle from './style';
  20. import useVariant from '../form/hooks/useVariants';
  21. const ActionNode = props => {
  22. const {
  23. actionClasses,
  24. actions = [],
  25. actionStyle
  26. } = props;
  27. return /*#__PURE__*/React.createElement("ul", {
  28. className: actionClasses,
  29. style: actionStyle
  30. }, actions.map((action, index) => {
  31. // Move this out since eslint not allow index key
  32. // And eslint-disable makes conflict with rollup
  33. // ref https://github.com/ant-design/ant-design/issues/46022
  34. const key = `action-${index}`;
  35. return /*#__PURE__*/React.createElement("li", {
  36. style: {
  37. width: `${100 / actions.length}%`
  38. },
  39. key: key
  40. }, /*#__PURE__*/React.createElement("span", null, action));
  41. }));
  42. };
  43. const Card = /*#__PURE__*/React.forwardRef((props, ref) => {
  44. const {
  45. prefixCls: customizePrefixCls,
  46. className,
  47. rootClassName,
  48. style,
  49. extra,
  50. headStyle = {},
  51. bodyStyle = {},
  52. title,
  53. loading,
  54. bordered,
  55. variant: customVariant,
  56. size: customizeSize,
  57. type,
  58. cover,
  59. actions,
  60. tabList,
  61. children,
  62. activeTabKey,
  63. defaultActiveTabKey,
  64. tabBarExtraContent,
  65. hoverable,
  66. tabProps = {},
  67. classNames: customClassNames,
  68. styles: customStyles
  69. } = props,
  70. others = __rest(props, ["prefixCls", "className", "rootClassName", "style", "extra", "headStyle", "bodyStyle", "title", "loading", "bordered", "variant", "size", "type", "cover", "actions", "tabList", "children", "activeTabKey", "defaultActiveTabKey", "tabBarExtraContent", "hoverable", "tabProps", "classNames", "styles"]);
  71. const {
  72. getPrefixCls,
  73. direction,
  74. card
  75. } = React.useContext(ConfigContext);
  76. const [variant] = useVariant('card', customVariant, bordered);
  77. // =================Warning===================
  78. if (process.env.NODE_ENV !== 'production') {
  79. const warning = devUseWarning('Card');
  80. [['headStyle', 'styles.header'], ['bodyStyle', 'styles.body'], ['bordered', 'variant']].forEach(([deprecatedName, newName]) => {
  81. warning.deprecated(!(deprecatedName in props), deprecatedName, newName);
  82. });
  83. }
  84. const onTabChange = key => {
  85. var _a;
  86. (_a = props.onTabChange) === null || _a === void 0 ? void 0 : _a.call(props, key);
  87. };
  88. const moduleClass = moduleName => {
  89. var _a;
  90. return classNames((_a = card === null || card === void 0 ? void 0 : card.classNames) === null || _a === void 0 ? void 0 : _a[moduleName], customClassNames === null || customClassNames === void 0 ? void 0 : customClassNames[moduleName]);
  91. };
  92. const moduleStyle = moduleName => {
  93. var _a;
  94. return Object.assign(Object.assign({}, (_a = card === null || card === void 0 ? void 0 : card.styles) === null || _a === void 0 ? void 0 : _a[moduleName]), customStyles === null || customStyles === void 0 ? void 0 : customStyles[moduleName]);
  95. };
  96. const isContainGrid = React.useMemo(() => {
  97. let containGrid = false;
  98. React.Children.forEach(children, element => {
  99. if ((element === null || element === void 0 ? void 0 : element.type) === Grid) {
  100. containGrid = true;
  101. }
  102. });
  103. return containGrid;
  104. }, [children]);
  105. const prefixCls = getPrefixCls('card', customizePrefixCls);
  106. const [wrapCSSVar, hashId, cssVarCls] = useStyle(prefixCls);
  107. const loadingBlock = /*#__PURE__*/React.createElement(Skeleton, {
  108. loading: true,
  109. active: true,
  110. paragraph: {
  111. rows: 4
  112. },
  113. title: false
  114. }, children);
  115. const hasActiveTabKey = activeTabKey !== undefined;
  116. const extraProps = Object.assign(Object.assign({}, tabProps), {
  117. [hasActiveTabKey ? 'activeKey' : 'defaultActiveKey']: hasActiveTabKey ? activeTabKey : defaultActiveTabKey,
  118. tabBarExtraContent
  119. });
  120. let head;
  121. const mergedSize = useSize(customizeSize);
  122. const tabSize = !mergedSize || mergedSize === 'default' ? 'large' : mergedSize;
  123. const tabs = tabList ? (/*#__PURE__*/React.createElement(Tabs, Object.assign({
  124. size: tabSize
  125. }, extraProps, {
  126. className: `${prefixCls}-head-tabs`,
  127. onChange: onTabChange,
  128. items: tabList.map(_a => {
  129. var {
  130. tab
  131. } = _a,
  132. item = __rest(_a, ["tab"]);
  133. return Object.assign({
  134. label: tab
  135. }, item);
  136. })
  137. }))) : null;
  138. if (title || extra || tabs) {
  139. const headClasses = classNames(`${prefixCls}-head`, moduleClass('header'));
  140. const titleClasses = classNames(`${prefixCls}-head-title`, moduleClass('title'));
  141. const extraClasses = classNames(`${prefixCls}-extra`, moduleClass('extra'));
  142. const mergedHeadStyle = Object.assign(Object.assign({}, headStyle), moduleStyle('header'));
  143. head = /*#__PURE__*/React.createElement("div", {
  144. className: headClasses,
  145. style: mergedHeadStyle
  146. }, /*#__PURE__*/React.createElement("div", {
  147. className: `${prefixCls}-head-wrapper`
  148. }, title && (/*#__PURE__*/React.createElement("div", {
  149. className: titleClasses,
  150. style: moduleStyle('title')
  151. }, title)), extra && (/*#__PURE__*/React.createElement("div", {
  152. className: extraClasses,
  153. style: moduleStyle('extra')
  154. }, extra))), tabs);
  155. }
  156. const coverClasses = classNames(`${prefixCls}-cover`, moduleClass('cover'));
  157. const coverDom = cover ? (/*#__PURE__*/React.createElement("div", {
  158. className: coverClasses,
  159. style: moduleStyle('cover')
  160. }, cover)) : null;
  161. const bodyClasses = classNames(`${prefixCls}-body`, moduleClass('body'));
  162. const mergedBodyStyle = Object.assign(Object.assign({}, bodyStyle), moduleStyle('body'));
  163. const body = /*#__PURE__*/React.createElement("div", {
  164. className: bodyClasses,
  165. style: mergedBodyStyle
  166. }, loading ? loadingBlock : children);
  167. const actionClasses = classNames(`${prefixCls}-actions`, moduleClass('actions'));
  168. const actionDom = (actions === null || actions === void 0 ? void 0 : actions.length) ? (/*#__PURE__*/React.createElement(ActionNode, {
  169. actionClasses: actionClasses,
  170. actionStyle: moduleStyle('actions'),
  171. actions: actions
  172. })) : null;
  173. const divProps = omit(others, ['onTabChange']);
  174. const classString = classNames(prefixCls, card === null || card === void 0 ? void 0 : card.className, {
  175. [`${prefixCls}-loading`]: loading,
  176. [`${prefixCls}-bordered`]: variant !== 'borderless',
  177. [`${prefixCls}-hoverable`]: hoverable,
  178. [`${prefixCls}-contain-grid`]: isContainGrid,
  179. [`${prefixCls}-contain-tabs`]: tabList === null || tabList === void 0 ? void 0 : tabList.length,
  180. [`${prefixCls}-${mergedSize}`]: mergedSize,
  181. [`${prefixCls}-type-${type}`]: !!type,
  182. [`${prefixCls}-rtl`]: direction === 'rtl'
  183. }, className, rootClassName, hashId, cssVarCls);
  184. const mergedStyle = Object.assign(Object.assign({}, card === null || card === void 0 ? void 0 : card.style), style);
  185. return wrapCSSVar(/*#__PURE__*/React.createElement("div", Object.assign({
  186. ref: ref
  187. }, divProps, {
  188. className: classString,
  189. style: mergedStyle
  190. }), head, coverDom, body, actionDom));
  191. });
  192. export default Card;