Skip to content

Commit b1cc3d0

Browse files
authored
Merge pull request #504 from devforth/feature/AdminForth/1320/add-url-property-to-menu-item-
feat: add URL property to AdminForthConfigMenuItem and handle URL cli…
2 parents 6f37567 + c941abe commit b1cc3d0

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

adminforth/documentation/docs/tutorial/03-Customization/10-menuConfiguration.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,3 +278,23 @@ auth: {
278278
This syntax can be use to get unique avatar for each user of hardcode avatar, but it makes more sense to use it with [upload plugin](https://adminforth.dev/docs/tutorial/Plugins/upload/#using-plugin-for-uploading-avatar)
279279

280280

281+
## Custom URL
282+
You can use the url property to override default navigation. This is useful for linking to pre-filtered lists or external sites.
283+
284+
285+
```ts title='./index.ts'
286+
287+
menu: [
288+
{
289+
label: 'Posts',
290+
icon: 'flowbite:book-open-outline',
291+
resourceId: 'posts',
292+
//diff-add
293+
url: '/resource/aparts?filter__country__in=["DE"]',
294+
//diff-add
295+
isOpenInNewTab: true // You can also add isOpenInNewTab: true to open the link in a new browser tab
296+
},
297+
],
298+
299+
```
300+
> 👆 Please note start internal URLs with a leading / to ensure correct routing.

adminforth/spa/src/components/MenuLink.vue

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
<template>
22
<RouterLink
3-
:to="{name: item.resourceId ? 'resource-list' : item.path, params: item.resourceId ? { resourceId: item.resourceId }: {}}"
3+
:to="item.url || { name: item.resourceId ? 'resource-list' : item.path, params: item.resourceId ? { resourceId: item.resourceId }: {}}"
4+
:target="item.isOpenInNewTab ? '_blank' : '_self'"
45
class="af-menu-link flex group relative items-center w-full py-2 text-lightSidebarText dark:text-darkSidebarText rounded-default transition-all duration-200 ease-in-out"
56
:class="{
67
'hover:bg-lightSidebarItemHover hover:text-lightSidebarTextHover dark:hover:bg-darkSidebarItemHover dark:hover:text-darkSidebarTextHover active:bg-lightSidebarActive dark:active:bg-darkSidebarHover': !['divider', 'gap', 'heading'].includes(item.type),
78
'pl-6 pr-3.5': (isChild && !isSidebarIconOnly && !isSidebarHovering) || (isChild && isSidebarIconOnly && isSidebarHovering),
89
'px-3.5 ': !isChild || (isSidebarIconOnly && !isSidebarHovering),
910
'max-w-12': isSidebarIconOnly && !isSidebarHovering,
10-
'bg-lightSidebarItemActive dark:bg-darkSidebarItemActive': item.resourceId ?
11-
($route.params.resourceId === item.resourceId && $route.name === 'resource-list') :
12-
($route.name === item.path)
11+
'bg-lightSidebarItemActive dark:bg-darkSidebarItemActive': isItemActive(item)
1312
}"
1413
>
1514
<component v-if="item.icon" :is="getIcon(item.icon)"
@@ -61,6 +60,21 @@ import { Tooltip } from '@/afcl';
6160
import { ref, watch, computed } from 'vue';
6261
import { useCoreStore } from '@/stores/core';
6362
import { IconFileImageOutline } from '@iconify-prerendered/vue-flowbite';
63+
import { useRoute } from 'vue-router';
64+
65+
const route = useRoute();
66+
67+
const isItemActive = (item: any) => {
68+
if (item.url) {
69+
return route.fullPath === item.url;
70+
}
71+
72+
if (item.resourceId) {
73+
return route.params.resourceId === item.resourceId && route.name === 'resource-list';
74+
}
75+
76+
return route.name === item.path;
77+
};
6478
6579
const props = defineProps(['item', 'isChild', 'isSidebarIconOnly', 'isSidebarHovering']);
6680

adminforth/types/Common.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,6 +1122,8 @@ export interface AdminForthConfigMenuItem {
11221122
* Item id will be automatically generated from hashed resourceId+Path+label
11231123
*/
11241124
itemId?: string, // todo move to runtime type
1125+
1126+
url?: string
11251127
}
11261128

11271129

0 commit comments

Comments
 (0)