LoginItem.js 3.31 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;

damon.chen's avatar
damon.chen committed
10
class WrapFormItem extends Component {
陈帅's avatar
陈帅 committed
11 12 13 14
  static defaultProps = {
    buttonText: '获取验证码',
  };

jim's avatar
jim committed
15 16 17 18 19 20
  constructor(props) {
    super(props);
    this.state = {
      count: 0,
    };
  }
陈帅's avatar
陈帅 committed
21

jim's avatar
jim committed
22
  componentDidMount() {
陈帅's avatar
陈帅 committed
23 24 25
    const { updateActive, name } = this.props;
    if (updateActive) {
      updateActive(name);
jim's avatar
jim committed
26 27
    }
  }
陈帅's avatar
陈帅 committed
28

jim's avatar
jim committed
29 30 31
  componentWillUnmount() {
    clearInterval(this.interval);
  }
陈帅's avatar
陈帅 committed
32

jim's avatar
jim committed
33
  onGetCaptcha = () => {
34 35 36 37 38 39 40 41 42
    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
43 44
    }
  };
陈帅's avatar
陈帅 committed
45

46
  getFormItemOptions = ({ onChange, defaultValue, customprops, rules }) => {
jim's avatar
jim committed
47
    const options = {
48
      rules: rules || customprops.rules,
ddcat1115's avatar
ddcat1115 committed
49
    };
jim's avatar
jim committed
50 51 52 53 54 55 56
    if (onChange) {
      options.onChange = onChange;
    }
    if (defaultValue) {
      options.initialValue = defaultValue;
    }
    return options;
ddcat1115's avatar
ddcat1115 committed
57
  };
陈帅's avatar
陈帅 committed
58

59
  runGetCaptchaCountDown = () => {
陈帅's avatar
陈帅 committed
60 61
    const { countDown } = this.props;
    let count = countDown || 59;
62 63 64 65 66 67 68 69 70
    this.setState({ count });
    this.interval = setInterval(() => {
      count -= 1;
      this.setState({ count });
      if (count === 0) {
        clearInterval(this.interval);
      }
    }, 1000);
  };
陈帅's avatar
陈帅 committed
71

jim's avatar
jim committed
72 73 74
  render() {
    const { count } = this.state;

陈帅's avatar
陈帅 committed
75 76 77
    const {
      form: { getFieldDecorator },
    } = this.props;
jim's avatar
jim committed
78 79 80 81 82 83 84 85

    // 这么写是为了防止restProps中 带入 onChange, defaultValue, rules props
    const {
      onChange,
      customprops,
      defaultValue,
      rules,
      name,
陈帅's avatar
陈帅 committed
86
      buttonText,
jim's avatar
jim committed
87
      updateActive,
陈帅's avatar
陈帅 committed
88
      type,
jim's avatar
jim committed
89 90 91 92 93 94 95
      ...restProps
    } = this.props;

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

    const otherProps = restProps || {};
陈帅's avatar
陈帅 committed
96
    if (type === 'Captcha') {
97
      const inputProps = omit(otherProps, ['onGetCaptcha', 'countDown']);
jim's avatar
jim committed
98 99 100 101
      return (
        <FormItem>
          <Row gutter={8}>
            <Col span={16}>
陈帅's avatar
陈帅 committed
102
              {getFieldDecorator(name, options)(<Input {...customprops} {...inputProps} />)}
jim's avatar
jim committed
103 104 105 106 107 108 109 110
            </Col>
            <Col span={8}>
              <Button
                disabled={count}
                className={styles.getCaptcha}
                size="large"
                onClick={this.onGetCaptcha}
              >
陈帅's avatar
陈帅 committed
111
                {count ? `${count} s` : buttonText}
jim's avatar
jim committed
112 113 114 115 116 117 118 119
              </Button>
            </Col>
          </Row>
        </FormItem>
      );
    }
    return (
      <FormItem>
陈帅's avatar
陈帅 committed
120
        {getFieldDecorator(name, options)(<Input {...customprops} {...otherProps} />)}
jim's avatar
jim committed
121 122 123
      </FormItem>
    );
  }
ddcat1115's avatar
ddcat1115 committed
124 125 126
}

const LoginItem = {};
jim's avatar
jim committed
127 128
Object.keys(ItemMap).forEach(key => {
  const item = ItemMap[key];
129 130 131 132 133 134 135 136 137 138 139 140 141 142
  LoginItem[key] = props => (
    <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
143 144 145
});

export default LoginItem;