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

54 lines
1.1 KiB
JavaScript
Raw Normal View History

//@flow
import * as React from "react";
2018-10-19 08:44:03 +02:00
import {Link, Route} from "react-router-dom";
// TODO mostly copy of PrimaryNavigationLink
type Props = {
to: string,
2018-12-21 13:41:34 +01:00
icon?: 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) => {
2018-12-21 13:41:34 +01:00
const { to, icon, label } = this.props;
let showIcon = null;
if (icon) {
2019-01-23 17:14:29 +01:00
showIcon = (<><i className={icon} />{" "}</>);
2018-12-21 13:41:34 +01: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}
{label}
</Link>
</li>
);
};
render() {
const { to, activeOnlyWhenExact } = this.props;
2018-12-21 13:41:34 +01:00
return (
<Route path={to} exact={activeOnlyWhenExact} children={this.renderLink} />
);
}
}
export default NavLink;