LoginItem.js 3.3 KB

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