index.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
  2. import { createVNode as _createVNode } from "vue";
  3. import { defineComponent, shallowRef, computed } from 'vue';
  4. import PropTypes from '../_util/vue-types';
  5. import { filterEmpty, flattenChildren, isEmptyContent } from '../_util/props-util';
  6. import ArrowLeftOutlined from "@ant-design/icons-vue/es/icons/ArrowLeftOutlined";
  7. import ArrowRightOutlined from "@ant-design/icons-vue/es/icons/ArrowRightOutlined";
  8. import Breadcrumb from '../breadcrumb';
  9. import Avatar from '../avatar';
  10. import TransButton from '../_util/transButton';
  11. import LocaleReceiver from '../locale-provider/LocaleReceiver';
  12. import { objectType, vNodeType, withInstall } from '../_util/type';
  13. import useConfigInject from '../config-provider/hooks/useConfigInject';
  14. import classNames from '../_util/classNames';
  15. import ResizeObserver from '../vc-resize-observer';
  16. import useDestroyed from '../_util/hooks/useDestroyed';
  17. import Space from '../space';
  18. // CSSINJS
  19. import useStyle from './style';
  20. export const pageHeaderProps = () => ({
  21. backIcon: vNodeType(),
  22. prefixCls: String,
  23. title: vNodeType(),
  24. subTitle: vNodeType(),
  25. breadcrumb: PropTypes.object,
  26. tags: vNodeType(),
  27. footer: vNodeType(),
  28. extra: vNodeType(),
  29. avatar: objectType(),
  30. ghost: {
  31. type: Boolean,
  32. default: undefined
  33. },
  34. onBack: Function
  35. });
  36. const PageHeader = defineComponent({
  37. compatConfig: {
  38. MODE: 3
  39. },
  40. name: 'APageHeader',
  41. inheritAttrs: false,
  42. props: pageHeaderProps(),
  43. // emits: ['back'],
  44. slots: Object,
  45. setup(props, _ref) {
  46. let {
  47. emit,
  48. slots,
  49. attrs
  50. } = _ref;
  51. const {
  52. prefixCls,
  53. direction,
  54. pageHeader
  55. } = useConfigInject('page-header', props);
  56. // style
  57. const [wrapSSR, hashId] = useStyle(prefixCls);
  58. const compact = shallowRef(false);
  59. const isDestroyed = useDestroyed();
  60. const onResize = _ref2 => {
  61. let {
  62. width
  63. } = _ref2;
  64. if (!isDestroyed.value) {
  65. compact.value = width < 768;
  66. }
  67. };
  68. const ghost = computed(() => {
  69. var _a, _b, _c;
  70. return (_c = (_a = props.ghost) !== null && _a !== void 0 ? _a : (_b = pageHeader === null || pageHeader === void 0 ? void 0 : pageHeader.value) === null || _b === void 0 ? void 0 : _b.ghost) !== null && _c !== void 0 ? _c : true;
  71. });
  72. const getBackIcon = () => {
  73. var _a, _b, _c;
  74. return (_c = (_a = props.backIcon) !== null && _a !== void 0 ? _a : (_b = slots.backIcon) === null || _b === void 0 ? void 0 : _b.call(slots)) !== null && _c !== void 0 ? _c : direction.value === 'rtl' ? _createVNode(ArrowRightOutlined, null, null) : _createVNode(ArrowLeftOutlined, null, null);
  75. };
  76. const renderBack = backIcon => {
  77. if (!backIcon || !props.onBack) {
  78. return null;
  79. }
  80. return _createVNode(LocaleReceiver, {
  81. "componentName": "PageHeader",
  82. "children": _ref3 => {
  83. let {
  84. back
  85. } = _ref3;
  86. return _createVNode("div", {
  87. "class": `${prefixCls.value}-back`
  88. }, [_createVNode(TransButton, {
  89. "onClick": e => {
  90. emit('back', e);
  91. },
  92. "class": `${prefixCls.value}-back-button`,
  93. "aria-label": back
  94. }, {
  95. default: () => [backIcon]
  96. })]);
  97. }
  98. }, null);
  99. };
  100. const renderBreadcrumb = () => {
  101. var _a;
  102. return props.breadcrumb ? _createVNode(Breadcrumb, props.breadcrumb, null) : (_a = slots.breadcrumb) === null || _a === void 0 ? void 0 : _a.call(slots);
  103. };
  104. const renderTitle = () => {
  105. var _a, _b, _c, _d, _e, _f, _g, _h, _j;
  106. const {
  107. avatar
  108. } = props;
  109. const title = (_a = props.title) !== null && _a !== void 0 ? _a : (_b = slots.title) === null || _b === void 0 ? void 0 : _b.call(slots);
  110. const subTitle = (_c = props.subTitle) !== null && _c !== void 0 ? _c : (_d = slots.subTitle) === null || _d === void 0 ? void 0 : _d.call(slots);
  111. const tags = (_e = props.tags) !== null && _e !== void 0 ? _e : (_f = slots.tags) === null || _f === void 0 ? void 0 : _f.call(slots);
  112. const extra = (_g = props.extra) !== null && _g !== void 0 ? _g : (_h = slots.extra) === null || _h === void 0 ? void 0 : _h.call(slots);
  113. const headingPrefixCls = `${prefixCls.value}-heading`;
  114. const hasHeading = title || subTitle || tags || extra;
  115. // If there is nothing, return a null
  116. if (!hasHeading) {
  117. return null;
  118. }
  119. const backIcon = getBackIcon();
  120. const backIconDom = renderBack(backIcon);
  121. const hasTitle = backIconDom || avatar || hasHeading;
  122. return _createVNode("div", {
  123. "class": headingPrefixCls
  124. }, [hasTitle && _createVNode("div", {
  125. "class": `${headingPrefixCls}-left`
  126. }, [backIconDom, avatar ? _createVNode(Avatar, avatar, null) : (_j = slots.avatar) === null || _j === void 0 ? void 0 : _j.call(slots), title && _createVNode("span", {
  127. "class": `${headingPrefixCls}-title`,
  128. "title": typeof title === 'string' ? title : undefined
  129. }, [title]), subTitle && _createVNode("span", {
  130. "class": `${headingPrefixCls}-sub-title`,
  131. "title": typeof subTitle === 'string' ? subTitle : undefined
  132. }, [subTitle]), tags && _createVNode("span", {
  133. "class": `${headingPrefixCls}-tags`
  134. }, [tags])]), extra && _createVNode("span", {
  135. "class": `${headingPrefixCls}-extra`
  136. }, [_createVNode(Space, null, {
  137. default: () => [extra]
  138. })])]);
  139. };
  140. const renderFooter = () => {
  141. var _a, _b;
  142. const footer = (_a = props.footer) !== null && _a !== void 0 ? _a : filterEmpty((_b = slots.footer) === null || _b === void 0 ? void 0 : _b.call(slots));
  143. return isEmptyContent(footer) ? null : _createVNode("div", {
  144. "class": `${prefixCls.value}-footer`
  145. }, [footer]);
  146. };
  147. const renderChildren = children => {
  148. return _createVNode("div", {
  149. "class": `${prefixCls.value}-content`
  150. }, [children]);
  151. };
  152. return () => {
  153. var _a, _b;
  154. const hasBreadcrumb = ((_a = props.breadcrumb) === null || _a === void 0 ? void 0 : _a.routes) || slots.breadcrumb;
  155. const hasFooter = props.footer || slots.footer;
  156. const children = flattenChildren((_b = slots.default) === null || _b === void 0 ? void 0 : _b.call(slots));
  157. const className = classNames(prefixCls.value, {
  158. 'has-breadcrumb': hasBreadcrumb,
  159. 'has-footer': hasFooter,
  160. [`${prefixCls.value}-ghost`]: ghost.value,
  161. [`${prefixCls.value}-rtl`]: direction.value === 'rtl',
  162. [`${prefixCls.value}-compact`]: compact.value
  163. }, attrs.class, hashId.value);
  164. return wrapSSR(_createVNode(ResizeObserver, {
  165. "onResize": onResize
  166. }, {
  167. default: () => [_createVNode("div", _objectSpread(_objectSpread({}, attrs), {}, {
  168. "class": className
  169. }), [renderBreadcrumb(), renderTitle(), children.length ? renderChildren(children) : null, renderFooter()])]
  170. }));
  171. };
  172. }
  173. });
  174. export default withInstall(PageHeader);