Created
June 9, 2017 02:29
-
-
Save arackaf/9dc98ad603ea7da8a5b90ae177f50e68 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
| class Accordion extends Component { | |
| constructor(props) { | |
| super(props); | |
| let state = {openedHash: {}}; | |
| Children.toArray(props.children).forEach(child => { | |
| if (child){ | |
| state.openedHash[child.props.name] = !!child.props.defaultOpen | |
| } | |
| }); | |
| this.state = state; | |
| } | |
| expandAll = () =>{ | |
| let {openedHash} = this.state; | |
| Object.keys(openedHash).forEach(k => openedHash[k] = true); | |
| this.setState({openedHash}); | |
| } | |
| collapseAll = () =>{ | |
| let {openedHash} = this.state; | |
| Object.keys(openedHash).forEach(k => openedHash[k] = false); | |
| this.setState({openedHash}); | |
| } | |
| onToggle = name => { | |
| let openedHash = this.state.openedHash; | |
| this.setState({openedHash: {...openedHash, [name]: !openedHash[name]}}); | |
| } | |
| render() { | |
| let {openedHash} = this.state; | |
| let {children} = this.props; | |
| return ( | |
| <div> | |
| <button onClick={this.expandAll}>Expand All</button> | |
| <button onClick={this.collapseAll}>Collapse All</button> | |
| {Children.toArray(children).filter(c => c).map(child => cloneElement(child, { | |
| expanded: openedHash[child.props.name], | |
| onToggle: this.onToggle | |
| }))} | |
| </div> | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment