Skip to content

Instantly share code, notes, and snippets.

@marxian
Created October 15, 2020 15:56
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 marxian/479ef80074f6adf04ab3150b3926486f to your computer and use it in GitHub Desktop.
Save marxian/479ef80074f6adf04ab3150b3926486f to your computer and use it in GitHub Desktop.
import React, { useState } from "react";
import PropTypes from "prop-types";
import Tab from "./Tab";
const Tabs = (props) => {
const [ activeTab, setActiveTab ] = useState(props.children[0].props.label);
return (
<div className="tabs">
<ol className="tab-list">
{props.children.map((child) => (
<Tab
activeTab={activeTab}
key={child.props.label}
label={child.props.label}
onClick={() => setActiveTab(child.props.label)}
/>
)}
</ol>
<div className="tab-content">
{props.children.map((child) => (
child.props.label !== activeTab ? child.props.children : null
))}
</div>
</div>
);
};
Tabs.propTypes = {
children: PropTypes.instanceOf(Array).isRequired,
}
export default Tabs;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment