Skip to content

feat(nav-menu): [nav-menu] 新增 icon 属性及icon slot,支持配置菜单图标 #3181

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions examples/sites/demos/apis/nav-menu.js
Original file line number Diff line number Diff line change
@@ -127,6 +127,16 @@ export default {
},
mode: ['pc'],
pcDemo: 'slot-toolbar'
},
{
name: 'icon',
defaultValue: '',
desc: {
'zh-CN': '自定义菜单图标插槽',
'en-US': 'Customize the menu icon'
},
mode: ['pc'],
pcDemo: 'menu-icon'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
]
}
@@ -153,6 +163,7 @@ interface IMenuItem {
interface IDataItem {
title: string
url: string
icon?: Commonent

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The type Commonent seems to be a typo. It should likely be Component. Please correct this to avoid potential runtime errors.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix typo in IDataItem interface

There's a typo in the type definition for the icon property - "Commonent" should be "Component".

-  icon?: Commonent 
+  icon?: Component 
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
icon?: Commonent
icon?: Component

children?: IDataItem[]
}`
},
150 changes: 150 additions & 0 deletions examples/sites/demos/pc/app/nav-menu/menu-icon-composition-api.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
<template>
<div class="preview">
<tiny-nav-menu :data="menuData"></tiny-nav-menu>
</div>
</template>

<script setup lang="jsx">
import { ref } from 'vue'
import { TinyNavMenu } from '@opentiny/vue'
import { iconTotal, iconGenerating } from '@opentiny/vue-icon'

const menuData = ref([
{
title: '首页',
url: '',
id: '1',
icon: iconTotal()
},
{
title: '指南',
url: '',
id: '2',
children: [
{
title: '引入组件',
url: '',
id: '2-1',
icon: iconTotal()
},
{
title: '后端适配器',
url: '',
id: '2-2'
}
]
},
{
title: '组件',
url: '',
id: '3',
children: [
{
title: '表单组件',
url: '',
id: '3-1',
children: [
{
title: 'Datepicker 日期',
url: 'datepicker',
id: '3-1-1',
icon: iconTotal()
},
{
title: 'Cascader 级联选择器',
url: 'cascader',
id: '3-1-2',
icon: iconGenerating()
},
{
title: 'DropTimes 下拉时间',
url: 'droptimes',
id: '3-1-3'
}
]
},
{
title: '数据展示',
url: '',
id: '3-2',
children: [
{
title: 'Card 卡片',
url: 'card',
id: '3-2-1'
},
{
title: 'Collapse 折叠面板',
url: 'collapse',
id: '3-2-2'
},
{
title: 'Guide 引导',
url: 'guide',
id: '3-2-3'
}
]
},
{
title: '导航组件',
url: '',
id: '3-3',
children: [
{
title: 'ToggleMenu 收缩菜单',
url: 'toggleMenu',
id: '3-3-1'
},
{
title: 'TreeMenu 树型菜单',
url: 'treemenu',
id: '3-3-2'
},
{
title: 'Breadcrumb 面包屑',
url: 'breadcrumb',
id: '3-3-3'
}
]
},
{
title: '业务组件',
url: '',
id: '3-4',
children: [
{
title: 'Amount 金额',
url: 'amount',
id: '3-4-1'
},
{
title: 'Area 片区',
url: 'area',
id: '3-4-2'
},
{
title: 'Company 公司',
url: 'company',
id: '3-4-3'
}
]
}
]
},
{
title: '其他',
url: 'crop',
id: '4'
}
])
</script>

<style scoped>
.preview {
min-height: 450px;
}

.preview .tiny-nav-menu a:hover {
text-decoration: none;
}
</style>
11 changes: 11 additions & 0 deletions examples/sites/demos/pc/app/nav-menu/menu-icon.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { test, expect } from '@playwright/test'

test('菜单图标', async ({ page }) => {
page.on('pageerror', (exception) => expect(exception).toBeNull())
await page.goto('nav-menu#menu-icon')
const preview = page.locator('#menu-icon')
const icon = preview.locator('.menu-icon')
const iconSvg = preview.locator('.menu-icon svg')
await expect(icon).toBeVisible()
await expect(iconSvg).toBeVisible()
})
173 changes: 173 additions & 0 deletions examples/sites/demos/pc/app/nav-menu/menu-icon.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
<template>
<div class="preview">
<h4>配置模式</h4>
<tiny-nav-menu :data="menuData" overflow="fixed"> </tiny-nav-menu>

<h4>slot 模式</h4>
<tiny-nav-menu :data="menuData" overflow="fixed">
<template #icon="{ selected, index, item }">
<IconGenerating v-if="selected" />
<IconTotal v-else />
</template>
</tiny-nav-menu>
</div>
</template>

<script lang="jsx">
import { TinyNavMenu } from '@opentiny/vue'
import { iconTotal, iconGenerating } from '@opentiny/vue-icon'

export default {
components: {
TinyNavMenu,
IconGenerating: iconGenerating(),
IconTotal: iconTotal()
},
data() {
return {
menuData: [
{
title: '首页',
url: '',
id: '1',
icon: iconTotal()
},
{
title: '指南',
url: '',
id: '2',
children: [
{
title: '引入组件',
url: '',
id: '2-1',
icon: iconTotal()
},
{
title: '后端适配器',
url: '',
id: '2-2'
}
]
},
{
title: '组件',
url: '',
id: '3',
children: [
{
title: '表单组件',
url: '',
id: '3-1',
children: [
{
title: 'Datepicker 日期',
url: 'datepicker',
id: '3-1-1',
icon: iconTotal()
},
{
title: 'Cascader 级联选择器',
url: 'cascader',
id: '3-1-2',
icon: iconGenerating()
},
{
title: 'DropTimes 下拉时间',
url: 'droptimes',
id: '3-1-3'
}
]
},
{
title: '数据展示',
url: '',
id: '3-2',
children: [
{
title: 'Card 卡片',
url: 'card',
id: '3-2-1'
},
{
title: 'Collapse 折叠面板',
url: 'collapse',
id: '3-2-2'
},
{
title: 'Guide 引导',
url: 'guide',
id: '3-2-3'
}
]
},
{
title: '导航组件',
url: '',
id: '3-3',
children: [
{
title: 'ToggleMenu 收缩菜单',
url: 'toggleMenu',
id: '3-3-1'
},
{
title: 'TreeMenu 树型菜单',
url: 'treemenu',
id: '3-3-2'
},
{
title: 'Breadcrumb 面包屑',
url: 'breadcrumb',
id: '3-3-3'
}
]
},
{
title: '业务组件',
url: '',
id: '3-4',
children: [
{
title: 'Amount 金额',
url: 'amount',
id: '3-4-1'
},
{
title: 'Area 片区',
url: 'area',
id: '3-4-2'
},
{
title: 'Company 公司',
url: 'company',
id: '3-4-3'
}
]
}
]
},
{
title: '其他',
url: 'crop',
id: '4'
}
]
}
}
}
</script>

<style scoped>
.preview {
min-height: 450px;
}

.preview > .tiny-nav-menu {
margin-bottom: 24px;
}

.preview .tiny-nav-menu a:hover {
text-decoration: none;
}
</style>
12 changes: 12 additions & 0 deletions examples/sites/demos/pc/app/nav-menu/webdoc/nav-menu.js
Original file line number Diff line number Diff line change
@@ -54,6 +54,18 @@ export default {
},
codeFiles: ['slot-logo.vue']
},
{
demoId: 'menu-icon',
name: {
'zh-CN': '菜单图标',
'en-US': 'Menu Icon'
},
desc: {
'zh-CN': '通过 <code>icon</code> 字段配置展示菜单图标。',
'en-US': 'Display menu icon through <code>icon</code> field of data.'
},
codeFiles: ['menu-icon.vue']
},
{
demoId: 'before-skip',
name: {
5 changes: 3 additions & 2 deletions packages/renderless/src/nav-menu/index.ts
Original file line number Diff line number Diff line change
@@ -169,7 +169,8 @@ export const initData =
id: item.id,
pid: item.pid,
isFullUrl: props.allowFullUrl && isFullUrl(router),
target: item.target
target: item.target,
icon: item.icon
}
}

@@ -577,7 +578,7 @@ export const handleTitleMouseenter =
const text = target.textContent
const font = window.getComputedStyle(target).font
const rect = target.getBoundingClientRect()
const res = omitText(text, font, rect.width + 2)
const res = omitText(text, font, rect.width + 4)

if (target && res.o) {
const tooltip = vm.$refs.tooltip
1 change: 1 addition & 0 deletions packages/renderless/types/nav-menu.type.ts
Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@ export interface menuItemType {
url: string
children?: menuItemType[]
target?: string
icon?: any
}

export interface whitchSubMenuType {
89 changes: 85 additions & 4 deletions packages/theme/src/nav-menu/index.less
Original file line number Diff line number Diff line change
@@ -59,18 +59,37 @@
line-height: var(--tv-NavMenu-height);
box-sizing: border-box;

.menu-icon {
display: inline-block;

svg {
vertical-align: text-bottom;
margin-right: var(--tv-NavMenu-icon-margin-right);
font-size: var(--tv-NavMenu-icon-size);
fill: var(--tv-NavMenu-icon-color);
}
}

&.active,
&:hover {
// 规范选中后,背景色修改
color: var(--tv-NavMenu-item-text-color);
text-decoration: none;
font-weight: var(--tv-NavMenu-item-text-font-weight);

svg {
fill: var(--tv-NavMenu-icon-color-hover);
}
}

&.selected {
position: relative;
font-weight: var(--tv-NavMenu-item-text-font-weight);

svg {
fill: var(--tv-NavMenu-icon-color-hover);
}

&:after {
content: ' ';
width: 100%;
@@ -179,6 +198,10 @@
&.selected {
position: relative;

svg {
fill: var(--tv-NavMenu-icon-color-hover) !important;
}

&:before {
content: '';
width: var(--tv-NavMenu-popmenu-more-item-before-width);
@@ -206,6 +229,17 @@
overflow: hidden;
text-overflow: ellipsis;

.menu-icon {
display: inline-block;

svg {
vertical-align: text-bottom;
margin-right: var(--tv-NavMenu-icon-margin-right);
font-size: var(--tv-NavMenu-icon-size);
fill: var(--tv-NavMenu-icon-color);
}
}

&.showicon {
width: calc(100% - 12px);
}
@@ -253,11 +287,26 @@
overflow: hidden;
text-overflow: ellipsis;
flex: 1;

.menu-icon {
display: inline-block;

svg {
vertical-align: text-bottom;
margin-right: var(--tv-NavMenu-icon-margin-right);
font-size: var(--tv-NavMenu-icon-size);
fill: var(--tv-NavMenu-icon-color);
}
}
}

> span.selected,
> a.selected {
color: var(--tv-NavMenu-popmenu-selected-text-color);

svg {
fill: var(--tv-NavMenu-icon-color-hover);
}
}

&:only-child {
@@ -285,7 +334,18 @@

&:hover {
color: var(--tv-NavMenu-popmenu-text-color);
text-decoration: underline;

> a:after,
> span:after {
content: '';
position: absolute;
left: 0;
bottom: 0;
width: 100%;
height: 1px;
background: var(--tv-NavMenu-item-selected-underline-bg-color);
z-index: 1;
}
}

&.active,
@@ -302,6 +362,18 @@
text-decoration: none;
font-size: var(--tv-NavMenu-popmenu-text-font-size);
cursor: pointer;
position: relative;

.menu-icon {
display: inline-block;

svg {
vertical-align: text-bottom;
margin-right: var(--tv-NavMenu-icon-margin-right);
font-size: var(--tv-NavMenu-icon-size);
fill: var(--tv-NavMenu-icon-color);
}
}

&.selected {
color: var(--tv-NavMenu-popmenu-selected-text-color);
@@ -317,15 +389,20 @@
background: var(--tv-NavMenu-item-selected-underline-bg-color);
z-index: 1;
}
}

&:hover {
color: var(--tv-NavMenu-popmenu-text-color);
svg {
fill: var(--tv-NavMenu-icon-color-hover);
}
}

&:hover,
&.active,
&:active {
color: var(--tv-NavMenu-popmenu-text-color);

svg {
fill: var(--tv-NavMenu-icon-color-hover);
}
}
}

@@ -401,6 +478,10 @@
float: right;
margin-left: 10px;
}

svg {
vertical-align: text-bottom;
}
}

> .slot-mobile-menu {
8 changes: 8 additions & 0 deletions packages/theme/src/nav-menu/vars.less
Original file line number Diff line number Diff line change
@@ -81,4 +81,12 @@
--tv-NavMenu-slot-logo-icon-color: var(--tv-color-icon-hover, #191919);
// 自定义 Logo大小
--tv-NavMenu-slot-logo-icon-size: var(--tv-icon-size, 16px);
// 导航菜单图标右侧边距
--tv-NavMenu-icon-margin-right: var(--tv-space-base);
// 导航菜单图标大小
--tv-NavMenu-icon-size: var(--tv-icon-size);
// 导航菜单图标色
--tv-NavMenu-icon-color: var(--tv-color-icon);
// 导航菜单图标悬浮色
--tv-NavMenu-icon-color-hover: var(--tv-color-icon-hover);
}
51 changes: 48 additions & 3 deletions packages/vue/src/nav-menu/__tests__/nav-menu.test.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { IconTotal } from '@opentiny/vue-icon'
import { IconTotal, IconShare } from '@opentiny/vue-icon'
import NavMenu from '@opentiny/vue-nav-menu'
import { describe, expect, test } from 'vitest'
import { mountPcMode } from '@opentiny-internal/vue-test-utils'

describe('PC Mode', () => {
const mount = mountPcMode

const IconTotalComponent = IconTotal()
const IconShareComponent = IconShare()

const navMenuMockData = [
{
title: '首页',
@@ -200,8 +203,18 @@ describe('PC Mode', () => {
]
}
]

const IconTotalComponent = IconTotal()
const navMenuMockDataWithIcon = [
{
title: '首页',
url: '',
icon: IconShareComponent
},
{
title: '导航',
url: '',
icon: ''
}
]

/**
* attrs
@@ -213,6 +226,12 @@ describe('PC Mode', () => {
expect(navMenu.vm.state.data.length).toBe(3)
})

test('menu icon', async () => {
const wrapper = mount(() => <NavMenu data={navMenuMockDataWithIcon}></NavMenu>)
const iconCom = wrapper.findComponent({ name: 'TinyIconShare' })
expect(iconCom.vm).toBeTruthy()
})

test.todo(
'overflow 设置一级菜单无法在当前菜单容器里显示完全时的处理策略。可选项有 auto / retract / fixed / hidden。默认为 auto'
)
@@ -237,4 +256,30 @@ describe('PC Mode', () => {
const iconTotalSvg = wrapper.find('.slot-logo')
expect(iconTotalSvg.exists()).toBeTruthy()
})

test('icon slot', async () => {
const wrapper = mount(() => (
<NavMenu data={navMenuMockDataWithIcon}>
{{
icon: () => <IconTotalComponent></IconTotalComponent>
}}
</NavMenu>
))
const menuIconDom = wrapper.find('.menu-icon')
expect(menuIconDom.exists()).toBeTruthy()
})

test('slot prioity higher than icon attr', async () => {
const wrapper = mount(() => (
<NavMenu data={navMenuMockDataWithIcon}>
{{
icon: () => <IconTotalComponent class="icon-total"></IconTotalComponent>
}}
</NavMenu>
))
const iconTotalSvg = wrapper.findComponent({ name: 'TinyIconTotal' })
const iconShareSvg = wrapper.findComponent({ name: 'TinyIconShare' })
expect(iconTotalSvg.vm).toBeTruthy()
expect(iconShareSvg.exists()).toBe(false)
})
})
28 changes: 27 additions & 1 deletion packages/vue/src/nav-menu/src/pc.vue
Original file line number Diff line number Diff line change
@@ -36,8 +36,14 @@
@mouseenter="showSubMenu(item.children, { index }, $event)"
@mouseleave="willHideSubMenu"
@click="clickMenu(item, index)"
>{{ item.title }}</component
>
<div v-if="slots.icon || item.icon" class="menu-icon">
<slot name="icon" :item="item" :index="index" :selected="getTabSelected(item, index)">
<component :is="item.icon" class="tiny-svg-size tiny-nav-menu__icon" />
</slot>
</div>
{{ item.title }}
</component>
</li>
</ul>
</div>
@@ -85,6 +91,11 @@
@mouseleave="leaveMoreMune"
@click="clickMenu(item, index)"
>
<div v-if="slots.icon || item.icon" class="menu-icon">
<slot name="icon" :item="item" :index="index" :selected="getLeftSelected(item, index)">
<component :is="item.icon" class="tiny-svg-size tiny-nav-menu__icon" />
</slot>
</div>
{{ item.title }}
</component>
<icon-chevron-right v-if="item.children" class="more-icon"></icon-chevron-right>
@@ -115,6 +126,16 @@
@mouseleave="handleTitleMouseleave"
:class="{ selected: index === state.subIndex && state.subItemSelectedIndex === -1 }"
>
<div v-if="slots.icon || group.icon" class="menu-icon">
<slot
name="icon"
:item="group"
:index="index"
:selected="index === state.subIndex && state.subItemSelectedIndex === -1"
>
<component :is="group.icon" class="tiny-svg-size tiny-nav-menu__icon" />
</slot>
</div>
{{ group.title }}
</component>
<icon-chevron-right v-if="group.url" class="go-to-icon"></icon-chevron-right>
@@ -136,6 +157,11 @@
selected: getLastChildSelected(item, i, index)
}"
>
<div v-if="slots.icon || item.icon" class="menu-icon">
<slot name="icon" :item="item" :index="index" :selected="getLastChildSelected(item, i, index)">
<component :is="item.icon" class="tiny-svg-size tiny-nav-menu__icon" />
</slot>
</div>
{{ item.title }}
</component>