I would like to dynamically fill the 'menuItems' property of a MenuItem (https://www.material-ui.com/#/components/menu) from an array of values. I found several posts about using the map syntax, and indeed I managed to make it work to fill the Menu with MenuItem elements. However I didn't manage to make it work to fill the menuItems array (nested menus).
Any help appreciated, I'm new with react and javascript so it is likely I'm missing something obvious. Thanks
Here is what I wrote:
class PopupMenu extends React.Component {
constructor(props) {
super(props);
this.state = { menu: props.menu, key: (props.menu.id + "_menu"), open: false, actions: null};
}
...
async createActionOrMenu(actionOrMenu) {
if (actionOrMenu[1] == true) {
// submenu
let menu = actionOrMenu[0];
let actionsOrMenus = await window.epic.content(menu);
return <MenuItem
primaryText={label(menu.title)}
menuItems={actionsOrMenus.map(this.createActionOrMenu.bind(this))} // the line I have problem with
/>
} else {
//action
let action = actionOrMenu[0];
return <MenuAction action={action}/>
}
}
createActions(actionsOrMenus) {
if (!actionsOrMenus || actionsOrMenus.length === 0) {
return;
}
return actionsOrMenus.map(this.createActionOrMenu.bind(this)); // the map syntax to dynamically fill elements: works like charm
}
render() {
return (
<div>
<FlatButton
className="menubar_menu"
onClick={this.handleClick}
label={label(this.state.menu.title)}
hoverColor="lightgrey"
primary = {this.state.open}
/>
<Popover
open={this.state.open}
anchorEl={this.state.anchorEl}
anchorOrigin={{horizontal: 'left', vertical: 'bottom'}}
targetOrigin={{horizontal: 'left', vertical: 'top'}}
onRequestClose={this.handleRequestClose}
>
<Menu key= {this.state.key+"popup"}>
{this.createActions(this.state.actions)}
</Menu>
</Popover>
</div>
);
}
}
menuItemssupplied toMenuItem, expected a ReactNode. in MenuItem