Skip to content

Instantly share code, notes, and snippets.

@soska
Created December 28, 2015 19:15
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 soska/1371da5540137f34d242 to your computer and use it in GitHub Desktop.
Save soska/1371da5540137f34d242 to your computer and use it in GitHub Desktop.
Tabs Component
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