LoginItem.js 3.52 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';
3
import { formatMessage } from 'umi/locale';
ddcat1115's avatar
ddcat1115 committed
4 5
import omit from 'omit.js';
import styles from './index.less';
jim's avatar
jim committed
6 7
import ItemMap from './map';
import LoginContext from './loginContext';
ddcat1115's avatar
ddcat1115 committed
8 9 10

const FormItem = Form.Item;

damon.chen's avatar
damon.chen committed
11
class WrapFormItem extends Component {
陈帅's avatar
陈帅 committed
12
  static defaultProps = {
13 14
    getCaptchaButtonText: formatMessage({ id: 'form.captcha' }),
    getCaptchaSecondText: formatMessage({ id: 'form.captcha.second' }),
陈帅's avatar
陈帅 committed
15 16
  };

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

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

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

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

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

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

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

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

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

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

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

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

export default LoginItem;