33from __future__ import annotations
44
55import reflex as rx
6- import reflex_chakra as rc
76from pcweb .components .docpage .navbar .state import NavbarState
87from .state import SidebarState , SideBarItem , SideBarBase
98
@@ -28,14 +27,15 @@ def sidebar_link(*children, **props):
2827
2928
3029def sidebar_leaf (
30+ item_index : str ,
3131 item : SideBarItem ,
3232 url : str ,
3333) -> rx .Component :
3434 """Get the leaf node of the sidebar."""
3535 item .link = item .link .replace ("_" , "-" ).rstrip ("/" ) + "/"
3636 return (
37- rx .el . li (
38- rc . accordion_item (
37+ rx .accordion . item (
38+ rx . accordion . header (
3939 sidebar_link (
4040 rx .flex (
4141 rx .text (
@@ -55,13 +55,14 @@ def sidebar_leaf(
5555 ),
5656 href = item .link ,
5757 ),
58- border = "none" ,
59- width = "100%" ,
60- )
58+ ),
59+ value = item_index ,
60+ border = "none" ,
61+ width = "100%" ,
6162 )
6263 if item .outer
63- else rx .el . li (
64- rc . accordion_item (
64+ else rx .accordion . item (
65+ rx . accordion . header (
6566 rx .cond (
6667 item .link == url ,
6768 sidebar_link (
@@ -87,9 +88,10 @@ def sidebar_leaf(
8788 href = item .link ,
8889 ),
8990 ),
90- border = "none" ,
91- width = "100%" ,
92- )
91+ ),
92+ border = "none" ,
93+ value = item_index ,
94+ width = "100%" ,
9395 )
9496 )
9597
@@ -128,42 +130,49 @@ def sidebar_icon(name):
128130
129131
130132def sidebar_item_comp (
133+ item_index : str ,
131134 item : SideBarItem ,
132135 index : list [int ],
133136 url : str ,
134137):
135- # print(index)
136- return rx .cond (
137- not item .children ,
138- sidebar_leaf (item = item , url = url ),
139- rc .accordion_item (
140- rc .accordion_button (
141- sidebar_icon (item .names ),
142- rx .text (
143- item .names ,
144- class_name = "font-small" ,
138+ index = rx .Var .create (index )
139+ return (
140+ sidebar_leaf (item_index = item_index , item = item , url = url )
141+ if not item .children
142+ else rx .accordion .item (
143+ rx .accordion .header (
144+ rx .accordion .trigger (
145+ sidebar_icon (item .names ),
146+ rx .text (
147+ item .names ,
148+ class_name = "font-small" ,
149+ ),
150+ rx .box (class_name = "flex-grow" ),
151+ rx .accordion .icon (class_name = "size-4" ),
152+ class_name = "flex items-center !bg-transparent !hover:bg-transparent !py-2 !pr-0 !pl-2 w-full text-slate-9 aria-expanded:text-slate-11 hover:text-slate-11 transition-color" ,
145153 ),
146- rx .box (class_name = "flex-grow" ),
147- rc .accordion_icon (class_name = "size-4" ),
148- class_name = "items-center !bg-transparent !hover:bg-transparent !py-2 !pr-0 !pl-2 w-full text-slate-9 aria-expanded:text-slate-11 hover:text-slate-11 transition-color" ,
149154 ),
150- rc .accordion_panel (
151- rc .accordion (
152- rx .el .ul (
153- * [
154- sidebar_item_comp (child , index , url )
155- for child in item .children
156- ],
157- class_name = "flex flex-col items-start gap-4 !ml-[15px] list-none [box-shadow:inset_1.25px_0_0_0_var(--c-slate-4)]" ,
158- ),
159- allow_multiple = True ,
160- default_index = index [1 :2 ] if index else [],
161- class_name = "!my-2" ,
155+ rx .accordion .content (
156+ rx .accordion .root (
157+ * [
158+ sidebar_item_comp (
159+ item_index = "index" + str (child_index ),
160+ item = child ,
161+ index = index [1 :],
162+ url = url ,
163+ )
164+ for child_index , child in enumerate (item .children )
165+ ],
166+ type = "multiple" ,
167+ collapsible = True ,
168+ default_value = index [:1 ].foreach (lambda x : "index" + x .to_string ()),
169+ class_name = "!my-2 flex flex-col items-start gap-4 !ml-[15px] list-none [box-shadow:inset_1.25px_0_0_0_var(--c-slate-4)]" ,
162170 ),
163171 class_name = "!p-0 w-full" ,
164172 ),
173+ value = item_index ,
165174 class_name = "border-none w-full" ,
166- ),
175+ )
167176 )
168177
169178
@@ -304,17 +313,19 @@ def create_sidebar_section(
304313 href = section_url ,
305314 class_name = "py-3" ,
306315 ),
307- rc .accordion (
316+ rx .accordion . root (
308317 * [
309318 sidebar_item_comp (
319+ item_index = "index" + str (item_index ),
310320 item = item ,
311- index = index if nested else [- 1 ],
321+ index = index [ 1 :] if nested else [],
312322 url = url ,
313323 )
314- for item in items
324+ for item_index , item in enumerate ( items )
315325 ],
316- allow_multiple = True ,
317- default_index = rx .cond (index , index , []),
326+ type = "multiple" ,
327+ collapsible = True ,
328+ default_value = index [:1 ].foreach (lambda x : "index" + x .to_string ()),
318329 class_name = "ml-0 pl-0 w-full" ,
319330 ),
320331 class_name = "flex flex-col items-start ml-0 w-full" ,
0 commit comments