Created
December 28, 2015 19:15
-
-
Save soska/1371da5540137f34d242 to your computer and use it in GitHub Desktop.
Tabs Component
This file contains hidden or 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, {Children, cloneElement} from 'react'; | |
import cx from 'classnames'; | |
export const Tabs = ({ | |
onChange = () => {}, | |
selectedIndex = 0, | |
TabListComponent = TabList, | |
children, | |
}) => { | |
let tabNames = Children.map(children, child => { | |
return child.props.name; | |
}); | |
return ( | |
<div className="tabs"> | |
<TabListComponent tabs={tabNames} onSelect={onChange} selectedIndex={selectedIndex}/> | |
<div className="tabs__inner"> | |
{Children.map(children, (child,index) => { | |
return cloneElement(child, {selectedIndex, index, ...child.props}); | |
})} | |
</div> | |
</div> | |
); | |
}; | |
export const TabPanel = ({ | |
name, | |
children, | |
selectedIndex, | |
index, | |
}) => { | |
const selected = (selectedIndex == index); | |
if (selected) { | |
return ( | |
<div className="tabs__panel"> | |
{children} | |
</div> | |
); | |
} else{ | |
return <span /> | |
} | |
} | |
export const TabList = ({ | |
tabs, | |
onSelect, | |
selectedIndex | |
}) => { | |
return ( | |
<div className="tabs__list"> | |
{tabs.map( (tab,index) => { | |
const selected = (selectedIndex == index); | |
return ( | |
<a | |
key={tab} | |
href="#" | |
className={cx(['tabs__link',{selected}])} | |
onClick={(e)=>{ | |
e.preventDefault(); | |
onSelect(index); | |
}}>{tab}</a> | |
); | |
})} | |
</div> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment