LoginItem.js 2.91 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 26 27 28 29
      componentDidMount() {
        if (this.context.updateActive) {
          this.context.updateActive(this.props.name);
        }
      }
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
30

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

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

ddcat1115's avatar
ddcat1115 committed
50 51 52 53 54 55 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
      render() {
        const { getFieldDecorator } = this.context.form;
        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
101
Object.keys(map).forEach(item => {
ddcat1115's avatar
ddcat1115 committed
102 103 104 105 106 107 108 109
  LoginItem[item] = generator({
    defaultProps: map[item].props,
    defaultRules: map[item].rules,
    type: item,
  })(map[item].component);
});

export default LoginItem;