1- import { h } from 'vue'
1+ import { defineComponent , h , computed , onMounted , ref } from 'vue'
22import type { VNode } from 'vue'
33import { RouterLink , useRoute } from 'vue-router'
44import type { RouteLocationNormalizedLoaded } from 'vue-router'
@@ -41,81 +41,91 @@ const isActiveItem = (route: RouteLocationNormalizedLoaded, item: ResolvedSideba
4141 return false
4242}
4343
44- const renderItem = ( item : ResolvedSidebarItem ) : VNode => {
45- const route = useRoute ( )
46- if ( item . children && ! item . link . includes ( '.html' ) ) {
47- return h (
48- CNavGroup ,
49- {
50- active : item . children . some ( ( child ) => isActiveItem ( route , child ) ) ,
51- compact : true ,
52- } ,
53- {
54- togglerContent : ( ) => [
55- h ( CIcon , {
56- customClassName : 'nav-icon text-primary' ,
57- icon : [ '512 512' , item . icon ] ,
58- height : 64 ,
59- width : 64 ,
60- } ) ,
61- item . text ,
62- ] ,
63- default : ( ) => item . children . map ( ( child ) => renderItem ( child ) ) ,
64- } ,
65- )
66- }
67-
68- return h (
69- RouterLink ,
70- {
71- to : item . link ,
72- custom : true ,
44+ const SidebarNav = defineComponent ( {
45+ name : 'SidebarNav' ,
46+ props : {
47+ items : {
48+ type : Array ,
49+ required : true ,
7350 } ,
74- {
75- default : ( props ) =>
76- h (
77- CNavItem ,
51+ } ,
52+ setup ( props ) {
53+ const route = useRoute ( )
54+ const firstRender = ref ( true )
55+
56+ onMounted ( ( ) => {
57+ firstRender . value = false
58+ } )
59+
60+ const renderItem = ( item : ResolvedSidebarItem ) : VNode => {
61+ if ( item . children && ! item . link . includes ( '.html' ) ) {
62+ const visible = computed ( ( ) => item . children . some ( ( child ) => isActiveItem ( route , child ) ) )
63+
64+ return h (
65+ CNavGroup ,
7866 {
79- active : props . isActive ,
80- disabled : item . disabled ,
81- href : withBase ( item . link ) ,
67+ ...( firstRender . value && { visible : visible . value } ) ,
68+ compact : true ,
8269 } ,
8370 {
84- default : ( ) => [
71+ togglerContent : ( ) => [
72+ h ( CIcon , {
73+ customClassName : 'nav-icon text-primary' ,
74+ icon : [ '512 512' , item . icon ] ,
75+ height : 64 ,
76+ width : 64 ,
77+ } ) ,
8578 item . text ,
86- item . badge &&
87- h (
88- CBadge ,
89- {
90- class : 'ms-auto' ,
91- color : item . badge . color ,
92- } ,
93- {
94- default : ( ) => item . badge . text ,
95- } ,
96- ) ,
9779 ] ,
80+ default : ( ) => item . children . map ( ( child ) => renderItem ( child ) ) ,
9881 } ,
99- ) ,
100- } ,
101- )
102- }
82+ )
83+ }
10384
104- export const SidebarNav = ( { items } ) => {
105- return h (
106- CSidebarNav ,
107- { } ,
108- {
109- default : ( ) => items . map ( ( item ) => renderItem ( item ) ) ,
110- } ,
111- )
112- }
85+ return h (
86+ RouterLink ,
87+ {
88+ to : item . link ,
89+ custom : true ,
90+ } ,
91+ {
92+ default : ( props ) =>
93+ h (
94+ CNavItem ,
95+ {
96+ active : props . isActive ,
97+ disabled : item . disabled ,
98+ href : withBase ( item . link ) ,
99+ } ,
100+ {
101+ default : ( ) => [
102+ item . text ,
103+ item . badge &&
104+ h (
105+ CBadge ,
106+ {
107+ class : 'ms-auto' ,
108+ color : item . badge . color ,
109+ } ,
110+ {
111+ default : ( ) => item . badge . text ,
112+ } ,
113+ ) ,
114+ ] ,
115+ } ,
116+ ) ,
117+ } ,
118+ )
119+ }
113120
114- SidebarNav . displayName = 'SidebarNav'
121+ return ( ) => h (
122+ CSidebarNav ,
123+ { } ,
124+ {
125+ default : ( ) => props . items . map ( ( item : any ) => renderItem ( item ) ) ,
126+ }
127+ )
128+ } ,
129+ } )
115130
116- SidebarNav . props = {
117- items : {
118- type : Array ,
119- required : true ,
120- } ,
121- }
131+ export { SidebarNav }
0 commit comments