All files / components/nav-sidebar nav-sidebar-menu-item.jsx

64.1% Statements 25/39
69.57% Branches 16/23
50% Functions 4/8
64.1% Lines 25/39
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              3x 3x 3x 3x 3x       3x 1x                       2x                               3x 3x 3x 3x 1x   3x 1x   1x       1x 1x           2x   3x                                                                 3x 3x 3x                 1x                 1x                  
import React from 'react';
import ReactDOM from 'react-dom';
import $ from 'jquery';
import PropTypes from 'prop-types';
 
class NavSidebarMenuItem extends React.Component {
  constructor(props) {
    super(props);
    this.state = { classes: '' };
    this.checkHeaderType = this.checkHeaderType.bind(this);
    this.generateContent = this.generateContent.bind(this);
    this.dropHandler = this.dropHandler.bind(this);
  }
 
  checkHeaderType(multiViewer = '') {
    if (this.props.isHeader) {
      return (
        <div>
          <span>{this.props.heading}</span>
          {multiViewer || this.props.labels.length <= 0 ? '' : (
            <span className="pull-right-container">
              {this.props.labels}
            </span>
          )}
          {multiViewer}
        </div>
      );
    }
    return (
      <a href={this.props.link} >
        <i className={`fa ${this.props.icon} text-center`} />
        &nbsp;
        <span>{this.props.heading}</span>
        {multiViewer || this.props.labels.length <= 0 ? '' : (
          <span className="pull-right-container">
            {this.props.labels}
          </span>
        )}
        {multiViewer}
      </a>
    );
  }
 
  generateContent() {
    const classType = [];
    let header = '';
    let content = '';
    if (this.props.isHeader) {
      classType.push('header');
    }
    if (this.props.children) {
      classType.push('treeview');
      const multiViewer = (
        <span className="pull-right-container">
          <i className="fa fa-angle-left pull-right"></i>
        </span>
      );
      header = this.checkHeaderType(multiViewer);
      content = (
        <ul className="treeview-menu">
          {this.props.children}
        </ul>
      );
    } else {
      header = this.checkHeaderType();
    }
    return {
      header: header,
      content: content,
      classType: classType
    };
  }
 
  dropHandler(e) {
    const thisList = ReactDOM.findDOMNode(this);
    const tabButton = thisList.children[0].children[thisList.children[0].children.length - 1].children[0];
    if ((e.target === thisList.children[0]) || (e.target === thisList.children[0].children[thisList.children[0].children.length - 1].children[0])) {
      //  If what the user clicks is the same as the drop-down list item's header area, or it's first child's last child's child (the drop-down icon)
      if (thisList.className.indexOf('active') === -1) {
        tabButton.classList.remove('fa-angle-left');
        tabButton.classList.add('fa-angle-down');
        $(thisList.children[1]).slideDown(500, 'swing', () => {
            thisList.classList.add('active');
            thisList.children[1].classList.add('menu-open');
          }
        );
      } else {
        tabButton.classList.remove('fa-angle-down');
        tabButton.classList.add('fa-angle-left');
        $(thisList.children[1]).slideUp(500, 'swing', () => {
            thisList.classList.remove('active');
            thisList.children[1].classList.remove('menu-open');
          }
        );
      }
    }
  }
 
  render() {
    const content = this.generateContent();
    const clickHandler = this.props.children ? this.dropHandler : this.props.onClick;
    return (
      <li className={content.classType.join(' ')} onClick={clickHandler}>
        {content.header}
        {content.content}
      </li>
    );
  }
}
 
NavSidebarMenuItem.propTypes = {
  heading: PropTypes.string,
  link: PropTypes.string,
  icon: PropTypes.string,
  isHeader: PropTypes.bool,
  onClick: PropTypes.func,
  labels: PropTypes.array
};
 
NavSidebarMenuItem.defaultProps = {
  heading: '',
  isHeader: false,
  icon: '',
  onClick: () => {},
  labels: []
};
 
export default NavSidebarMenuItem;