1414 * limitations under the License.
1515 */
1616
17+ import { useMemo } from 'react'
1718import { Link , NavLink , useRouteMatch } from 'react-router-dom'
1819import { motion } from 'framer-motion'
1920
@@ -26,19 +27,8 @@ import { getClassNameBySizeMap, tabGroupClassMap } from './TabGroup.utils'
2627
2728import './TabGroup.scss'
2829
29- const MotionLayoutUnderline = ( {
30- layoutId,
31- alignActiveBorderWithContainer,
32- } : {
33- layoutId : string
34- alignActiveBorderWithContainer : boolean
35- } ) => (
36- < motion . div
37- layout = "position"
38- layoutId = { layoutId }
39- className = "underline bcb-5"
40- style = { { ...( alignActiveBorderWithContainer ? { bottom : - 1 } : { } ) } }
41- />
30+ const MotionLayoutUnderline = ( { layoutId } : { layoutId : string } ) => (
31+ < motion . div layout = "position" layoutId = { layoutId } className = "underline bcb-5" />
4232)
4333
4434const Tab = ( {
@@ -49,7 +39,6 @@ const Tab = ({
4939 icon,
5040 size,
5141 badge = null ,
52- alignActiveBorderWithContainer,
5342 hideTopPadding,
5443 showIndicator,
5544 showError,
@@ -59,16 +48,18 @@ const Tab = ({
5948 shouldWrapTooltip,
6049 tooltipProps,
6150 uniqueGroupId,
62- } : TabProps &
63- Pick < TabGroupProps , 'size' | 'alignActiveBorderWithContainer' | 'hideTopPadding' > &
64- AdditionalTabProps ) => {
51+ } : TabProps & Pick < TabGroupProps , 'size' | 'hideTopPadding' > & AdditionalTabProps ) => {
6552 const { path } = useRouteMatch ( )
6653 const pathToMatch = tabType === 'navLink' || tabType === 'link' ? getPathnameToMatch ( props . to , path ) : ''
54+
55+ // using match to define if tab is active as useRouteMatch return an object if path is matched otherwise return null/undefined
6756 const match = useRouteMatch ( pathToMatch )
6857
58+ const isTabActive = tabType === 'button' ? active : ! ! match
59+
6960 const { tabClassName, iconClassName, badgeClassName } = getClassNameBySizeMap ( {
7061 hideTopPadding,
71- alignActiveBorderWithContainer ,
62+ isTabActive ,
7263 } ) [ size ]
7364
7465 const onClickHandler = (
@@ -142,19 +133,12 @@ const Tab = ({
142133 }
143134 }
144135
145- const isTabActive = tabType === 'button' ? active : ! ! match
146-
147136 const renderTabContainer = ( ) => (
148137 < li
149138 className = { `tab-group__tab lh-20 ${ active ? 'cb-5 fw-6' : 'cn-9 fw-4' } ${ tabType === 'block' ? 'tab-group__tab--block' : '' } ${ disabled ? 'dc__disabled' : 'cursor' } ` }
150139 >
151140 { getTabComponent ( ) }
152- { isTabActive && (
153- < MotionLayoutUnderline
154- layoutId = { uniqueGroupId }
155- alignActiveBorderWithContainer = { alignActiveBorderWithContainer }
156- />
157- ) }
141+ { isTabActive && < MotionLayoutUnderline layoutId = { uniqueGroupId } /> }
158142 </ li >
159143 )
160144
@@ -169,11 +153,11 @@ export const TabGroup = ({
169153 tabs = [ ] ,
170154 size = ComponentSizeType . large ,
171155 rightComponent,
172- alignActiveBorderWithContainer,
173156 hideTopPadding,
174157} : TabGroupProps ) => {
175158 // Unique layoutId for motion.div to handle multiple tab groups on same page
176- const uniqueGroupId = tabs . map ( ( tab ) => tab . label ) . join ( '-' )
159+ // Using tab labels so that id remains same on re mount as well
160+ const uniqueGroupId = useMemo ( ( ) => tabs . map ( ( tab ) => tab . label ) . join ( '-' ) , [ ] )
177161
178162 return (
179163 < div className = "flexbox dc__align-items-center dc__content-space" >
@@ -183,7 +167,6 @@ export const TabGroup = ({
183167 key = { id }
184168 id = { id }
185169 size = { size }
186- alignActiveBorderWithContainer = { alignActiveBorderWithContainer }
187170 hideTopPadding = { hideTopPadding }
188171 uniqueGroupId = { uniqueGroupId }
189172 { ...resProps }
0 commit comments