Skip to content

Instantly share code, notes, and snippets.

@manduks
Created July 27, 2017 21:39
Show Gist options
  • Save manduks/333955a4b2fae02cf15006d3726dacfc to your computer and use it in GitHub Desktop.
Save manduks/333955a4b2fae02cf15006d3726dacfc to your computer and use it in GitHub Desktop.
import React from 'react';
import PropTypes from 'prop-types';
import { Link, withRouter } from 'react-router-dom';
class NavAccordion extends React.PureComponent {
constructor(props) {
super();
this.state = {
showChildren: this.showChildren(props),
};
}
showChildren = (props) => {
const { location, children } = props;
const locations = React.Children.map(children, c => c.props.to);
return locations.includes(location.pathname);
};
toogleChildren = (e) => {
e.preventDefault();
const { showChildren } = this.state;
this.setState({
showChildren: !showChildren,
});
};
// eslint-disable-next-line
samePath = (location, component, value1, value2) => {
return location.pathname === component.props.to ? value1 : value2;
}
render() {
const { title, children, location } = this.props;
return (
<li>
<Link to="" role="button" className="nav-label" onClick={this.toogleChildren}>
{title} <span className="caret" />
</Link>
{this.state.showChildren && children)}
</li>
);
}
}
NavAccordion.propTypes = {
title: PropTypes.string.isRequired,
children: PropTypes.array,
location: PropTypes.object,
};
NavAccordion.defaultProps = {
title: '',
children: [],
location: {},
};
export default withRouter(NavAccordion);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment