import React, { Fragment, memo, useCallback, useMemo } from 'react'; import type { KeyboardEvent } from 'react'; import { useLocation } from 'react-router-dom'; import clsx from 'clsx'; import join from 'lodash/join'; import { useToggle } from 'react-use'; import Collapse from '@mui/material/Collapse'; import Grid from '@mui/material/Grid'; import MuiListItem from '@mui/material/ListItem'; import ListItemIcon from '@mui/material/ListItemIcon'; import ListItemText from '@mui/material/ListItemText'; import Typography from '@mui/material/Typography'; import ExpandMoreRoundedIcon from '@mui/icons-material/ExpandMoreRounded'; import ChevronRightRoundedIcon from '@mui/icons-material/ChevronRightRounded'; import { Link } from '../../@navigation/link'; import type { NavItem as Item } from '../../@navigation/breadcrumbs'; import { trimSpaces } from '../../../utils'; import type { NestedListItemProps, SidebarItemProps } from './types'; const BaseSidebarItem = (props: SidebarItemProps) => { const { Icon: IconComponent, items, name, rootPath, classes, styles, badgeText, sidebarState } = props; const [open, toggleOpen] = useToggle(false); const location = useLocation(); const currentPath = location.pathname; const parentCheck = useMemo(() => { let isParent = false; if (currentPath === rootPath) { return true; } items?.forEach(item => { if (item.path && (item.path === currentPath || currentPath.includes(item.path))) { isParent = true; } else if (item.subItems?.length) { item.subItems.forEach(subItem => { if ( subItem.path && (subItem.path === currentPath || currentPath.includes(subItem.path)) ) { isParent = true; } }); } }); return isParent; }, [items, currentPath, rootPath]); const childCheck = useCallback( (path, subItems) => { let isChild = false; if (currentPath === path || (path && currentPath.includes(path))) { return true; } else if (subItems?.length) { subItems.forEach((subItem: Item) => { if ( subItem.path && (subItem.path === currentPath || currentPath.includes(subItem.path)) ) { isChild = true; } }); } return isChild; }, [currentPath] ); // For accessibility const handleKeyDown = useCallback( (e: KeyboardEvent) => { switch (e.code) { case 'Space': case 'Enter': e.preventDefault(); toggleOpen(); break; default: } }, [toggleOpen] ); return items?.length && !rootPath ? ( {IconComponent && ( )} {badgeText && ( {badgeText} )} {open ? ( ) : ( )} {items?.map(item => { const { name: innerName, path = '', subItems, badgeText: itemBadgeText } = item; return subItems?.length ? ( ) : ( {itemBadgeText && ( {itemBadgeText} )} ); })} ) : ( {IconComponent && ( )} {badgeText && ( {badgeText} )} ); }; BaseSidebarItem.defaultProps = { items: [], rootPath: '' }; export const SidebarItemBase = memo(BaseSidebarItem); const NestedListItem = memo((props: NestedListItemProps) => { const { innerName, styles, classes, currentPath, itemBadgeText, path, subItems = [], childCheck } = props; const [open, toggleOpen] = useToggle(false); return ( {itemBadgeText && ( {itemBadgeText} )} {open ? ( ) : ( )} {subItems.map(subItem => { const { name: subName, path: subPath, badgeText: subItemBadgeText } = subItem; return ( {subItemBadgeText && ( {subItemBadgeText} )} ); })} ); });