ant-design/components/page-header/index.tsx
vagusX 149729e524
use ant design icons 4.0 (#18217)
* feat: use @ant-design/icons@4.0

* feat: use createFromIconfontCN to make site works

* feat: update doc for Icon

* feat: use icon in component Alert

* feat: use icon in component Avatar

* feat: use icon in component Breadcrumb

* feat: use icon in component Button

* feat: use icon in component Cascader

* feat: use icon in component Collapse

* feat: use icon in component Datepicker

* feat: use icon in component Dropdown

* feat: use icon in component Form

* feat: use icon in component Input

* feat: use icon in component InputNumber

* feat: use icon in component Layout

* feat: use icon in component Mention

* feat: use icon in component Message

* feat: use icon in component Modal

* feat: use icon in component Notification

* feat: use icon in component PageHeader

* feat: use icon in component Pagination

* feat: use icon in component Popconfirm

* feat: use icon in component Progress

* feat: use icon in component Rate

* feat: use icon in component Result

* feat: use icon in component Select

* feat: use icon in component Step

* feat: use icon in component Switch

* feat: use icon in component Table

* feat: use icon in component Tab

* feat: use icon in component Tag

* feat: handle rest component which using Icon

* fix: remove unused vars

* feat: use latest alpha ant design icons

* fix: failed test in uploadlist.test.js

* test: update snapshot for icons

* doc: add Icon for site

* doc: use @ant-design/icons in site

* chore: use latest icons

* fix: tslint issue

* fix: test cases

* fix: types for react

* fix: lint rules for import orders

* fix: use @ant-design/icons@4.0.0-alpha.5 to avoid insert css in server render

* fix: eslint error in demo/**.md

* inject antd icons

* update snapshot

* fix site

* doc: update docs

* fix: code snippets icon in site

* feat: use latest @ant-design/icons

* fix: icon props in message
2019-08-13 14:07:17 +08:00

128 lines
3.6 KiB
TypeScript

import * as React from 'react';
import classnames from 'classnames';
import { ArrowLeft } from '@ant-design/icons';
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
import Divider from '../divider';
import Tag from '../tag';
import Breadcrumb, { BreadcrumbProps } from '../breadcrumb';
import TransButton from '../_util/transButton';
import LocaleReceiver from '../locale-provider/LocaleReceiver';
export interface PageHeaderProps {
backIcon?: React.ReactNode;
prefixCls?: string;
title: React.ReactNode;
subTitle?: React.ReactNode;
style?: React.CSSProperties;
breadcrumb?: BreadcrumbProps;
tags?: React.ReactElement<Tag> | React.ReactElement<Tag>[];
footer?: React.ReactNode;
extra?: React.ReactNode;
onBack?: (e: React.MouseEvent<HTMLDivElement>) => void;
className?: string;
}
const renderBack = (
prefixCls: string,
backIcon?: React.ReactNode,
onBack?: (e: React.MouseEvent<HTMLElement>) => void,
) => {
if (!backIcon || !onBack) {
return null;
}
return (
<LocaleReceiver componentName="PageHeader">
{({ back }: { back: string }) => (
<div className={`${prefixCls}-back`}>
<TransButton
onClick={(e: React.MouseEvent<HTMLDivElement>) => {
if (onBack) {
onBack(e);
}
}}
className={`${prefixCls}-back-button`}
aria-label={back}
>
{backIcon}
</TransButton>
<Divider type="vertical" />
</div>
)}
</LocaleReceiver>
);
};
const renderBreadcrumb = (breadcrumb: BreadcrumbProps) => {
return <Breadcrumb {...breadcrumb} />;
};
const renderHeader = (prefixCls: string, props: PageHeaderProps) => {
const { breadcrumb, backIcon, onBack } = props;
if (breadcrumb && breadcrumb.routes) {
return renderBreadcrumb(breadcrumb);
}
return renderBack(prefixCls, backIcon, onBack);
};
const renderTitle = (prefixCls: string, props: PageHeaderProps) => {
const { title, subTitle, tags, extra } = props;
const headingPrefixCls = `${prefixCls}-heading`;
if (title || subTitle || tags || extra) {
return (
<div className={headingPrefixCls}>
{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>}
</div>
);
}
return null;
};
const renderFooter = (prefixCls: string, footer: React.ReactNode) => {
if (footer) {
return <div className={`${prefixCls}-footer`}>{footer}</div>;
}
return null;
};
const PageHeader: React.SFC<PageHeaderProps> = props => (
<ConfigConsumer>
{({ getPrefixCls }: ConfigConsumerProps) => {
const {
prefixCls: customizePrefixCls,
style,
footer,
children,
className: customizeClassName,
} = props;
const prefixCls = getPrefixCls('page-header', customizePrefixCls);
const className = classnames(
prefixCls,
{
[`${prefixCls}-has-footer`]: footer,
},
customizeClassName,
);
return (
<div className={className} style={style}>
{renderHeader(prefixCls, props)}
{renderTitle(prefixCls, props)}
{children && <div className={`${prefixCls}-content`}>{children}</div>}
{renderFooter(prefixCls, footer)}
</div>
);
}}
</ConfigConsumer>
);
PageHeader.defaultProps = {
backIcon: <ArrowLeft />,
};
export default PageHeader;