Commit 123401a9 authored by 陈帅's avatar 陈帅

Optimize the Breadcrumb

parent 93cc3870
...@@ -60,21 +60,25 @@ export default class PageHeader extends PureComponent { ...@@ -60,21 +60,25 @@ export default class PageHeader extends PureComponent {
// Generated according to props // Generated according to props
conversionFromProps = () => { conversionFromProps = () => {
const { breadcrumbList, breadcrumbSeparator, linkElement = 'a' } = this.props; const { breadcrumbList, breadcrumbSeparator, linkElement = 'a' } = this.props;
return ( return (
<Breadcrumb className={styles.breadcrumb} separator={breadcrumbSeparator}> <Breadcrumb className={styles.breadcrumb} separator={breadcrumbSeparator}>
{breadcrumbList.map(item => ( {breadcrumbList.map(item => {
<Breadcrumb.Item key={item.title}> const title = this.props.itemRender ? this.props.itemRender(item) : item.title;
{item.href return (
? createElement( <Breadcrumb.Item key={item.title}>
linkElement, {item.href
{ ? createElement(
[linkElement === 'a' ? 'href' : 'to']: item.href, linkElement,
}, {
item.title [linkElement === 'a' ? 'href' : 'to']: item.href,
) },
: item.title} title
</Breadcrumb.Item> )
))} : title}
</Breadcrumb.Item>
);
})}
</Breadcrumb> </Breadcrumb>
); );
}; };
...@@ -85,14 +89,18 @@ export default class PageHeader extends PureComponent { ...@@ -85,14 +89,18 @@ export default class PageHeader extends PureComponent {
const pathSnippets = urlToList(routerLocation.pathname); const pathSnippets = urlToList(routerLocation.pathname);
// Loop data mosaic routing // Loop data mosaic routing
const extraBreadcrumbItems = pathSnippets.map((url, index) => { const extraBreadcrumbItems = pathSnippets.map((url, index) => {
console.log(url);
const currentBreadcrumb = getBreadcrumb(breadcrumbNameMap, url); const currentBreadcrumb = getBreadcrumb(breadcrumbNameMap, url);
const isLinkable = index !== pathSnippets.length - 1 && currentBreadcrumb.component; const isLinkable = index !== pathSnippets.length - 1 && currentBreadcrumb.component;
const name = this.props.itemRender
? this.props.itemRender(currentBreadcrumb)
: currentBreadcrumb.name;
return currentBreadcrumb.name && !currentBreadcrumb.hideInBreadcrumb ? ( return currentBreadcrumb.name && !currentBreadcrumb.hideInBreadcrumb ? (
<Breadcrumb.Item key={url}> <Breadcrumb.Item key={url}>
{createElement( {createElement(
isLinkable ? linkElement : 'span', isLinkable ? linkElement : 'span',
{ [linkElement === 'a' ? 'href' : 'to']: url }, { [linkElement === 'a' ? 'href' : 'to']: url },
currentBreadcrumb.name name
)} )}
</Breadcrumb.Item> </Breadcrumb.Item>
) : null; ) : null;
......
...@@ -28,6 +28,7 @@ order: 11 ...@@ -28,6 +28,7 @@ order: 11
| tabActiveKey | 当前高亮的 tab 项 | string | - | | tabActiveKey | 当前高亮的 tab 项 | string | - |
| tabDefaultActiveKey | 默认高亮的 tab 项 | string | 第一项 | | tabDefaultActiveKey | 默认高亮的 tab 项 | string | 第一项 |
| onTabChange | 切换面板的回调 | (key) => void | - | | onTabChange | 切换面板的回调 | (key) => void | - |
| itemRender | 自定义节点方法 | (menuItem) => ReactNode | - |
| linkElement | 定义链接的元素,默认为 `a`,可传入 react-router 的 Link | string\|ReactElement | - | | linkElement | 定义链接的元素,默认为 `a`,可传入 react-router 的 Link | string\|ReactElement | - |
> 面包屑的配置方式有三种,一是直接配置 `breadcrumbList`,二是结合 `react-router@2` `react-router@3`,配置 `routes` 及 `params` 实现,类似 [面包屑 Demo](https://ant.design/components/breadcrumb-cn/#components-breadcrumb-demo-router),三是结合 `react-router@4`,配置 `location` `breadcrumbNameMap`,优先级依次递减,脚手架中使用最后一种。 对于后两种用法,你也可以将 `routes` `params` 及 `location` `breadcrumbNameMap` 放到 context 中,组件会自动获取。 > 面包屑的配置方式有三种,一是直接配置 `breadcrumbList`,二是结合 `react-router@2` `react-router@3`,配置 `routes` 及 `params` 实现,类似 [面包屑 Demo](https://ant.design/components/breadcrumb-cn/#components-breadcrumb-demo-router),三是结合 `react-router@4`,配置 `location` `breadcrumbNameMap`,优先级依次递减,脚手架中使用最后一种。 对于后两种用法,你也可以将 `routes` `params` 及 `location` `breadcrumbNameMap` 放到 context 中,组件会自动获取。
...@@ -26,18 +26,18 @@ const { AuthorizedRoute, check } = Authorized; ...@@ -26,18 +26,18 @@ const { AuthorizedRoute, check } = Authorized;
* @param {Object} menuData 菜单配置 * @param {Object} menuData 菜单配置
* @param {Object} routerData 路由配置 * @param {Object} routerData 路由配置
*/ */
const getBreadcrumbNameMap = (menuData, routerData) => { const getBreadcrumbNameMap = (meun, router) => {
const result = {}; const routerMap = {};
const childResult = {}; const mergeMeunAndRouter = meunData => {
for (const i of menuData) { meunData.forEach(meunItem => {
if (!routerData[i.path]) { routerMap[meunItem.path] = Object.assign(meunItem, router);
result[i.path] = i; if (meunItem.children) {
} mergeMeunAndRouter(meunItem.children);
if (i.children) { }
Object.assign(childResult, getBreadcrumbNameMap(i.children, routerData)); });
} };
} mergeMeunAndRouter(meun);
return Object.assign({}, routerData, result, childResult); return routerMap;
}; };
const query = { const query = {
...@@ -68,6 +68,7 @@ const query = { ...@@ -68,6 +68,7 @@ const query = {
class BasicLayout extends React.PureComponent { class BasicLayout extends React.PureComponent {
getContext() { getContext() {
const { location, routerData, menuData } = this.props; const { location, routerData, menuData } = this.props;
console.log(getBreadcrumbNameMap(menuData, routerData));
return { return {
location, location,
breadcrumbNameMap: getBreadcrumbNameMap(menuData, routerData), breadcrumbNameMap: getBreadcrumbNameMap(menuData, routerData),
......
...@@ -18,6 +18,12 @@ const PageHeaderLayout = ({ children, wrapperClassName, top, ...restProps }) => ...@@ -18,6 +18,12 @@ const PageHeaderLayout = ({ children, wrapperClassName, top, ...restProps }) =>
key="pageheader" key="pageheader"
{...restProps} {...restProps}
linkElement={Link} linkElement={Link}
itemRender={item => {
if (item.locale) {
return <FormattedMessage id={item.locale} defaultMessage={item.name} />;
}
return item.name;
}}
/> />
); );
}} }}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment