99 class =" sidebar-group-items"
1010 v-if =" open || !collapsable"
1111 >
12- <li v-for =" child in item?.children" >
13- <SidebarSectionHeader
14- v-if =" child ?.type === ' section-header' "
15- :item =" child "
16- />
17- <SidebarLink
18- v-else
19- :closeSidebarDrawer =" closeSidebarDrawer "
20- :item =" child "
21- />
22- </li >
12+ <template v-for =" (section , sIdx ) in sections " :key =" sIdx " >
13+ <template v-if =" section .header " >
14+ <li >
15+ <SidebarSectionHeader
16+ :item =" section .header "
17+ :collapsible =" true "
18+ :expanded =" ! collapsedSections [section .key ]"
19+ @toggle =" toggleSection (section .key )"
20+ />
21+ <DropdownTransition >
22+ <ul
23+ v-if =" !collapsedSections[section.key]"
24+ class =" sidebar-section-items"
25+ >
26+ <li v-for =" (child, cIdx) in section.items" :key =" cIdx" >
27+ <SidebarLink
28+ :closeSidebarDrawer =" closeSidebarDrawer "
29+ :item =" child "
30+ />
31+ </li >
32+ </ul >
33+ </DropdownTransition >
34+ </li >
35+ </template >
36+ <template v-else >
37+ <li v-for =" (child, cIdx) in section.items" :key =" cIdx" >
38+ <SidebarLink
39+ :closeSidebarDrawer =" closeSidebarDrawer "
40+ :item =" child "
41+ />
42+ </li >
43+ </template >
44+ </template >
2345 </ul >
2446 </DropdownTransition >
2547 </div >
2648</template >
2749
2850<script setup>
51+ import { computed , reactive } from ' vue'
2952import SidebarLink from ' ./SidebarLink.vue'
3053import SidebarSectionHeader from ' ./SidebarSectionHeader.vue'
3154import DropdownTransition from " ../components/DropdownTransition.vue" ;
@@ -52,6 +75,28 @@ const props = defineProps({
5275 default : () => {}
5376 }
5477})
78+
79+ const sections = computed (() => {
80+ const children = props .item ? .children || []
81+ const result = []
82+ let current = { header: null , items: [], key: ' __leading__' }
83+ for (const child of children) {
84+ if (child? .type === ' section-header' ) {
85+ if (current .header || current .items .length ) result .push (current)
86+ current = { header: child, items: [], key: child .title || ` section-${ result .length } ` }
87+ } else {
88+ current .items .push (child)
89+ }
90+ }
91+ if (current .header || current .items .length ) result .push (current)
92+ return result
93+ })
94+
95+ const collapsedSections = reactive ({})
96+
97+ const toggleSection = (key ) => {
98+ collapsedSections[key] = ! collapsedSections[key]
99+ }
55100< / script>
56101
57102< style lang= " stylus" >
@@ -95,4 +140,10 @@ const props = defineProps({
95140.sidebar - group- items
96141 transition height .1s ease- out
97142 overflow hidden
143+
144+ .sidebar - section- items
145+ padding 0
146+ margin 0
147+ list- style- type none
148+ overflow hidden
98149< / style>
0 commit comments