LoginItem.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import React, { Component } from 'react';
  2. import { Form, Input, Button, Row, Col } from 'antd';
  3. import omit from 'omit.js';
  4. import styles from './index.less';
  5. import ItemMap from './map';
  6. import LoginContext from './loginContext';
  7. const FormItem = Form.Item;
  8. class WarpFormItem extends Component {
  9. constructor(props) {
  10. super(props);
  11. this.state = {
  12. count: 0,
  13. };
  14. }
  15. componentDidMount() {
  16. const { updateActive, name } = this.props;
  17. if (updateActive) {
  18. updateActive(name);
  19. }
  20. }
  21. componentWillUnmount() {
  22. clearInterval(this.interval);
  23. }
  24. onGetCaptcha = () => {
  25. const { onGetCaptcha } = this.props;
  26. const result = onGetCaptcha ? onGetCaptcha() : null;
  27. if (result === false) {
  28. return;
  29. }
  30. if (result instanceof Promise) {
  31. result.then(this.runGetCaptchaCountDown);
  32. } else {
  33. this.runGetCaptchaCountDown();
  34. }
  35. };
  36. getFormItemOptions = ({ onChange, defaultValue, rules }) => {
  37. const options = {
  38. rules: rules || this.customprops.rules,
  39. };
  40. if (onChange) {
  41. options.onChange = onChange;
  42. }
  43. if (defaultValue) {
  44. options.initialValue = defaultValue;
  45. }
  46. return options;
  47. };
  48. runGetCaptchaCountDown = () => {
  49. const { countDown } = this.props;
  50. let count = countDown || 59;
  51. this.setState({ count });
  52. this.interval = setInterval(() => {
  53. count -= 1;
  54. this.setState({ count });
  55. if (count === 0) {
  56. clearInterval(this.interval);
  57. }
  58. }, 1000);
  59. };
  60. render() {
  61. const { count } = this.state;
  62. const {
  63. form: { getFieldDecorator },
  64. } = this.props;
  65. // 这么写是为了防止restProps中 带入 onChange, defaultValue, rules props
  66. const {
  67. onChange,
  68. customprops,
  69. defaultValue,
  70. rules,
  71. name,
  72. buttonText,
  73. updateActive,
  74. type,
  75. ...restProps
  76. } = this.props;
  77. // get getFieldDecorator props
  78. const options = this.getFormItemOptions(this.props);
  79. const otherProps = restProps || {};
  80. if (type === 'Captcha') {
  81. const inputProps = omit(otherProps, ['onGetCaptcha']);
  82. return (
  83. <FormItem>
  84. <Row gutter={8}>
  85. <Col span={16}>
  86. {getFieldDecorator(name, options)(<Input {...customprops} {...inputProps} />)}
  87. </Col>
  88. <Col span={8}>
  89. <Button
  90. disabled={count}
  91. className={styles.getCaptcha}
  92. size="large"
  93. onClick={this.onGetCaptcha}
  94. >
  95. {count ? `${count} s` : buttonText}
  96. </Button>
  97. </Col>
  98. </Row>
  99. </FormItem>
  100. );
  101. }
  102. return (
  103. <FormItem>
  104. {getFieldDecorator(name, options)(<Input {...customprops} {...otherProps} />)}
  105. </FormItem>
  106. );
  107. }
  108. }
  109. const LoginItem = {};
  110. Object.keys(ItemMap).forEach(key => {
  111. const item = ItemMap[key];
  112. LoginItem[key] = props => {
  113. return (
  114. <LoginContext.Consumer>
  115. {context => (
  116. <WarpFormItem
  117. customprops={item.props}
  118. {...props}
  119. rules={item.rules}
  120. type={key}
  121. updateActive={context.updateActive}
  122. form={context.form}
  123. />
  124. )}
  125. </LoginContext.Consumer>
  126. );
  127. };
  128. });
  129. export default LoginItem;