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

191 lines
6.1 KiB
React
Raw Normal View History

2016-02-29 14:08:40 +08:00
import React from 'react';
2016-04-18 16:38:07 +08:00
import { FormattedMessage } from 'react-intl';
2016-02-29 14:08:40 +08:00
import { Link } from 'react-router';
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-04-25 12:07:15 +08:00
import { Select, Menu, Row, Col, Icon, Button } from 'antd';
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-06-27 11:57:56 +08:00
this.onDocumentClick = (e) => {
if (document.querySelector('#header .nav').contains(e.target)) {
return;
}
2016-03-24 17:13:19 +08:00
this.setState({
menuVisible: false,
});
2016-05-09 11:34:45 +08:00
};
this.state = {
menuVisible: false,
menuMode: 'horizontal',
isFirstFrame: true,
};
}
componentDidMount() {
window.addEventListener('scroll', this.onScroll);
document.addEventListener('click', this.onDocumentClick);
2016-06-27 11:57:56 +08:00
document.addEventListener('touchstart', this.onDocumentClick);
2016-03-24 17:13:19 +08:00
enquire.register('only screen and (min-width: 320px) and (max-width: 767px)', {
match: () => {
this.setState({ menuMode: 'inline' });
},
unmatch: () => {
this.setState({ menuMode: 'horizontal' });
},
});
}
2016-05-09 11:34:45 +08:00
componentWillUnmount() {
window.removeEventListener('scroll', this.onScroll);
document.removeEventListener('click', this.onDocumentClick);
2016-06-27 11:57:56 +08:00
document.removeEventListener('touchstart', this.onDocumentClick);
2016-05-09 11:34:45 +08:00
}
2016-03-30 16:16:18 +08:00
handleMenuIconClick = (e) => {
2016-03-24 17:13:19 +08:00
e.stopPropagation();
e.nativeEvent.stopImmediatePropagation();
this.setState({
menuVisible: true,
});
}
2016-03-30 16:16:18 +08:00
handleSearch = (value) => {
2016-06-15 19:47:19 +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);
location.reload();
}
}
2016-03-01 16:20:32 +08:00
render() {
2016-07-26 15:24:48 +08:00
const { location, components } = this.props;
const module = location.pathname.split('/').slice(0, -1).join('/');
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-06-09 15:00:44 +08:00
const options = components
2016-06-15 15:55:39 +08:00
.map(({ meta }) => {
const pathSnippet = meta.filename.split('/')[1];
const url = `/components/${pathSnippet}`;
return (
<Option value={url} key={url} data-label={`${(meta.title || meta.english).toLowerCase()} ${meta.subtitle || meta.chinese}`}>
<strong>{meta.title || meta.english}</strong>
<span className="ant-component-decs">{meta.subtitle || meta.chinese}</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-03-01 16:20:32 +08:00
return (
2016-04-28 15:42:02 +08:00
<header id="header" className={headerClassName}>
2016-03-01 16:33:19 +08:00
<Row>
2016-03-24 17:13:19 +08:00
<Col lg={4} md={6} sm={7} xs={24}>
<Icon
className="nav-phone-icon"
2016-03-30 16:16:18 +08:00
onClick={this.handleMenuIconClick}
type="menu"
/>
2016-03-01 16:33:19 +08:00
<Link to="/" 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-06-27 11:57:56 +08:00
<Col className={`nav ${this.state.menuVisible ? 'nav-show' : ''}`}
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-04-27 18:07:48 +08:00
placeholder="搜索组件..."
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-05-13 11:38:35 +08:00
{
2016-07-26 15:24:48 +08:00
window.location.port ? (
2016-05-13 11:38:35 +08:00
<Button id="lang" type="ghost" size="small" onClick={this.handleLangChange}>
<FormattedMessage id="app.header.lang" />
</Button>
) : null
}
2016-03-24 17:13:19 +08:00
<Menu mode={this.state.menuMode} selectedKeys={[activeMenuItem]} id="nav">
2016-03-01 16:33:19 +08:00
<Menu.Item key="home">
<Link to="/">
2016-04-18 16:38:07 +08:00
<FormattedMessage id="app.header.menu.home" />
2016-03-01 16:33:19 +08:00
</Link>
</Menu.Item>
2016-03-10 16:56:17 +08:00
<Menu.Item key="docs/practice">
<Link to="/docs/practice/cases">
2016-04-18 16:38:07 +08:00
<FormattedMessage id="app.header.menu.practice" />
2016-03-02 11:57:37 +08:00
</Link>
2016-03-01 16:33:19 +08:00
</Menu.Item>
2016-03-10 16:56:17 +08:00
<Menu.Item key="docs/pattern">
<Link to="/docs/pattern/navigation">
2016-04-18 16:38:07 +08:00
<FormattedMessage id="app.header.menu.pattern" />
2016-03-02 11:57:37 +08:00
</Link>
2016-03-01 16:33:19 +08:00
</Menu.Item>
2016-05-08 16:24:23 +08:00
<Menu.Item key="docs/react">
<Link to="/docs/react/introduce">
2016-04-18 16:38:07 +08:00
<FormattedMessage id="app.header.menu.react" />
2016-03-01 16:33:19 +08:00
</Link>
</Menu.Item>
2016-03-10 16:56:17 +08:00
<Menu.Item key="docs/spec">
<Link to="/docs/spec/introduce">
2016-04-18 16:38:07 +08:00
<FormattedMessage id="app.header.menu.spec" />
2016-03-01 16:33:19 +08:00
</Link>
</Menu.Item>
2016-03-10 16:56:17 +08:00
<Menu.Item key="docs/resource">
<Link to="/docs/resource/download">
2016-04-18 16:38:07 +08:00
<FormattedMessage id="app.header.menu.resource" />
2016-03-01 16:33:19 +08:00
</Link>
</Menu.Item>
</Menu>
</Col>
</Row>
2016-03-01 16:20:32 +08:00
</header>
);
}
2016-02-29 14:08:40 +08:00
}