LoginItem.js 3.08 KB
Newer Older
ddcat1115's avatar
ddcat1115 committed
1 2 3 4 5 6 7 8 9 10
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { Form, Button, Row, Col } from 'antd';
import omit from 'omit.js';
import styles from './index.less';
import map from './map';

const FormItem = Form.Item;

function generator({ defaultProps, defaultRules, type }) {
jim's avatar
jim committed
11
  return WrappedComponent => {
ddcat1115's avatar
ddcat1115 committed
12 13 14 15 16
    return class BasicComponent extends Component {
      static contextTypes = {
        form: PropTypes.object,
        updateActive: PropTypes.func,
      };
陈帅's avatar
陈帅 committed
17

18 19 20 21
      static defaultProps = {
        buttonText: '获取验证码',
      };

ddcat1115's avatar
ddcat1115 committed
22 23 24 25 26 27
      constructor(props) {
        super(props);
        this.state = {
          count: 0,
        };
      }
陈帅's avatar
陈帅 committed
28

ddcat1115's avatar
ddcat1115 committed
29
      componentDidMount() {
30 31 32 33
        const { updateActive } = this.context;
        const { name } = this.props;
        if (updateActive) {
          updateActive(name);
ddcat1115's avatar
ddcat1115 committed
34 35
        }
      }
陈帅's avatar
陈帅 committed
36

ddcat1115's avatar
ddcat1115 committed
37 38 39
      componentWillUnmount() {
        clearInterval(this.interval);
      }
陈帅's avatar
陈帅 committed
40

ddcat1115's avatar
ddcat1115 committed
41 42 43
      onGetCaptcha = () => {
        let count = 59;
        this.setState({ count });
44 45 46
        const { onGetCaptcha } = this.props;
        if (onGetCaptcha) {
          onGetCaptcha();
ddcat1115's avatar
ddcat1115 committed
47 48 49 50 51 52 53 54
        }
        this.interval = setInterval(() => {
          count -= 1;
          this.setState({ count });
          if (count === 0) {
            clearInterval(this.interval);
          }
        }, 1000);
jim's avatar
jim committed
55
      };
陈帅's avatar
陈帅 committed
56

ddcat1115's avatar
ddcat1115 committed
57
      render() {
58 59
        const { form } = this.context;
        const { getFieldDecorator } = form;
ddcat1115's avatar
ddcat1115 committed
60 61
        const options = {};
        let otherProps = {};
62
        const { onChange, defaultValue, buttonText, rules, name, ...restProps } = this.props;
ddcat1115's avatar
ddcat1115 committed
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
        const { count } = this.state;
        options.rules = rules || defaultRules;
        if (onChange) {
          options.onChange = onChange;
        }
        if (defaultValue) {
          options.initialValue = defaultValue;
        }
        otherProps = restProps || otherProps;
        if (type === 'Captcha') {
          const inputProps = omit(otherProps, ['onGetCaptcha']);
          return (
            <FormItem>
              <Row gutter={8}>
                <Col span={16}>
                  {getFieldDecorator(name, options)(
                    <WrappedComponent {...defaultProps} {...inputProps} />
                  )}
                </Col>
                <Col span={8}>
                  <Button
                    disabled={count}
                    className={styles.getCaptcha}
                    size="large"
                    onClick={this.onGetCaptcha}
                  >
89
                    {count ? `${count} s` : buttonText}
ddcat1115's avatar
ddcat1115 committed
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
                  </Button>
                </Col>
              </Row>
            </FormItem>
          );
        }
        return (
          <FormItem>
            {getFieldDecorator(name, options)(
              <WrappedComponent {...defaultProps} {...otherProps} />
            )}
          </FormItem>
        );
      }
    };
  };
}

const LoginItem = {};
jim's avatar
jim committed
109
Object.keys(map).forEach(item => {
ddcat1115's avatar
ddcat1115 committed
110 111 112 113 114 115 116 117
  LoginItem[item] = generator({
    defaultProps: map[item].props,
    defaultRules: map[item].rules,
    type: item,
  })(map[item].component);
});

export default LoginItem;