Card.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
  2. import { resolveDirective as _resolveDirective, createVNode as _createVNode } from "vue";
  3. import { isVNode, defineComponent } from 'vue';
  4. import Tabs from '../tabs';
  5. import PropTypes from '../_util/vue-types';
  6. import { flattenChildren, isEmptyElement, filterEmptyWithUndefined } from '../_util/props-util';
  7. import isPlainObject from 'lodash-es/isPlainObject';
  8. import useConfigInject from '../config-provider/hooks/useConfigInject';
  9. import devWarning from '../vc-util/devWarning';
  10. import useStyle from './style';
  11. import Skeleton from '../skeleton';
  12. import { customRenderSlot } from '../_util/vnode';
  13. const {
  14. TabPane
  15. } = Tabs;
  16. export const cardProps = () => ({
  17. prefixCls: String,
  18. title: PropTypes.any,
  19. extra: PropTypes.any,
  20. bordered: {
  21. type: Boolean,
  22. default: true
  23. },
  24. bodyStyle: {
  25. type: Object,
  26. default: undefined
  27. },
  28. headStyle: {
  29. type: Object,
  30. default: undefined
  31. },
  32. loading: {
  33. type: Boolean,
  34. default: false
  35. },
  36. hoverable: {
  37. type: Boolean,
  38. default: false
  39. },
  40. type: {
  41. type: String
  42. },
  43. size: {
  44. type: String
  45. },
  46. actions: PropTypes.any,
  47. tabList: {
  48. type: Array
  49. },
  50. tabBarExtraContent: PropTypes.any,
  51. activeTabKey: String,
  52. defaultActiveTabKey: String,
  53. cover: PropTypes.any,
  54. onTabChange: {
  55. type: Function
  56. }
  57. });
  58. const Card = defineComponent({
  59. compatConfig: {
  60. MODE: 3
  61. },
  62. name: 'ACard',
  63. inheritAttrs: false,
  64. props: cardProps(),
  65. slots: Object,
  66. setup(props, _ref) {
  67. let {
  68. slots,
  69. attrs
  70. } = _ref;
  71. const {
  72. prefixCls,
  73. direction,
  74. size
  75. } = useConfigInject('card', props);
  76. const [wrapSSR, hashId] = useStyle(prefixCls);
  77. const getAction = actions => {
  78. const actionList = actions.map((action, index) => isVNode(action) && !isEmptyElement(action) || !isVNode(action) ? _createVNode("li", {
  79. "style": {
  80. width: `${100 / actions.length}%`
  81. },
  82. "key": `action-${index}`
  83. }, [_createVNode("span", null, [action])]) : null);
  84. return actionList;
  85. };
  86. const triggerTabChange = key => {
  87. var _a;
  88. (_a = props.onTabChange) === null || _a === void 0 ? void 0 : _a.call(props, key);
  89. };
  90. const isContainGrid = function () {
  91. let obj = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  92. let containGrid;
  93. obj.forEach(element => {
  94. if (element && isPlainObject(element.type) && element.type.__ANT_CARD_GRID) {
  95. containGrid = true;
  96. }
  97. });
  98. return containGrid;
  99. };
  100. return () => {
  101. var _a, _b, _c, _d, _e, _f;
  102. const {
  103. headStyle = {},
  104. bodyStyle = {},
  105. loading,
  106. bordered = true,
  107. type,
  108. tabList,
  109. hoverable,
  110. activeTabKey,
  111. defaultActiveTabKey,
  112. tabBarExtraContent = filterEmptyWithUndefined((_a = slots.tabBarExtraContent) === null || _a === void 0 ? void 0 : _a.call(slots)),
  113. title = filterEmptyWithUndefined((_b = slots.title) === null || _b === void 0 ? void 0 : _b.call(slots)),
  114. extra = filterEmptyWithUndefined((_c = slots.extra) === null || _c === void 0 ? void 0 : _c.call(slots)),
  115. actions = filterEmptyWithUndefined((_d = slots.actions) === null || _d === void 0 ? void 0 : _d.call(slots)),
  116. cover = filterEmptyWithUndefined((_e = slots.cover) === null || _e === void 0 ? void 0 : _e.call(slots))
  117. } = props;
  118. const children = flattenChildren((_f = slots.default) === null || _f === void 0 ? void 0 : _f.call(slots));
  119. const pre = prefixCls.value;
  120. const classString = {
  121. [`${pre}`]: true,
  122. [hashId.value]: true,
  123. [`${pre}-loading`]: loading,
  124. [`${pre}-bordered`]: bordered,
  125. [`${pre}-hoverable`]: !!hoverable,
  126. [`${pre}-contain-grid`]: isContainGrid(children),
  127. [`${pre}-contain-tabs`]: tabList && tabList.length,
  128. [`${pre}-${size.value}`]: size.value,
  129. [`${pre}-type-${type}`]: !!type,
  130. [`${pre}-rtl`]: direction.value === 'rtl'
  131. };
  132. const loadingBlock = _createVNode(Skeleton, {
  133. "loading": true,
  134. "active": true,
  135. "paragraph": {
  136. rows: 4
  137. },
  138. "title": false
  139. }, {
  140. default: () => [children]
  141. });
  142. const hasActiveTabKey = activeTabKey !== undefined;
  143. const tabsProps = {
  144. size: 'large',
  145. [hasActiveTabKey ? 'activeKey' : 'defaultActiveKey']: hasActiveTabKey ? activeTabKey : defaultActiveTabKey,
  146. onChange: triggerTabChange,
  147. class: `${pre}-head-tabs`
  148. };
  149. let head;
  150. const tabs = tabList && tabList.length ? _createVNode(Tabs, tabsProps, {
  151. default: () => [tabList.map(item => {
  152. const {
  153. tab: temp,
  154. slots: itemSlots
  155. } = item;
  156. const name = itemSlots === null || itemSlots === void 0 ? void 0 : itemSlots.tab;
  157. devWarning(!itemSlots, 'Card', `tabList slots is deprecated, Please use \`customTab\` instead.`);
  158. let tab = temp !== undefined ? temp : slots[name] ? slots[name](item) : null;
  159. tab = customRenderSlot(slots, 'customTab', item, () => [tab]);
  160. return _createVNode(TabPane, {
  161. "tab": tab,
  162. "key": item.key,
  163. "disabled": item.disabled
  164. }, null);
  165. })],
  166. rightExtra: tabBarExtraContent ? () => tabBarExtraContent : null
  167. }) : null;
  168. if (title || extra || tabs) {
  169. head = _createVNode("div", {
  170. "class": `${pre}-head`,
  171. "style": headStyle
  172. }, [_createVNode("div", {
  173. "class": `${pre}-head-wrapper`
  174. }, [title && _createVNode("div", {
  175. "class": `${pre}-head-title`
  176. }, [title]), extra && _createVNode("div", {
  177. "class": `${pre}-extra`
  178. }, [extra])]), tabs]);
  179. }
  180. const coverDom = cover ? _createVNode("div", {
  181. "class": `${pre}-cover`
  182. }, [cover]) : null;
  183. const body = _createVNode("div", {
  184. "class": `${pre}-body`,
  185. "style": bodyStyle
  186. }, [loading ? loadingBlock : children]);
  187. const actionDom = actions && actions.length ? _createVNode("ul", {
  188. "class": `${pre}-actions`
  189. }, [getAction(actions)]) : null;
  190. return wrapSSR(_createVNode("div", _objectSpread(_objectSpread({
  191. "ref": "cardContainerRef"
  192. }, attrs), {}, {
  193. "class": [classString, attrs.class]
  194. }), [head, coverDom, children && children.length ? body : null, actionDom]));
  195. };
  196. }
  197. });
  198. export default Card;