Skip to content

Commit

Permalink
refactor: use (typed) mapping instead of nested ternary
Browse files Browse the repository at this point in the history
  • Loading branch information
Alunara committed Jan 14, 2025
1 parent c2978d0 commit 238ff27
Showing 1 changed file with 26 additions and 23 deletions.
49 changes: 26 additions & 23 deletions packages/curve-ui-kit/src/shared/ui/TabsSwitcher.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ import Tabs, { type TabsProps } from '@mui/material/Tabs'
import Tab, { type TabProps } from '@mui/material/Tab'
import { TabSwitcherVariants, TABS_VARIANT_CLASSES, TABS_HEIGHT_CLASSES } from '../../themes/tabs'
import Typography, { type TypographyProps } from '@mui/material/Typography'
import type { TypographyVariantKey } from '@ui-kit/themes/typography'

const defaultTextVariants = {
small: 'buttonS',
medium: 'buttonM',
large: 'headingMBold',
} as const satisfies Record<keyof typeof TABS_HEIGHT_CLASSES, TypographyVariantKey>

export type TabOption<T> = Pick<TabProps, 'label' | 'disabled' | 'icon' | 'sx'> & {
value: T
Expand All @@ -27,26 +34,22 @@ export const TabsSwitcher = <T extends string | number>({
value,
textVariant,
...props
}: TabsSwitcherProps<T>) => {
const defaultTextVariant = size === 'small' ? 'buttonS' : size === 'medium' ? 'buttonM' : 'headingMBold'

return (
<Tabs
variant={muiVariant}
textColor="inherit"
value={value ?? false}
onChange={(_, newValue) => onChange?.(newValue)}
className={`${TABS_VARIANT_CLASSES[variant]} ${TABS_HEIGHT_CLASSES[size]}`}
{...props}
>
{options.map(({ value, label, ...props }) => (
<Tab
key={value}
value={value}
label={<Typography variant={textVariant ?? defaultTextVariant}>{label}</Typography>}
{...props}
/>
))}
</Tabs>
)
}
}: TabsSwitcherProps<T>) => (
<Tabs
variant={muiVariant}
textColor="inherit"
value={value ?? false}
onChange={(_, newValue) => onChange?.(newValue)}
className={`${TABS_VARIANT_CLASSES[variant]} ${TABS_HEIGHT_CLASSES[size]}`}
{...props}
>
{options.map(({ value, label, ...props }) => (
<Tab
key={value}
value={value}
label={<Typography variant={textVariant ?? defaultTextVariants[size]}>{label}</Typography>}
{...props}
/>
))}
</Tabs>
)

0 comments on commit 238ff27

Please sign in to comment.