import React, { PureComponent } from 'react'; import PropTypes from 'prop-types'; import { Breadcrumb, Tabs } from 'antd'; import { Link } from 'dva/router'; import classNames from 'classnames'; import styles from './index.less'; const { TabPane } = Tabs; function itemRender(route, params, routes, paths) { const last = routes.indexOf(route) === routes.length - 1; return (last || !route.component) ? {route.breadcrumbName} : {route.breadcrumbName}; } export default class PageHeader extends PureComponent { static contextTypes = { routes: PropTypes.array, params: PropTypes.object, location: PropTypes.object, breadcrumbNameMap: PropTypes.object, }; onChange = (key) => { if (this.props.onTabChange) { this.props.onTabChange(key); } }; getBreadcrumbProps = () => { return { routes: this.props.routes || this.context.routes, params: this.props.params || this.context.params, location: this.props.location || this.context.location, breadcrumbNameMap: this.props.breadcrumbNameMap || this.context.breadcrumbNameMap, }; }; render() { const { routes, params, location, breadcrumbNameMap } = this.getBreadcrumbProps(); const { title, logo, action, content, extraContent, breadcrumbList, tabList, className } = this.props; const clsString = classNames(styles.pageHeader, className); let breadcrumb; if (routes && params) { breadcrumb = ( route.breadcrumbName)} params={params} itemRender={itemRender} /> ); } else if (location && location.pathname) { const pathSnippets = location.pathname.split('/').filter(i => i); const extraBreadcrumbItems = pathSnippets.map((_, index) => { const url = `/${pathSnippets.slice(0, index + 1).join('/')}`; return ( {breadcrumbNameMap[url] || breadcrumbNameMap[url.replace('/', '')] || url} ); }); const breadcrumbItems = [( Home )].concat(extraBreadcrumbItems); breadcrumb = ( {breadcrumbItems} ); } else if (breadcrumbList && breadcrumbList.length) { breadcrumb = ( { breadcrumbList.map(item => ( {item.href ? {item.title} : item.title} ) ) } ); } else { breadcrumb = null; } const tabDefaultValue = tabList && tabList.filter(item => item.default)[0]; return (
{breadcrumb}
{logo &&
{logo}
}
{title &&

{title}

} {action &&
{action}
}
{content &&
{content}
} {extraContent &&
{extraContent}
}
{ tabList && tabList.length && { tabList.map(item => ) } }
); } }