Last active
November 5, 2017 02:31
-
-
Save giancarlosisasi/1589ca9a88187e5a5d181052651627a0 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 Dropdown extends Component { | |
| state = { | |
| show: false | |
| } | |
| componentDidMount() { | |
| document.addEventListener('click', this.handleClickOutside, true); | |
| } | |
| componentWillUnmount() { | |
| document.removeEventListener('click', this.handleClickOutside, true); | |
| } | |
| showContent = () => this.setState({ show: true }) | |
| hideContent = () => this.setState({ show: false }) | |
| // Handle function when user click outside container | |
| handleClickOutside = (e) => { | |
| const { onClickOutside } = this.props; | |
| if (!this.container.contains(e.target) && this.state.show) { | |
| onClickOutside(e); | |
| } | |
| } | |
| render() { | |
| if (!show) return null; | |
| return ( | |
| <div ref={(el) => { this.container = el; }}> | |
| the content dude | |
| </div> | |
| ) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment