forked from vueComponent/ant-design-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.tsx
186 lines (173 loc) · 6.12 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import type { ExtractPropTypes, PropType } from 'vue';
import { defineComponent, ref, computed } from 'vue';
import PropTypes from '../_util/vue-types';
import { filterEmpty, flattenChildren, isEmptyContent } from '../_util/props-util';
import ArrowLeftOutlined from '@ant-design/icons-vue/ArrowLeftOutlined';
import ArrowRightOutlined from '@ant-design/icons-vue/ArrowRightOutlined';
import Breadcrumb from '../breadcrumb';
import type { AvatarProps } from '../avatar';
import Avatar from '../avatar';
import TransButton from '../_util/transButton';
import LocaleReceiver from '../locale-provider/LocaleReceiver';
import { objectType, vNodeType, withInstall } from '../_util/type';
import useConfigInject from '../config-provider/hooks/useConfigInject';
import classNames from '../_util/classNames';
import ResizeObserver from '../vc-resize-observer';
import useDestroyed from '../_util/hooks/useDestroyed';
import type { MouseEventHandler } from '../_util/EventInterface';
import Space from '../space';
// CSSINJS
import useStyle from './style';
export const pageHeaderProps = () => ({
backIcon: vNodeType(),
prefixCls: String,
title: vNodeType(),
subTitle: vNodeType(),
breadcrumb: PropTypes.object,
tags: vNodeType(),
footer: vNodeType(),
extra: vNodeType(),
avatar: objectType<AvatarProps>(),
ghost: { type: Boolean, default: undefined },
onBack: Function as PropType<MouseEventHandler>,
});
export type PageHeaderProps = Partial<ExtractPropTypes<ReturnType<typeof pageHeaderProps>>>;
const PageHeader = defineComponent({
compatConfig: { MODE: 3 },
name: 'APageHeader',
inheritAttrs: false,
props: pageHeaderProps(),
// emits: ['back'],
slots: ['backIcon', 'avatar', 'breadcrumb', 'title', 'subTitle', 'tags', 'extra', 'footer'],
setup(props, { emit, slots, attrs }) {
const { prefixCls, direction, pageHeader } = useConfigInject('page-header', props);
// style
const [wrapSSR, hashId] = useStyle(prefixCls);
const compact = ref(false);
const isDestroyed = useDestroyed();
const onResize = ({ width }: { width: number }) => {
if (!isDestroyed.value) {
compact.value = width < 768;
}
};
const ghost = computed(() => props.ghost ?? pageHeader?.value?.ghost ?? true);
const getBackIcon = () => {
return (
props.backIcon ??
slots.backIcon?.() ??
(direction.value === 'rtl' ? <ArrowRightOutlined /> : <ArrowLeftOutlined />)
);
};
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>
);
};
const renderBreadcrumb = () => {
return props.breadcrumb ? <Breadcrumb {...props.breadcrumb} /> : slots.breadcrumb?.();
};
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`}>
<Space>{extra}</Space>
</span>
)}
</div>
);
};
const renderFooter = () => {
const footer = props.footer ?? filterEmpty(slots.footer?.());
return isEmptyContent(footer) ? null : (
<div class={`${prefixCls.value}-footer`}>{footer}</div>
);
};
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,
},
attrs.class,
hashId.value,
);
return wrapSSR(
<ResizeObserver onResize={onResize}>
<div {...attrs} class={className}>
{renderBreadcrumb()}
{renderTitle()}
{children.length ? renderChildren(children) : null}
{renderFooter()}
</div>
</ResizeObserver>,
);
};
},
});
export default withInstall(PageHeader);