All files / components/tabs tabs.jsx

80.95% Statements 17/21
76.92% Branches 20/26
66.67% Functions 4/6
85% Lines 17/20
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          1x 1x 1x               1x 2x 2x                             1x 2x 2x           1x 1x 1x 1x 1x 1x                               1x                                                                   1x                  
import React from 'react';
import PropTypes from 'prop-types';
 
class Tabs extends React.Component {
  constructor(props) {
    super(props);
    this.state = { activeTab: this.props.defaultTab };
    this.changeTab = this.changeTab.bind(this);
  }
 
  changeTab(id) {
    this.setState({ activeTab: id });
  }
 
  render() {
    const tabs = this.props.tabs.map((tab, t) => {
      const activeness = `${((typeof this.state.activeTab === 'number' && this.state.activeTab === t) || (typeof this.state.activeTab === 'string' && this.state.activeTab === tab.id)) ? ' active' : ' disabled'}`;
      return (
        <li className={activeness} key={`tab-${t}`}>
          <a
            href={`#${tab.id}`}
            data-toggle="tab"
            onClick={() => {
              this.changeTab(tab.id);
              if (tab.onClick) { tab.onClick(); }
            }}
          >
            {tab.subject}
          </a>
        </li>
      );
    });
    const content = this.props.tabs.map((tab, t) => {
      const activeness = ((typeof this.state.activeTab === 'number' && this.state.activeTab === t) || (typeof this.state.activeTab === 'string' && this.state.activeTab === tab.id)) ? ' active' : '';
      return (
        <div className={`tab-pane${activeness}`} id={tab.id} key={`tab-pane-${t}`}>
          {tab.content}
        </div>
      );
    });
    const xs = this.props.widthXS || this.props.width;
    const sm = this.props.widthSM || xs;
    const md = this.props.widthMD || sm;
    const lg = this.props.widthLG || md;
    const xl = this.props.widthXL || lg;
    return (
      <div className={`col-xs-${xs} col-sm-${sm} col-md-${md} col-lg-${lg} col-xl-${xl}`}>
        <div className="nav-tabs-custom">
          <ul className={`nav nav-tabs${this.props.pullRight ? ' pull-right' : ''} ${this.props.tabTheme}`}>
            {tabs}
            {this.props.children}
          </ul>
          <div className="tab-content">
            {content}
          </div>
        </div>
      </div>
    );
  }
}
 
Tabs.propTypes = {
  width: PropTypes.oneOfType([
    PropTypes.number,
    PropTypes.string
  ]),
  widthXS: PropTypes.oneOfType([
    PropTypes.number,
    PropTypes.string
  ]),
  widthSM: PropTypes.oneOfType([
    PropTypes.number,
    PropTypes.string
  ]),
  widthMD: PropTypes.oneOfType([
    PropTypes.number,
    PropTypes.string
  ]),
  widthLG: PropTypes.oneOfType([
    PropTypes.number,
    PropTypes.string
  ]),
  widthXL: PropTypes.oneOfType([
    PropTypes.number,
    PropTypes.string
  ]),
  pullRight: PropTypes.bool,
  tabs: PropTypes.array,
  defaultTab: PropTypes.oneOfType([
    PropTypes.number,
    PropTypes.string
  ]),
  tabTheme: PropTypes.string
};
 
Tabs.defaultProps = {
  width: 12,
  pullRight: false,
  tabs: [],
  defaultTab: 0,
  tabTheme: 'tab-default'
};
 
export default Tabs;