Last active
October 26, 2020 16:21
-
-
Save abinavseelan/f723b184e76f0e00bf779cdbb35bdfee to your computer and use it in GitHub Desktop.
Component for a dropdown interaction (6)
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
import React, { Component } from 'react'; | |
class Card extends Component { | |
constructor() { | |
super(); | |
this.state = { | |
showMenu: false, | |
}; | |
this.showMenu = this.showMenu.bind(this); | |
this.closeMenu = this.closeMenu.bind(this); | |
} | |
showMenu(event) { | |
event.preventDefault(); | |
this.setState({ showMenu: true }, () => { | |
document.addEventListener('click', this.closeMenu); | |
}); | |
} | |
closeMenu(event) { | |
if (!this.dropdownMenu.contains(event.target)) { | |
this.setState({ showMenu: false }, () => { | |
document.removeEventListener('click', this.closeMenu); | |
}); | |
} | |
} | |
render() { | |
return ( | |
<div> | |
<button onClick={this.showMenu}> | |
Show menu | |
</button> | |
{ | |
this.state.showMenu | |
? ( | |
<div | |
className="menu" | |
ref={(element) => { | |
this.dropdownMenu = element; | |
}} | |
> | |
<button> Menu item 1 </button> | |
<button> Menu item 2 </button> | |
<button> Menu item 3 </button> | |
</div> | |
) | |
: ( | |
null | |
) | |
} | |
</div> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi would you please be able to help me in converting all of this code into a functional component and is it possible?