diff --git a/packages/core-components/src/layout/Sidebar/Bar.test.tsx b/packages/core-components/src/layout/Sidebar/Bar.test.tsx index 56815305fb..68b05fc393 100644 --- a/packages/core-components/src/layout/Sidebar/Bar.test.tsx +++ b/packages/core-components/src/layout/Sidebar/Bar.test.tsx @@ -25,34 +25,31 @@ import { MiscIcon } from './icons/MiscIcon'; import { Sidebar, SidebarExpandButton } from './Bar'; import { SidebarItem, SidebarSearchField } from './Items'; import { SidebarSubItem } from './SidebarSubItem'; +import { Submenu } from './Submenu'; async function renderScalableSidebar() { await renderInTestApp( {}} to="/search" /> - {}} - text="Catalog" - hasSubMenu - submenuTitle="Catalog" - > - - + {}} text="Catalog"> + + + + diff --git a/packages/core-components/src/layout/Sidebar/Items.tsx b/packages/core-components/src/layout/Sidebar/Items.tsx index 3c4fc6ffd0..f1b51e497f 100644 --- a/packages/core-components/src/layout/Sidebar/Items.tsx +++ b/packages/core-components/src/layout/Sidebar/Items.tsx @@ -28,7 +28,6 @@ import React, { Children, forwardRef, KeyboardEventHandler, - PropsWithChildren, ReactNode, useContext, useState, @@ -170,27 +169,30 @@ const useStyles = makeStyles( type ItemWithSubmenuProps = { label?: string; - title?: string; hasNotifications?: boolean; icon: IconComponent; - children: ReactNode; + submenu: ReactNode; }; const ItemWithSubmenu = ({ label, - title, hasNotifications = false, icon: Icon, - children, -}: PropsWithChildren) => { + submenu, +}: ItemWithSubmenuProps) => { const classes = useStyles(); const [isHoveredOn, setIsHoveredOn] = useState(false); - const toPathnames: string[] = []; + let submenuItems: ReactNode; + Children.forEach(submenu, element => { + if (!React.isValidElement(element)) return; + submenuItems = element.props.children; + }); + const toPathnames: string[] = []; let isActive; const { pathname: locationPathname } = useLocation(); - // Menu item is active if any of its children have active paths - Children.forEach(children, element => { + // SidebarItem is active if any of submenu items have active paths + Children.forEach(submenuItems, element => { if (!React.isValidElement(element)) return; if (element.props.dropdownItems) { element.props.dropdownItems.map((item: { to: string }) => @@ -267,7 +269,7 @@ const ItemWithSubmenu = ({ )} - {isHoveredOn && {children}} + {isHoveredOn && submenu} ); @@ -277,8 +279,6 @@ type SidebarItemBaseProps = { icon: IconComponent; text?: string; hasNotifications?: boolean; - hasSubMenu?: boolean; - submenuTitle?: string; disableHighlight?: boolean; children?: ReactNode; className?: string; @@ -354,8 +354,6 @@ export const SidebarItem = forwardRef((props, ref) => { icon: Icon, text, hasNotifications = false, - hasSubMenu = false, - submenuTitle, disableHighlight = false, onClick, children, @@ -409,17 +407,28 @@ export const SidebarItem = forwardRef((props, ref) => { ), }; - // If submenu prop is true, return ItemWithSubmenu - if (hasSubMenu) { + let hasSubmenu = false; + let submenu: any; + const componentType = ( + + <> + + ).type; + Children.forEach(children, element => { + if (!React.isValidElement(element)) return; + if (element.type === componentType) { + submenu = element; + hasSubmenu = true; + } + }); + if (hasSubmenu) { return ( - {children} - + submenu={submenu} + /> ); } diff --git a/packages/core-components/src/layout/Sidebar/Sidebar.stories.tsx b/packages/core-components/src/layout/Sidebar/Sidebar.stories.tsx index 0a41f421a2..e25693cc81 100644 --- a/packages/core-components/src/layout/Sidebar/Sidebar.stories.tsx +++ b/packages/core-components/src/layout/Sidebar/Sidebar.stories.tsx @@ -35,6 +35,7 @@ import { CatalogSidebarLogo } from './icons/CatalogSidebarLogo'; import { APIsIcon } from './icons/APIsIcon'; import { ServicesIcon } from './icons/ServicesIcon'; import { MiscIcon } from './icons/MiscIcon'; +import { Submenu } from './Submenu'; export default { title: 'Layout/Sidebar', @@ -67,37 +68,33 @@ export const SampleScalableSidebar = () => ( - {}} - text="Catalog" - hasSubMenu - submenuTitle="Catalog" - > - - - - - - + {}} text="Catalog"> + + + + + + + +