I'm trying to implement a drop down wrapper. The element wrapped by the DropDownWrapper is expected to toggle the display of drop-down through onClick.
class DropdownMenuWrapper extends React.Component {
constructor(props) {
super(props);
this.state = {
active: true,
};
this.handleDropDownDisplay = this.handleDropDownDisplay.bind(this);
this.displayDropDownItems = this.displayDropDownItems.bind(this);
}
displayDropDownItems(menuItems, onSelectMenuItem, children) {
return (
menuItems.map(item => (
<a
className={cx('navItem')}
key={item.value}
onClick={() => onSelectMenuItem(children)}
>
{item.logo}
{item.display}
</a>
))
);
}
handleDropDownDisplay() {
this.setState({
active: !this.state.active,
});
}
render() {
const {
className, menuItems, onSelectMenuItem, children, label,
} = this.props;
return (
<div>
{children}
<nav className={className} aria-label={label}>
{this.state.active && this.displayDropDownItems(menuItems, onSelectMenuItem, children)}
</nav>
</div>
);
}
}
export default DropdownMenuWrapper;
Here I want to achieve dropdown toggle on the wrapped button below
<DropdownMenuWrapper
menuItems={[
{ value: 'dashboard', display: 'Dashboard' },
{ value: 'myProfile', display: 'My Profile' },
]}
>
<Button />
</DropdownMenuWrapper>
Is there a way I can use {children} onClick to change the state of the Parent component (DropdownMenuWrapper) in this case?