LoginItem.js 3.29 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
class WarpFormItem extends Component {
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

jim's avatar
jim committed
46 47 48
  getFormItemOptions = ({ onChange, defaultValue, rules }) => {
    const options = {
      rules: rules || this.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,
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') {
jim's avatar
jim committed
97 98 99 100 101
      const inputProps = omit(otherProps, ['onGetCaptcha']);
      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}
              >
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;