Skip to content

Instantly share code, notes, and snippets.

@dadamssg
Last active July 26, 2017 18:32
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 dadamssg/921ef01a12b964945f0e9c3c1afbdadd to your computer and use it in GitHub Desktop.
Save dadamssg/921ef01a12b964945f0e9c3c1afbdadd to your computer and use it in GitHub Desktop.
import React from 'react'
import { Route, Link } from 'react-router-dom'
const TabLink = ({ to, exact, title }) => (
<Route path={to} exact={exact} children={({ match }) => (
<li className={`nav-item ${match ? 'active' : ''}`}>
<Link to={to} className='nav-link' role='tab'>{title}</Link>
</li>
)} />
)
const TabPanel = ({ to, exact, content }) => (
<Route path={to} exact={exact} children={({ match }) => (
<div className={`tab-pane ${match ? 'active' : ''}`} role='tabpanel'>
{match ? content : null}
</div>
)} />
)
export function Tabs ({ children }) {
return (
<span>
<ul className='nav nav-tabs' role='tablist'>
{React.Children.map(children, tab => (
<TabLink
key={`nav-item-${tab.props.to}`}
to={tab.props.to}
exact={tab.props.exact}
title={tab.props.title}
/>
))}
</ul>
<div className='tab-content'>
{React.Children.map(children, tab => (
<TabPanel
key={`tab-panel-${tab.props.to}`}
to={tab.props.to}
exact={tab.props.exact}
content={tab.props.children}
/>
))}
</div>
</span>
)
}
export const Tab = () => {}
import {Tabs, Tab} from './Tabs'
function PageWithTabs ({ match }) {
return (
<Tabs>
<Tab title='Foo' exact to={`${match.url}`}>
FOO
</Tab>
<Tab title='Bar' to={`${match.url}/bar`}>
<Foobar />
</Tab>
<Tab title='Baz' to={`${match.url}/baz`}>
BAZ
</Tab>
</Tabs>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment