All files / src Tabs.js

92.68% Statements 38/41
75% Branches 12/16
100% Functions 11/11
92.68% Lines 38/41

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167                          5x 5x       5x                                   2x   2x         2x               2x 1x     1x                           7x   7x                                 4x   4x 4x 4x 4x   4x           4x 4x     4x 4x   4x   4x                               3x 3x               1x       4x 4x 1x 1x 1x 1x         4x       4x 4x   4x               1x 1x  
import React, {Children, cloneElement, Component} from 'react'
import PropTypes from 'prop-types'
import classNames from 'classnames'
 
class TabsTitle extends Component {
  static propTypes = {
    onClick: PropTypes.func,
    href: PropTypes.string,
    title: PropTypes.string,
    active: PropTypes.bool,
  }
 
  render() {
    const {onClick, href, title, active} = this.props
    const className = classNames('rev-TabsTitle', {
      'rev-TabsTitle--selected': active,
    })
 
    return (
      <li className={className}>
        <a className="rev-TabsTitle-link" href={href || '#'} onClick={onClick}>
          {title}
        </a>
      </li>
    )
  }
}
 
class TabsPanel extends Component {
  static propTypes = {
    active: PropTypes.bool,
    renderHiddenTabs: PropTypes.bool,
    children: PropTypes.node,
  }
  /* eslint complexity: [2, 4] */
  render() {
    const {children, active, renderHiddenTabs} = this.props
 
    const className = classNames(
      'rev-TabsItem-panel--selected',
      'rev-TabsItem-panel'
    )
 
    Iif (renderHiddenTabs) {
      if (!active) {
        return (
          <div style={{display: 'none'}} className={className}>
            {children}
          </div>
        )
      }
    } else if (!active) {
      return null
    }
 
    return (
      <div style={{display: 'block'}} className={className}>
        {children}
      </div>
    )
  }
}
 
class TabsItem extends Component {
  static propTypes = {
    renderTitle: PropTypes.bool,
  }
 
  render() {
    const {renderTitle, ...props} = this.props
 
    return renderTitle ? <TabsTitle {...props} /> : <TabsPanel {...props} />
  }
}
 
export default class Tabs extends Component {
  static propTypes = {
    active: PropTypes.oneOfType([
      PropTypes.number,
      PropTypes.array,
      PropTypes.object,
    ]),
    renderHiddenTabs: PropTypes.bool,
    className: PropTypes.string,
    children: PropTypes.node,
  }
 
  render() {
    const {children, className, active, renderHiddenTabs} = this.props
 
    let activeKey = active
    const rewriteItem = (child) => {
      activeKey = activeKey || child.props.contentKey // default to first child
      const {contentKey} = child.props
 
      return cloneElement(child, {
        active: activeKey === contentKey,
        renderHiddenTabs,
      })
    }
 
    const rewriteItemToTitle = (item) => {
      return cloneElement(item, {renderTitle: true})
    }
 
    const items = Children.map(children, rewriteItem)
    const titles = items.map(rewriteItemToTitle)
 
    const divClassName = classNames(className, 'rev-Tabs')
 
    return (
      <div className={divClassName}>
        <ul className="rev-Tabs-titles">{titles}</ul>
        <div className="rev-Tabs-content">{items}</div>
      </div>
    )
  }
}
 
class StatefulTabs extends Component {
  static propTypes = {
    defaultActive: PropTypes.number,
    children: PropTypes.node,
  }
 
  constructor(props) {
    super(props)
    this.state = {
      active:
        props.defaultActive ||
        Children.toArray(props.children)[0].props.contentKey,
    }
  }
 
  setActive = (contentKey) => {
    this.setState({active: contentKey})
  }
 
  rewriteChild = (child) => {
    const {contentKey, onClick} = child.props
    const newOnClick = (e, ...args) => {
      e.preventDefault()
      this.setActive(contentKey)
      Eif (onClick) {
        return onClick(e, ...args)
      }
      return null
    }
 
    return cloneElement(child, {onClick: newOnClick})
  }
 
  render() {
    const {active} = this.state
    const {children, ...props} = this.props
 
    return (
      <Tabs {...props} active={active}>
        {Children.map(children, this.rewriteChild)}
      </Tabs>
    )
  }
}
 
Tabs.Stateful = StatefulTabs
Tabs.Item = TabsItem