Skip to content

Instantly share code, notes, and snippets.

@fernandodof
Last active October 9, 2021 22:13
Show Gist options
  • Save fernandodof/761f8c98183983c202636c680f374d76 to your computer and use it in GitHub Desktop.
Save fernandodof/761f8c98183983c202636c680f374d76 to your computer and use it in GitHub Desktop.
tabs
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