LoginItem.tsx 4.76 KB
Newer Older
陈帅's avatar
陈帅 committed
1
import { Button, Col, Form, Input, Row } from 'antd';
陈帅's avatar
陈帅 committed
2
import React, { Component } from 'react';
陈帅's avatar
陈帅 committed
3

陈帅's avatar
陈帅 committed
4
import { FormComponentProps } from 'antd/es/form';
陈帅's avatar
陈帅 committed
5
import omit from 'omit.js';
陈帅's avatar
陈帅 committed
6
import ItemMap from './map';
陈帅's avatar
陈帅 committed
7
import LoginContext, { ILoginContext } from './LoginContext';
陈帅's avatar
陈帅 committed
8
import styles from './index.less';
陈帅's avatar
陈帅 committed
9 10 11 12 13 14 15

type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;

export type WrappedLoginItemProps = Omit<LoginItemProps, 'form' | 'type' | 'updateActive'>;
export type LoginItemKeyType = keyof typeof ItemMap;
export type LoginItemType = { [K in keyof typeof ItemMap]: React.FC<WrappedLoginItemProps> };

陈帅's avatar
陈帅 committed
16
export interface LoginItemProps {
陈帅's avatar
陈帅 committed
17 18 19 20 21 22 23 24 25 26
  name?: string;
  rules?: any[];
  style?: React.CSSProperties;
  onGetCaptcha?: (event?: MouseEvent) => void | Promise<any> | false;
  placeholder?: string;
  buttonText?: React.ReactNode;
  onPressEnter?: (e: any) => void;
  countDown?: number;
  getCaptchaButtonText?: string;
  getCaptchaSecondText?: string;
陈帅's avatar
陈帅 committed
27 28
  updateActive?: ILoginContext['updateActive'];
  type?: string;
陈帅's avatar
陈帅 committed
29
  defaultValue?: string;
陈帅's avatar
陈帅 committed
30
  form?: FormComponentProps['form'];
陈帅's avatar
陈帅 committed
31 32
  customProps?: { [key: string]: any };
  onChange?: (e: any) => void;
陈帅's avatar
陈帅 committed
33
  tabUtil?: any;
陈帅's avatar
陈帅 committed
34 35 36 37 38 39 40 41 42 43 44 45 46
}

interface LoginItemState {
  count: number;
}

const FormItem = Form.Item;

class WrapFormItem extends Component<LoginItemProps, LoginItemState> {
  static defaultProps = {
    getCaptchaButtonText: 'captcha',
    getCaptchaSecondText: 'second',
  };
陈帅's avatar
陈帅 committed
47

陈帅's avatar
陈帅 committed
48
  interval: number | undefined;
陈帅's avatar
陈帅 committed
49 50 51 52 53 54 55

  constructor(props: LoginItemProps) {
    super(props);
    this.state = {
      count: 0,
    };
  }
陈帅's avatar
陈帅 committed
56

陈帅's avatar
陈帅 committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
  componentDidMount() {
    const { updateActive, name = '' } = this.props;
    if (updateActive) {
      updateActive(name);
    }
  }

  componentWillUnmount() {
    clearInterval(this.interval);
  }

  onGetCaptcha = () => {
    const { onGetCaptcha } = this.props;
    const result = onGetCaptcha ? onGetCaptcha() : null;
    if (result === false) {
      return;
    }
    if (result instanceof Promise) {
      result.then(this.runGetCaptchaCountDown);
    } else {
      this.runGetCaptchaCountDown();
    }
  };

  getFormItemOptions = ({ onChange, defaultValue, customProps = {}, rules }: LoginItemProps) => {
    const options: {
陈帅's avatar
陈帅 committed
83
      rules?: any[];
陈帅's avatar
陈帅 committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
      onChange?: LoginItemProps['onChange'];
      initialValue?: LoginItemProps['defaultValue'];
    } = {
      rules: rules || customProps.rules,
    };
    if (onChange) {
      options.onChange = onChange;
    }
    if (defaultValue) {
      options.initialValue = defaultValue;
    }
    return options;
  };

  runGetCaptchaCountDown = () => {
    const { countDown } = this.props;
    let count = countDown || 59;
    this.setState({ count });
    this.interval = window.setInterval(() => {
      count -= 1;
      this.setState({ count });
      if (count === 0) {
        clearInterval(this.interval);
      }
    }, 1000);
  };

  render() {
    const { count } = this.state;

陈帅's avatar
陈帅 committed
114
    // 这么写是为了防止restProps中 带入 onChange, defaultValue, rules props tabUtil
陈帅's avatar
陈帅 committed
115 116 117 118 119 120 121 122 123 124 125
    const {
      onChange,
      customProps,
      defaultValue,
      rules,
      name,
      getCaptchaButtonText,
      getCaptchaSecondText,
      updateActive,
      type,
      form,
陈帅's avatar
陈帅 committed
126
      tabUtil,
陈帅's avatar
陈帅 committed
127 128 129 130 131 132 133 134 135 136 137 138
      ...restProps
    } = this.props;
    if (!name) {
      return null;
    }
    if (!form) {
      return null;
    }
    const { getFieldDecorator } = form;
    // get getFieldDecorator props
    const options = this.getFormItemOptions(this.props);
    const otherProps = restProps || {};
陈帅's avatar
陈帅 committed
139

陈帅's avatar
陈帅 committed
140 141
    if (type === 'Captcha') {
      const inputProps = omit(otherProps, ['onGetCaptcha', 'countDown']);
陈帅's avatar
陈帅 committed
142

陈帅's avatar
陈帅 committed
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
      return (
        <FormItem>
          <Row gutter={8}>
            <Col span={16}>
              {getFieldDecorator(name, options)(<Input {...customProps} {...inputProps} />)}
            </Col>
            <Col span={8}>
              <Button
                disabled={!!count}
                className={styles.getCaptcha}
                size="large"
                onClick={this.onGetCaptcha}
              >
                {count ? `${count} ${getCaptchaSecondText}` : getCaptchaButtonText}
              </Button>
            </Col>
          </Row>
        </FormItem>
      );
    }
    return (
      <FormItem>
        {getFieldDecorator(name, options)(<Input {...customProps} {...otherProps} />)}
      </FormItem>
    );
  }
}

const LoginItem: Partial<LoginItemType> = {};

Object.keys(ItemMap).forEach(key => {
  const item = ItemMap[key];
  LoginItem[key] = (props: LoginItemProps) => (
    <LoginContext.Consumer>
陈帅's avatar
陈帅 committed
177
      {context => (
陈帅's avatar
陈帅 committed
178 179 180 181 182 183 184 185 186
        <WrapFormItem
          customProps={item.props}
          rules={item.rules}
          {...props}
          type={key}
          {...context}
          updateActive={context.updateActive}
        />
      )}
陈帅's avatar
陈帅 committed
187 188 189 190 191
    </LoginContext.Consumer>
  );
});

export default LoginItem as LoginItemType;