1- import React from "react" ;
2- import { PanelSideBarProvider , PanelSideBarLayout , PanelItem , PanelLinkRendererProps } from "react-pattern-ui" ;
1+ import React , { PropsWithChildren , ReactNode } from "react" ;
2+ import { PanelSideBarProvider , PanelSideBarLayout , PanelItem , PanelLinkRendererProps , usePanelSideBarContext } from "react-pattern-ui" ;
33import { faBars , faCogs } from "@fortawesome/free-solid-svg-icons" ;
44
55type AppRoutes = "home" | "settings" | "dropdownTest" | "dropdown-test1" | "dropdown-test2" ;
66type TSideBarMenuItem = PanelItem < AppRoutes > ;
77
88// Configuration object for avoiding duplicated code
9- interface PanelSideBarConfiguration {
9+ interface PanelSideBarConfiguration extends PropsWithChildren {
1010 renderFirstItemsLevelAsTiles ?: boolean ;
1111 useToggleButton ?: boolean ;
1212}
1313
1414const getPanelSidebarInternal = ( items : TSideBarMenuItem [ ] , config ?: PanelSideBarConfiguration ) => {
15- const { renderFirstItemsLevelAsTiles = true , useToggleButton = false } = config ?? { } ;
15+ const { renderFirstItemsLevelAsTiles = true , useToggleButton = false , children } = config ?? { } ;
1616 return (
1717 < PanelSideBarProvider
1818 menuItems = { items }
@@ -32,13 +32,13 @@ const getPanelSidebarInternal = (items: TSideBarMenuItem[], config?: PanelSideBa
3232 ) }
3333 >
3434 < PanelSideBarLayout useToggleButton = { useToggleButton } >
35- < div id = "pageContent" > Cypress</ div >
35+ < div id = "pageContent" > { children ?? " Cypress" } </ div >
3636 </ PanelSideBarLayout >
3737 </ PanelSideBarProvider >
3838 ) ;
3939} ;
4040
41- const getSidebarItems = ( active ?: boolean , disabled ?: boolean ) : TSideBarMenuItem [ ] => [
41+ const getSidebarItems = ( active ?: boolean , disabled ?: boolean , expanded ?: boolean ) : TSideBarMenuItem [ ] => [
4242 {
4343 id : "home" ,
4444 title : "Home" ,
@@ -65,6 +65,7 @@ const getSidebarItems = (active?: boolean, disabled?: boolean): TSideBarMenuItem
6565 {
6666 title : "Dropdown" ,
6767 id : "dropdownTest" ,
68+ expanded,
6869 children : [
6970 {
7071 title : "Dropdown test 1" ,
@@ -81,19 +82,24 @@ const getSidebarItems = (active?: boolean, disabled?: boolean): TSideBarMenuItem
8182 } ,
8283] ;
8384
84- const PanelSideBarWithTiles = ( props : { active ?: boolean ; disabled ?: boolean } ) => {
85- const { active, disabled } = props ;
86- return getPanelSidebarInternal ( getSidebarItems ( active , disabled ) ) ;
85+ interface PanelSideBarProps extends PropsWithChildren {
86+ active ?: boolean ;
87+ disabled ?: boolean ;
88+ expanded ?: boolean ;
89+ }
90+
91+ const PanelSideBarWithTiles = ( props : PanelSideBarProps ) => {
92+ const { active, disabled, expanded, children } = props ;
93+ return getPanelSidebarInternal ( getSidebarItems ( active , disabled , expanded ) , { children } ) ;
8794} ;
8895
89- const PanelSideBarNoTiles = ( props : { active ?: boolean ; disabled ?: boolean } ) => {
96+ const PanelSideBarNoTiles = ( props : PanelSideBarProps ) => {
9097 const { active, disabled } = props ;
9198 return getPanelSidebarInternal ( getSidebarItems ( active , disabled ) , { renderFirstItemsLevelAsTiles : false , useToggleButton : true } ) ;
9299} ;
93100
94101describe ( "PanelSidebar.cy.tsx" , ( ) => {
95102 it ( "icon and titles rendered correctly" , ( ) => {
96- const sidebarItems = getSidebarItems ( ) ;
97103 cy . mount ( < PanelSideBarWithTiles /> ) ;
98104
99105 // Check if icon are rendered
@@ -143,7 +149,6 @@ describe("PanelSidebar.cy.tsx", () => {
143149 } ) ;
144150
145151 it ( "toggle sidebar" , ( ) => {
146- const sidebarItems = getSidebarItems ( ) ;
147152 cy . mount ( < PanelSideBarWithTiles /> ) ;
148153
149154 // Check toggle sidebar
@@ -172,4 +177,73 @@ describe("PanelSidebar.cy.tsx", () => {
172177 cy . get ( "#sidebar-toggle" ) . click ( ) ;
173178 cy . get ( "#side-nav" ) . should ( "have.css" , "width" , "0px" ) ;
174179 } ) ;
180+
181+ it ( "check dropdown correctly pre-expanded" , ( ) => {
182+ cy . mount ( < PanelSideBarWithTiles expanded /> ) ;
183+ cy . get ( "button[title=Settings]" ) . click ( ) ;
184+ cy . get ( "#dropdown-test1" ) . should ( "be.visible" ) ;
185+ cy . get ( "#dropdown-test2" ) . should ( "be.visible" ) ;
186+ } ) ;
187+
188+ it ( "dynamically toggle menu item" , ( ) => {
189+ const Button = ( ) => {
190+ const { toggleMenuItem } = usePanelSideBarContext ( ) ;
191+ return (
192+ < button id = "test-toggle" onClick = { ( ) => toggleMenuItem ( "dropdownTest" ) } >
193+ Toggle
194+ </ button >
195+ ) ;
196+ } ;
197+
198+ cy . mount ( < PanelSideBarWithTiles expanded children = { < Button /> } /> ) ;
199+ cy . get ( "button[title=Settings]" ) . click ( ) ;
200+ cy . get ( "li:has(.dropdown-toggle)" ) . should ( "be.visible" ) . should ( "have.class" , "menu-open" ) ;
201+ cy . get ( "#test-toggle" ) . click ( ) ;
202+ cy . get ( "li:has(.dropdown-toggle)" ) . should ( "be.visible" ) . should ( "not.have.class" , "menu-open" ) ;
203+ cy . get ( "#test-toggle" ) . click ( ) ;
204+ cy . get ( "li:has(.dropdown-toggle)" ) . should ( "be.visible" ) . should ( "have.class" , "menu-open" ) ;
205+ } ) ;
206+
207+ it ( "dynamically hide menu items" , ( ) => {
208+ const Button = ( ) => {
209+ const { setHiddenMenuItemsIds } = usePanelSideBarContext ( ) ;
210+ return (
211+ < button id = "test-hide" onClick = { ( ) => setHiddenMenuItemsIds ( [ "dropdown-test1" , "dropdown-test2" ] ) } >
212+ Hide
213+ </ button >
214+ ) ;
215+ } ;
216+
217+ cy . mount ( < PanelSideBarWithTiles expanded children = { < Button /> } /> ) ;
218+ cy . get ( "button[title=Settings]" ) . click ( ) ;
219+ cy . get ( "#dropdown-test1" ) . should ( "be.visible" ) ;
220+ cy . get ( "#dropdown-test2" ) . should ( "be.visible" ) ;
221+ cy . get ( "#test-hide" ) . click ( ) ;
222+ cy . get ( "#dropdown-test1" ) . should ( "not.be.visible" ) ;
223+ cy . get ( "#dropdown-test2" ) . should ( "not.be.visible" ) ;
224+ } ) ;
225+
226+ it ( "dynamically open and close menu item" , ( ) => {
227+ const Button = ( ) => {
228+ const { openMenuItems, closeMenuItems } = usePanelSideBarContext ( ) ;
229+ return (
230+ < >
231+ < button id = "test-open-item" onClick = { ( ) => openMenuItems ( [ "dropdownTest" ] ) } >
232+ Open
233+ </ button >
234+ < button id = "test-close-item" onClick = { ( ) => closeMenuItems ( [ "dropdownTest" ] ) } >
235+ Close
236+ </ button >
237+ </ >
238+ ) ;
239+ } ;
240+
241+ cy . mount ( < PanelSideBarWithTiles children = { < Button /> } /> ) ;
242+ cy . get ( "button[title=Settings]" ) . click ( ) ;
243+ cy . get ( "li:has(.dropdown-toggle)" ) . should ( "be.visible" ) . should ( "not.have.class" , "menu-open" ) ;
244+ cy . get ( "#test-open-item" ) . click ( ) ;
245+ cy . get ( "li:has(.dropdown-toggle)" ) . should ( "be.visible" ) . should ( "have.class" , "menu-open" ) ;
246+ cy . get ( "#test-close-item" ) . click ( ) ;
247+ cy . get ( "li:has(.dropdown-toggle)" ) . should ( "be.visible" ) . should ( "not.have.class" , "menu-open" ) ;
248+ } ) ;
175249} ) ;
0 commit comments