Last active
May 4, 2022 14:40
-
-
Save kugimiya/e9dc90f626fb6a59077a66c088e5fafa to your computer and use it in GitHub Desktop.
Tabs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Tabs | |
className={{ wrapper: styles.tabs }} | |
items={[ | |
{ | |
id: 'transitions', | |
title: text('lk.history.transitions'), | |
content: <TransitionsTab />, | |
}, | |
{ | |
id: 'purchases', | |
title: text('lk.history.purchases'), | |
content: <PurchasesTab />, | |
}, | |
{ | |
id: 'receives', | |
title: text('lk.history.receives'), | |
content: <ReceivingsTab />, | |
}, | |
]} | |
/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import classNames from 'classnames'; | |
import { memo, useState } from 'react'; | |
import { Box } from '../Box'; | |
import { Text } from '../Text'; | |
import styles from './styles.module.css'; | |
export type TabItem = { | |
id: string; | |
title: string; | |
content: JSX.Element; | |
}; | |
export type TabsProps = { | |
items: TabItem[]; | |
center?: boolean | undefined; | |
onTabChange?: (id: string) => void; | |
className?: { | |
wrapper?: string; | |
text?: string; | |
}; | |
}; | |
export const Tabs = memo(({ items, onTabChange, className, center }: TabsProps): JSX.Element => { | |
if (!items.length) { | |
throw new Error('Items array should contain at least 1 item'); | |
} | |
const [currentTab, setCurrentTab] = useState(items[0].id); | |
const current = items.find((i) => i.id === currentTab); | |
return ( | |
<Box flexDirection="column" className={classNames(styles.tabsRoot, className?.wrapper)}> | |
<Box className={styles.tabsBox} justifyContent={center ? 'center' : ''}> | |
{items.map((item) => ( | |
<Box | |
key={item.id} | |
className={classNames({ | |
[styles.tabsItem]: true, | |
[styles.active]: item.id === currentTab, | |
})} | |
onClick={() => { | |
setCurrentTab(item.id); | |
if (onTabChange) { | |
onTabChange(item.id); | |
} | |
}} | |
alignItems="center" | |
> | |
<Text color="INHERIT" className={styles.tabName}> | |
{item.title} | |
</Text> | |
</Box> | |
))} | |
</Box> | |
<Box className={styles.tabContentRoot}>{current?.content}</Box> | |
</Box> | |
); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment