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

184 lines
6.0 KiB
React
Raw Normal View History

2016-02-29 14:08:40 +08:00
import React from 'react';
import { Link } from 'react-router';
2016-09-08 16:53:50 +08:00
import { FormattedMessage } from 'react-intl';
2016-03-24 17:13:19 +08:00
import enquire from 'enquire.js';
2016-04-28 15:42:02 +08:00
import debounce from 'lodash.debounce';
import classNames from 'classnames';
2016-09-26 12:12:44 +08:00
import { Select, Menu, Row, Col, Icon, Button, Popover } from 'antd';
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: React.PropTypes.object.isRequired,
2016-04-20 18:51:31 +08:00
intl: React.PropTypes.object.isRequired,
}
2016-03-24 17:13:19 +08:00
constructor(props) {
super(props);
2016-05-09 11:34:45 +08:00
this.onScroll = debounce(() => {
2016-04-28 15:42:02 +08:00
const scrollTop = document.body.scrollTop || document.documentElement.scrollTop;
const clientHeight = document.documentElement.clientHeight;
if (scrollTop >= clientHeight) {
this.setState({ isFirstFrame: false });
} else {
this.setState({ isFirstFrame: true });
}
2016-05-09 11:34:45 +08:00
}, 100);
2016-04-28 15:42:02 +08:00
2016-05-09 11:34:45 +08:00
this.state = {
menuMode: 'horizontal',
isFirstFrame: true,
};
}
componentDidMount() {
window.addEventListener('scroll', this.onScroll);
2016-09-23 10:14:57 +08:00
enquire.register('only screen and (min-width: 320px) and (max-width: 940px)', {
2016-03-24 17:13:19 +08:00
match: () => {
this.setState({ menuMode: 'inline' });
},
unmatch: () => {
this.setState({ menuMode: 'horizontal' });
},
});
}
2016-05-09 11:34:45 +08:00
componentWillUnmount() {
window.removeEventListener('scroll', this.onScroll);
2016-03-24 17:13:19 +08:00
}
2016-03-30 16:16:18 +08:00
handleSearch = (value) => {
2016-08-03 16:16:05 +08:00
this.context.router.push({ pathname: `${value}/` });
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 = () => {
if (typeof localStorage !== 'undefined') {
const locale = this.context.intl.locale === 'zh-CN' ? 'en-US' : 'zh-CN';
localStorage.setItem('locale', locale);
2016-10-18 12:04:09 +08:00
location.search = `?locale=${locale}`;
2016-04-25 12:07:15 +08:00
}
}
2016-03-01 16:20:32 +08:00
render() {
2016-08-03 17:51:33 +08:00
const { location, picked } = this.props;
const components = picked.components;
2016-08-03 15:41:18 +08:00
const module = location.pathname.replace(/\/$/, '')
.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 excludedSuffix = locale === 'zh-CN' ? 'en-US.md' : 'zh-CN.md';
2016-06-09 15:00:44 +08:00
const options = components
2016-09-08 16:53:50 +08:00
.filter(({ meta }) => !meta.filename.endsWith(excludedSuffix))
.map(({ meta }) => {
const pathSnippet = meta.filename.split('/')[1];
const url = `/components/${pathSnippet}`;
2016-09-21 11:28:38 +08:00
const subtitle = meta.subtitle;
2016-09-08 16:53:50 +08:00
return (
2016-09-21 11:28:38 +08:00
<Option value={url} key={url} data-label={`${meta.title.toLowerCase()} ${subtitle || ''}`}>
<strong>{meta.title}</strong>
2016-09-08 16:53:50 +08:00
{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': !this.state.isFirstFrame,
});
2016-09-26 12:12:44 +08:00
const menuMode = this.state.menuMode;
2016-10-18 12:04:09 +08:00
const query = location.query;
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">
2016-10-18 12:04:09 +08:00
<Link to={{ query, pathname: '/' }}>
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">
2016-10-18 12:04:09 +08:00
<Link to={{ query, pathname: '/docs/spec/introduce' }}>
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">
2016-10-18 12:04:09 +08:00
<Link to={{ query, pathname: '/docs/react/introduce' }}>
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">
2016-10-18 12:04:09 +08:00
<Link to={{ query, pathname: '/docs/pattern/navigation' }}>
2016-10-07 14:33:08 +08:00
<FormattedMessage id="app.header.menu.pattern" />
</Link>
</Menu.Item>
<Menu.Item key="docs/practice">
2016-10-18 12:04:09 +08:00
<Link to={{ query, pathname: '/docs/practice/cases' }}>
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">
2016-10-18 12:04:09 +08:00
<Link to={{ query, pathname: '/docs/resource/download' }}>
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"
2016-11-06 17:09:06 +08:00
arrowPointAtCenter
2016-09-26 12:12:44 +08:00
>
<Icon
className="nav-phone-icon"
type="menu"
/>
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}>
2016-10-18 12:04:09 +08:00
<Link to={{ query, pathname: '/' }} 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-09-26 12:12:44 +08:00
<Col lg={20} md={18} sm={17} xs={0} style={{ display: 'block' }}>
2016-03-01 16:33:19 +08:00
<div id="search-box">
2016-03-10 16:01:39 +08:00
<Select combobox
2016-03-23 22:36:26 +08:00
dropdownClassName="component-select"
2016-08-17 11:00:52 +08:00
placeholder={searchPlaceholder}
2016-04-18 15:15:47 +08:00
value={undefined}
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}
>
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
}