LoginItem.js 3 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

ddcat1115's avatar
ddcat1115 committed
18 19 20 21 22 23
      constructor(props) {
        super(props);
        this.state = {
          count: 0,
        };
      }
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
24

ddcat1115's avatar
ddcat1115 committed
25
      componentDidMount() {
26 27 28 29
        const { updateActive } = this.context;
        const { name } = this.props;
        if (updateActive) {
          updateActive(name);
ddcat1115's avatar
ddcat1115 committed
30 31
        }
      }
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
32

ddcat1115's avatar
ddcat1115 committed
33 34 35
      componentWillUnmount() {
        clearInterval(this.interval);
      }
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
36

ddcat1115's avatar
ddcat1115 committed
37 38 39
      onGetCaptcha = () => {
        let count = 59;
        this.setState({ count });
40 41 42
        const { onGetCaptcha } = this.props;
        if (onGetCaptcha) {
          onGetCaptcha();
ddcat1115's avatar
ddcat1115 committed
43 44 45 46 47 48 49 50
        }
        this.interval = setInterval(() => {
          count -= 1;
          this.setState({ count });
          if (count === 0) {
            clearInterval(this.interval);
          }
        }, 1000);
jim's avatar
jim committed
51
      };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
52

ddcat1115's avatar
ddcat1115 committed
53
      render() {
54 55
        const { form } = this.context;
        const { getFieldDecorator } = form;
ddcat1115's avatar
ddcat1115 committed
56 57 58 59 60 61 62 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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
        const options = {};
        let otherProps = {};
        const { onChange, defaultValue, rules, name, ...restProps } = this.props;
        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}
                  >
                    {count ? `${count} s` : 'θŽ·ε–ιͺŒθ―η '}
                  </Button>
                </Col>
              </Row>
            </FormItem>
          );
        }
        return (
          <FormItem>
            {getFieldDecorator(name, options)(
              <WrappedComponent {...defaultProps} {...otherProps} />
            )}
          </FormItem>
        );
      }
    };
  };
}

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

export default LoginItem;