2022-03-26 14:52:54 +00:00
|
|
|
import type { ExtractPropTypes, PropType } from 'vue';
|
2021-06-26 01:35:40 +00:00
|
|
|
import { defineComponent, ref, computed } from 'vue';
|
2020-03-07 11:45:13 +00:00
|
|
|
import PropTypes from '../_util/vue-types';
|
2021-06-19 14:47:28 +00:00
|
|
|
import { filterEmpty, flattenChildren, isEmptyContent } from '../_util/props-util';
|
2020-04-05 06:19:12 +00:00
|
|
|
import ArrowLeftOutlined from '@ant-design/icons-vue/ArrowLeftOutlined';
|
2021-06-07 09:35:03 +00:00
|
|
|
import ArrowRightOutlined from '@ant-design/icons-vue/ArrowRightOutlined';
|
2020-03-07 11:45:13 +00:00
|
|
|
import Breadcrumb from '../breadcrumb';
|
|
|
|
import Avatar from '../avatar';
|
|
|
|
import TransButton from '../_util/transButton';
|
|
|
|
import LocaleReceiver from '../locale-provider/LocaleReceiver';
|
2020-11-01 07:03:33 +00:00
|
|
|
import { withInstall } from '../_util/type';
|
2021-06-07 09:35:03 +00:00
|
|
|
import useConfigInject from '../_util/hooks/useConfigInject';
|
|
|
|
import classNames from '../_util/classNames';
|
|
|
|
import ResizeObserver from '../vc-resize-observer';
|
2022-03-12 01:56:32 +00:00
|
|
|
import useDestroyed from '../_util/hooks/useDestroyed';
|
2022-03-26 14:52:54 +00:00
|
|
|
import type { MouseEventHandler } from '../_util/EventInterface';
|
2020-03-07 11:45:13 +00:00
|
|
|
|
2022-03-26 14:52:54 +00:00
|
|
|
export const pageHeaderProps = () => ({
|
2021-12-16 09:20:18 +00:00
|
|
|
backIcon: PropTypes.any,
|
2022-03-26 14:52:54 +00:00
|
|
|
prefixCls: String,
|
2021-12-16 09:20:18 +00:00
|
|
|
title: PropTypes.any,
|
|
|
|
subTitle: PropTypes.any,
|
2020-03-07 11:45:13 +00:00
|
|
|
breadcrumb: PropTypes.object,
|
|
|
|
tags: PropTypes.any,
|
2021-12-16 09:20:18 +00:00
|
|
|
footer: PropTypes.any,
|
|
|
|
extra: PropTypes.any,
|
2020-03-07 11:45:13 +00:00
|
|
|
avatar: PropTypes.object,
|
2022-03-26 14:52:54 +00:00
|
|
|
ghost: { type: Boolean, default: undefined },
|
|
|
|
onBack: Function as PropType<MouseEventHandler>,
|
|
|
|
});
|
2020-03-07 11:45:13 +00:00
|
|
|
|
2022-03-26 14:52:54 +00:00
|
|
|
export type PageHeaderProps = Partial<ExtractPropTypes<ReturnType<typeof pageHeaderProps>>>;
|
2020-03-07 11:45:13 +00:00
|
|
|
|
2021-06-07 09:35:03 +00:00
|
|
|
const PageHeader = defineComponent({
|
|
|
|
name: 'APageHeader',
|
2022-03-26 14:52:54 +00:00
|
|
|
props: pageHeaderProps(),
|
|
|
|
// emits: ['back'],
|
2021-06-07 09:35:03 +00:00
|
|
|
slots: ['backIcon', 'avatar', 'breadcrumb', 'title', 'subTitle', 'tags', 'extra', 'footer'],
|
|
|
|
setup(props, { emit, slots }) {
|
|
|
|
const { prefixCls, direction, pageHeader } = useConfigInject('page-header', props);
|
|
|
|
const compact = ref(false);
|
2022-03-12 01:56:32 +00:00
|
|
|
const isDestroyed = useDestroyed();
|
2021-06-07 09:35:03 +00:00
|
|
|
const onResize = ({ width }: { width: number }) => {
|
2022-03-12 01:56:32 +00:00
|
|
|
if (!isDestroyed.value) {
|
|
|
|
compact.value = width < 768;
|
|
|
|
}
|
2021-06-07 09:35:03 +00:00
|
|
|
};
|
2021-06-19 14:47:28 +00:00
|
|
|
const ghost = computed(() => props.ghost ?? pageHeader.value?.ghost ?? true);
|
2020-03-07 11:45:13 +00:00
|
|
|
|
2021-06-07 09:35:03 +00:00
|
|
|
const getBackIcon = () => {
|
|
|
|
return (
|
|
|
|
props.backIcon ??
|
|
|
|
slots.backIcon?.() ??
|
|
|
|
(direction.value === 'rtl' ? <ArrowRightOutlined /> : <ArrowLeftOutlined />)
|
|
|
|
);
|
|
|
|
};
|
2020-03-07 11:45:13 +00:00
|
|
|
|
2021-06-07 09:35:03 +00:00
|
|
|
const renderBack = (backIcon: any) => {
|
|
|
|
if (!backIcon || !props.onBack) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<LocaleReceiver
|
|
|
|
componentName="PageHeader"
|
|
|
|
children={({ back }: any) => (
|
|
|
|
<div class={`${prefixCls.value}-back`}>
|
|
|
|
<TransButton
|
|
|
|
onClick={e => {
|
|
|
|
emit('back', e);
|
|
|
|
}}
|
|
|
|
class={`${prefixCls.value}-back-button`}
|
|
|
|
aria-label={back}
|
|
|
|
>
|
|
|
|
{backIcon}
|
|
|
|
</TransButton>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
></LocaleReceiver>
|
|
|
|
);
|
|
|
|
};
|
2020-03-07 11:45:13 +00:00
|
|
|
|
2021-06-07 09:35:03 +00:00
|
|
|
const renderBreadcrumb = () => {
|
|
|
|
return props.breadcrumb ? <Breadcrumb {...props.breadcrumb} /> : slots.breadcrumb?.();
|
|
|
|
};
|
2020-03-07 11:45:13 +00:00
|
|
|
|
2021-06-07 09:35:03 +00:00
|
|
|
const renderTitle = () => {
|
|
|
|
const { avatar } = props;
|
|
|
|
const title = props.title ?? slots.title?.();
|
|
|
|
const subTitle = props.subTitle ?? slots.subTitle?.();
|
|
|
|
const tags = props.tags ?? slots.tags?.();
|
|
|
|
const extra = props.extra ?? slots.extra?.();
|
|
|
|
const headingPrefixCls = `${prefixCls.value}-heading`;
|
|
|
|
const hasHeading = title || subTitle || tags || extra;
|
|
|
|
// If there is nothing, return a null
|
|
|
|
if (!hasHeading) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const backIcon = getBackIcon();
|
|
|
|
const backIconDom = renderBack(backIcon);
|
|
|
|
const hasTitle = backIconDom || avatar || hasHeading;
|
|
|
|
return (
|
|
|
|
<div class={headingPrefixCls}>
|
|
|
|
{hasTitle && (
|
|
|
|
<div class={`${headingPrefixCls}-left`}>
|
|
|
|
{backIconDom}
|
|
|
|
{avatar ? <Avatar {...avatar} /> : slots.avatar?.()}
|
|
|
|
{title && (
|
|
|
|
<span
|
|
|
|
class={`${headingPrefixCls}-title`}
|
|
|
|
title={typeof title === 'string' ? title : undefined}
|
|
|
|
>
|
|
|
|
{title}
|
|
|
|
</span>
|
|
|
|
)}
|
|
|
|
{subTitle && (
|
|
|
|
<span
|
|
|
|
class={`${headingPrefixCls}-sub-title`}
|
|
|
|
title={typeof subTitle === 'string' ? subTitle : undefined}
|
|
|
|
>
|
|
|
|
{subTitle}
|
|
|
|
</span>
|
|
|
|
)}
|
|
|
|
{tags && <span class={`${headingPrefixCls}-tags`}>{tags}</span>}
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
{extra && <span class={`${headingPrefixCls}-extra`}>{extra}</span>}
|
|
|
|
</div>
|
|
|
|
);
|
2020-06-08 14:00:59 +00:00
|
|
|
};
|
2020-03-07 11:45:13 +00:00
|
|
|
|
2021-06-07 09:35:03 +00:00
|
|
|
const renderFooter = () => {
|
2021-06-19 14:47:28 +00:00
|
|
|
const footer = props.footer ?? filterEmpty(slots.footer?.());
|
|
|
|
return isEmptyContent(footer) ? null : (
|
|
|
|
<div class={`${prefixCls.value}-footer`}>{footer}</div>
|
|
|
|
);
|
2021-06-07 09:35:03 +00:00
|
|
|
};
|
2020-03-07 11:45:13 +00:00
|
|
|
|
2021-06-07 09:35:03 +00:00
|
|
|
const renderChildren = (children: any) => {
|
|
|
|
return <div class={`${prefixCls.value}-content`}>{children}</div>;
|
|
|
|
};
|
|
|
|
return () => {
|
|
|
|
const hasBreadcrumb = props.breadcrumb?.routes || slots.breadcrumb;
|
|
|
|
const hasFooter = props.footer || slots.footer;
|
|
|
|
const children = flattenChildren(slots.default?.());
|
|
|
|
const className = classNames(prefixCls.value, {
|
|
|
|
'has-breadcrumb': hasBreadcrumb,
|
|
|
|
'has-footer': hasFooter,
|
|
|
|
[`${prefixCls.value}-ghost`]: ghost.value,
|
|
|
|
[`${prefixCls.value}-rtl`]: direction.value === 'rtl',
|
|
|
|
[`${prefixCls.value}-compact`]: compact.value,
|
|
|
|
});
|
|
|
|
return (
|
|
|
|
<ResizeObserver onResize={onResize}>
|
|
|
|
<div class={className}>
|
|
|
|
{renderBreadcrumb()}
|
|
|
|
{renderTitle()}
|
2021-06-07 14:50:31 +00:00
|
|
|
{children.length ? renderChildren(children) : null}
|
2021-06-07 09:35:03 +00:00
|
|
|
{renderFooter()}
|
|
|
|
</div>
|
|
|
|
</ResizeObserver>
|
|
|
|
);
|
|
|
|
};
|
2020-03-07 11:45:13 +00:00
|
|
|
},
|
2020-10-18 14:30:06 +00:00
|
|
|
});
|
2020-03-07 11:45:13 +00:00
|
|
|
|
2020-11-01 07:03:33 +00:00
|
|
|
export default withInstall(PageHeader);
|