diff --git a/scm-ui/ui-components/src/navigation/SubNavigation.tsx b/scm-ui/ui-components/src/navigation/SubNavigation.tsx index 0f3c8b40a7..27cfa40a9d 100644 --- a/scm-ui/ui-components/src/navigation/SubNavigation.tsx +++ b/scm-ui/ui-components/src/navigation/SubNavigation.tsx @@ -1,5 +1,5 @@ -import React, { FC, ReactElement, useContext } from "react"; -import { Link, Route } from "react-router-dom"; +import React, { FC, ReactElement, useContext, useEffect } from "react"; +import { Link, useRouteMatch } from "react-router-dom"; import classNames from "classnames"; import { MenuContext } from "./MenuContext"; @@ -14,52 +14,42 @@ type Props = { title?: string; }; -const SubNavigation: FC = ({ - to, - icon, - label, - activeOnlyWhenExact, - activeWhenMatch, - children, - collapsed, - title -}) => { - const menuContext = useContext(MenuContext); - - const isActive = (route: any) => { - return route.match || activeWhenMatch && activeWhenMatch(route); - }; - - const renderLink = (route: any) => { - let defaultIcon = "fas fa-cog"; - if (icon) { - defaultIcon = icon; - } - - let childrenList = null; - if (isActive(route)) { - if (menuContext.menuCollapsed) { - menuContext.setMenuCollapsed(false); - } - childrenList = ; - } - - return ( -
  • - - {collapsed ? "" : label} - - {childrenList} -
  • - ); - }; - - // removes last part of url +const SubNavigation: FC = ({ to, activeOnlyWhenExact, icon, collapsed, title, label, children }) => { const parents = to.split("/"); parents.splice(-1, 1); const parent = parents.join("/"); - return ; + const match = useRouteMatch({ + path: parent, + exact: activeOnlyWhenExact + }); + + const menuContext = useContext(MenuContext); + + useEffect(() => { + if (menuContext.menuCollapsed) { + menuContext.setMenuCollapsed(false); + } + }, [match]); + + let defaultIcon = "fas fa-cog"; + if (icon) { + defaultIcon = icon; + } + + let childrenList = null; + if (match) { + childrenList =
      {children}
    ; + } + + return ( +
  • + + {collapsed ? "" : label} + + {childrenList} +
  • + ); }; export default SubNavigation;