Last active
October 9, 2021 22:13
-
-
Save fernandodof/761f8c98183983c202636c680f374d76 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
import React, { ReactElement, useState } from 'react'; | |
import styles from './tabs.module.css'; | |
import TabTitle, { Props as TabTitleProps } from './TabTitle'; | |
type Props = { | |
children: ReactElement<TabTitleProps>[]; | |
preSelectedTabIndex?: number; | |
}; | |
const Tabs = (props: Props): JSX.Element => { | |
const { children, preSelectedTabIndex } = props; | |
// First tab is shown by default | |
const [selectedTabIndex, setSelectedTabIndex] = useState<number>(preSelectedTabIndex || 0); | |
return ( | |
<div className={styles.tabs}> | |
<ul> | |
{children.map((item, index) => ( | |
<TabTitle | |
key={item.props.title} | |
title={item.props.title} | |
index={index} | |
isActive={index === selectedTabIndex} | |
setSelectedTab={setSelectedTabIndex} | |
/> | |
))} | |
</ul> | |
{/* show selcted tab by index*/} | |
{children[selectedTabIndex]} | |
</div> | |
); | |
}; | |
export default Tabs; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment