index.js 2.96 KB
Newer Older
ddcat1115's avatar
ddcat1115 committed
1 2 3 4 5 6 7 8
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';
jim's avatar
jim committed
9
import LoginContext from './loginContext';
ddcat1115's avatar
ddcat1115 committed
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

class Login extends Component {
  static defaultProps = {
    className: '',
    defaultActiveKey: '',
    onTabChange: () => {},
    onSubmit: () => {},
  };
  static propTypes = {
    className: PropTypes.string,
    defaultActiveKey: PropTypes.string,
    onTabChange: PropTypes.func,
    onSubmit: PropTypes.func,
  };
  state = {
    type: this.props.defaultActiveKey,
    tabs: [],
    active: {},
  };
jim's avatar
jim committed
29 30 31 32 33 34 35
  onSwitch = type => {
    this.setState({
      type,
    });
    this.props.onTabChange(type);
  };
  getContext = () => {
ddcat1115's avatar
ddcat1115 committed
36 37
    return {
      tabUtil: {
jim's avatar
jim committed
38
        addTab: id => {
ddcat1115's avatar
ddcat1115 committed
39 40 41 42
          this.setState({
            tabs: [...this.state.tabs, id],
          });
        },
jim's avatar
jim committed
43
        removeTab: id => {
ddcat1115's avatar
ddcat1115 committed
44 45 46 47 48 49
          this.setState({
            tabs: this.state.tabs.filter(currentId => currentId !== id),
          });
        },
      },
      form: this.props.form,
jim's avatar
jim committed
50
      updateActive: activeItem => {
ddcat1115's avatar
ddcat1115 committed
51 52 53 54 55 56 57 58 59 60 61
        const { type, active } = this.state;
        if (active[type]) {
          active[type].push(activeItem);
        } else {
          active[type] = [activeItem];
        }
        this.setState({
          active,
        });
      },
    };
jim's avatar
jim committed
62 63
  };
  handleSubmit = e => {
ddcat1115's avatar
ddcat1115 committed
64 65 66
    e.preventDefault();
    const { active, type } = this.state;
    const activeFileds = active[type];
jim's avatar
jim committed
67 68 69 70
    this.props.form.validateFields(activeFileds, { force: true }, (err, values) => {
      this.props.onSubmit(err, values);
    });
  };
ddcat1115's avatar
ddcat1115 committed
71 72 73 74 75
  render() {
    const { className, children } = this.props;
    const { type, tabs } = this.state;
    const TabChildren = [];
    const otherChildren = [];
jim's avatar
jim committed
76
    React.Children.forEach(children, item => {
77 78 79
      if (!item) {
        return;
      }
ddcat1115's avatar
ddcat1115 committed
80
      // eslint-disable-next-line
81
      if (item.type.typeName === 'LoginTab') {
ddcat1115's avatar
ddcat1115 committed
82 83 84 85 86 87
        TabChildren.push(item);
      } else {
        otherChildren.push(item);
      }
    });
    return (
jim's avatar
jim committed
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
      <LoginContext.Provider value={this.getContext()}>
        <div className={classNames(className, styles.login)}>
          <Form onSubmit={this.handleSubmit}>
            {tabs.length ? (
              <>
                <Tabs
                  animated={false}
                  className={styles.tabs}
                  activeKey={type}
                  onChange={this.onSwitch}
                >
                  {TabChildren}
                </Tabs>
                {otherChildren}
              </>
            ) : (
              [...children]
            )}
          </Form>
        </div>
      </LoginContext.Provider>
ddcat1115's avatar
ddcat1115 committed
109 110 111 112 113 114
    );
  }
}

Login.Tab = LoginTab;
Login.Submit = LoginSubmit;
jim's avatar
jim committed
115
Object.keys(LoginItem).forEach(item => {
ddcat1115's avatar
ddcat1115 committed
116 117 118
  Login[item] = LoginItem[item];
});

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