2019-03-02 15:51:08 +08:00
|
|
|
import * as React from 'react';
|
2020-07-20 13:41:05 +08:00
|
|
|
import classNames from 'classnames';
|
2020-03-02 12:09:38 +08:00
|
|
|
import ArrowLeftOutlined from '@ant-design/icons/ArrowLeftOutlined';
|
|
|
|
import ArrowRightOutlined from '@ant-design/icons/ArrowRightOutlined';
|
2020-04-10 19:25:31 +08:00
|
|
|
import ResizeObserver from 'rc-resize-observer';
|
2020-11-04 14:12:45 +08:00
|
|
|
import { ConfigConsumer, ConfigConsumerProps, DirectionType } from '../config-provider';
|
2020-04-28 17:42:23 +08:00
|
|
|
import { TagType } from '../tag';
|
2019-06-20 19:31:59 +08:00
|
|
|
import Breadcrumb, { BreadcrumbProps } from '../breadcrumb';
|
2019-08-25 15:38:51 +08:00
|
|
|
import Avatar, { AvatarProps } from '../avatar';
|
2019-04-19 17:52:27 +08:00
|
|
|
import TransButton from '../_util/transButton';
|
|
|
|
import LocaleReceiver from '../locale-provider/LocaleReceiver';
|
2019-03-02 15:51:08 +08:00
|
|
|
|
|
|
|
export interface PageHeaderProps {
|
|
|
|
backIcon?: React.ReactNode;
|
|
|
|
prefixCls?: string;
|
|
|
|
title: React.ReactNode;
|
|
|
|
subTitle?: React.ReactNode;
|
|
|
|
style?: React.CSSProperties;
|
|
|
|
breadcrumb?: BreadcrumbProps;
|
2020-04-28 17:42:23 +08:00
|
|
|
tags?: React.ReactElement<TagType> | React.ReactElement<TagType>[];
|
2019-03-02 15:51:08 +08:00
|
|
|
footer?: React.ReactNode;
|
|
|
|
extra?: React.ReactNode;
|
2019-08-25 15:38:51 +08:00
|
|
|
avatar?: AvatarProps;
|
2019-04-25 12:18:01 +08:00
|
|
|
onBack?: (e: React.MouseEvent<HTMLDivElement>) => void;
|
2019-03-04 14:54:28 +08:00
|
|
|
className?: string;
|
2019-10-15 11:46:12 +08:00
|
|
|
ghost?: boolean;
|
2019-03-02 15:51:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const renderBack = (
|
|
|
|
prefixCls: string,
|
|
|
|
backIcon?: React.ReactNode,
|
|
|
|
onBack?: (e: React.MouseEvent<HTMLElement>) => void,
|
|
|
|
) => {
|
|
|
|
if (!backIcon || !onBack) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return (
|
2019-04-19 17:52:27 +08:00
|
|
|
<LocaleReceiver componentName="PageHeader">
|
|
|
|
{({ back }: { back: string }) => (
|
2019-04-25 15:13:13 +08:00
|
|
|
<div className={`${prefixCls}-back`}>
|
2019-04-25 12:18:01 +08:00
|
|
|
<TransButton
|
|
|
|
onClick={(e: React.MouseEvent<HTMLDivElement>) => {
|
|
|
|
if (onBack) {
|
|
|
|
onBack(e);
|
|
|
|
}
|
|
|
|
}}
|
2019-04-25 15:13:13 +08:00
|
|
|
className={`${prefixCls}-back-button`}
|
2019-04-25 12:18:01 +08:00
|
|
|
aria-label={back}
|
|
|
|
>
|
2019-04-20 10:56:11 +08:00
|
|
|
{backIcon}
|
|
|
|
</TransButton>
|
2019-04-19 17:52:27 +08:00
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</LocaleReceiver>
|
2019-03-02 15:51:08 +08:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const renderBreadcrumb = (breadcrumb: BreadcrumbProps) => {
|
|
|
|
return <Breadcrumb {...breadcrumb} />;
|
|
|
|
};
|
|
|
|
|
2020-11-04 14:12:45 +08:00
|
|
|
const getBackIcon = (props: PageHeaderProps, direction: DirectionType = 'ltr') => {
|
2020-01-02 19:10:16 +08:00
|
|
|
if (props.backIcon !== undefined) {
|
|
|
|
return props.backIcon;
|
|
|
|
}
|
|
|
|
return direction === 'rtl' ? <ArrowRightOutlined /> : <ArrowLeftOutlined />;
|
|
|
|
};
|
|
|
|
|
2020-11-04 14:12:45 +08:00
|
|
|
const renderTitle = (
|
|
|
|
prefixCls: string,
|
|
|
|
props: PageHeaderProps,
|
|
|
|
direction: DirectionType = 'ltr',
|
|
|
|
) => {
|
2020-01-02 19:10:16 +08:00
|
|
|
const { title, avatar, subTitle, tags, extra, onBack } = props;
|
2019-06-26 18:49:18 +08:00
|
|
|
const headingPrefixCls = `${prefixCls}-heading`;
|
2020-11-02 13:43:03 +08:00
|
|
|
const hasHeading = title || subTitle || tags || extra;
|
|
|
|
// 如果 什么都没有,直接返回一个 null
|
|
|
|
if (!hasHeading) {
|
2020-11-02 12:12:46 +08:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const backIcon = getBackIcon(props, direction);
|
|
|
|
const backIconDom = renderBack(prefixCls, backIcon, onBack);
|
2020-11-02 13:43:03 +08:00
|
|
|
const hasTitle = backIconDom || avatar || hasHeading;
|
2020-11-02 12:12:46 +08:00
|
|
|
return (
|
|
|
|
<div className={headingPrefixCls}>
|
|
|
|
{hasTitle && (
|
|
|
|
<div className={`${headingPrefixCls}-left`}>
|
|
|
|
{backIconDom}
|
|
|
|
{avatar && <Avatar {...avatar} />}
|
|
|
|
{title && (
|
|
|
|
<span
|
|
|
|
className={`${headingPrefixCls}-title`}
|
|
|
|
title={typeof title === 'string' ? title : undefined}
|
|
|
|
>
|
|
|
|
{title}
|
|
|
|
</span>
|
|
|
|
)}
|
|
|
|
{subTitle && (
|
|
|
|
<span
|
|
|
|
className={`${headingPrefixCls}-sub-title`}
|
|
|
|
title={typeof subTitle === 'string' ? subTitle : undefined}
|
|
|
|
>
|
|
|
|
{subTitle}
|
|
|
|
</span>
|
2020-04-10 19:25:31 +08:00
|
|
|
)}
|
2020-11-02 12:12:46 +08:00
|
|
|
{tags && <span className={`${headingPrefixCls}-tags`}>{tags}</span>}
|
2020-04-10 19:25:31 +08:00
|
|
|
</div>
|
2020-11-02 12:12:46 +08:00
|
|
|
)}
|
|
|
|
{extra && <span className={`${headingPrefixCls}-extra`}>{extra}</span>}
|
|
|
|
</div>
|
|
|
|
);
|
2019-03-02 15:51:08 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const renderFooter = (prefixCls: string, footer: React.ReactNode) => {
|
|
|
|
if (footer) {
|
|
|
|
return <div className={`${prefixCls}-footer`}>{footer}</div>;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
2019-08-25 15:38:51 +08:00
|
|
|
const renderChildren = (prefixCls: string, children: React.ReactNode) => {
|
|
|
|
return <div className={`${prefixCls}-content`}>{children}</div>;
|
|
|
|
};
|
|
|
|
|
2020-04-10 19:25:31 +08:00
|
|
|
const PageHeader: React.FC<PageHeaderProps> = props => {
|
|
|
|
const [compact, updateCompact] = React.useState(false);
|
|
|
|
const onResize = ({ width }: { width: number }) => {
|
2020-04-15 12:44:35 +08:00
|
|
|
updateCompact(width < 768);
|
2020-04-10 19:25:31 +08:00
|
|
|
};
|
|
|
|
return (
|
|
|
|
<ConfigConsumer>
|
|
|
|
{({ getPrefixCls, pageHeader, direction }: ConfigConsumerProps) => {
|
|
|
|
const {
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
style,
|
|
|
|
footer,
|
|
|
|
children,
|
|
|
|
breadcrumb,
|
|
|
|
className: customizeClassName,
|
|
|
|
} = props;
|
|
|
|
let ghost: undefined | boolean = true;
|
2019-10-15 11:46:12 +08:00
|
|
|
|
2020-04-10 19:25:31 +08:00
|
|
|
// Use `ghost` from `props` or from `ConfigProvider` instead.
|
|
|
|
if ('ghost' in props) {
|
|
|
|
ghost = props.ghost;
|
|
|
|
} else if (pageHeader && 'ghost' in pageHeader) {
|
|
|
|
ghost = pageHeader.ghost;
|
|
|
|
}
|
2019-03-04 14:54:28 +08:00
|
|
|
|
2020-04-10 19:25:31 +08:00
|
|
|
const prefixCls = getPrefixCls('page-header', customizePrefixCls);
|
|
|
|
const breadcrumbDom = breadcrumb && breadcrumb.routes ? renderBreadcrumb(breadcrumb) : null;
|
2020-07-20 13:41:05 +08:00
|
|
|
const className = classNames(prefixCls, customizeClassName, {
|
2020-04-10 19:25:31 +08:00
|
|
|
'has-breadcrumb': breadcrumbDom,
|
|
|
|
'has-footer': footer,
|
|
|
|
[`${prefixCls}-ghost`]: ghost,
|
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
|
|
[`${prefixCls}-compact`]: compact,
|
|
|
|
});
|
2019-03-04 14:54:28 +08:00
|
|
|
|
2020-04-10 19:25:31 +08:00
|
|
|
return (
|
|
|
|
<ResizeObserver onResize={onResize}>
|
|
|
|
<div className={className} style={style}>
|
|
|
|
{breadcrumbDom}
|
|
|
|
{renderTitle(prefixCls, props, direction)}
|
|
|
|
{children && renderChildren(prefixCls, children)}
|
|
|
|
{renderFooter(prefixCls, footer)}
|
|
|
|
</div>
|
|
|
|
</ResizeObserver>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
</ConfigConsumer>
|
|
|
|
);
|
|
|
|
};
|
2019-03-02 15:51:08 +08:00
|
|
|
|
|
|
|
export default PageHeader;
|