Created
June 4, 2018 03:27
-
-
Save esr360/cbb5ee66d2d2a148cacccecd8bb3bbca to your computer and use it in GitHub Desktop.
This file contains 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
Accordion.defaultProps = { | |
name: 'accordion' | |
}; | |
class Accordion extends React.Component { | |
constructor(props) { | |
super(props); | |
this.panels = []; | |
this.state = { activePanel: null }; | |
} | |
toggle(index) { | |
this.setState({ | |
activePanel: (this.panels[index] === this.state.activePanel) ? null : this.panels[index] | |
}); | |
} | |
isActive(index) { | |
return (this.panels[index] === this.state.activePanel) ? true : false; | |
} | |
render() { | |
return ( | |
<Module {...this.props}> | |
{this.props.panels.map(({ title, content }, index) => ( | |
<Component name='panel' | |
key={index} | |
ref={ref => this.panels[index] = ref} | |
modifiers={this.isActive(index) ? 'active' : false} | |
> | |
<Component name='title' onClick={this.toggle.bind(this, index)}> | |
{title} | |
</Component> | |
<Component name='content'>{content}</Component> | |
</Component> | |
))} | |
</Module> | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment