LoginTab.tsx 1.12 KB
Newer Older
陈帅's avatar
陈帅 committed
1 2
import React, { Component } from 'react';
import { Tabs } from 'antd';
陈帅's avatar
陈帅 committed
3
import { TabPaneProps } from 'antd/es/tabs';
陈帅's avatar
陈帅 committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
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
21
  uniqueId: string;
陈帅's avatar
陈帅 committed
22 23 24 25 26 27
  constructor(props: LoginTabProps) {
    super(props);
    this.uniqueId = generateId('login-tab-');
  }
  componentDidMount() {
    const { tabUtil } = this.props;
陈帅's avatar
陈帅 committed
28 29 30
    if (tabUtil) {
      tabUtil.addTab(this.uniqueId);
    }
陈帅's avatar
陈帅 committed
31 32 33 34 35 36 37 38
  }

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

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

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

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