Skip to content

Instantly share code, notes, and snippets.

@kwoncharles
Last active December 26, 2020 08:22
Show Gist options
  • Save kwoncharles/cf2ced1ece0e8745c37187238e2ddf39 to your computer and use it in GitHub Desktop.
Save kwoncharles/cf2ced1ece0e8745c37187238e2ddf39 to your computer and use it in GitHub Desktop.
composition tab3
// Tabs.js
import React from 'react'
function Tabs({ children }) {
const [selectedTab, setTab] = React.useState(initialTab)
return (
<ul className="tab-container">
{React.Children.map(children, child => (
React.cloneElement(child, {
isSelected: child.props.value === selectedTab,
onSelect: () => setTab(child.props.value),
})
))}
</ul>
)
}
Tabs.Item = ({ isSelected, onSelect, children }) => (
<li
onClick={onSelect}
className={`tab-item ${isSelected ? 'selected' : ''}`}
>
{children}
</li>
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment