index.tsx 1.53 KB
Newer Older
1
import React, { Component } from 'react';
2
import { Icon } from 'antd';
zinkey's avatar
zinkey committed
3
import Link from 'umi/link';
4
import debounce from 'lodash/debounce';
偏右's avatar
偏右 committed
5
import styles from './index.less';
何乐's avatar
何乐 committed
6
import RightContent, { GlobalHeaderRightProps } from './RightContent';
偏右's avatar
偏右 committed
7

何乐's avatar
何乐 committed
8 9 10 11 12 13 14 15 16
type PartialGlobalHeaderRightProps = {
  [K in
    | 'onMenuClick'
    | 'onNoticeClear'
    | 'onNoticeVisibleChange'
    | 'currentUser']?: GlobalHeaderRightProps[K]
};

export interface GlobalHeaderProps extends PartialGlobalHeaderRightProps {
17 18 19 20 21 22 23 24
  collapsed?: boolean;
  onCollapse?: (collapsed: boolean) => void;
  isMobile?: boolean;
  logo?: string;
}

export default class GlobalHeader extends Component<GlobalHeaderProps> {
  triggerResizeEvent = debounce(() => {
偏右's avatar
偏右 committed
25 26 27
    const event = document.createEvent('HTMLEvents');
    event.initEvent('resize', true, false);
    window.dispatchEvent(event);
28
  });
何乐's avatar
何乐 committed
29 30 31
  componentWillUnmount() {
    this.triggerResizeEvent.cancel();
  }
jim's avatar
jim committed
32 33
  toggle = () => {
    const { collapsed, onCollapse } = this.props;
何乐's avatar
何乐 committed
34
    if (onCollapse) onCollapse(!collapsed);
jim's avatar
jim committed
35 36
    this.triggerResizeEvent();
  };
偏右's avatar
偏右 committed
37
  render() {
jim's avatar
jim committed
38
    const { collapsed, isMobile, logo } = this.props;
偏右's avatar
偏右 committed
39
    return (
40
      <div className={styles.header}>
41
        {isMobile && (
jim's avatar
jim committed
42 43
          <Link to="/" className={styles.logo} key="logo">
            <img src={logo} alt="logo" width="32" />
44 45
          </Link>
        )}
46 47 48
        <span className={styles.trigger} onClick={this.toggle}>
          <Icon type={collapsed ? 'menu-unfold' : 'menu-fold'} />
        </span>
jim's avatar
jim committed
49
        <RightContent {...this.props} />
50
      </div>
偏右's avatar
偏右 committed
51 52 53
    );
  }
}