动态菜单和动态路由的 antd pro

LoginItem.js 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import React, { Component } from 'react';
  2. import PropTypes from 'prop-types';
  3. import { Form, Button, Row, Col } from 'antd';
  4. import omit from 'omit.js';
  5. import styles from './index.less';
  6. import map from './map';
  7. const FormItem = Form.Item;
  8. function generator({ defaultProps, defaultRules, type }) {
  9. return WrappedComponent => {
  10. return class BasicComponent extends Component {
  11. static contextTypes = {
  12. form: PropTypes.object,
  13. updateActive: PropTypes.func,
  14. };
  15. constructor(props) {
  16. super(props);
  17. this.state = {
  18. count: 0,
  19. };
  20. }
  21. componentDidMount() {
  22. if (this.context.updateActive) {
  23. this.context.updateActive(this.props.name);
  24. }
  25. }
  26. componentWillUnmount() {
  27. clearInterval(this.interval);
  28. }
  29. onGetCaptcha = () => {
  30. let count = 59;
  31. this.setState({ count });
  32. if (this.props.onGetCaptcha) {
  33. this.props.onGetCaptcha();
  34. }
  35. this.interval = setInterval(() => {
  36. count -= 1;
  37. this.setState({ count });
  38. if (count === 0) {
  39. clearInterval(this.interval);
  40. }
  41. }, 1000);
  42. };
  43. render() {
  44. const { getFieldDecorator } = this.context.form;
  45. const options = {};
  46. let otherProps = {};
  47. const { onChange, defaultValue, rules, name, ...restProps } = this.props;
  48. const { count } = this.state;
  49. options.rules = rules || defaultRules;
  50. if (onChange) {
  51. options.onChange = onChange;
  52. }
  53. if (defaultValue) {
  54. options.initialValue = defaultValue;
  55. }
  56. otherProps = restProps || otherProps;
  57. if (type === 'Captcha') {
  58. const inputProps = omit(otherProps, ['onGetCaptcha']);
  59. return (
  60. <FormItem>
  61. <Row gutter={8}>
  62. <Col span={16}>
  63. {getFieldDecorator(name, options)(
  64. <WrappedComponent {...defaultProps} {...inputProps} />
  65. )}
  66. </Col>
  67. <Col span={8}>
  68. <Button
  69. disabled={count}
  70. className={styles.getCaptcha}
  71. size="large"
  72. onClick={this.onGetCaptcha}
  73. >
  74. {count ? `${count} s` : '获取验证码'}
  75. </Button>
  76. </Col>
  77. </Row>
  78. </FormItem>
  79. );
  80. }
  81. return (
  82. <FormItem>
  83. {getFieldDecorator(name, options)(
  84. <WrappedComponent {...defaultProps} {...otherProps} />
  85. )}
  86. </FormItem>
  87. );
  88. }
  89. };
  90. };
  91. }
  92. const LoginItem = {};
  93. Object.keys(map).forEach(item => {
  94. LoginItem[item] = generator({
  95. defaultProps: map[item].props,
  96. defaultRules: map[item].rules,
  97. type: item,
  98. })(map[item].component);
  99. });
  100. export default LoginItem;