LoginTab.tsx 1.12 KB
Newer Older
陈帅's avatar
陈帅 committed
1
import React, { Component } from 'react';
陈帅's avatar
陈帅 committed
2

陈帅's avatar
陈帅 committed
3
import { TabPaneProps } from 'antd/es/tabs';
陈帅's avatar
陈帅 committed
4
import { Tabs } from 'antd';
陈帅's avatar
陈帅 committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
import LoginContext, { ILoginContext } from './LoginContext';

const { TabPane } = Tabs;

const generateId = (() => {
  let i = 0;
  return (prefix = '') => {
    i += 1;
    return `${prefix}${i}`;
  };
})();

interface LoginTabProps extends TabPaneProps {
  tabUtil: ILoginContext['tabUtil'];
}

class LoginTab extends Component<LoginTabProps> {
陈帅's avatar
陈帅 committed
22
  uniqueId: string;
陈帅's avatar
陈帅 committed
23

陈帅's avatar
陈帅 committed
24 25 26 27
  constructor(props: LoginTabProps) {
    super(props);
    this.uniqueId = generateId('login-tab-');
  }
陈帅's avatar
陈帅 committed
28

陈帅's avatar
陈帅 committed
29 30
  componentDidMount() {
    const { tabUtil } = this.props;
陈帅's avatar
陈帅 committed
31 32 33
    if (tabUtil) {
      tabUtil.addTab(this.uniqueId);
    }
陈帅's avatar
陈帅 committed
34 35 36 37 38 39 40 41
  }

  render() {
    const { children } = this.props;
    return <TabPane {...this.props}>{children}</TabPane>;
  }
}

陈帅's avatar
陈帅 committed
42 43 44
const WrapContext: React.SFC<TabPaneProps> & {
  typeName: string;
} = props => (
陈帅's avatar
陈帅 committed
45 46 47 48 49 50
  <LoginContext.Consumer>
    {value => <LoginTab tabUtil={value.tabUtil} {...props} />}
  </LoginContext.Consumer>
);

// 标志位 用来判断是不是自定义组件
陈帅's avatar
陈帅 committed
51
WrapContext.typeName = 'LoginTab';
陈帅's avatar
陈帅 committed
52

陈帅's avatar
陈帅 committed
53
export default WrapContext;