Skip to content

Instantly share code, notes, and snippets.

@mupkoo
Created February 20, 2015 11:50
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 mupkoo/2c1ee9894cd9106cacdc to your computer and use it in GitHub Desktop.
Save mupkoo/2c1ee9894cd9106cacdc to your computer and use it in GitHub Desktop.
Accordion component ReactJS
var Accordion = React.createClass({
changeCurrent: function (index) {
this.setState({ currentSection: index });
},
getInitialState: function() {
return {
currentSection: 0
};
},
render: function () {
return (
<div className="accordion">
{this.getSections()}
</div>
);
},
getSections: function () {
return this.props.children.map(function (section, index) {
return React.addons.cloneWithProps(section, {
key: index,
index: index,
changeCurrent: this.changeCurrent,
isOpened: (index === this.state.currentSection)
});
}.bind(this));
}
});
var AccordionSection = React.createClass({
changeCurrent: function (e) {
this.props.changeCurrent(this.props.index);
},
render: function() {
var classNames = 'section'
if (this.props.isOpened) {
classNames += ' opened';
}
return (
<div className={classNames}>
<h2 onClick={this.changeCurrent}>{this.props.title}</h2>
<div className="content">
{this.props.children}
</div>
</div>
);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment