Files
SCM-Manager/scm-ui-components/packages/ui-components/src/navigation/NavLink.js

45 lines
943 B
JavaScript
Raw Normal View History

//@flow
import * as React from "react";
import { Route, Link } from "react-router-dom";
// TODO mostly copy of PrimaryNavigationLink
type Props = {
to: string,
label: string,
2018-10-09 11:53:06 +02:00
activeOnlyWhenExact?: boolean,
activeWhenMatch?: (route: any) => boolean
};
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));
}
renderLink = (route: any) => {
const { to, label } = this.props;
return (
<li>
2018-10-09 11:53:06 +02:00
<Link className={this.isActive(route) ? "is-active" : ""} to={to}>
{label}
</Link>
</li>
);
};
render() {
const { to, activeOnlyWhenExact } = this.props;
return (
<Route path={to} exact={activeOnlyWhenExact} children={this.renderLink} />
);
}
}
export default NavLink;