2019-03-02 15:51:08 +08:00
|
|
|
import * as React from 'react';
|
2019-06-20 19:31:59 +08:00
|
|
|
import classnames from 'classnames';
|
2020-01-02 19:10:16 +08:00
|
|
|
import { ArrowLeftOutlined, ArrowRightOutlined } from '@ant-design/icons';
|
2019-06-20 19:31:59 +08:00
|
|
|
|
2019-03-02 15:51:08 +08:00
|
|
|
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
|
|
|
|
import Tag 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;
|
2019-04-16 00:36:25 +08:00
|
|
|
tags?: React.ReactElement<Tag> | React.ReactElement<Tag>[];
|
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-01-02 19:10:16 +08:00
|
|
|
const getBackIcon = (props: PageHeaderProps, direction: string = 'ltr') => {
|
|
|
|
if (props.backIcon !== undefined) {
|
|
|
|
return props.backIcon;
|
|
|
|
}
|
|
|
|
return direction === 'rtl' ? <ArrowRightOutlined /> : <ArrowLeftOutlined />;
|
|
|
|
};
|
|
|
|
|
|
|
|
const renderTitle = (prefixCls: string, props: PageHeaderProps, direction: string = 'ltr') => {
|
|
|
|
const { title, avatar, subTitle, tags, extra, onBack } = props;
|
2019-06-26 18:49:18 +08:00
|
|
|
const headingPrefixCls = `${prefixCls}-heading`;
|
2019-05-09 23:19:10 +08:00
|
|
|
if (title || subTitle || tags || extra) {
|
2020-01-02 19:10:16 +08:00
|
|
|
const backIcon = getBackIcon(props, direction);
|
2019-08-25 15:38:51 +08:00
|
|
|
const backIconDom = renderBack(prefixCls, backIcon, onBack);
|
2019-05-09 23:19:10 +08:00
|
|
|
return (
|
2019-06-26 18:49:18 +08:00
|
|
|
<div className={headingPrefixCls}>
|
2019-08-25 15:38:51 +08:00
|
|
|
{backIconDom}
|
|
|
|
{avatar && <Avatar {...avatar} />}
|
2019-06-26 18:49:18 +08:00
|
|
|
{title && <span className={`${headingPrefixCls}-title`}>{title}</span>}
|
|
|
|
{subTitle && <span className={`${headingPrefixCls}-sub-title`}>{subTitle}</span>}
|
|
|
|
{tags && <span className={`${headingPrefixCls}-tags`}>{tags}</span>}
|
|
|
|
{extra && <span className={`${headingPrefixCls}-extra`}>{extra}</span>}
|
2019-05-09 23:19:10 +08:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return null;
|
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>;
|
|
|
|
};
|
|
|
|
|
2019-03-02 15:51:08 +08:00
|
|
|
const PageHeader: React.SFC<PageHeaderProps> = props => (
|
|
|
|
<ConfigConsumer>
|
2020-01-02 19:10:16 +08:00
|
|
|
{({ getPrefixCls, pageHeader, direction }: ConfigConsumerProps) => {
|
2019-03-04 14:54:28 +08:00
|
|
|
const {
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
style,
|
|
|
|
footer,
|
|
|
|
children,
|
2019-08-25 15:38:51 +08:00
|
|
|
breadcrumb,
|
2019-03-04 14:54:28 +08:00
|
|
|
className: customizeClassName,
|
|
|
|
} = props;
|
2019-10-15 11:46:12 +08:00
|
|
|
let ghost: undefined | boolean = true;
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2019-03-02 15:51:08 +08:00
|
|
|
const prefixCls = getPrefixCls('page-header', customizePrefixCls);
|
2019-09-06 13:30:47 +08:00
|
|
|
const breadcrumbDom = breadcrumb && breadcrumb.routes ? renderBreadcrumb(breadcrumb) : null;
|
2019-08-25 15:38:51 +08:00
|
|
|
const className = classnames(prefixCls, customizeClassName, {
|
|
|
|
'has-breadcrumb': breadcrumbDom,
|
|
|
|
'has-footer': footer,
|
2019-10-15 11:46:12 +08:00
|
|
|
[`${prefixCls}-ghost`]: ghost,
|
2020-01-02 19:10:16 +08:00
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
2019-08-25 15:38:51 +08:00
|
|
|
});
|
2019-03-04 14:54:28 +08:00
|
|
|
|
2019-03-02 15:51:08 +08:00
|
|
|
return (
|
|
|
|
<div className={className} style={style}>
|
2019-08-25 15:38:51 +08:00
|
|
|
{breadcrumbDom}
|
2020-01-02 19:10:16 +08:00
|
|
|
{renderTitle(prefixCls, props, direction)}
|
2019-08-25 15:38:51 +08:00
|
|
|
{children && renderChildren(prefixCls, children)}
|
2019-03-02 15:51:08 +08:00
|
|
|
{renderFooter(prefixCls, footer)}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
</ConfigConsumer>
|
|
|
|
);
|
|
|
|
|
|
|
|
export default PageHeader;
|