Skip to content

Commit

Permalink
feat: ✨ added router hook
Browse files Browse the repository at this point in the history
  • Loading branch information
Aydawka committed Nov 13, 2023
1 parent cd0eb0e commit 3dab10a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/components/sidebar/AppSidebar.vue
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<script setup lang="ts">
import { Icon } from "@iconify/vue";
import type { MenuOption } from "naive-ui";
import type { MenuInst, MenuOption } from "naive-ui";
import { NLayoutSider, NMenu, NSpace } from "naive-ui";
import { computed, h } from "vue";
// import {ref} from "vue/dist/vue";
import { RouterLink, useRoute, useRouter } from "vue-router";
import { useSidebarStore } from "@/stores/sidebar";
Expand Down Expand Up @@ -380,6 +381,20 @@ const defaultExpandedKeys = computed(() => {
}
return [];
});
const selectedKeyRef = ref("1");
const menuInstRef = ref<MenuInst | null>(null);
const selectAndExpand = (key: string) => {
selectedKeyRef.value = key;
menuInstRef.value?.showOption(key);
};
router.beforeEach((to, from) => {
if (typeof to.name !== "string") return;
const name: string = to.meta && to.meta.menuItem ? (to.meta.menuItem as string) : to.name;
selectAndExpand(name);
});
</script>

<template>
Expand All @@ -397,6 +412,8 @@ const defaultExpandedKeys = computed(() => {
<n-space vertical justify="space-between" class="h-full">
<div class="flex flex-col justify-start divide-y">
<n-menu
ref="menuInstRef"
v-model:value="selectedKeyRef"
:collapsed-width="64"
:collapsed-icon-size="22"
:default-expanded-keys="defaultExpandedKeys"
Expand All @@ -407,6 +424,8 @@ const defaultExpandedKeys = computed(() => {
</div>

<n-menu
ref="menuInstRef"
v-model:value="selectedKeyRef"
:collapsed-width="64"
:collapsed-icon-size="22"
:collapsed="sidebarCollapsed"
Expand Down
3 changes: 3 additions & 0 deletions src/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ const router = createRouter({
name: "studies:new-study",
path: "new",
component: NewStudy,
meta: { menuItem: "studies:all-studies" },
},
],
},
Expand Down Expand Up @@ -140,6 +141,7 @@ const router = createRouter({
name: "study:add-participant",
path: "add",
component: AddParticipant,
meta: { menuItem: "study:participants" },
},
],
},
Expand Down Expand Up @@ -170,6 +172,7 @@ const router = createRouter({
name: "dataset:new",
path: "new",
component: NewDataset,
meta: { menuItem: "study:all-datasets" },
},
],
},
Expand Down

0 comments on commit 3dab10a

Please sign in to comment.