Skip to content

Instantly share code, notes, and snippets.

@jakelear
Last active May 23, 2018 03:18
Show Gist options
  • Save jakelear/e6a30b540b68662883c0c2ab87984c7b to your computer and use it in GitHub Desktop.
Save jakelear/e6a30b540b68662883c0c2ab87984c7b to your computer and use it in GitHub Desktop.
<TabContainer tabs={['Tab One Label', 'Tab Two Label']}>
<ChildComponent1 />
<ChildComponent2 />
</TabContainer>
import React from 'react';
import PropTypes from 'prop-types';
export default class TabContainer extends React.Component {
static propTypes = {
children: PropTypes.node.isRequired,
tabs: PropTypes.array.isRequired,
};
state = {
selected: 1,
}
showTab(index) {
this.setState({
selected: index,
});
}
get tabs () {
const { tabs } = this.props;
return tabs.map((tab, index) => {
return (
<li key={tab} onClick={() => this.showTab(index)}>
{tab}
</li>
);
});
}
get currentTab () {
return this.props.children[this.state.selected];
}
render () {
return (
<div>
<ul>
{this.tabs}
</ul>
{this.currentTab}
</div>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment