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

247 lines
8.0 KiB
React
Raw Normal View History

import React from 'react';
import PropTypes from 'prop-types';
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';
import { Select, Menu, Row, Col, Icon, Popover, AutoComplete, Input, Badge } from 'antd';
import * as utils from '../utils';
import { version as antdVersion } from '../../../../package.json';
2016-08-23 21:00:35 +08:00
2017-10-09 13:23:20 +08:00
const { Option } = AutoComplete;
const searchEngine = 'Google';
const searchLink = 'https://www.google.com/#q=site:ant.design+';
2016-02-29 14:08:40 +08:00
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);
2017-10-09 13:23:20 +08:00
const { searchInput } = this;
/* eslint-disable global-require */
require('enquire.js')
2017-02-26 15:41:50 +08:00
.register('only screen and (min-width: 0) and (max-width: 992px)', {
match: () => {
this.setState({ menuMode: 'inline' });
},
unmatch: () => {
this.setState({ menuMode: 'horizontal' });
},
});
document.addEventListener('keyup', (event) => {
if (event.keyCode === 83 && event.target === document.body) {
searchInput.focus();
}
});
/* eslint-enable global-require */
2016-03-24 17:13:19 +08:00
}
2016-03-30 16:16:18 +08:00
handleSearch = (value) => {
if (value === searchEngine) {
window.location.href = `${searchLink}${this.state.inputValue}`;
return;
}
const { intl, router } = this.context;
this.setState({
inputValue: '',
}, () => {
router.push({ pathname: utils.getLocalizedPathname(`${value}/`, intl.locale === 'zh-CN') });
this.searchInput.blur();
});
}
handleInputChange = (value) => {
this.setState({
inputValue: value,
});
}
handleShowMenu = () => {
this.setState({
menuVisible: true,
});
}
handleHideMenu = () => {
this.setState({
menuVisible: false,
});
2016-02-29 14:08:40 +08:00
}
2017-02-21 13:36:17 +08:00
onMenuVisibleChange = (visible) => {
this.setState({
menuVisible: visible,
});
}
2016-03-30 16:16:18 +08:00
handleSelectFilter = (value, option) => {
const optionValue = option.props['data-label'];
return optionValue === searchEngine ||
optionValue.indexOf(value.toLowerCase()) > -1;
}
handleVersionChange = (url) => {
const currentUrl = window.location.href;
const currentPathname = window.location.pathname;
window.location.href = currentUrl.replace(window.location.origin, url)
.replace(currentPathname, utils.getLocalizedPathname(currentPathname));
}
2016-03-01 16:20:32 +08:00
render() {
const { inputValue, menuMode, menuVisible } = this.state;
2017-10-09 13:23:20 +08:00
const {
location, picked, isFirstScreen, themeConfig,
} = this.props;
const docVersions = { ...themeConfig.docVersions, [antdVersion]: antdVersion };
const versionOptions = Object.keys(docVersions)
.map(version => <Option value={docVersions[version]} key={version}>{version}</Option>);
2017-10-09 13:23:20 +08:00
const { components } = picked;
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
2017-10-09 13:23:20 +08:00
const { locale } = this.context.intl;
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}`;
2017-10-09 13:23:20 +08:00
const { subtitle } = meta;
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
options.push(
<Option key="searchEngine" value={searchEngine} data-label={searchEngine}>
<FormattedMessage id="app.header.search" />
</Option>
);
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 = [
<Select
key="version"
className="version"
size="small"
dropdownMatchSelectWidth={false}
defaultValue={antdVersion}
onChange={this.handleVersionChange}
getPopupContainer={trigger => trigger.parentNode}
>
{versionOptions}
</Select>,
2017-11-29 11:22:15 +08:00
<Menu className="menu-site" mode={menuMode} selectedKeys={[activeMenuItem]} id="nav" key="nav">
2016-09-26 12:12:44 +08:00
<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>
2016-09-26 12:12:44 +08:00
<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>
2017-10-31 16:32:09 +08:00
<Menu.Item key="pro">
2017-10-31 21:58:41 +08:00
<a
href="http://pro.ant.design"
className="header-link"
target="_blank"
rel="noopener noreferrer"
>
2017-10-31 18:07:37 +08:00
<FormattedMessage id="app.header.menu.pro" />
2017-11-12 14:10:41 +08:00
<span style={{ display: 'inline-block', position: 'relative', top: -2, width: 6, marginLeft: 4 }}>
2017-10-31 18:07:37 +08:00
<Badge dot />
</span>
</a>
</Menu.Item>
2016-09-26 12:12:44 +08:00
</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}>
2017-10-09 13:23:20 +08:00
{menuMode === 'inline' ? (
<Popover
overlayClassName="popover-menu"
placement="bottomRight"
content={menu}
trigger="click"
visible={menuVisible}
arrowPointAtCenter
onVisibleChange={this.onMenuVisibleChange}
>
<Icon
className="nav-phone-icon"
type="menu"
onClick={this.handleShowMenu}
/>
</Popover>
) : null}
2016-03-01 16:33:19 +08:00
<Row>
<Col xxl={4} xl={5} lg={5} md={8} sm={24} xs={24}>
<Link to={utils.getLocalizedPathname('/', isZhCN)} id="logo">
<img alt="logo" src="https://gw.alipayobjects.com/zos/rmsportal/KDpgvguMpGfqaHPjicRK.svg" />
<span>ANT DESIGN</span>
2016-03-01 16:20:32 +08:00
</Link>
2016-03-01 16:33:19 +08:00
</Col>
<Col xxl={20} xl={19} lg={19} md={16} sm={0} xs={0}>
2016-03-01 16:33:19 +08:00
<div id="search-box">
<Icon type="search" />
<AutoComplete
dataSource={options}
value={inputValue}
2016-03-23 22:36:26 +08:00
dropdownClassName="component-select"
2016-08-17 11:00:52 +08:00
placeholder={searchPlaceholder}
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}
>
<Input ref={ref => this.searchInput = ref} />
</AutoComplete>
2016-03-01 16:33:19 +08:00
</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
}