function Menu(props) {
const [items, setItems] = useState(Object.values(data.ITEMS))
const [showChild, setShowChild] = useState(0)
const [windowWidth, setWindowWidth] = useState(window.innerWidth)
function setChild(e, event) {
if(showChild > 0 && showChild == e) {
$('.i_h_menu_child').fadeOut(function () {
setShowChild(0)
})
}
else
setShowChild(e)
event.preventDefault()
}
/*function componentDidMount() {
updateWidthElements()
}*/
function addCatalog(arr) {
let catalog = {
'ID': 'CATALOG',
'NAME': 'Каталог',
'SECTION_PAGE_URL': '',
'I_CHILD': []
}
arr.unshift(catalog)
return arr
}
function updateWidthElements() {
//setWindowWidth(window.innerWidth)
let wMenu = document.querySelector('.i_h_menu_react').clientWidth,
wBody = document.querySelector('.i_body').clientWidth,
elements = items
//console.log([wMenu, wBody])
if(wMenu > wBody) {
let el = elements.pop()
//items = items
if(elements[0]['ID'] == 'CATALOG') {
elements[0]['I_CHILD'].push(el)
}
else {
elements = addCatalog(elements)
elements[0]['I_CHILD'].push(el)
}
setItems(elements)
console.log(items)
//updateWidthElements()
}
}
useEffect(updateWidthElements, [])
useEffect(() => {
window.addEventListener("resize", updateWidthElements);
return () => {
window.removeEventListener("resize", updateWidthElements)
};
})
return (
<div className="i_h_menu_react">
{items.map((item, index) =>
<div className="i_h_menu_block" key={index}>
<a href={item.SECTION_PAGE_URL} className="i_h_menu_link" onClick={(e) => setChild(item.ID, e)}>
<span>{item.NAME}</span>
</a>
{item.I_CHILD && showChild == item.ID &&
<MenuChild items={Object.values(item.I_CHILD)} />
}
</div>
)}
</div>
)
}
function MenuChild(props) {
return (
<div className="i_h_menu_child">
{props.items.map((item, index) =>
<div className="i_h_menu_block" key={index}>
<a href={item.SECTION_PAGE_URL} className="i_h_menu_link">
<span>{item.NAME}</span>
</a>
{item.I_CHILD &&
<MenuChild items={Object.values(item.I_CHILD)} />
}
</div>
)}
</div>
)
}
ReactDOM.render(<Menu />, menu_div);