Last active
August 29, 2015 14:09
-
-
Save charlesponti/2de7968fa1bc444c7a2b to your computer and use it in GitHub Desktop.
ReactJS TabPanel
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
| // Usage | |
| var React = require('react'); | |
| var TabPanel = require('./TabPanel.jsx'); | |
| var MyTabPanel = React.createClass({ | |
| mixins: [TabPanel], | |
| getDefaultProps: function() { | |
| return { | |
| tabs: { | |
| 'Tab One': <div>Tab One</div>, | |
| 'Tab Two': <div>Tab Two</div>, | |
| }, | |
| default: 'Tab One' | |
| }; | |
| } | |
| }); | |
| React.render(<MyTabPanel/>, document.querySelector('body')) |
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
| 'use strict'; | |
| // Module dependencies | |
| var React = require('react'); | |
| var TabPanel = { | |
| propTypes: { | |
| // Require tabs configuration | |
| tabs: React.PropTypes.object.isRequired | |
| }, | |
| /** | |
| * Get initial state of tab panel | |
| * @return {object} | |
| */ | |
| getInitialState: function() { | |
| return { component: undefined }; | |
| }, | |
| /** | |
| * Handle click event of tab | |
| * @param {SyntheticEvent} e | |
| * @param {string} id | |
| */ | |
| onClick: function(e, id) { | |
| var tab = e.target.dataset.tab; | |
| this.setState({ component: this.props.tabs[tab] }); | |
| }, | |
| /** | |
| * Render component | |
| * @return {ReactElement} | |
| */ | |
| render: function() { | |
| var tabs = Object.keys(this.props.tabs).map(function(title) { | |
| return ( | |
| <li onClick={this.onClick} role="presentation"> | |
| <a data-tab={title}>{title}</a> | |
| </li> | |
| ); | |
| }.bind(this)); | |
| return ( | |
| <div> | |
| <ul className="nav nav-pills" role="tablist" ref="tabpanelNav"> | |
| {tabs} | |
| </ul> | |
| <div ref="tabpanelBody"> | |
| {this.state.component} | |
| </div> | |
| </div> | |
| ) | |
| } | |
| }; | |
| module.exports = TabPanel; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment