import React from 'react'; import { Link } from 'react-router'; import { FormattedMessage } from 'react-intl'; import enquire from 'enquire.js'; import debounce from 'lodash.debounce'; import classNames from 'classnames'; import { Select, Menu, Row, Col, Icon, Button, Popover } from 'antd'; const Option = Select.Option; export default class Header extends React.Component { static contextTypes = { router: React.PropTypes.object.isRequired, intl: React.PropTypes.object.isRequired, } constructor(props) { super(props); this.onScroll = debounce(() => { 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 }); } }, 100); this.state = { menuMode: 'horizontal', isFirstFrame: true, }; } componentDidMount() { window.addEventListener('scroll', this.onScroll); enquire.register('only screen and (min-width: 320px) and (max-width: 940px)', { match: () => { this.setState({ menuMode: 'inline' }); }, unmatch: () => { this.setState({ menuMode: 'horizontal' }); }, }); } componentWillUnmount() { window.removeEventListener('scroll', this.onScroll); } handleSearch = (value) => { this.context.router.push({ pathname: `${value}/` }); } handleSelectFilter = (value, option) => { return option.props['data-label'].indexOf(value.toLowerCase()) > -1; } handleLangChange = () => { if (typeof localStorage !== 'undefined') { const locale = this.context.intl.locale === 'zh-CN' ? 'en-US' : 'zh-CN'; localStorage.setItem('locale', locale); location.search = `?locale=${locale}`; } } render() { const { location, picked } = this.props; const components = picked.components; const module = location.pathname.replace(/\/$/, '') .split('/').slice(0, -1) .join('/'); let activeMenuItem = module || 'home'; if (activeMenuItem === 'components' || location.pathname === 'changelog') { activeMenuItem = 'docs/react'; } const locale = this.context.intl.locale; const excludedSuffix = locale === 'zh-CN' ? 'en-US.md' : 'zh-CN.md'; 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 ( ); }); const headerClassName = classNames({ clearfix: true, 'home-nav-white': !this.state.isFirstFrame, }); const menuMode = this.state.menuMode; const query = location.query; const menu = [ , , ]; const searchPlaceholder = locale === 'zh-CN' ? '搜索组件...' : 'Search Components...'; return ( ); } }