Skip to content

Commit b2bde69

Browse files
committed
refactor: unify menu type definitions and improve naming consistency
- Replaced `ItemType` with custom type `MyMenu` in components - Renamed `menu.ts` to `menus.ts` for plural consistency - Introduced `types/menu.ts` to define `MyMenu` interface and conversion utility - Updated imports and variable names accordingly - Adjusted layout menu to use Ant Design's `items` property - Added `skipLibCheck` option in tsconfig for faster type checking Signed-off-by: Lei Wang <ssst0n3@gmail.com>
1 parent 78ae2e5 commit b2bde69

6 files changed

Lines changed: 45 additions & 23 deletions

File tree

src/components/index/Desc.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<script setup lang="ts">
2-
import {ItemType} from "ant-design-vue";
2+
import type {MyMenu} from "@/types/menu";
33
44
const props = defineProps<{
5-
menu: ItemType,
5+
menu: MyMenu,
66
}>();
77
</script>
88

src/components/index/Index.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<script setup lang="ts">
22
33
import ImageWall from "../ImageWall.vue";
4-
import {menu} from "@/data/menu";
4+
import {menus} from "@/data/menus";
55
import Desc from "@/components/index/Desc.vue";
66
</script>
77

@@ -17,7 +17,7 @@ import Desc from "@/components/index/Desc.vue";
1717
</span>
1818
</a-row>
1919
<a-row>
20-
<a-col v-for="item in menu" :key="'1' + item.key" :span="12" class="graph">
20+
<a-col v-for="item in menus" :key="'1' + item.key" :span="12" class="graph">
2121
<Desc :menu="item"/>
2222
</a-col>
2323
</a-row>

src/components/layout/Menu.vue

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import {inject} from "vue";
33
import {themes} from "@/theme";
44
import type {Theme} from "@/theme";
55
import {useRouter} from "vue-router";
6-
import {menu} from "@/data/menu"
7-
import {ItemType} from "ant-design-vue";
6+
import {antdMenus as menus} from "@/data/menus"
7+
import type {ItemType} from "ant-design-vue";
88
99
const theme = inject<Theme>('theme', themes.blue);
1010
const router = useRouter()
@@ -19,26 +19,17 @@ const go = (item: ItemType) => {
1919
:theme="{
2020
components: {
2121
Menu: {
22-
colorItemBg: theme.colorPrimary, // background color of the menu
23-
colorBgElevated: theme.colorPrimary, // background color of the second level menu
24-
colorItemTextHover: 'white', // hover color of the menu
25-
colorItemTextSelectedHorizontal: 'white', // hover color of the menu text underline
22+
colorItemBg: theme.colorPrimary, // background color of the menus
23+
colorBgElevated: theme.colorPrimary, // background color of the second level menus
24+
colorItemTextHover: 'white', // hover color of the menus
25+
colorItemTextSelectedHorizontal: 'white', // hover color of the menus text underline
2626
colorItemTextSelected: 'white',
2727
colorItemBgSelected: theme.colorPrimary,
2828
}
2929
},
3030
}"
3131
>
32-
<a-menu class="menu" mode="horizontal">
33-
<a-menu-item
34-
v-for="item in menu"
35-
:key="item.key"
36-
@click="() => go(item)"
37-
>
38-
<component :is="item.icon" style="margin-right:8px;"/>
39-
{{ item.label }}
40-
</a-menu-item>
41-
</a-menu>
32+
<a-menu :items="menus" class="menu" mode="horizontal" @click="go"/>
4233
</a-config-provider>
4334
</template>
4435

src/data/menu.ts renamed to src/data/menus.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import {BugOutlined, FlagOutlined, InfoCircleOutlined, SecurityScanOutlined} from "@ant-design/icons-vue";
2-
import {ItemType} from "ant-design-vue";
2+
import type {MyMenu} from "@/types/menu";
3+
import {MyMenu2AntdItemType} from "@/types/menu";
4+
import type {ItemType} from "ant-design-vue";
35

4-
const menu: ItemType = [
6+
const menus: MyMenu[] = [
57
{
68
label: 'Vulnerability',
79
key: 'vul',
@@ -28,4 +30,6 @@ const menu: ItemType = [
2830
}
2931
];
3032

31-
export {menu};
33+
const antdMenus: ItemType[] = menus.map(MyMenu2AntdItemType);
34+
35+
export {menus, antdMenus};

src/types/menu.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import type {FunctionalComponent} from "vue";
2+
import {h} from "vue";
3+
import type {AntdIconProps} from "@ant-design/icons-vue/lib/components/AntdIcon";
4+
import type {ItemType} from "ant-design-vue";
5+
6+
type IconType = FunctionalComponent<AntdIconProps>;
7+
8+
export interface MyMenu {
9+
index?: number;
10+
key: string;
11+
label: string;
12+
icon?: IconType;
13+
description?: string;
14+
children?: MyMenu[];
15+
}
16+
17+
export function MyMenu2AntdItemType(item: MyMenu): ItemType {
18+
const children = item.children?.map(child => MyMenu2AntdItemType(child));
19+
20+
return {
21+
label: item.label,
22+
key: item.key,
23+
icon: item.icon ? h(item.icon) : undefined,
24+
children: children,
25+
};
26+
}

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"files": [],
33
"compilerOptions": {
4+
"skipLibCheck": true,
45
"baseUrl": ".",
56
"paths": {
67
"@/*": ["src/*"]

0 commit comments

Comments
 (0)