Skip to content

Commit 086171c

Browse files
committed
refactor(CNavGroup): update nested nav behavior
1 parent 351bfc5 commit 086171c

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

src/components/nav/CNavGroup.ts

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { defineComponent, h, Transition, ref, RendererElement, onMounted } from 'vue'
1+
import { defineComponent, h, onMounted, onUpdated, ref, RendererElement, Transition } from 'vue'
22

33
const CNavGroup = defineComponent({
44
name: 'CNavGroup',
@@ -8,19 +8,44 @@ const CNavGroup = defineComponent({
88
*/
99
visible: {
1010
type: Boolean,
11+
default: false,
1112
required: false,
1213
},
1314
},
14-
setup(props, { slots }) {
15+
emits: ['visibleChange'],
16+
setup(props, { emit, slots }) {
1517
const visible = ref(props.visible)
1618
const navGroupRef = ref()
1719

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+
1834
onMounted(() => {
1935
props.visible && navGroupRef.value.classList.add('show')
2036
})
2137

38+
onUpdated(() => {
39+
visible.value = props.visible
40+
41+
if (visible.value === false) {
42+
visibleGroup.value = undefined
43+
}
44+
})
45+
2246
const handleTogglerClick = function () {
2347
visible.value = !visible.value
48+
emit('visibleChange', visible.value)
2449
}
2550
const handleBeforeEnter = (el: RendererElement) => {
2651
el.style.height = '0px'
@@ -86,7 +111,13 @@ const CNavGroup = defineComponent({
86111
{
87112
class: 'nav-group-items',
88113
},
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+
),
90121
),
91122
},
92123
),

0 commit comments

Comments
 (0)