Created
February 1, 2018 20:47
-
-
Save ghengeveld/07523be5539ec03b2f20bcb78b6a30ca to your computer and use it in GitHub Desktop.
React Accordion
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
class Accordion extends React.Component { | |
constructor(props) { | |
super(props) | |
this.state = { | |
activeItem: null, | |
} | |
} | |
toggle = (event, index) => this.setState(state => ({ activeItem: state.activeItem === index ? null : index })) | |
render() { | |
return ( | |
<div> | |
{React.Children.map(this.props.children, child => { | |
return React.cloneElement(child, { toggle: this.toggle }) | |
})} | |
</div> | |
) | |
} | |
} | |
const AccordionItem = ({ label, children, toggle }) => ( | |
<div> | |
<button onClick={toggle}>{label}</button> | |
{children} | |
</div> | |
) | |
const Footer = props => ( | |
<Accordion> | |
<AccordionItem label="A">Foo</AccordionItem> | |
<AccordionItem label="B">Bar</AccordionItem> | |
</Accordion> | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment