index.js 3.11 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

class Login extends Component {
  static propTypes = {
    className: PropTypes.string,
    defaultActiveKey: PropTypes.string,
    onTabChange: PropTypes.func,
    onSubmit: PropTypes.func,
  };
jim's avatar
jim committed
18 19 20 21 22 23 24 25

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

陈帅's avatar
陈帅 committed
26 27 28 29 30 31 32 33 34
  constructor(props) {
    super(props);
    this.state = {
      type: props.defaultActiveKey,
      tabs: [],
      active: {},
    };
  }

jim's avatar
jim committed
35 36 37 38
  onSwitch = type => {
    this.setState({
      type,
    });
陈帅's avatar
陈帅 committed
39 40
    const { onTabChange } = this.props;
    onTabChange(type);
jim's avatar
jim committed
41
  };
陈帅's avatar
陈帅 committed
42

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

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

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

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

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