refactor website

This commit is contained in:
Benjy Cui 2016-03-07 10:32:14 +08:00
parent 844a132cfe
commit 7fbb2ddc47
2 changed files with 33 additions and 34 deletions

View File

@ -1,7 +1,6 @@
import React from 'react';
import { Link } from 'react-router';
import { Row, Col, Menu } from '../../../';
import * as utils from '../utils';
const SubMenu = Menu.SubMenu;
function dashed(name) {
@ -15,6 +14,11 @@ export default class MainContent extends React.Component {
this.generateMenuItem = this.generateMenuItem.bind(this);
}
getActiveMenuItem(props, index) {
const routes = props.routes;
return routes[routes.length - 1].path || index;
}
generateMenuItem(item) {
const key = dashed(item.english);
const text = item.chinese || item.english;
@ -29,7 +33,7 @@ export default class MainContent extends React.Component {
</a>;
return (
<Menu.Item key={key} disabled={disabled}>
<Menu.Item key={key} disabled={disabled}>
{ child }
</Menu.Item>
);
@ -53,10 +57,34 @@ export default class MainContent extends React.Component {
return [...topLevel, ...subMenu];
}
flattenMenu(menu) {
if (menu.type === Menu.Item) {
return menu;
}
if (Array.isArray(menu)) {
return menu.reduce((acc, item) => {
return acc.concat(this.flattenMenu(item));
}, []);
}
return this.flattenMenu(menu.props.children);
}
getFooterNav(menuItems, activeMenuItem) {
const menuItemsList = this.flattenMenu(menuItems);
const activeMenuItemIndex = menuItemsList.findIndex((menuItem) => {
return menuItem.key === activeMenuItem;
});
const prev = menuItemsList[activeMenuItemIndex - 1];
const next = menuItemsList[activeMenuItemIndex + 1];
return { prev, next };
}
render() {
const activeMenuItem = utils.getActiveMenuItem(this.props);
const activeMenuItem = this.getActiveMenuItem(this.props);
const menuItems = this.getMenuItems();
const { prev, next } = utils.getFooterNav(menuItems, activeMenuItem);
const { prev, next } = this.getFooterNav(menuItems, activeMenuItem);
return (
<Row className="main-wrapper">

View File

@ -1,7 +1,7 @@
import React from 'react';
import ReactDOM from 'react-dom';
import hljs from 'highlight.js';
import antd, { Menu } from '../../';
import antd from '../../';
function isHeading(type) {
return /h[1-6]/i.test(type);
@ -63,32 +63,3 @@ export function objectToComponent(object, index) {
children && children.map(objectToComponent) // `hr` has no children
);
}
export function flattenMenu(menu) {
if (menu.type === Menu.Item) {
return menu;
}
if (Array.isArray(menu)) {
return menu.reduce((acc, item) => {
return acc.concat(flattenMenu(item));
}, []);
}
return flattenMenu(menu.props.children);
}
export function getActiveMenuItem(props, index) {
const routes = props.routes;
return routes[routes.length - 1].path || index;
}
export function getFooterNav(menuItems, activeMenuItem) {
const menuItemsList = flattenMenu(menuItems);
const activeMenuItemIndex = menuItemsList.findIndex((menuItem) => {
return menuItem.key === activeMenuItem;
});
const prev = menuItemsList[activeMenuItemIndex - 1];
const next = menuItemsList[activeMenuItemIndex + 1];
return { prev, next };
}