ant-design/site/theme/template/Layout/Header.jsx

198 lines
6.4 KiB
React
Raw Normal View History

import React, { PropTypes } from 'react';
2017-01-17 15:24:13 +08:00
import { Link } from 'bisheng/router';
2016-09-08 16:53:50 +08:00
import { FormattedMessage } from 'react-intl';
2016-04-28 15:42:02 +08:00
import classNames from 'classnames';
2016-09-26 12:12:44 +08:00
import { Select, Menu, Row, Col, Icon, Button, Popover } from 'antd';
import * as utils from '../utils';
2016-08-23 21:00:35 +08:00
2016-02-29 14:08:40 +08:00
const Option = Select.Option;
2016-03-01 16:20:32 +08:00
export default class Header extends React.Component {
static contextTypes = {
router: PropTypes.object.isRequired,
intl: PropTypes.object.isRequired,
}
state = {
inputValue: '',
menuVisible: false,
menuMode: 'horizontal',
};
2016-05-09 11:34:45 +08:00
componentDidMount() {
this.context.router.listen(this.handleHideMenu);
/* eslint-disable global-require */
require('enquire.js')
2016-12-25 19:18:08 +08:00
.register('only screen and (min-width: 320px) and (max-width: 1024px)', {
match: () => {
this.setState({ menuMode: 'inline' });
},
unmatch: () => {
this.setState({ menuMode: 'horizontal' });
},
});
/* eslint-enable global-require */
2016-03-24 17:13:19 +08:00
}
2016-03-30 16:16:18 +08:00
handleSearch = (value) => {
const { intl, router } = this.context;
this.setState({
inputValue: '',
}, () => {
router.push({ pathname: utils.getLocalizedPathname(`${value}/`, intl.locale === 'zh-CN') });
});
}
handleInputChange = (value) => {
this.setState({
inputValue: value,
});
}
handleShowMenu = () => {
this.setState({
menuVisible: true,
});
}
handleHideMenu = () => {
this.setState({
menuVisible: false,
});
2016-02-29 14:08:40 +08:00
}
2016-03-30 16:16:18 +08:00
handleSelectFilter = (value, option) => {
return option.props['data-label'].indexOf(value.toLowerCase()) > -1;
}
2016-04-25 12:07:15 +08:00
handleLangChange = () => {
const pathname = this.props.location.pathname;
2017-01-06 09:08:36 +08:00
if (pathname === '/') {
location.pathname = utils.getLocalizedPathname(pathname, !utils.isZhCN(pathname));
} else {
location.href = location.href.replace(location.pathname, utils.getLocalizedPathname(pathname, !utils.isZhCN(pathname)));
}
2016-04-25 12:07:15 +08:00
}
2016-03-01 16:20:32 +08:00
render() {
const { inputValue, menuMode, menuVisible } = this.state;
const { location, picked, isFirstScreen } = this.props;
2016-08-03 17:51:33 +08:00
const components = picked.components;
const module = location.pathname.replace(/(^\/|\/$)/g, '').split('/').slice(0, -1).join('/');
2016-07-26 15:24:48 +08:00
let activeMenuItem = module || 'home';
if (activeMenuItem === 'components' || location.pathname === 'changelog') {
2016-06-15 15:55:39 +08:00
activeMenuItem = 'docs/react';
}
2016-03-01 16:20:32 +08:00
2016-09-08 16:53:50 +08:00
const locale = this.context.intl.locale;
const isZhCN = locale === 'zh-CN';
const excludedSuffix = isZhCN ? 'en-US.md' : 'zh-CN.md';
2016-06-09 15:00:44 +08:00
const options = components
.filter(({ meta }) => !meta.filename.endsWith(excludedSuffix))
.map(({ meta }) => {
const pathSnippet = meta.filename.split('/')[1];
const url = `/components/${pathSnippet}`;
const subtitle = meta.subtitle;
return (
<Option value={url} key={url} data-label={`${meta.title.toLowerCase()} ${subtitle || ''}`}>
<strong>{meta.title}</strong>
{subtitle && <span className="ant-component-decs">{subtitle}</span>}
</Option>
);
});
2016-03-01 16:20:32 +08:00
2016-04-28 15:42:02 +08:00
const headerClassName = classNames({
clearfix: true,
'home-nav-white': !isFirstScreen,
2016-04-28 15:42:02 +08:00
});
2016-09-26 12:12:44 +08:00
const menu = [
2016-10-07 14:33:08 +08:00
<Button className="lang" type="ghost" size="small" onClick={this.handleLangChange} key="lang">
2016-09-26 12:12:44 +08:00
<FormattedMessage id="app.header.lang" />
</Button>,
<Menu mode={menuMode} selectedKeys={[activeMenuItem]} id="nav" key="nav">
<Menu.Item key="home">
<Link to={utils.getLocalizedPathname('/', isZhCN)}>
2016-09-26 12:12:44 +08:00
<FormattedMessage id="app.header.menu.home" />
</Link>
</Menu.Item>
2016-10-07 14:33:08 +08:00
<Menu.Item key="docs/spec">
<Link to={utils.getLocalizedPathname('/docs/spec/introduce', isZhCN)}>
2016-10-07 14:33:08 +08:00
<FormattedMessage id="app.header.menu.spec" />
2016-09-26 12:12:44 +08:00
</Link>
</Menu.Item>
<Menu.Item key="docs/react">
<Link to={utils.getLocalizedPathname('/docs/react/introduce', isZhCN)}>
2016-09-26 12:12:44 +08:00
<FormattedMessage id="app.header.menu.components" />
</Link>
</Menu.Item>
2016-10-07 14:33:08 +08:00
<Menu.Item key="docs/pattern">
<Link to={utils.getLocalizedPathname('/docs/pattern/navigation', isZhCN)}>
2016-10-07 14:33:08 +08:00
<FormattedMessage id="app.header.menu.pattern" />
</Link>
</Menu.Item>
<Menu.Item key="docs/practice">
<Link to={utils.getLocalizedPathname('/docs/practice/cases', isZhCN)}>
2016-10-07 14:33:08 +08:00
<FormattedMessage id="app.header.menu.practice" />
2016-09-26 12:12:44 +08:00
</Link>
</Menu.Item>
<Menu.Item key="docs/resource">
<Link to={utils.getLocalizedPathname('/docs/resource/download', isZhCN)}>
2016-09-26 12:12:44 +08:00
<FormattedMessage id="app.header.menu.resource" />
</Link>
</Menu.Item>
</Menu>,
];
2016-10-07 14:33:08 +08:00
const searchPlaceholder = locale === 'zh-CN' ? '搜索组件...' : 'Search Components...';
2016-03-01 16:20:32 +08:00
return (
2016-04-28 15:42:02 +08:00
<header id="header" className={headerClassName}>
2016-11-06 17:09:06 +08:00
{menuMode === 'inline' ? <Popover
overlayClassName="popover-menu"
2016-09-26 12:12:44 +08:00
placement="bottomRight"
2016-11-06 17:09:06 +08:00
content={menu}
2016-09-26 12:12:44 +08:00
trigger="click"
visible={menuVisible}
2016-11-06 17:09:06 +08:00
arrowPointAtCenter
2016-09-26 12:12:44 +08:00
>
<Icon
className="nav-phone-icon"
type="menu"
onClick={this.handleShowMenu}
2016-09-26 12:12:44 +08:00
/>
2016-11-06 17:09:06 +08:00
</Popover> : null}
2016-03-01 16:33:19 +08:00
<Row>
2016-11-06 17:09:06 +08:00
<Col lg={4} md={6} sm={24} xs={24}>
<Link to={utils.getLocalizedPathname('/', isZhCN)} id="logo">
<img alt="logo" src="https://t.alipayobjects.com/images/rmsweb/T1B9hfXcdvXXXXXXXX.svg" />
2016-03-01 16:33:19 +08:00
<span>Ant Design</span>
2016-03-01 16:20:32 +08:00
</Link>
2016-03-01 16:33:19 +08:00
</Col>
2016-12-25 19:18:08 +08:00
<Col lg={20} md={18} sm={0} xs={0}>
2016-03-01 16:33:19 +08:00
<div id="search-box">
2016-11-11 19:47:11 +08:00
<Select
combobox
value={inputValue}
dropdownStyle={{ display: inputValue ? 'block' : 'none' }}
2016-03-23 22:36:26 +08:00
dropdownClassName="component-select"
2016-08-17 11:00:52 +08:00
placeholder={searchPlaceholder}
2016-03-28 14:12:26 +08:00
optionFilterProp="data-label"
2016-06-15 19:37:54 +08:00
optionLabelProp="data-label"
2016-03-30 16:16:18 +08:00
filterOption={this.handleSelectFilter}
onSelect={this.handleSearch}
onSearch={this.handleInputChange}
2016-11-21 17:54:17 +08:00
getPopupContainer={trigger => trigger.parentNode}
>
2016-03-01 16:33:19 +08:00
{options}
</Select>
</div>
2016-09-26 12:12:44 +08:00
{menuMode === 'horizontal' ? menu : null}
2016-03-01 16:33:19 +08:00
</Col>
</Row>
2016-03-01 16:20:32 +08:00
</header>
);
}
2016-02-29 14:08:40 +08:00
}