Skip to content
This repository was archived by the owner on Jan 20, 2021. It is now read-only.

Commit dd20907

Browse files
author
Hoang Nguyen
authored
Handle redirect to sub menu when click parent menu (#876)
* Handle redirect to sub menu when click parent menu * renamed the function * allow click on menu for desktop * reset cache path when click menu item without submenu * Fixes click parent menu with full expanded * removed the expand submenu block segment
1 parent b334645 commit dd20907

1 file changed

Lines changed: 27 additions & 6 deletions

File tree

src/components/menu/menu.js

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ export default {
4747
return {
4848
openKeys: [],
4949
selectedKeys: [],
50-
cachedOpenKeys: []
50+
cachedOpenKeys: [],
51+
cachedPath: null
5152
}
5253
},
5354
computed: {
@@ -104,6 +105,7 @@ export default {
104105
})
105106
}
106107

108+
this.cachedPath = this.selectedKeys[0]
107109
this.collapsed ? (this.cachedOpenKeys = openKeys) : (this.openKeys = openKeys)
108110
},
109111

@@ -123,36 +125,55 @@ export default {
123125
return (
124126
<Item {...{ key: menu.path }}>
125127
<router-link {...{ props }}>
126-
{this.renderIcon(menu.meta.icon)}
128+
{this.renderIcon(menu.meta.icon, menu)}
127129
<span>{this.$t(menu.meta.title)}</span>
128130
</router-link>
129131
</Item>
130132
)
131133
},
132134
renderSubMenu (menu) {
133135
const itemArr = []
136+
const on = {
137+
click: () => {
138+
this.handleClickParentMenu(menu)
139+
}
140+
}
134141
if (!menu.hideChildrenInMenu) {
135142
menu.children.forEach(item => itemArr.push(this.renderItem(item)))
136143
}
137144
return (
138145
<SubMenu {...{ key: menu.path }}>
139146
<span slot="title">
140-
{this.renderIcon(menu.meta.icon)}
141-
<span>{this.$t(menu.meta.title)}</span>
147+
{this.renderIcon(menu.meta.icon, menu)}
148+
<span {...{ on: on }}>{this.$t(menu.meta.title)}</span>
142149
</span>
143150
{itemArr}
144151
</SubMenu>
145152
)
146153
},
147-
renderIcon (icon) {
154+
renderIcon (icon, menuItem) {
148155
if (icon === 'none' || icon === undefined) {
149156
return null
150157
}
151158
const props = {}
159+
const on = {
160+
click: () => {
161+
this.handleClickParentMenu(menuItem)
162+
}
163+
}
152164
typeof (icon) === 'object' ? props.component = icon : props.type = icon
153165
return (
154-
<Icon {... { props } }/>
166+
<Icon {... { props, on } } />
155167
)
168+
},
169+
handleClickParentMenu (menuItem) {
170+
if (this.cachedPath === menuItem.redirect) {
171+
return
172+
}
173+
if (menuItem.redirect) {
174+
this.cachedPath = menuItem.redirect
175+
setTimeout(() => this.$router.push({ path: menuItem.path }))
176+
}
156177
}
157178
},
158179
render () {

0 commit comments

Comments
 (0)