Commit 93cc3870 authored by 陈帅's avatar 陈帅

optimize menu

parent c9a09d85
...@@ -371,19 +371,22 @@ const menuData = [ ...@@ -371,19 +371,22 @@ const menuData = [
}, },
]; ];
function formatter(data, parentPath = '/', parentAuthority) { function formatter(data, parentPath = '/', parentAuthority, parentName) {
return data.map(item => { return data.map(item => {
let { path } = item; let { path } = item;
const id = parentName ? `${parentName}.${item.name}` : `menu.${item.name}`;
if (!isUrl(path)) { if (!isUrl(path)) {
path = parentPath + item.path; path = parentPath + item.path;
} }
const result = { const result = {
...item, ...item,
path, path,
locale: id,
authority: item.authority || parentAuthority, authority: item.authority || parentAuthority,
}; };
if (item.children) { if (item.children) {
result.children = formatter(item.children, `${parentPath}${item.path}/`, item.authority); result.children = formatter(item.children, `${parentPath}${item.path}/`, item.authority, id);
} }
return result; return result;
}); });
......
...@@ -79,10 +79,9 @@ export default class BaseMenu extends PureComponent { ...@@ -79,10 +79,9 @@ export default class BaseMenu extends PureComponent {
/** /**
* get SubMenu or Item * get SubMenu or Item
*/ */
getSubMenuOrItem = (item, parent) => { getSubMenuOrItem = item => {
const id = parent ? `${parent}.${item.name}` : `menu.${item.name}`;
if (item.children && item.children.some(child => child.name)) { if (item.children && item.children.some(child => child.name)) {
const name = <FormattedMessage defaultMessage={item.name} id={id} />; const name = <FormattedMessage defaultMessage={item.name} id={item.locale} />;
return ( return (
<SubMenu <SubMenu
title={ title={
...@@ -97,11 +96,11 @@ export default class BaseMenu extends PureComponent { ...@@ -97,11 +96,11 @@ export default class BaseMenu extends PureComponent {
} }
key={item.path} key={item.path}
> >
{this.getNavMenuItems(item.children, id)} {this.getNavMenuItems(item.children)}
</SubMenu> </SubMenu>
); );
} else { } else {
return <Menu.Item key={item.path}>{this.getMenuItemPath(item, parent)}</Menu.Item>; return <Menu.Item key={item.path}>{this.getMenuItemPath(item)}</Menu.Item>;
} }
}; };
...@@ -110,9 +109,8 @@ export default class BaseMenu extends PureComponent { ...@@ -110,9 +109,8 @@ export default class BaseMenu extends PureComponent {
* Judge whether it is http link.return a or Link * Judge whether it is http link.return a or Link
* @memberof SiderMenu * @memberof SiderMenu
*/ */
getMenuItemPath = (item, parent) => { getMenuItemPath = item => {
const id = parent ? `${parent}.${item.name}` : `menu.${item.name}`; const name = <FormattedMessage defaultMessage={item.name} id={item.locale} />;
const name = <FormattedMessage defaultMessage={item.name} id={id} />;
const itemPath = this.conversionPath(item.path); const itemPath = this.conversionPath(item.path);
const icon = getIcon(item.icon); const icon = getIcon(item.icon);
const { target } = item; const { target } = item;
......
...@@ -105,17 +105,16 @@ export default class SiderMenu extends PureComponent { ...@@ -105,17 +105,16 @@ export default class SiderMenu extends PureComponent {
* @memberof SiderMenu * @memberof SiderMenu
*/ */
getMenuItemPath = item => { getMenuItemPath = item => {
console.log(item);
const itemPath = this.conversionPath(item.path); const itemPath = this.conversionPath(item.path);
const icon = getIcon(item.icon); const icon = getIcon(item.icon);
const { target, name } = item; const { target, name, locale } = item;
// Is it a http link // Is it a http link
if (/^https?:\/\//.test(itemPath)) { if (/^https?:\/\//.test(itemPath)) {
return ( return (
<a href={itemPath} target={target}> <a href={itemPath} target={target}>
{icon} {icon}
<span> <span>
<FormattedMessage id={name} /> <FormattedMessage id={locale} defaultMessage={name} />
</span> </span>
</a> </a>
); );
...@@ -136,7 +135,7 @@ export default class SiderMenu extends PureComponent { ...@@ -136,7 +135,7 @@ export default class SiderMenu extends PureComponent {
> >
{icon} {icon}
<span> <span>
<FormattedMessage id={name} /> <FormattedMessage id={locale} defaultMessage={name} />
</span> </span>
</Link> </Link>
); );
...@@ -159,7 +158,7 @@ export default class SiderMenu extends PureComponent { ...@@ -159,7 +158,7 @@ export default class SiderMenu extends PureComponent {
<span>{item.name}</span> <span>{item.name}</span>
</span> </span>
) : ( ) : (
item.name <FormattedMessage id={item.locale} defaultMessage={item.name} />
) )
} }
key={item.path} key={item.path}
......
...@@ -7,7 +7,7 @@ export default { ...@@ -7,7 +7,7 @@ export default {
data: appLocaleData, data: appLocaleData,
antd: antdEn, antd: antdEn,
messages: { messages: {
'menu.home': 'home', 'menu.home': 'Home',
'menu.dashboard': 'Dashboard', 'menu.dashboard': 'Dashboard',
'menu.dashboard.analysis': 'Analysis', 'menu.dashboard.analysis': 'Analysis',
'menu.dashboard.monitor': 'Monitor', 'menu.dashboard.monitor': 'Monitor',
......
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