diff --git a/x-pack/plugins/enterprise_search/public/applications/shared/layout/side_nav.test.tsx b/x-pack/plugins/enterprise_search/public/applications/shared/layout/side_nav.test.tsx index e3e9872f892a47..9eaa2ba4c4d6f2 100644 --- a/x-pack/plugins/enterprise_search/public/applications/shared/layout/side_nav.test.tsx +++ b/x-pack/plugins/enterprise_search/public/applications/shared/layout/side_nav.test.tsx @@ -105,6 +105,32 @@ describe('SideNavLink', () => { expect(wrapper.find('.enterpriseSearchNavLinks__subNav')).toHaveLength(1); expect(wrapper.find('[data-test-subj="subNav"]')).toHaveLength(1); }); + + describe('shouldShowActiveForSubroutes', () => { + it("won't set an active class when route is a subroute of 'to'", () => { + (useLocation as jest.Mock).mockImplementationOnce(() => ({ pathname: '/documents/1234' })); + + const wrapper = shallow( + + Link + + ); + + expect(wrapper.find('.enterpriseSearchNavLinks__item--isActive')).toHaveLength(0); + }); + + it('sets an active class if the current path is a subRoute of "to", and shouldShowActiveForSubroutes is true', () => { + (useLocation as jest.Mock).mockImplementationOnce(() => ({ pathname: '/documents/1234' })); + + const wrapper = shallow( + + Link + + ); + + expect(wrapper.find('.enterpriseSearchNavLinks__item--isActive')).toHaveLength(1); + }); + }); }); describe('SideNavItem', () => { diff --git a/x-pack/plugins/enterprise_search/public/applications/shared/layout/side_nav.tsx b/x-pack/plugins/enterprise_search/public/applications/shared/layout/side_nav.tsx index 6c4e1d084c16dc..c75a48d5af41db 100644 --- a/x-pack/plugins/enterprise_search/public/applications/shared/layout/side_nav.tsx +++ b/x-pack/plugins/enterprise_search/public/applications/shared/layout/side_nav.tsx @@ -63,6 +63,7 @@ export const SideNav: React.FC = ({ product, children }) => { interface SideNavLinkProps { to: string; + shouldShowActiveForSubroutes?: boolean; isExternal?: boolean; className?: string; isRoot?: boolean; @@ -70,8 +71,9 @@ interface SideNavLinkProps { } export const SideNavLink: React.FC = ({ - isExternal, to, + shouldShowActiveForSubroutes = false, + isExternal, children, className, isRoot, @@ -82,7 +84,10 @@ export const SideNavLink: React.FC = ({ const { pathname } = useLocation(); const currentPath = stripTrailingSlash(pathname); - const isActive = currentPath === to || (isRoot && currentPath === ''); + const isActive = + currentPath === to || + (shouldShowActiveForSubroutes && currentPath.startsWith(to)) || + (isRoot && currentPath === ''); const classes = classNames('enterpriseSearchNavLinks__item', className, { 'enterpriseSearchNavLinks__item--isActive': !isExternal && isActive, // eslint-disable-line @typescript-eslint/naming-convention