index.tsx 3.92 KB
Newer Older
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
1
import React from 'react';
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
2
import { formatMessage } from 'umi-plugin-react/locale';
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
3 4 5 6 7 8 9
import { PageHeader, Tabs, Typography } from 'antd';
import { connect } from 'dva';
import classNames from 'classnames';
import GridContent from './GridContent';
import ConnectState from '@/models/connect';
import { ContentWidth } from 'config/defaultSettings';
import styles from './index.less';
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
10
import { conversionBreadcrumbList } from './Breadcrumb';
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
import { MenuDataItem } from '../SiderMenu';
import * as H from 'history';

const { Title } = Typography;

/**
 * render Footer tabList
 * In order to be compatible with the old version of the PageHeader
 * basically all the functions are implemented.
 */
const renderFooter = ({
  tabList,
  onTabChange,
  tabBarExtraContent,
}: Partial<PageHeaderWrapperProps>) => {
  return tabList && tabList.length ? (
    <Tabs
      className={styles.tabs}
      onChange={key => {
        if (onTabChange) {
          onTabChange(key);
        }
      }}
      tabBarExtraContent={tabBarExtraContent}
    >
      {tabList.map(item => (
        <Tabs.TabPane tab={item.tab} key={item.key} />
      ))}
    </Tabs>
  ) : null;
};

export interface PageHeaderWrapperProps {
  title?: React.ReactNode | string | number;
  logo?: React.ReactNode | string;
  action?: React.ReactNode | string;
  content?: React.ReactNode;
  extraContent?: React.ReactNode;
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
49
  breadcrumbList?: Array<{ title: string; href: string }>;
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
50 51 52 53 54
  tabList?: Array<{ key: string; tab: React.ReactNode }>;
  tabActiveKey?: string;
  onTabChange?: (key: string) => void;
  tabBarExtraContent?: React.ReactNode;
  style?: React.CSSProperties;
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
55
  home?: string;
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
  wide?: boolean;
  contentWidth?: ContentWidth;
  className?: string;
  children?: React.ReactNode;
  wrapperClassName?: string;
  top?: React.ReactNode;
  location?: H.Location;
  breadcrumbNameMap?: { [path: string]: MenuDataItem };
}

class PageHeaderWrapper extends React.Component<PageHeaderWrapperProps> {
  mergePropsAndChildren = (): PageHeaderWrapperProps => {
    return {
      ...this.props,
    };
  };
  renderPageHeader = () => {
    const {
      children,
      contentWidth,
      wrapperClassName,
      top,
      title,
      content,
      logo,
      extraContent,
      ...restProps
    } = this.mergePropsAndChildren();
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
84 85 86
    let pageTitle = title;
    const breadcrumb = conversionBreadcrumbList({
      ...restProps,
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
87 88 89 90
      home: formatMessage({
        id: 'menu.home',
        defaultMessage: 'Home',
      }),
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
91 92 93 94 95 96 97 98
    });
    if (!title && breadcrumb.routes) {
      const router = breadcrumb.routes[breadcrumb.routes.length - 1];
      if (router) {
        pageTitle = router.breadcrumbName;
      }
    }
    if (!pageTitle && !content) {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
99 100 101 102 103 104 105 106 107 108 109
      return;
    }
    return (
      <PageHeader
        title={
          <Title
            level={4}
            style={{
              marginBottom: 0,
            }}
          >
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
110
            {pageTitle}
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
111 112 113
          </Title>
        }
        {...restProps}
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
114
        breadcrumb={breadcrumb}
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
        className={styles.pageHeader}
        footer={renderFooter(restProps)}
      >
        <div className={styles.detail}>
          {logo && <div className={styles.logo}>{logo}</div>}
          <div className={styles.main}>
            <div className={styles.row}>
              {content && <div className={styles.content}>{content}</div>}
              {extraContent && <div className={styles.extraContent}>{extraContent}</div>}
            </div>
          </div>
        </div>
      </PageHeader>
    );
  };
  render() {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
131
    const { children } = this.mergePropsAndChildren();
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
    return (
      <div style={{ margin: '-24px -24px 0' }} className={classNames(classNames, styles.main)}>
        {children ? (
          <div className={styles['children-content']}>
            <GridContent>{children}</GridContent>
          </div>
        ) : null}
      </div>
    );
  }
}

export default connect(({ setting }: ConnectState) => ({
  contentWidth: setting.contentWidth,
}))(PageHeaderWrapper);