|
14 | 14 | * limitations under the License.
|
15 | 15 | */
|
16 | 16 |
|
17 |
| -import { useMemo } from 'react' |
18 |
| -import { Link, NavLink, useRouteMatch } from 'react-router-dom' |
| 17 | +import { useEffect, useMemo, useRef, useState } from 'react' |
| 18 | +import { Link, NavLink, useLocation } from 'react-router-dom' |
19 | 19 | import { motion } from 'framer-motion'
|
20 | 20 |
|
21 | 21 | import { Tooltip } from '@Common/Tooltip'
|
22 | 22 | import { ComponentSizeType } from '@Shared/constants'
|
23 | 23 |
|
24 |
| -import { getPathnameToMatch, getTabBadge, getTabDescription, getTabIcon, getTabIndicator } from './TabGroup.helpers' |
| 24 | +import { getTabBadge, getTabDescription, getTabIcon, getTabIndicator } from './TabGroup.helpers' |
25 | 25 | import { AdditionalTabProps, TabGroupProps, TabProps } from './TabGroup.types'
|
26 | 26 | import { getClassNameBySizeMap, tabGroupClassMap } from './TabGroup.utils'
|
27 | 27 |
|
@@ -57,13 +57,17 @@ const Tab = ({
|
57 | 57 | tooltipProps,
|
58 | 58 | uniqueGroupId,
|
59 | 59 | }: TabProps & Pick<TabGroupProps, 'size' | 'hideTopPadding'> & AdditionalTabProps) => {
|
60 |
| - const { path } = useRouteMatch() |
61 |
| - const pathToMatch = tabType === 'navLink' || tabType === 'link' ? getPathnameToMatch(props.to, path) : '' |
62 |
| - |
63 |
| - // using match to define if tab is active as useRouteMatch return an object if path is matched otherwise return null/undefined |
64 |
| - const match = useRouteMatch(pathToMatch) |
65 |
| - |
66 |
| - const isTabActive = tabType === 'button' ? active : !!match |
| 60 | + const { pathname, search } = useLocation() |
| 61 | + const ref = useRef<HTMLAnchorElement>(null) |
| 62 | + const [isTabActive, setIsTabActive] = useState(tabType === 'button' && active) |
| 63 | + |
| 64 | + useEffect(() => { |
| 65 | + if (tabType === 'navLink') { |
| 66 | + setIsTabActive(ref.current?.classList.contains('active') || false) |
| 67 | + return |
| 68 | + } |
| 69 | + setIsTabActive(active) |
| 70 | + }, [active, tabType, pathname, search]) |
67 | 71 |
|
68 | 72 | const { tabClassName, iconClassName, badgeClassName } = getClassNameBySizeMap({
|
69 | 73 | hideTopPadding,
|
@@ -110,6 +114,7 @@ const Tab = ({
|
110 | 114 | case 'navLink':
|
111 | 115 | return (
|
112 | 116 | <NavLink
|
| 117 | + ref={ref} |
113 | 118 | className={`${tabClassName} dc__no-decor flexbox-col tab-group__tab__nav-link ${disabled ? 'cursor-not-allowed' : ''}`}
|
114 | 119 | aria-disabled={disabled}
|
115 | 120 | {...props}
|
|
0 commit comments