Skip to content

Commit ddff3ac

Browse files
committed
refactor: Collapsible List, DeploymentConfigDiffNavigation: add support in NavLink to check for search params for active state
1 parent 5697f74 commit ddff3ac

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

src/Shared/Components/CollapsibleList/CollapsibleList.component.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { ReactComponent as ICExpand } from '@Icons/ic-expand.svg'
77

88
import { Collapse } from '../Collapse'
99
import { CollapsibleListProps } from './CollapsibleList.types'
10+
import { checkNavLinkActiveState } from './utils'
1011
import './CollapsibleList.scss'
1112

1213
const renderWithTippy = (tippyProps: TippyProps) => (children: React.ReactElement) => (
@@ -66,6 +67,7 @@ export const CollapsibleList = ({ config, onCollapseBtnClick }: CollapsibleListP
6667
key={title}
6768
to={href}
6869
className="collapsible__item flexbox dc__align-items-center dc__gap-8 dc__no-decor br-4 py-6 px-8 cursor"
70+
isActive={checkNavLinkActiveState(href)}
6971
onClick={(e) => {
7072
// Prevent navigation to the same page
7173
if (href === pathname) {
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { NavLinkProps } from 'react-router-dom'
2+
3+
/**
4+
* Determines if a navigation link is active based on the current location.
5+
*
6+
* This function checks if the provided `href` matches the current location's pathname
7+
* and/or search query, and returns a boolean indicating whether the link should be considered active.
8+
*
9+
* @param href - The target URL or path to compare against the current location.
10+
*
11+
* @returns A function that takes the current location and returns a boolean:
12+
* - `true` if the `href` matches the current location's pathname and/or search query.
13+
* - `false` otherwise.
14+
*/
15+
export const checkNavLinkActiveState =
16+
(href: string): NavLinkProps['isActive'] =>
17+
(_, location) => {
18+
const [pathString, queryString] = href.split('?')
19+
20+
if (pathString && queryString) {
21+
return `${location.pathname}${location.search}` === href
22+
}
23+
if (pathString) {
24+
return location.pathname === pathString
25+
}
26+
if (queryString) {
27+
return location.search === `?${queryString}`
28+
}
29+
return false
30+
}

src/Shared/Components/DeploymentConfigDiff/DeploymentConfigDiffNavigation.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { ReactComponent as ICInfoOutlined } from '@Icons/ic-info-outlined.svg'
77
import { ReactComponent as ICDiffFileUpdated } from '@Icons/ic-diff-file-updated.svg'
88
import { StyledRadioGroup } from '@Common/index'
99

10+
import { checkNavLinkActiveState } from '../CollapsibleList/utils'
1011
import { CollapsibleList } from '../CollapsibleList'
1112
import { DeploymentConfigDiffNavigationProps } from './DeploymentConfigDiff.types'
1213

@@ -107,6 +108,7 @@ export const DeploymentConfigDiffNavigation = ({
107108
<NavLink
108109
key={title}
109110
data-testid="env-deployment-template"
111+
isActive={checkNavLinkActiveState(href)}
110112
className="dc__nav-item cursor dc__gap-8 fs-13 lh-32 cn-7 w-100 br-4 px-8 flexbox dc__align-items-center dc__content-space dc__no-decor"
111113
to={href}
112114
onClick={onClick}

0 commit comments

Comments
 (0)