Skip to content

Commit

Permalink
TabBar updates
Browse files Browse the repository at this point in the history
  • Loading branch information
AlejoYarce committed Jan 9, 2025
1 parent 3576191 commit fb88369
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/components/TabBar/TabBar.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export const PanelTabDisabled: Story = {
},
}

export const PanelTabWitIcons: Story = {
export const PanelTabWithIcons: Story = {
args: {
variant: 'panel',
tabs: [
Expand Down
70 changes: 55 additions & 15 deletions src/components/TabBar/index.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,51 @@
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import React from 'react'
import React, { useState } from 'react'

import { Box, Tabs } from '@chakra-ui/react'
import { TabBarContainer, TabBarItemPanel, TabBarItemView } from './styled'
import { TabBarProps } from './types'
import {
TabBarContainer,
TabBarItemPanel,
TabBarItemView,
TabBarItemViewDivider,
} from './styled'
import { TabBarItemProps, TabBarProps } from './types'

const getTabIndex = (tabs: TabBarItemProps[], selectedTab?: string) =>
tabs.findIndex((t) => t.value === selectedTab)

const getBorderSide = (selectedTabIndex: number) => {
if (selectedTabIndex === 1) {
return 'none'
}

if (selectedTabIndex === 2) {
return 'left'
}

return 'right'
}

const TabBar = ({
variant = 'panel',
defaultValue,
tabs,
onTabClick,
}: TabBarProps) => {
const [selectedTabIndex, setSelectedTabIndex] = useState(
getTabIndex(tabs, defaultValue) || 0,
)

const isView = variant === 'view'

const handleOnTabClick = (tabValue: string) => {
setSelectedTabIndex(getTabIndex(tabs, tabValue))

if (onTabClick) {
onTabClick(tabValue)
}
}

const TabBarItem = variant === 'view' ? TabBarItemView : TabBarItemPanel
const TabBarItem = isView ? TabBarItemView : TabBarItemPanel

return (
<TabBarContainer variant={variant}>
Expand All @@ -27,17 +55,29 @@ const TabBar = ({
onFocusChange={({ focusedValue }) => handleOnTabClick(focusedValue)}
>
<Tabs.List alignItems='center' border='none'>
{tabs.map((tab) => (
<TabBarItem
key={tab.label}
aria-label={tab['aria-label'] || tab.label}
{...tab}
>
<Box display='flex' alignItems='center' gap='5px'>
{tab.icon}
{tab.label}
</Box>
</TabBarItem>
{tabs.map((tab, idx) => (
<>
{isView &&
idx === 1 &&
getBorderSide(selectedTabIndex) === 'left' ? (
<TabBarItemViewDivider />
) : null}
<TabBarItem
key={tab.label}
aria-label={tab['aria-label'] || tab.label}
{...tab}
>
<Box display='flex' alignItems='center' gap='5px'>
{tab.icon}
{tab.label}
</Box>
</TabBarItem>
{isView &&
idx === 1 &&
getBorderSide(selectedTabIndex) === 'right' ? (
<TabBarItemViewDivider />
) : null}
</>
))}
</Tabs.List>
</Tabs.Root>
Expand Down
10 changes: 8 additions & 2 deletions src/components/TabBar/styled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ export const TabBarContainer = styled.div<{
`
border: 1px solid ${getThemedColor('neutral', 400)};
background-color: ${getThemedColor('neutral', 200)};
border-radius: 2px;
border-radius: 4px;
padding: 4px;
`}
`

export const DefaultTab = styled(Tabs.Trigger)`
width: 100%;
width: 99%;
height: 40px;
padding: 8px 16px;
border-radius: 0px;
Expand Down Expand Up @@ -91,6 +91,12 @@ export const TabBarItemPanel = styled(DefaultTab)`
}
`

export const TabBarItemViewDivider = styled.div`
width: 4px;
height: 24px;
background-color: ${getThemedColor('neutral', 400)};
`

export const TabBarItemView = styled(DefaultTab)`
height: 32px;
border-radius: 2px;
Expand Down

0 comments on commit fb88369

Please sign in to comment.