katoikia-app/web-ui/web-react/src/AppMenu.js

185 lines
4.6 KiB
JavaScript
Raw Normal View History

2022-07-06 04:15:11 +00:00
import React, { useState } from 'react';
import { NavLink } from 'react-router-dom';
import { CSSTransition } from 'react-transition-group';
import classNames from 'classnames';
2022-07-25 04:38:48 +00:00
import { Ripple } from 'primereact/ripple';
2022-07-06 04:15:11 +00:00
import { Badge } from 'primereact/badge';
const AppSubmenu = (props) => {
2022-07-25 04:38:48 +00:00
const [activeIndex, setActiveIndex] = useState(null);
2022-07-06 04:15:11 +00:00
2022-07-25 04:38:48 +00:00
const onMenuItemClick = (event, item, index) => {
//avoid processing disabled items
if (item.disabled) {
event.preventDefault();
return true;
2022-07-06 04:15:11 +00:00
}
2022-07-25 04:38:48 +00:00
//execute command
if (item.command) {
item.command({ originalEvent: event, item: item });
2022-07-06 04:15:11 +00:00
}
2022-07-25 04:38:48 +00:00
if (index === activeIndex) setActiveIndex(null);
else setActiveIndex(index);
2022-07-06 04:15:11 +00:00
2022-07-25 04:38:48 +00:00
if (props.onMenuItemClick) {
props.onMenuItemClick({
originalEvent: event,
item: item,
});
2022-07-06 04:15:11 +00:00
}
2022-07-25 04:38:48 +00:00
};
2022-07-06 04:15:11 +00:00
2022-07-25 04:38:48 +00:00
const onKeyDown = (event) => {
if (event.code === 'Enter' || event.code === 'Space') {
event.preventDefault();
event.target.click();
2022-07-06 04:15:11 +00:00
}
2022-07-25 04:38:48 +00:00
};
2022-07-06 04:15:11 +00:00
2022-07-25 04:38:48 +00:00
const renderLinkContent = (item) => {
let submenuIcon = item.items && (
<i className="pi pi-fw pi-angle-down menuitem-toggle-icon"></i>
);
let badge = item.badge && <Badge value={item.badge} />;
2022-07-06 04:15:11 +00:00
2022-07-25 04:38:48 +00:00
return (
<React.Fragment>
<i className={item.icon}></i>
<span>{item.label}</span>
{submenuIcon}
{badge}
<Ripple />
</React.Fragment>
);
};
const renderLink = (item, i) => {
let content = renderLinkContent(item);
if (item.to) {
return (
<NavLink
aria-label={item.label}
onKeyDown={onKeyDown}
role="menuitem"
className="p-ripple"
activeClassName="router-link-active router-link-exact-active"
to={item.to}
onClick={(e) => onMenuItemClick(e, item, i)}
exact
target={item.target}
>
{content}
</NavLink>
);
} else {
return (
<a
tabIndex="0"
aria-label={item.label}
onKeyDown={onKeyDown}
role="menuitem"
href={item.url}
className="p-ripple"
onClick={(e) => onMenuItemClick(e, item, i)}
target={item.target}
>
{content}
</a>
);
}
};
let items =
props.items &&
props.items.map((item, i) => {
let active = activeIndex === i;
let styleClass = classNames(item.badgeStyleClass, {
'layout-menuitem-category': props.root,
'active-menuitem': active && !item.to,
});
if (props.root) {
return (
<li className={styleClass} key={i} role="none">
{props.root === true && (
<React.Fragment>
<div
className="layout-menuitem-root-text"
aria-label={item.label}
>
{item.label}
</div>
<AppSubmenu
items={item.items}
onMenuItemClick={props.onMenuItemClick}
/>
</React.Fragment>
)}
</li>
);
} else {
return (
<li className={styleClass} key={i} role="none">
{renderLink(item, i)}
<CSSTransition
classNames="layout-submenu-wrapper"
timeout={{ enter: 1000, exit: 450 }}
in={active}
unmountOnExit
>
<AppSubmenu
items={item.items}
onMenuItemClick={props.onMenuItemClick}
/>
</CSSTransition>
</li>
);
}
2022-07-06 04:15:11 +00:00
});
2022-07-25 04:38:48 +00:00
return items ? (
<ul className={props.className} role="menu">
{items}
</ul>
) : null;
};
2022-07-06 04:15:11 +00:00
export const AppMenu = (props) => {
2022-07-25 04:38:48 +00:00
return (
<div className="layout-menu-container">
<AppSubmenu
items={props.model}
className="layout-menu"
onMenuItemClick={props.onMenuItemClick}
root={true}
role="menu"
/>
<a
href="https://www.primefaces.org/primeblocks-react"
className="block mt-3"
>
<img
alt="primeblocks"
className="w-full"
src={
props.layoutColorMode === 'light'
? 'assets/layout/images/banner-primeblocks.png'
: 'assets/layout/images/banner-primeblocks-dark.png'
}
/>
</a>
</div>
);
};
2022-07-06 04:15:11 +00:00
return (
<div className="layout-menu-container">
<AppSubmenu items={props.model} className="layout-menu" onMenuItemClick={props.onMenuItemClick} root={true} role="menu" />
2022-07-06 04:15:11 +00:00
</div>
);
}