LoginItem.js 2.97 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 26 27 28 29 30 31 32 33 34
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 = () => {
    let count = 59;
    this.setState({ count });
    if (this.props.onGetCaptcha) {
      this.props.onGetCaptcha();
    }
    this.interval = setInterval(() => {
      count -= 1;
      this.setState({ count });
      if (count === 0) {
ddcat1115's avatar
ddcat1115 committed
35 36
        clearInterval(this.interval);
      }
jim's avatar
jim committed
37 38 39 40 41
    }, 1000);
  };
  getFormItemOptions = ({ onChange, defaultValue, rules }) => {
    const options = {
      rules: rules || this.customprops.rules,
ddcat1115's avatar
ddcat1115 committed
42
    };
jim's avatar
jim committed
43 44 45 46 47 48 49
    if (onChange) {
      options.onChange = onChange;
    }
    if (defaultValue) {
      options.initialValue = defaultValue;
    }
    return options;
ddcat1115's avatar
ddcat1115 committed
50
  };
jim's avatar
jim committed
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 { 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
101 102 103
}

const LoginItem = {};
jim's avatar
jim committed
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
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
122 123 124
});

export default LoginItem;