index.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import React, { Component } from 'react';
  2. import PropTypes from 'prop-types';
  3. import { Form, Tabs } from 'antd';
  4. import classNames from 'classnames';
  5. import LoginItem from './LoginItem';
  6. import LoginTab from './LoginTab';
  7. import LoginSubmit from './LoginSubmit';
  8. import styles from './index.less';
  9. @Form.create()
  10. class Login extends Component {
  11. static defaultProps = {
  12. className: '',
  13. defaultActiveKey: '',
  14. onTabChange: () => {},
  15. onSubmit: () => {},
  16. };
  17. static propTypes = {
  18. className: PropTypes.string,
  19. defaultActiveKey: PropTypes.string,
  20. onTabChange: PropTypes.func,
  21. onSubmit: PropTypes.func,
  22. };
  23. static childContextTypes = {
  24. tabUtil: PropTypes.object,
  25. form: PropTypes.object,
  26. updateActive: PropTypes.func,
  27. };
  28. state = {
  29. type: this.props.defaultActiveKey,
  30. tabs: [],
  31. active: {},
  32. };
  33. getChildContext() {
  34. return {
  35. tabUtil: {
  36. addTab: (id) => {
  37. this.setState({
  38. tabs: [...this.state.tabs, id],
  39. });
  40. },
  41. removeTab: (id) => {
  42. this.setState({
  43. tabs: this.state.tabs.filter(currentId => currentId !== id),
  44. });
  45. },
  46. },
  47. form: this.props.form,
  48. updateActive: (activeItem) => {
  49. const { type, active } = this.state;
  50. if (active[type]) {
  51. active[type].push(activeItem);
  52. } else {
  53. active[type] = [activeItem];
  54. }
  55. this.setState({
  56. active,
  57. });
  58. },
  59. };
  60. }
  61. onSwitch = (type) => {
  62. this.setState({
  63. type,
  64. });
  65. this.props.onTabChange(type);
  66. }
  67. handleSubmit = (e) => {
  68. e.preventDefault();
  69. const { active, type } = this.state;
  70. const activeFileds = active[type];
  71. this.props.form.validateFields(activeFileds, { force: true },
  72. (err, values) => {
  73. this.props.onSubmit(err, values);
  74. }
  75. );
  76. }
  77. render() {
  78. const { className, children } = this.props;
  79. const { type, tabs } = this.state;
  80. const TabChildren = [];
  81. const otherChildren = [];
  82. React.Children.forEach(children, (item) => {
  83. // eslint-disable-next-line
  84. if (item.type.__ANT_PRO_LOGIN_TAB) {
  85. TabChildren.push(item);
  86. } else {
  87. otherChildren.push(item);
  88. }
  89. });
  90. return (
  91. <div className={classNames(className, styles.login)}>
  92. <Form onSubmit={this.handleSubmit}>
  93. {
  94. tabs.length ? (
  95. <div>
  96. <Tabs
  97. animated={false}
  98. className={styles.tabs}
  99. activeKey={type}
  100. onChange={this.onSwitch}
  101. >
  102. {TabChildren}
  103. </Tabs>
  104. {otherChildren}
  105. </div>
  106. ) : children
  107. }
  108. </Form>
  109. </div>
  110. );
  111. }
  112. }
  113. Login.Tab = LoginTab;
  114. Login.Submit = LoginSubmit;
  115. Object.keys(LoginItem).forEach((item) => {
  116. Login[item] = LoginItem[item];
  117. });
  118. export default Login;