LoginItem.js 3.24 KB
Newer Older
ddcat1115's avatar
ddcat1115 committed
1
import React, { Component } from 'react';
jim's avatar
jim committed
2
import { Form, Input, Button, Row, Col } from 'antd';
ddcat1115's avatar
ddcat1115 committed
3 4
import omit from 'omit.js';
import styles from './index.less';
jim's avatar
jim committed
5 6
import ItemMap from './map';
import LoginContext from './loginContext';
ddcat1115's avatar
ddcat1115 committed
7 8 9

const FormItem = Form.Item;

jim's avatar
jim committed
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
class WarpFormItem extends Component {
  constructor(props) {
    super(props);
    this.state = {
      count: 0,
    };
  }
  componentDidMount() {
    if (this.props.updateActive) {
      this.props.updateActive(this.props.name);
    }
  }
  componentWillUnmount() {
    clearInterval(this.interval);
  }
  onGetCaptcha = () => {
26 27 28 29 30 31 32 33 34
    const { onGetCaptcha } = this.props;
    const result = onGetCaptcha ? onGetCaptcha() : null;
    if (result === false) {
      return;
    }
    if (result instanceof Promise) {
      result.then(this.runGetCaptchaCountDown);
    } else {
      this.runGetCaptchaCountDown();
jim's avatar
jim committed
35 36 37 38 39
    }
  };
  getFormItemOptions = ({ onChange, defaultValue, rules }) => {
    const options = {
      rules: rules || this.customprops.rules,
ddcat1115's avatar
ddcat1115 committed
40
    };
jim's avatar
jim committed
41 42 43 44 45 46 47
    if (onChange) {
      options.onChange = onChange;
    }
    if (defaultValue) {
      options.initialValue = defaultValue;
    }
    return options;
ddcat1115's avatar
ddcat1115 committed
48
  };
49 50 51 52 53 54 55 56 57 58 59
  runGetCaptchaCountDown = () => {
    let count = this.props.countDown || 59;
    this.setState({ count });
    this.interval = setInterval(() => {
      count -= 1;
      this.setState({ count });
      if (count === 0) {
        clearInterval(this.interval);
      }
    }, 1000);
  };
jim's avatar
jim committed
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 105 106 107 108 109
  render() {
    const { count } = this.state;

    const { getFieldDecorator } = this.props.form;

    // 这么写是为了防止restProps中 带入 onChange, defaultValue, rules props
    const {
      onChange,
      customprops,
      defaultValue,
      rules,
      name,
      updateActive,
      ...restProps
    } = this.props;

    // get getFieldDecorator props
    const options = this.getFormItemOptions(this.props);

    const otherProps = restProps || {};
    if (this.props.type === 'Captcha') {
      const inputProps = omit(otherProps, ['onGetCaptcha']);
      return (
        <FormItem>
          <Row gutter={8}>
            <Col span={16}>
              {getFieldDecorator(name, options)(
                <Input {...this.props.customprops} {...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)(<Input {...this.props.customprops} {...otherProps} />)}
      </FormItem>
    );
  }
ddcat1115's avatar
ddcat1115 committed
110 111 112
}

const LoginItem = {};
jim's avatar
jim committed
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
Object.keys(ItemMap).forEach(key => {
  const item = ItemMap[key];
  LoginItem[key] = props => {
    return (
      <LoginContext.Consumer>
        {context => (
          <WarpFormItem
            customprops={item.props}
            {...props}
            rules={item.rules}
            type={key}
            updateActive={context.updateActive}
            form={context.form}
          />
        )}
      </LoginContext.Consumer>
    );
  };
ddcat1115's avatar
ddcat1115 committed
131 132 133
});

export default LoginItem;