Skip to content

Commit 0f75650

Browse files
committed
feat: add demo for modify menu badge data
1 parent f6faeb0 commit 0f75650

File tree

5 files changed

+135
-2
lines changed

5 files changed

+135
-2
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export { default as MenuBadge } from './components/menu-badge.vue';
12
export * from './components/normal-menu';
23
export { default as Menu } from './menu.vue';
34
export type * from './types';

packages/stores/src/modules/access.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,25 @@ interface AccessState {
4141
*/
4242
export const useAccessStore = defineStore('core-access', {
4343
actions: {
44+
getMenuByPath(path: string) {
45+
function findMenu(
46+
menus: MenuRecordRaw[],
47+
path: string,
48+
): MenuRecordRaw | undefined {
49+
for (const menu of menus) {
50+
if (menu.path === path) {
51+
return menu;
52+
}
53+
if (menu.children) {
54+
const matched = findMenu(menu.children, path);
55+
if (matched) {
56+
return matched;
57+
}
58+
}
59+
}
60+
}
61+
return findMenu(this.accessMenus, path);
62+
},
4463
setAccessCodes(codes: string[]) {
4564
this.accessCodes = codes;
4665
},

playground/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
},
3131
"dependencies": {
3232
"@tanstack/vue-query": "catalog:",
33+
"@vben-core/menu-ui": "workspace:*",
3334
"@vben/access": "workspace:*",
3435
"@vben/common-ui": "workspace:*",
3536
"@vben/constants": "workspace:*",
Lines changed: 111 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,116 @@
11
<script lang="ts" setup>
2-
import { Fallback } from '@vben/common-ui';
2+
import { reactive } from 'vue';
3+
import { useRoute } from 'vue-router';
4+
5+
import { Page } from '@vben/common-ui';
6+
import { useAccessStore } from '@vben/stores';
7+
import { MenuBadge } from '@vben-core/menu-ui';
8+
9+
import { Button, Card, Radio, RadioGroup } from 'ant-design-vue';
10+
11+
import { useVbenForm } from '#/adapter/form';
12+
13+
const colors = [
14+
{ label: '预设:默认', value: 'default' },
15+
{ label: '预设:关键', value: 'destructive' },
16+
{ label: '预设:主要', value: 'primary' },
17+
{ label: '预设:成功', value: 'success' },
18+
{ label: '自定义', value: 'bg-gray-200 text-black' },
19+
];
20+
21+
const route = useRoute();
22+
const accessStore = useAccessStore();
23+
const menu = accessStore.getMenuByPath(route.path);
24+
const badgeProps = reactive({
25+
badge: menu?.badge as string,
26+
badgeType: menu?.badge ? 'normal' : (menu?.badgeType as 'dot' | 'normal'),
27+
badgeVariants: menu?.badgeVariants as string,
28+
});
29+
30+
const [Form] = useVbenForm({
31+
handleValuesChange(values) {
32+
badgeProps.badge = values.badge;
33+
badgeProps.badgeType = values.badgeType;
34+
badgeProps.badgeVariants = values.badgeVariants;
35+
},
36+
schema: [
37+
{
38+
component: 'RadioGroup',
39+
componentProps: {
40+
buttonStyle: 'solid',
41+
options: [
42+
{ label: '点徽标', value: 'dot' },
43+
{ label: '文字徽标', value: 'normal' },
44+
],
45+
optionType: 'button',
46+
},
47+
defaultValue: badgeProps.badgeType,
48+
fieldName: 'badgeType',
49+
label: '类型',
50+
},
51+
{
52+
component: 'Input',
53+
componentProps: {
54+
maxLength: 4,
55+
placeholder: '请输入徽标内容',
56+
style: { width: '200px' },
57+
},
58+
defaultValue: badgeProps.badge,
59+
fieldName: 'badge',
60+
label: '徽标内容',
61+
},
62+
{
63+
component: 'RadioGroup',
64+
defaultValue: badgeProps.badgeVariants,
65+
fieldName: 'badgeVariants',
66+
label: '颜色',
67+
},
68+
{
69+
component: 'Input',
70+
fieldName: 'action',
71+
},
72+
],
73+
showDefaultActions: false,
74+
});
75+
76+
function updateMenuBadge() {
77+
if (menu) {
78+
menu.badge = badgeProps.badge;
79+
menu.badgeType = badgeProps.badgeType;
80+
menu.badgeVariants = badgeProps.badgeVariants;
81+
}
82+
}
383
</script>
484

585
<template>
6-
<Fallback description="用于徽标示例" status="coming-soon" title="徽标示例" />
86+
<Page
87+
description="菜单项上可以显示徽标,这些徽标可以主动更新"
88+
title="菜单徽标"
89+
>
90+
<Card title="徽标更新">
91+
<Form>
92+
<template #badgeVariants="slotProps">
93+
<RadioGroup v-bind="slotProps">
94+
<Radio
95+
v-for="color in colors"
96+
:key="color.value"
97+
:value="color.value"
98+
>
99+
<div
100+
:title="color.label"
101+
class="flex h-[14px] w-[50px] items-center justify-start"
102+
>
103+
<MenuBadge
104+
v-bind="{ ...badgeProps, badgeVariants: color.value }"
105+
/>
106+
</div>
107+
</Radio>
108+
</RadioGroup>
109+
</template>
110+
<template #action>
111+
<Button type="primary" @click="updateMenuBadge">更新徽标</Button>
112+
</template>
113+
</Form>
114+
</Card>
115+
</Page>
7116
</template>

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)