Skip to content

Instantly share code, notes, and snippets.

@kaiomagalhaes
Last active November 28, 2021 18:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaiomagalhaes/ec0470400f7b5134c12a553f679caaae to your computer and use it in GitHub Desktop.
Save kaiomagalhaes/ec0470400f7b5134c12a553f679caaae to your computer and use it in GitHub Desktop.
Tab gist
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