LoginItem.js 3.41 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
  static defaultProps = {
陈帅's avatar
陈帅 committed
12 13
    getCaptchaButtonText: 'captcha',
    getCaptchaSecondText: 'second',
陈帅's avatar
陈帅 committed
14 15
  };

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

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

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

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

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

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

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

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

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

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

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

const LoginItem = {};
jim's avatar
jim committed
129 130
Object.keys(ItemMap).forEach(key => {
  const item = ItemMap[key];
131 132 133
  LoginItem[key] = props => (
    <LoginContext.Consumer>
      {context => (
134
        <WrapFormItem
135 136
          customprops={item.props}
          rules={item.rules}
137
          {...props}
138 139 140 141 142 143 144
          type={key}
          updateActive={context.updateActive}
          form={context.form}
        />
      )}
    </LoginContext.Consumer>
  );
ddcat1115's avatar
ddcat1115 committed
145 146 147
});

export default LoginItem;