Skip to content

Commit af65e13

Browse files
authored
[Enterprise Search] Added a shouldShowActiveForSubroutes option (#83338)
1 parent e1500bf commit af65e13

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

x-pack/plugins/enterprise_search/public/applications/shared/layout/side_nav.test.tsx

+26
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,32 @@ describe('SideNavLink', () => {
105105
expect(wrapper.find('.enterpriseSearchNavLinks__subNav')).toHaveLength(1);
106106
expect(wrapper.find('[data-test-subj="subNav"]')).toHaveLength(1);
107107
});
108+
109+
describe('shouldShowActiveForSubroutes', () => {
110+
it("won't set an active class when route is a subroute of 'to'", () => {
111+
(useLocation as jest.Mock).mockImplementationOnce(() => ({ pathname: '/documents/1234' }));
112+
113+
const wrapper = shallow(
114+
<SideNavLink to="/documents" isRoot>
115+
Link
116+
</SideNavLink>
117+
);
118+
119+
expect(wrapper.find('.enterpriseSearchNavLinks__item--isActive')).toHaveLength(0);
120+
});
121+
122+
it('sets an active class if the current path is a subRoute of "to", and shouldShowActiveForSubroutes is true', () => {
123+
(useLocation as jest.Mock).mockImplementationOnce(() => ({ pathname: '/documents/1234' }));
124+
125+
const wrapper = shallow(
126+
<SideNavLink to="/documents" isRoot shouldShowActiveForSubroutes>
127+
Link
128+
</SideNavLink>
129+
);
130+
131+
expect(wrapper.find('.enterpriseSearchNavLinks__item--isActive')).toHaveLength(1);
132+
});
133+
});
108134
});
109135

110136
describe('SideNavItem', () => {

x-pack/plugins/enterprise_search/public/applications/shared/layout/side_nav.tsx

+7-2
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,17 @@ export const SideNav: React.FC<SideNavProps> = ({ product, children }) => {
6363

6464
interface SideNavLinkProps {
6565
to: string;
66+
shouldShowActiveForSubroutes?: boolean;
6667
isExternal?: boolean;
6768
className?: string;
6869
isRoot?: boolean;
6970
subNav?: React.ReactNode;
7071
}
7172

7273
export const SideNavLink: React.FC<SideNavLinkProps> = ({
73-
isExternal,
7474
to,
75+
shouldShowActiveForSubroutes = false,
76+
isExternal,
7577
children,
7678
className,
7779
isRoot,
@@ -82,7 +84,10 @@ export const SideNavLink: React.FC<SideNavLinkProps> = ({
8284

8385
const { pathname } = useLocation();
8486
const currentPath = stripTrailingSlash(pathname);
85-
const isActive = currentPath === to || (isRoot && currentPath === '');
87+
const isActive =
88+
currentPath === to ||
89+
(shouldShowActiveForSubroutes && currentPath.startsWith(to)) ||
90+
(isRoot && currentPath === '');
8691

8792
const classes = classNames('enterpriseSearchNavLinks__item', className, {
8893
'enterpriseSearchNavLinks__item--isActive': !isExternal && isActive, // eslint-disable-line @typescript-eslint/naming-convention

0 commit comments

Comments
 (0)