Guest User

Untitled

a guest
Jan 16th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. import React from 'react';
  2. import { Tabs } from 'antd';
  3. import PropTypes from 'prop-types';
  4. import { browserHistory } from 'react-router';
  5.  
  6. function isLeftClickEvent(event) {
  7. return event.button === 0;
  8. }
  9.  
  10. function isModifiedEvent(event) {
  11. return !!(
  12. event.metaKey ||
  13. event.altKey ||
  14. event.ctrlKey ||
  15. event.shiftKey
  16. );
  17. }
  18.  
  19. function createLocationDescriptor(to, query, hash, state) {
  20. if (query || hash || state) {
  21. return { pathname: to, query, hash, state };
  22. }
  23.  
  24. return to;
  25. }
  26.  
  27. function resolveToLocation(to, router) {
  28. return typeof to === 'function' ? to(router.location) : to;
  29. }
  30.  
  31. const propTypes = {
  32. // onlyActiveOnIndex: PropTypes.bool.isRequired,
  33. // to: PropTypes.oneOfType([
  34. // PropTypes.string,
  35. // PropTypes.object,
  36. // PropTypes.func,
  37. // ]).isRequired,
  38. query: PropTypes.string,
  39. hash: PropTypes.string,
  40. state: PropTypes.object,
  41. action: PropTypes.oneOf([
  42. 'push',
  43. 'replace',
  44. ]).isRequired,
  45. onClick: PropTypes.func,
  46. active: PropTypes.bool,
  47. target: PropTypes.string,
  48. children: PropTypes.node.isRequired,
  49. };
  50.  
  51. const contextTypes = {
  52. router: PropTypes.object,
  53. };
  54.  
  55. const defaultProps = {
  56. onlyActiveOnIndex: false,
  57. action: 'push',
  58. };
  59.  
  60. class RoutedTabs extends React.Component {
  61. static propTypes = propTypes;
  62. static defaultProps = defaultProps;
  63. static contextTypes = contextTypes;
  64.  
  65. onClick = (event) => {
  66. const {
  67. to, query, hash, state, children, onClick, target, action,
  68. } = this.props;
  69.  
  70. const { router } = this.context;
  71.  
  72. const toLocation = resolveToLocation(to, router);
  73.  
  74. if (children.props.onClick) {
  75. children.props.onClick(event);
  76. }
  77.  
  78. if (onClick) {
  79. onClick(event);
  80. }
  81.  
  82. if (
  83. target ||
  84. event.defaultPrevented ||
  85. isModifiedEvent(event) ||
  86. !isLeftClickEvent(event)
  87. ) {
  88. return;
  89. }
  90.  
  91. event.preventDefault();
  92.  
  93. router[action](
  94. createLocationDescriptor(toLocation, query, hash, state)
  95. );
  96. };
  97.  
  98. // returns [activeKey, children]
  99. generateChildren = () => {
  100. let activeKey = null;
  101. const { router } = this.context;
  102.  
  103. const children = React.Children.map(this.props.children, (child) => {
  104. const toLocation = resolveToLocation(child.props.to, router);
  105. if (router.isActive(toLocation, false)){
  106. activeKey = ".$" + child.key.toString();
  107. }
  108.  
  109. return React.cloneElement(child, {to: undefined });
  110. });
  111. return [activeKey, children];
  112. }
  113.  
  114. onChange = (key) => {
  115. const childkey = key.substring(2);
  116. let route;
  117. React.Children.forEach(this.props.children, child => {
  118. if(child.key.toString() === childkey)
  119. route = child.props.to
  120. });
  121. if(route)
  122. browserHistory.push(route)
  123. }
  124.  
  125. render(){
  126. // const { onlyActiveOnIndex, to, children, ...props } = this.props;
  127.  
  128. // props.onClick = this.onClick;
  129.  
  130. // // Ignore if rendered outside Router context; simplifies unit testing.
  131. // if (router) {
  132. // props.href = router.createHref(toLocation);
  133.  
  134. // if (props.active == null) {
  135. // props.active = router.isActive(toLocation, onlyActiveOnIndex);
  136. // }
  137. // }
  138.  
  139. const [activeKey, childs] = this.generateChildren();
  140. return (
  141. <Tabs activeKey={activeKey} onChange={this.onChange}>
  142. {childs}
  143. </Tabs>
  144. );
  145.  
  146. // return React.cloneElement(React.Children.only(children), props);
  147. }
  148. }
  149.  
  150. export default RoutedTabs;
Add Comment
Please, Sign In to add comment