2018-09-03 16:17:36 +02:00
|
|
|
//@flow
|
|
|
|
|
import * as React from "react";
|
2018-10-19 08:44:03 +02:00
|
|
|
import {Link, Route} from "react-router-dom";
|
2018-09-03 16:17:36 +02:00
|
|
|
|
|
|
|
|
// TODO mostly copy of PrimaryNavigationLink
|
|
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
|
to: string,
|
2018-12-21 13:41:34 +01:00
|
|
|
icon?: string,
|
2018-09-03 16:17:36 +02:00
|
|
|
label: string,
|
2018-10-09 11:53:06 +02:00
|
|
|
activeOnlyWhenExact?: boolean,
|
|
|
|
|
activeWhenMatch?: (route: any) => boolean
|
2018-09-03 16:17:36 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class NavLink extends React.Component<Props> {
|
|
|
|
|
static defaultProps = {
|
|
|
|
|
activeOnlyWhenExact: true
|
|
|
|
|
};
|
|
|
|
|
|
2018-10-09 11:53:06 +02:00
|
|
|
|
|
|
|
|
isActive(route: any) {
|
|
|
|
|
const { activeWhenMatch } = this.props;
|
|
|
|
|
return route.match || (activeWhenMatch && activeWhenMatch(route));
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-03 16:17:36 +02:00
|
|
|
renderLink = (route: any) => {
|
2018-12-21 13:41:34 +01:00
|
|
|
const { to, icon, label } = this.props;
|
|
|
|
|
|
|
|
|
|
let showIcon = null;
|
|
|
|
|
if (icon) {
|
|
|
|
|
showIcon = (<><i className={icon}></i>{" "}</>);
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-03 16:17:36 +02:00
|
|
|
return (
|
|
|
|
|
<li>
|
2018-10-09 11:53:06 +02:00
|
|
|
<Link className={this.isActive(route) ? "is-active" : ""} to={to}>
|
2018-12-21 13:41:34 +01:00
|
|
|
{showIcon}
|
2018-09-03 16:17:36 +02:00
|
|
|
{label}
|
|
|
|
|
</Link>
|
|
|
|
|
</li>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
render() {
|
|
|
|
|
const { to, activeOnlyWhenExact } = this.props;
|
2018-12-21 13:41:34 +01:00
|
|
|
|
2018-09-03 16:17:36 +02:00
|
|
|
return (
|
|
|
|
|
<Route path={to} exact={activeOnlyWhenExact} children={this.renderLink} />
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default NavLink;
|