ant-design/site/component/Header/index.jsx

176 lines
5.5 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-11 10:12:48 +08:00
import { Select, Menu, Row, Col, Icon } from 'antd';
2016-02-29 14:08:40 +08:00
const Option = Select.Option;
import './index.less';
2016-03-22 11:32:49 +08:00
import componentsList from '../../../_data/react-components';
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.onDocumentClick = () => {
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-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-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-03-29 12:00:14 +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-03-01 16:20:32 +08:00
render() {
const routes = this.props.routes;
2016-05-10 14:39:44 +08:00
let activeMenuItem = routes[1].path || 'home';
activeMenuItem = activeMenuItem === 'components' ? 'docs/react' : activeMenuItem;
2016-03-01 16:20:32 +08:00
2016-04-20 18:51:31 +08:00
const locale = this.context.intl.locale;
const options = Object.keys(componentsList).map((key) => {
const value = componentsList[key];
return value.localized ? value[locale] : value;
}).filter(({ meta }) => {
return /^component/.test(meta.fileName);
}).map(({ meta }) => {
2016-03-28 14:12:26 +08:00
const pathSnippet = meta.fileName.split('/')[1];
const url = `/components/${pathSnippet}`;
2016-03-23 22:36:26 +08:00
return (
<Option value={url} key={url} data-label={`${meta.english.toLowerCase()} ${meta.chinese}`}>
2016-03-04 18:06:34 +08:00
<strong>{meta.english}</strong>
<span className="ant-component-decs">{meta.chinese}</span>
</Option>
);
});
2016-03-01 16:20:32 +08:00
2016-03-24 17:13:19 +08:00
const menuStyle = {
display: this.state.menuVisible ? 'block' : '',
};
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}
2016-03-24 17:13:19 +08:00
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-03-24 17:13:19 +08:00
<Col className={`nav ${this.state.menuVisible ? 'nav-show' : 'nav-hide'}`}
lg={20} md={18} sm={17} xs={0} style={menuStyle}>
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-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-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">
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">
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">
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">
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">
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
}