index.js 3.05 KB
Newer Older
ddcat1115's avatar
ddcat1115 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { Form, Tabs } from 'antd';
import classNames from 'classnames';
import LoginItem from './LoginItem';
import LoginTab from './LoginTab';
import LoginSubmit from './LoginSubmit';
import styles from './index.less';

class Login extends Component {
  static propTypes = {
    className: PropTypes.string,
    defaultActiveKey: PropTypes.string,
    onTabChange: PropTypes.func,
    onSubmit: PropTypes.func,
  };
jim's avatar
jim committed
17

ddcat1115's avatar
ddcat1115 committed
18 19 20 21 22
  static childContextTypes = {
    tabUtil: PropTypes.object,
    form: PropTypes.object,
    updateActive: PropTypes.func,
  };
jim's avatar
jim committed
23 24 25 26 27 28 29 30

  static defaultProps = {
    className: '',
    defaultActiveKey: '',
    onTabChange: () => {},
    onSubmit: () => {},
  };

31 32 33 34 35 36 37 38
  constructor(props) {
    super(props);
    this.state = {
      type: props.defaultActiveKey,
      tabs: [],
      active: {},
    };
  }
陈帅's avatar
陈帅 committed
39

ddcat1115's avatar
ddcat1115 committed
40
  getChildContext() {
41 42
    const { tabs } = this.state;
    const { form } = this.props;
ddcat1115's avatar
ddcat1115 committed
43 44
    return {
      tabUtil: {
jim's avatar
jim committed
45
        addTab: id => {
ddcat1115's avatar
ddcat1115 committed
46
          this.setState({
47
            tabs: [...tabs, id],
ddcat1115's avatar
ddcat1115 committed
48 49
          });
        },
jim's avatar
jim committed
50
        removeTab: id => {
ddcat1115's avatar
ddcat1115 committed
51
          this.setState({
52
            tabs: tabs.filter(currentId => currentId !== id),
ddcat1115's avatar
ddcat1115 committed
53 54 55
          });
        },
      },
56
      form,
jim's avatar
jim committed
57
      updateActive: activeItem => {
ddcat1115's avatar
ddcat1115 committed
58 59 60 61 62 63 64 65 66 67 68 69
        const { type, active } = this.state;
        if (active[type]) {
          active[type].push(activeItem);
        } else {
          active[type] = [activeItem];
        }
        this.setState({
          active,
        });
      },
    };
  }
陈帅's avatar
陈帅 committed
70

jim's avatar
jim committed
71
  onSwitch = type => {
72
    const { onTabChange } = this.props;
ddcat1115's avatar
ddcat1115 committed
73 74 75
    this.setState({
      type,
    });
76
    onTabChange(type);
jim's avatar
jim committed
77
  };
陈帅's avatar
陈帅 committed
78

jim's avatar
jim committed
79
  handleSubmit = e => {
ddcat1115's avatar
ddcat1115 committed
80 81
    e.preventDefault();
    const { active, type } = this.state;
82
    const { form, onSubmit } = this.props;
ddcat1115's avatar
ddcat1115 committed
83
    const activeFileds = active[type];
84 85
    form.validateFields(activeFileds, { force: true }, (err, values) => {
      onSubmit(err, values);
jim's avatar
jim committed
86 87
    });
  };
陈帅's avatar
陈帅 committed
88

ddcat1115's avatar
ddcat1115 committed
89 90 91 92 93
  render() {
    const { className, children } = this.props;
    const { type, tabs } = this.state;
    const TabChildren = [];
    const otherChildren = [];
jim's avatar
jim committed
94
    React.Children.forEach(children, item => {
95 96 97
      if (!item) {
        return;
      }
ddcat1115's avatar
ddcat1115 committed
98 99 100 101 102 103 104 105
      // eslint-disable-next-line
      if (item.type.__ANT_PRO_LOGIN_TAB) {
        TabChildren.push(item);
      } else {
        otherChildren.push(item);
      }
    });
    return (
106
      <div className={classNames(className, styles.login)}>
ddcat1115's avatar
ddcat1115 committed
107
        <Form onSubmit={this.handleSubmit}>
jim's avatar
jim committed
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
          {tabs.length ? (
            <div>
              <Tabs
                animated={false}
                className={styles.tabs}
                activeKey={type}
                onChange={this.onSwitch}
              >
                {TabChildren}
              </Tabs>
              {otherChildren}
            </div>
          ) : (
            [...children]
          )}
ddcat1115's avatar
ddcat1115 committed
123 124 125 126 127 128 129 130
        </Form>
      </div>
    );
  }
}

Login.Tab = LoginTab;
Login.Submit = LoginSubmit;
jim's avatar
jim committed
131
Object.keys(LoginItem).forEach(item => {
ddcat1115's avatar
ddcat1115 committed
132 133 134
  Login[item] = LoginItem[item];
});

jim's avatar
jim committed
135
export default Form.create()(Login);