Created
February 7, 2019 17:12
-
-
Save iamchristough/60719ae40662014cee4ab316a9dcc3aa to your computer and use it in GitHub Desktop.
Collapse React Component
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 Collapse extends React.Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
collapsed: !!props.collapsed | |
}; | |
this.style = { | |
collapsed: { | |
display: 'none' | |
}, | |
expanded: { | |
display: 'block' | |
}, | |
buttonStyle: { | |
display: 'block', | |
width: '100%' | |
} | |
}; | |
this.toggleCollapse = this.toggleCollapse.bind(this); | |
} | |
toggleCollapse() { | |
this.setState(state => ({ collapsed: !state.collapsed })); | |
} | |
render() { | |
return ( | |
<div> | |
<button style={this.style.buttonStyle} onClick={this.toggleCollapse}> | |
Show/Hide Content | |
</button> | |
<div | |
style= {this.state.collapsed ? this.style.collapsed : this.style.expanded} | |
aria-expanded = {this.state.collapsed} | |
> | |
{this.props.children} | |
</div> | |
</div> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment