class DropdownMenu extends React.Component {
getMenuItemTitle = (menuItem, index, depthLevel) => {
return menuItem.title;
};
getMenuItem = (menuItem, depthLevel, index) => {
let title = this.getMenuItemTitle(menuItem, index, depthLevel);
if (menuItem.submenu && menuItem.submenu.length > 0) {
return (
<li>
{title}
<DropdownMenu config={menuItem.submenu} submenu={true} />
</li>
);
} else {
return <li>{title}</li>;
}
};
render = () => {
let { config } = this.props;
let options = [];
config.map((item, index) => {
options.push(this.getMenuItem(item, 0, index));
});
if (this.props.submenu && this.props.submenu === true)
return <ul>{options}</ul>;
return <ul className="dropdown-menu">{options}</ul>;
};
}
ReactDOM.render(<DropdownMenu config={[
{
"title": "Option 1",
"submenu": null
},
{
"title": "Option 2",
"submenu": [
{
"title": "Option 2.1",
"submenu": [
{
"title": "Option 2.1.1",
"submenu": null
},
{
"title": "Option 2.1.2",
"submenu": null
}
]
},
{
"title": "Option 2.2",
"submenu": [
{
"title": "Option 2.2.1",
"submenu": null
},
{
"title": "Option 2.2.2",
"submenu": null
}
]
}
]
}
]}/>, document.querySelector("#app"))
.dropdown-menu {
height: 35px;
list-style: none;
margin: 0;
padding: 0;
float: left;
text-align: center;
}
.dropdown-menu li {
display: inline-block;
position: relative;
float: left;
width: 200px;
line-height: 35px;
text-decoration: none;
}
.dropdown-menu li li a {
font-size: 12px;
}
.dropdown-menu li:hover {
background: blue;
}
/*--- Sublist Styles ---*/
.dropdown-menu ul {
position: absolute;
display: none;
}
/*--- Hide Sub Sublists ---*/
.dropdown-menu li:hover ul ul {
display: none;
}
/*--- Sublevel UL's display and position on hover ---*/
.dropdown-menu li:hover ul {
display: block;
}
.dropdown-menu li li:hover ul {
margin-left: 200px;
/*This must be width of menu box*/
margin-top: -35px;
display: block;
}
nu-li {
padding: 10px;
}
.dropdown-submenu {
position: absolute;
left: 0px;
top: 0px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.3.1/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.3.1/umd/react-dom.production.min.js"></script>
<div id="app"></div>