import React from 'react'; import PropTypes from 'prop-types'; import { FormattedMessage, injectIntl } from 'react-intl'; import classNames from 'classnames'; import { UnorderedListOutlined } from '@ant-design/icons'; import { Select, Row, Col, Popover, Button } from 'antd'; import * as utils from '../../utils'; import { version as antdVersion } from '../../../../../package.json'; import Logo from './Logo'; import SearchBox from './SearchBox'; import More from './More'; import Navigation from './Navigation'; import Github from './Github'; import SiteContext from '../SiteContext'; import './index.less'; const RESPONSIVE_XS = 1120; const RESPONSIVE_SM = 1200; const { Option } = Select; let docsearch: any; if (typeof window !== 'undefined') { docsearch = require('docsearch.js'); // eslint-disable-line } function initDocSearch(locale: string) { if (!docsearch) { return; } const lang = locale === 'zh-CN' ? 'cn' : 'en'; docsearch({ apiKey: '60ac2c1a7d26ab713757e4a081e133d0', indexName: 'ant_design', inputSelector: '#search-box input', algoliaOptions: { facetFilters: [`tags:${lang}`] }, transformData(hits: { url: string }[]) { hits.forEach(hit => { hit.url = hit.url.replace('ant.design', window.location.host); // eslint-disable-line hit.url = hit.url.replace('https:', window.location.protocol); // eslint-disable-line }); return hits; }, debug: false, // Set debug to true if you want to inspect the dropdown }); } export interface HeaderProps { intl: { locale: string; }; location: { pathname: string }; themeConfig: { docVersions: Record }; changeDirection: (direction: string) => void; } interface HeaderState { menuVisible: boolean; windowWidth: number; searching: boolean; } class Header extends React.Component { static contextTypes = { router: PropTypes.object.isRequired, theme: PropTypes.oneOf(['default', 'dark']), direction: PropTypes.string, }; state = { menuVisible: false, windowWidth: 1400, searching: false, }; componentDidMount() { const { intl } = this.props; const { router } = this.context; router.listen(this.handleHideMenu); initDocSearch(intl.locale); window.addEventListener('resize', this.onWindowResize); this.onWindowResize(); } componentWillUnmount() { window.removeEventListener('resize', this.onWindowResize); } onWindowResize = () => { this.setState({ windowWidth: window.innerWidth, }); }; onTriggerSearching = (searching: boolean) => { this.setState({ searching }); }; handleShowMenu = () => { this.setState({ menuVisible: true, }); }; handleHideMenu = () => { this.setState({ menuVisible: false, }); }; onDirectionChange = () => { const { changeDirection } = this.props; const { direction } = this.context; if (direction !== 'rtl') { changeDirection('rtl'); } else { changeDirection('ltr'); } }; getNextDirectionText = () => { const { direction } = this.context; if (direction !== 'rtl') { return 'RTL'; } return 'LTR'; }; onMenuVisibleChange = (visible: boolean) => { this.setState({ menuVisible: visible, }); }; handleVersionChange = (url: string) => { const currentUrl = window.location.href; const currentPathname = window.location.pathname; window.location.href = currentUrl .replace(window.location.origin, url) .replace(currentPathname, utils.getLocalizedPathname(currentPathname)); }; onLangChange = () => { const { location: { pathname }, } = this.props; const currentProtocol = `${window.location.protocol}//`; const currentHref = window.location.href.substr(currentProtocol.length); if (utils.isLocalStorageNameSupported()) { localStorage.setItem('locale', utils.isZhCN(pathname) ? 'en-US' : 'zh-CN'); } window.location.href = currentProtocol + currentHref.replace( window.location.pathname, utils.getLocalizedPathname(pathname, !utils.isZhCN(pathname)), ); }; render() { return ( {({ isMobile }) => { const { menuVisible, windowWidth, searching } = this.state; const { location, themeConfig, intl: { locale }, } = this.props; const docVersions = { ...themeConfig.docVersions, [antdVersion]: antdVersion }; const versionOptions = Object.keys(docVersions).map(version => ( )); const pathname = location.pathname.replace(/(^\/|\/$)/g, ''); const isHome = ['', 'index', 'index-cn'].includes(pathname); const isZhCN = locale === 'zh-CN'; let responsive: null | 'narrow' | 'crowded' = null; if (windowWidth < RESPONSIVE_XS) { responsive = 'crowded'; } else if (windowWidth < RESPONSIVE_SM) { responsive = 'narrow'; } const headerClassName = classNames({ clearfix: true, 'home-header': isHome, }); const sharedProps = { isZhCN, }; const searchBox = ( ); const navigationNode = ( ); let menu: (React.ReactElement | null)[] = [ navigationNode, , , , , , ]; if (windowWidth < RESPONSIVE_XS) { menu = searching ? [] : [navigationNode]; } else if (windowWidth < RESPONSIVE_SM) { menu = searching ? [] : menu; } const colProps = isHome ? [{ flex: 'none' }, { flex: 'auto' }] : [ { xxl: 4, xl: 5, lg: 6, md: 6, sm: 24, xs: 24, }, { xxl: 20, xl: 19, lg: 18, md: 18, sm: 0, xs: 0, }, ]; return ( ); }} ); } } export default injectIntl(Header as any);