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`} />
<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;
|