2019-01-18 11:41:41 +01:00
|
|
|
//@flow
|
|
|
|
|
import * as React from "react";
|
2019-01-29 08:58:45 +01:00
|
|
|
import { Link, Route } from "react-router-dom";
|
2019-01-18 11:41:41 +01:00
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
|
to: string,
|
2019-01-23 17:12:10 +01:00
|
|
|
icon?: string,
|
2019-01-18 11:41:41 +01:00
|
|
|
label: string,
|
|
|
|
|
activeOnlyWhenExact?: boolean,
|
|
|
|
|
activeWhenMatch?: (route: any) => boolean,
|
|
|
|
|
children?: React.Node
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class SubNavigation extends React.Component<Props> {
|
|
|
|
|
static defaultProps = {
|
|
|
|
|
activeOnlyWhenExact: false
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
isActive(route: any) {
|
|
|
|
|
const { activeWhenMatch } = this.props;
|
|
|
|
|
return route.match || (activeWhenMatch && activeWhenMatch(route));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
renderLink = (route: any) => {
|
2019-01-23 17:12:10 +01:00
|
|
|
const { to, icon, label } = this.props;
|
|
|
|
|
|
|
|
|
|
let defaultIcon = "fas fa-cog";
|
|
|
|
|
if (icon) {
|
|
|
|
|
defaultIcon = icon;
|
|
|
|
|
}
|
2019-01-18 11:41:41 +01:00
|
|
|
|
|
|
|
|
let children = null;
|
2019-01-29 08:58:45 +01:00
|
|
|
if (this.isActive(route)) {
|
|
|
|
|
children = <ul className="sub-menu">{this.props.children}</ul>;
|
2019-01-18 11:41:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<li>
|
|
|
|
|
<Link className={this.isActive(route) ? "is-active" : ""} to={to}>
|
2019-01-29 08:58:45 +01:00
|
|
|
<i className={defaultIcon} /> {label}
|
2019-01-18 11:41:41 +01:00
|
|
|
</Link>
|
|
|
|
|
{children}
|
|
|
|
|
</li>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
render() {
|
|
|
|
|
const { to, activeOnlyWhenExact } = this.props;
|
2019-01-18 14:32:35 +01:00
|
|
|
|
|
|
|
|
// removes last part of url
|
|
|
|
|
let parents = to.split("/");
|
2019-01-29 08:58:45 +01:00
|
|
|
parents.splice(-1, 1);
|
2019-01-18 14:32:35 +01:00
|
|
|
let parent = parents.join("/");
|
|
|
|
|
|
2019-01-18 11:41:41 +01:00
|
|
|
return (
|
2019-01-29 08:58:45 +01:00
|
|
|
<Route
|
|
|
|
|
path={parent}
|
|
|
|
|
exact={activeOnlyWhenExact}
|
|
|
|
|
children={this.renderLink}
|
|
|
|
|
/>
|
2019-01-18 11:41:41 +01:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default SubNavigation;
|