Last active
November 28, 2021 18:53
-
-
Save kaiomagalhaes/ec0470400f7b5134c12a553f679caaae to your computer and use it in GitHub Desktop.
Tab gist
This file contains hidden or 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 React, { useState } from 'react'; | |
import classNames from 'classnames'; | |
import styles from './Tab.module.scss'; | |
import { Button } from 'agnosticDesignSystem'; | |
type MenuItem = { | |
title: string | React.ReactNode; | |
active?: boolean; | |
key: string; | |
}; | |
interface Props { | |
components: React.ReactNode[]; | |
menuItems: MenuItem[]; | |
variablesClassName?: string; | |
} | |
const Tab = (props: Props) => { | |
const { menuItems, components, variablesClassName } = props; | |
if (menuItems.length == 0) { | |
return <></> | |
} | |
const defaultKey = menuItems.find(item => item.active)?.key; | |
const [activeKey, setActiveKey] = useState(defaultKey || menuItems[0].key); | |
if (menuItems.length !== components.length) { | |
return <>It is necessary to provide the same amount of menu items and components.</>; | |
} | |
return ( | |
<div className={classNames(styles['tab-container'], variablesClassName)}> | |
<div className={classNames(styles['tab-menu-container'], variablesClassName)}> | |
{menuItems.map((item, index) => { | |
return ( | |
<Button | |
key={`${item.title}-${index}`} | |
variablesClassName={classNames( | |
styles['tab-menu-button'], | |
activeKey === item.key && styles['tab-menu-button-active'], | |
variablesClassName | |
)} | |
text={item.title} | |
onClick={() => setActiveKey(item.key)} | |
/> | |
); | |
})} | |
</div> | |
<div> | |
{components.map((component, index) => { | |
return <div key={index}>{menuItems[index].key === activeKey && component}</div>; | |
})} | |
</div> | |
</div> | |
); | |
}; | |
export default Tab; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment