Last active
May 23, 2018 03:18
-
-
Save jakelear/e6a30b540b68662883c0c2ab87984c7b to your computer and use it in GitHub Desktop.
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
<TabContainer tabs={['Tab One Label', 'Tab Two Label']}> | |
<ChildComponent1 /> | |
<ChildComponent2 /> | |
</TabContainer> |
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 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