Skip to content

Commit 261b0f5

Browse files
committed
refactor(AppSidebar): update navigation behavior
1 parent a8ad276 commit 261b0f5

File tree

1 file changed

+39
-34
lines changed

1 file changed

+39
-34
lines changed

src/components/AppSidebarNav.js

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,41 @@
1-
import { defineComponent, h, resolveComponent } from 'vue'
1+
import { defineComponent, h, onMounted, ref, resolveComponent } from 'vue'
22
import { RouterLink, useRoute } from 'vue-router'
33

44
import { CBadge, CSidebarNav, CNavItem, CNavGroup, CNavTitle } from '@coreui/vue'
55
import nav from '@/_nav.js'
66

7+
const normalizePath = (path) =>
8+
decodeURI(path)
9+
.replace(/#.*$/, '')
10+
.replace(/(index)?\.(html)$/, '')
11+
12+
const isActiveLink = (route, link) => {
13+
if (link === undefined) {
14+
return false
15+
}
16+
17+
if (route.hash === link) {
18+
return true
19+
}
20+
21+
const currentPath = normalizePath(route.path)
22+
const targetPath = normalizePath(link)
23+
24+
return currentPath === targetPath
25+
}
26+
27+
const isActiveItem = (route, item) => {
28+
if (isActiveLink(route, item.to)) {
29+
return true
30+
}
31+
32+
if (item.items) {
33+
return item.items.some((child) => isActiveItem(route, child))
34+
}
35+
36+
return false
37+
}
38+
739
const AppSidebarNav = defineComponent({
840
name: 'AppSidebarNav',
941
components: {
@@ -12,46 +44,19 @@ const AppSidebarNav = defineComponent({
1244
CNavTitle,
1345
},
1446
setup() {
15-
const normalizePath = (path) =>
16-
decodeURI(path)
17-
.replace(/#.*$/, '')
18-
.replace(/(index)?\.(html)$/, '')
19-
20-
const isActiveLink = (route, link) => {
21-
if (link === undefined) {
22-
return false
23-
}
24-
25-
if (route.hash === link) {
26-
return true
27-
}
28-
29-
const currentPath = normalizePath(route.path)
30-
const targetPath = normalizePath(link)
31-
32-
return currentPath === targetPath
33-
}
47+
const route = useRoute()
48+
const firstRender = ref(true)
3449

35-
const isActiveItem = (route, item) => {
36-
if (isActiveLink(route, item.to)) {
37-
return true
38-
}
39-
40-
if (item.items) {
41-
return item.items.some((child) => isActiveItem(route, child))
42-
}
43-
44-
return false
45-
}
50+
onMounted(() => {
51+
firstRender.value = false
52+
})
4653

4754
const renderItem = (item) => {
48-
const route = useRoute()
49-
5055
if (item.items) {
5156
return h(
5257
CNavGroup,
5358
{
54-
active: item.items.some((child) => isActiveItem(route, child)),
59+
...firstRender.value && { visible: item.items.some((child) => isActiveItem(route, child))}
5560
},
5661
{
5762
togglerContent: () => [

0 commit comments

Comments
 (0)