Skip to content

Commit aac6961

Browse files
committed
feat: remove prop for align active border with container, use default true
1 parent 134db80 commit aac6961

5 files changed

Lines changed: 24 additions & 48 deletions

File tree

src/Shared/Components/TabGroup/TabGroup.component.tsx

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17+
import { useMemo } from 'react'
1718
import { Link, NavLink, useRouteMatch } from 'react-router-dom'
1819
import { motion } from 'framer-motion'
1920

@@ -26,19 +27,8 @@ import { getClassNameBySizeMap, tabGroupClassMap } from './TabGroup.utils'
2627

2728
import './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

4434
const 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}

src/Shared/Components/TabGroup/TabGroup.helpers.tsx

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,9 @@ export const getTabDescription = (description: TabProps['description']) =>
7171
const replaceTrailingSlash = (pathname: string) => pathname.replace(/\/+$/, '')
7272

7373
export const getPathnameToMatch = (to: NavLinkProps['to'] | LinkProps['to'], currentPathname: string): string => {
74-
// Handling both absolute and relative paths
75-
if (typeof to === 'string') {
76-
return to.startsWith('/') ? to : `${replaceTrailingSlash(currentPathname)}/${to}`
77-
}
78-
if (typeof to === 'function') {
79-
return ''
80-
}
81-
if (to && typeof to === 'object' && 'pathname' in to) {
82-
const pathname = to.pathname || ''
74+
if (typeof to === 'string' || (to && typeof to === 'object' && 'pathname' in to)) {
75+
const pathname = typeof to === 'string' ? to : to.pathname || ''
76+
// handling absolute and relative paths
8377
return pathname.startsWith('/') ? pathname : `${replaceTrailingSlash(currentPathname)}/${pathname}`
8478
}
8579
return ''

src/Shared/Components/TabGroup/TabGroup.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@
3939
border-top-right-radius: 2px;
4040
}
4141

42+
&--active {
43+
@include svg-styles(var(--B500));
44+
}
45+
4246
&:hover:not(.tab-group__tab--block):not(.dc__disabled) {
4347
color: var(--B500);
4448
@include svg-styles(var(--B500));

src/Shared/Components/TabGroup/TabGroup.types.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,6 @@ export interface TabGroupProps {
152152
* Optional component to be rendered on the right side of the tab list.
153153
*/
154154
rightComponent?: React.ReactElement
155-
/**
156-
* Set to `true` to align the active tab's border with the bottom border of the parent container.
157-
* @default false
158-
*/
159-
alignActiveBorderWithContainer?: boolean
160155
/**
161156
* Determines if the top padding of the tab group should be hidden.
162157
* @default false

src/Shared/Components/TabGroup/TabGroup.utils.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ import { TabGroupProps } from './TabGroup.types'
2121

2222
export const getClassNameBySizeMap = ({
2323
hideTopPadding,
24-
alignActiveBorderWithContainer,
25-
}: Pick<TabGroupProps, 'hideTopPadding' | 'alignActiveBorderWithContainer'>): Record<
24+
isTabActive,
25+
}: Pick<TabGroupProps, 'hideTopPadding'> & { isTabActive: boolean }): Record<
2626
TabGroupProps['size'],
2727
{
2828
tabClassName: string
@@ -31,17 +31,17 @@ export const getClassNameBySizeMap = ({
3131
}
3232
> => ({
3333
[ComponentSizeType.medium]: {
34-
tabClassName: `fs-12 ${!hideTopPadding ? 'pt-6' : ''} ${alignActiveBorderWithContainer ? 'pb-5' : 'pb-6'}`,
34+
tabClassName: `fs-12 ${!hideTopPadding ? 'pt-6' : ''} ${isTabActive ? 'pb-3' : 'pb-5'}`,
3535
iconClassName: 'icon-dim-14',
3636
badgeClassName: 'fs-11 lh-18 tab-group__tab__badge--medium',
3737
},
3838
[ComponentSizeType.large]: {
39-
tabClassName: `fs-13 ${!hideTopPadding ? 'pt-8' : ''} ${alignActiveBorderWithContainer ? 'pb-7' : 'pb-8'}`,
39+
tabClassName: `fs-13 ${!hideTopPadding ? 'pt-8' : ''} ${isTabActive ? 'pb-5' : 'pb-7'}`,
4040
iconClassName: 'icon-dim-16',
4141
badgeClassName: 'fs-12 lh-20',
4242
},
4343
[ComponentSizeType.xl]: {
44-
tabClassName: `min-w-200 fs-13 ${!hideTopPadding ? 'pt-10' : ''} ${alignActiveBorderWithContainer ? 'pb-9' : 'pb-10'}`,
44+
tabClassName: `min-w-200 fs-13 ${!hideTopPadding ? 'pt-10' : ''} ${isTabActive ? 'pb-7' : 'pb-9'}`,
4545
iconClassName: 'icon-dim-16',
4646
badgeClassName: 'fs-12 lh-20',
4747
},

0 commit comments

Comments
 (0)