LoginItem.js 3.22 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
class WarpFormItem extends Component {
  constructor(props) {
    super(props);
    this.state = {
      count: 0,
    };
  }
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
17

jim's avatar
jim committed
18
  componentDidMount() {
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
19 20 21
    const { updateActive, name } = this.props;
    if (updateActive) {
      updateActive(name);
jim's avatar
jim committed
22 23
    }
  }
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
24

jim's avatar
jim committed
25 26 27
  componentWillUnmount() {
    clearInterval(this.interval);
  }
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
28

jim's avatar
jim committed
29
  onGetCaptcha = () => {
30 31 32 33 34 35 36 37 38
    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
39 40
    }
  };
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
41

jim's avatar
jim committed
42 43 44
  getFormItemOptions = ({ onChange, defaultValue, rules }) => {
    const options = {
      rules: rules || this.customprops.rules,
ddcat1115's avatar
ddcat1115 committed
45
    };
jim's avatar
jim committed
46 47 48 49 50 51 52
    if (onChange) {
      options.onChange = onChange;
    }
    if (defaultValue) {
      options.initialValue = defaultValue;
    }
    return options;
ddcat1115's avatar
ddcat1115 committed
53
  };
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
54

55
  runGetCaptchaCountDown = () => {
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
56 57
    const { countDown } = this.props;
    let count = countDown || 59;
58 59 60 61 62 63 64 65 66
    this.setState({ count });
    this.interval = setInterval(() => {
      count -= 1;
      this.setState({ count });
      if (count === 0) {
        clearInterval(this.interval);
      }
    }, 1000);
  };
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
67

jim's avatar
jim committed
68 69 70
  render() {
    const { count } = this.state;

้™ˆๅธ…'s avatar
้™ˆๅธ… committed
71 72 73
    const {
      form: { getFieldDecorator },
    } = this.props;
jim's avatar
jim committed
74 75 76 77 78 79 80 81

    // ่ฟ™ไนˆๅ†™ๆ˜ฏไธบไบ†้˜ฒๆญขrestPropsไธญ ๅธฆๅ…ฅ onChange, defaultValue, rules props
    const {
      onChange,
      customprops,
      defaultValue,
      rules,
      name,
82
      buttonText,
jim's avatar
jim committed
83
      updateActive,
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
84
      type,
jim's avatar
jim committed
85 86 87 88 89 90 91
      ...restProps
    } = this.props;

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

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

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

export default LoginItem;