1
- import { defineComponent , h , Transition , ref , RendererElement , onMounted } from 'vue'
1
+ import { defineComponent , h , onMounted , onUpdated , ref , RendererElement , Transition } from 'vue'
2
2
3
3
const CNavGroup = defineComponent ( {
4
4
name : 'CNavGroup' ,
@@ -8,19 +8,44 @@ const CNavGroup = defineComponent({
8
8
*/
9
9
visible : {
10
10
type : Boolean ,
11
+ default : false ,
11
12
required : false ,
12
13
} ,
13
14
} ,
14
- setup ( props , { slots } ) {
15
+ emits : [ 'visibleChange' ] ,
16
+ setup ( props , { emit, slots } ) {
15
17
const visible = ref ( props . visible )
16
18
const navGroupRef = ref ( )
17
19
20
+ const visibleGroup = ref ( )
21
+
22
+ const handleVisibleChange = ( visible : boolean , index : number ) => {
23
+ if ( visible ) {
24
+ visibleGroup . value = index
25
+ } else {
26
+ if ( visibleGroup . value === index ) {
27
+ visibleGroup . value = 0
28
+ }
29
+ }
30
+ }
31
+
32
+ const isVisible = ( index : number ) => Boolean ( visibleGroup . value === index )
33
+
18
34
onMounted ( ( ) => {
19
35
props . visible && navGroupRef . value . classList . add ( 'show' )
20
36
} )
21
37
38
+ onUpdated ( ( ) => {
39
+ visible . value = props . visible
40
+
41
+ if ( visible . value === false ) {
42
+ visibleGroup . value = undefined
43
+ }
44
+ } )
45
+
22
46
const handleTogglerClick = function ( ) {
23
47
visible . value = ! visible . value
48
+ emit ( 'visibleChange' , visible . value )
24
49
}
25
50
const handleBeforeEnter = ( el : RendererElement ) => {
26
51
el . style . height = '0px'
@@ -86,7 +111,13 @@ const CNavGroup = defineComponent({
86
111
{
87
112
class : 'nav-group-items' ,
88
113
} ,
89
- slots . default && slots . default ( ) ,
114
+ slots . default &&
115
+ slots . default ( ) . map ( ( vnode , index ) =>
116
+ h ( vnode , {
117
+ onVisibleChange : ( visible : boolean ) => handleVisibleChange ( visible , index ) ,
118
+ visible : isVisible ( index ) ,
119
+ } ) ,
120
+ ) ,
90
121
) ,
91
122
} ,
92
123
) ,
0 commit comments