diff --git a/packages/unity-react-core/src/components/SidebarMenu/SidebarMenu.stories.tsx b/packages/unity-react-core/src/components/SidebarMenu/SidebarMenu.stories.tsx index b02c1d9d8d..1e344c90a2 100644 --- a/packages/unity-react-core/src/components/SidebarMenu/SidebarMenu.stories.tsx +++ b/packages/unity-react-core/src/components/SidebarMenu/SidebarMenu.stories.tsx @@ -20,6 +20,7 @@ const defaultProps = { { href: "https://example.com", text: "Link 2.1", + isActive: false, }, { href: "https://example.com", @@ -56,18 +57,33 @@ const defaultProps = { text: "Link 5.2", }, ], - } - ] -} + }, + ], +}; -const sidebarMenuTemplate = args =>
- -
; +const sidebarMenuTemplate = args => ( +
+ +
+); export const Overview = { render: sidebarMenuTemplate.bind({}), name: "Sidebar", args: { ...defaultProps, - } + }, +}; + +const defaultProps2 = JSON.parse(JSON.stringify(defaultProps)); +defaultProps2.links[0].text = "Link 1"; +defaultProps2.links[0].isActive = false; +defaultProps2.links[1].items[0].isActive = true; +defaultProps2.links[1].items[0].text = "Active Link"; + +export const WithNestedActivePage = { + render: sidebarMenuTemplate.bind({}), + args: { + ...defaultProps2, + }, }; diff --git a/packages/unity-react-core/src/components/SidebarMenu/SidebarMenu.test.tsx b/packages/unity-react-core/src/components/SidebarMenu/SidebarMenu.test.tsx index d1743fc28f..6830e37202 100644 --- a/packages/unity-react-core/src/components/SidebarMenu/SidebarMenu.test.tsx +++ b/packages/unity-react-core/src/components/SidebarMenu/SidebarMenu.test.tsx @@ -10,6 +10,7 @@ const defaultProps: SidebarProps = { { href: "https://example.com", text: "Link 1", + isActive: true, }, { text: "Link 2 dropdown", @@ -23,24 +24,39 @@ const defaultProps: SidebarProps = { text: "Link 2.2", }, ], - } - ] + }, + ], }; -const renderComponent = (props:SidebarProps) => { +const defaultProps2 = JSON.parse(JSON.stringify(defaultProps)); +delete defaultProps2.links[0].isActive; +defaultProps2.links[1].items[0].isActive = true; + +const renderComponent = (props: SidebarProps) => { return render(); }; describe("SidebarMenu tests", () => { - let component:RenderResult; + let component: RenderResult; + + afterEach(cleanup); - beforeEach(() => { + it("should have top level active link", () => { component = renderComponent(defaultProps); + expect(component.getByText("Link 1")).toHaveClass("is-active"); + expect(component.queryByText("Link 2 dropdown")).toHaveAttribute( + "aria-expanded", + "false" + ); }); - afterEach(cleanup); - - it("should define component", () => { - expect(component).toBeDefined(); + it("should have nested level active link", () => { + component = renderComponent(defaultProps2); + expect(component.getByText("Link 2.1")).toBeVisible(); + expect(component.getByText("Link 2 dropdown")).toHaveClass("is-active"); + expect(component.queryByText("Link 2 dropdown")).toHaveAttribute( + "aria-expanded", + "true" + ); }); }); diff --git a/packages/unity-react-core/src/components/SidebarMenu/SidebarMenu.tsx b/packages/unity-react-core/src/components/SidebarMenu/SidebarMenu.tsx index ae7d326397..6383c31fe3 100644 --- a/packages/unity-react-core/src/components/SidebarMenu/SidebarMenu.tsx +++ b/packages/unity-react-core/src/components/SidebarMenu/SidebarMenu.tsx @@ -1,16 +1,18 @@ import React from "react"; import { GaEventWrapper } from "../GaEventWrapper/GaEventWrapper"; +interface SidebarItemProps { + href: string; + text: string; + isActive?: boolean; +} + export interface Link { href?: string; text: string; isActive?: boolean; - items?: Array<{ - href: string; - text: string; - isActive?: boolean; - }>; -}; + items?: SidebarItemProps[]; +} const defaultGaProps = { name: "onclick", @@ -47,19 +49,22 @@ export const SidebarMenu: React.FC = ({ title, links }) => { {links.map((link, index) => { if (link.items) { // Render dropdown card + const isExpanded = link.items.some(({ isActive }) => isActive); return ( -
+
{item.text} @@ -88,7 +93,10 @@ export const SidebarMenu: React.FC = ({ title, links }) => { } else { // Render regular link return ( -
+