Skip to content

Commit

Permalink
Support startsWith directly in isActive
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewballantyne committed Jun 14, 2022
1 parent 97aac63 commit ebe1717
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 9 deletions.
5 changes: 3 additions & 2 deletions frontend/public/components/nav/NavLink.tsx
Expand Up @@ -12,8 +12,9 @@ export class NavLink<P extends NavLinkProps> extends React.PureComponent<P> {
};

// eslint-disable-next-line @typescript-eslint/no-unused-vars, no-unused-vars
static isActive(...args): boolean {
throw new Error('not implemented');
static isActive(props: NavLinkProps, location: string, activeNamespace?: string): boolean {
const { startsWith } = props;
return startsWith?.some((path) => location.startsWith(path));
}

get to(): string {
Expand Down
6 changes: 5 additions & 1 deletion frontend/public/components/nav/NavLinkHref.tsx
Expand Up @@ -7,7 +7,11 @@ import { formatNamespacedRouteForHref, formatNamespacedRouteForResource } from '
export class NavLinkHref extends NavLink<NavLinkHrefProps> {
static isActive(props, resourcePath) {
const noNSHref = stripNS(props.href);
return resourcePath === noNSHref || _.startsWith(resourcePath, `${noNSHref}/`);
return (
NavLink.isActive(props, resourcePath) ||
resourcePath === noNSHref ||
_.startsWith(resourcePath, `${noNSHref}/`)
);
}

get to() {
Expand Down
1 change: 1 addition & 0 deletions frontend/public/components/nav/NavLinkResourceCluster.tsx
Expand Up @@ -13,6 +13,7 @@ export class NavLinkResourceCluster extends NavLink<NavLinkResourceClusterProps>
);
}
return (
NavLink.isActive(props, resourcePath) ||
resourcePath === props.resource ||
_.startsWith(resourcePath, `${props.resource}/`) ||
matchesModel(resourcePath, props.model)
Expand Down
2 changes: 1 addition & 1 deletion frontend/public/components/nav/NavLinkResourceNS.tsx
Expand Up @@ -14,7 +14,7 @@ export class NavLinkResourceNS extends NavLink<NavLinkResourceNSProps> {
return matchesExtensionModel(resourcePath, props.model);
}
const href = stripNS(formatNamespacedRouteForResource(props.resource, activeNamespace));
return matchesPath(resourcePath, href);
return NavLink.isActive(props, resourcePath) || matchesPath(resourcePath, href);
}

get to() {
Expand Down
6 changes: 1 addition & 5 deletions frontend/public/components/nav/NavLinkRoot.tsx
Expand Up @@ -26,15 +26,11 @@ const navLinkRootMapStateToProps = (
state: RootState,
{ component: Component, ...props }: NavLinkRootProps,
): NavLinkRootStateProps => {
const { startsWith } = props;

const activeNamespace = getActiveNamespace(state);
const location = stripNS(state.UI.get('location'));
return {
activeNamespace,
isActive:
startsWith?.some((path) => location.startsWith(path)) ||
Component.isActive(props, location, activeNamespace),
isActive: Component.isActive(props, location, activeNamespace),
};
};

Expand Down

0 comments on commit ebe1717

Please sign in to comment.