Commit 4ddbfe6a authored by Alan Wei's avatar Alan Wei Committed by ι™ˆεΈ…

refactor getFlatMenuKeys & getMeunMatchKeys to improve code readability & robustness

parent e09da6b5
...@@ -22,17 +22,37 @@ const getIcon = icon => { ...@@ -22,17 +22,37 @@ const getIcon = icon => {
return icon; return icon;
}; };
export const getMeunMatchKeys = (flatMenuKeys, path) => { /**
return flatMenuKeys.filter(item => { * Recursively flatten the data
return pathToRegexp(item).test(path); * [{path:string},{path:string}] => {path,path2}
}); * @param menu
}; */
export const getFlatMenuKeys = menu =>
menu.reduce((keys, item) => {
keys.push(item.path);
if (item.children) {
return keys.concat(getFlatMenuKeys(item.children));
}
return keys;
}, []);
/**
* Find all matched menu keys based on paths
* @param flatMenuKeys: [/abc, /abc/:id, /abc/:id/info]
* @param paths: [/abc, /abc/11, /abc/11/info]
*/
export const getMeunMatchKeys = (flatMenuKeys, paths) =>
paths.reduce(
(matchKeys, path) =>
matchKeys.concat(flatMenuKeys.filter(item => pathToRegexp(item).test(path))),
[]
);
export default class SiderMenu extends PureComponent { export default class SiderMenu extends PureComponent {
constructor(props) { constructor(props) {
super(props); super(props);
this.menus = props.menuData; this.menus = props.menuData;
this.flatMenuKeys = this.getFlatMenuKeys(props.menuData); this.flatMenuKeys = getFlatMenuKeys(props.menuData);
this.state = { this.state = {
openKeys: this.getDefaultCollapsedSubMenus(props), openKeys: this.getDefaultCollapsedSubMenus(props),
}; };
...@@ -51,26 +71,7 @@ export default class SiderMenu extends PureComponent { ...@@ -51,26 +71,7 @@ export default class SiderMenu extends PureComponent {
*/ */
getDefaultCollapsedSubMenus(props) { getDefaultCollapsedSubMenus(props) {
const { location: { pathname } } = props || this.props; const { location: { pathname } } = props || this.props;
return urlToList(pathname) return getMeunMatchKeys(this.flatMenuKeys, urlToList(pathname));
.map(item => {
return getMeunMatchKeys(this.flatMenuKeys, item)[0];
})
.filter(item => item);
}
/**
* Recursively flatten the data
* [{path:string},{path:string}] => {path,path2}
* @param menus
*/
getFlatMenuKeys(menus) {
let keys = [];
menus.forEach(item => {
if (item.children) {
keys = keys.concat(this.getFlatMenuKeys(item.children));
}
keys.push(item.path);
});
return keys;
} }
/** /**
* εˆ€ζ–­ζ˜―ε¦ζ˜―httpι“ΎζŽ₯.θΏ”ε›ž Link ζˆ– a * εˆ€ζ–­ζ˜―ε¦ζ˜―httpι“ΎζŽ₯.θΏ”ε›ž Link ζˆ– a
...@@ -159,7 +160,7 @@ export default class SiderMenu extends PureComponent { ...@@ -159,7 +160,7 @@ export default class SiderMenu extends PureComponent {
// Get the currently selected menu // Get the currently selected menu
getSelectedMenuKeys = () => { getSelectedMenuKeys = () => {
const { location: { pathname } } = this.props; const { location: { pathname } } = this.props;
return urlToList(pathname).map(itemPath => getMeunMatchKeys(this.flatMenuKeys, itemPath).pop()); return getMeunMatchKeys(this.flatMenuKeys, urlToList(pathname));
}; };
// conversion Path // conversion Path
// θ½¬εŒ–θ·―εΎ„ // θ½¬εŒ–θ·―εΎ„
......
import { getMeunMatchKeys } from './SiderMenu'; import { urlToList } from '../_utils/pathTools';
import { getFlatMenuKeys, getMeunMatchKeys } from './SiderMenu';
const meun = ['/dashboard', '/userinfo', '/dashboard/name', '/userinfo/:id', '/userinfo/:id/info']; const menu = [{
path: '/dashboard',
children: [{
path: '/dashboard/name',
}],
}, {
path: '/userinfo',
children: [{
path: '/userinfo/:id',
children: [{
path: '/userinfo/:id/info',
}],
}],
}];
describe('test meun match', () => { const flatMenuKeys = getFlatMenuKeys(menu);
describe('test convert tree structure menu paths to flat menu paths', () => {
it('simple menu', () => {
expect(flatMenuKeys).toEqual(
['/dashboard', '/dashboard/name', '/userinfo', '/userinfo/:id', '/userinfo/:id/info']
);
})
});
describe('test menu match', () => {
it('simple path', () => { it('simple path', () => {
expect(getMeunMatchKeys(meun, '/dashboard')).toEqual(['/dashboard']); expect(getMeunMatchKeys(flatMenuKeys, urlToList('/dashboard'), true)).toEqual(['/dashboard']);
}); });
it('error path', () => { it('error path', () => {
expect(getMeunMatchKeys(meun, '/dashboardname')).toEqual([]); expect(getMeunMatchKeys(flatMenuKeys, urlToList('/dashboardname'), true)).toEqual([]);
}); });
it('Secondary path', () => { it('Secondary path', () => {
expect(getMeunMatchKeys(meun, '/dashboard/name')).toEqual(['/dashboard/name']); expect(getMeunMatchKeys(flatMenuKeys, urlToList('/dashboard/name'), true)).toEqual(['/dashboard', '/dashboard/name']);
}); });
it('Parameter path', () => { it('Parameter path', () => {
expect(getMeunMatchKeys(meun, '/userinfo/2144')).toEqual(['/userinfo/:id']); expect(getMeunMatchKeys(flatMenuKeys, urlToList('/userinfo/2144'), true)).toEqual(['/userinfo', '/userinfo/:id']);
}); });
it('three parameter path', () => { it('three parameter path', () => {
expect(getMeunMatchKeys(meun, '/userinfo/2144/info')).toEqual(['/userinfo/:id/info']); expect(getMeunMatchKeys(flatMenuKeys, urlToList('/userinfo/2144/info'), false)).toEqual(['/userinfo', '/userinfo/:id', '/userinfo/:id/info']);
}); });
}); });
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