| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- import React, { Component } from 'react';
- import { Form, Input, Button, Row, Col } from 'antd';
- import omit from 'omit.js';
- import styles from './index.less';
- import ItemMap from './map';
- import LoginContext from './loginContext';
- const FormItem = Form.Item;
- class WarpFormItem extends Component {
- constructor(props) {
- super(props);
- this.state = {
- count: 0,
- };
- }
- 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, rules }) => {
- const options = {
- rules: rules || this.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 = setInterval(() => {
- count -= 1;
- this.setState({ count });
- if (count === 0) {
- clearInterval(this.interval);
- }
- }, 1000);
- };
- render() {
- const { count } = this.state;
- const {
- form: { getFieldDecorator },
- } = this.props;
- // 这么写是为了防止restProps中 带入 onChange, defaultValue, rules props
- const {
- onChange,
- customprops,
- defaultValue,
- rules,
- name,
- buttonText,
- updateActive,
- type,
- ...restProps
- } = this.props;
- // get getFieldDecorator props
- const options = this.getFormItemOptions(this.props);
- const otherProps = restProps || {};
- if (type === 'Captcha') {
- const inputProps = omit(otherProps, ['onGetCaptcha']);
- 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} s` : buttonText}
- </Button>
- </Col>
- </Row>
- </FormItem>
- );
- }
- return (
- <FormItem>
- {getFieldDecorator(name, options)(<Input {...customprops} {...otherProps} />)}
- </FormItem>
- );
- }
- }
- const LoginItem = {};
- Object.keys(ItemMap).forEach(key => {
- const item = ItemMap[key];
- LoginItem[key] = props => {
- return (
- <LoginContext.Consumer>
- {context => (
- <WarpFormItem
- customprops={item.props}
- {...props}
- rules={item.rules}
- type={key}
- updateActive={context.updateActive}
- form={context.form}
- />
- )}
- </LoginContext.Consumer>
- );
- };
- });
- export default LoginItem;
|