Skip to content

Commit

Permalink
feat: ✨ menu-header 和 menu-side 组件使用 jsx 重写 render
Browse files Browse the repository at this point in the history
  • Loading branch information
FairyEver committed Jun 8, 2020
1 parent 93414eb commit 0e54179
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 77 deletions.
68 changes: 41 additions & 27 deletions src/layout/header-aside/components/libs/util.menu.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,46 @@
// 创建 el-menu-item
/**
* @description 创建菜单
* @param {Function} h createElement
* @param {Object} menu 菜单项
*/
export function elMenuItem (h, menu) {
return h('el-menu-item', { key: menu.path, props: { index: menu.path } }, [
...menu.icon ? [
h('i', { attrs: { class: `fa fa-${menu.icon}` } })
] : [],
...menu.icon === undefined & !menu.iconSvg ? [
h('i', { attrs: { class: 'fa fa-file-o' } })
] : [],
...menu.iconSvg ? [
h('d2-icon-svg', { props: { name: menu.iconSvg } })
] : [],
h('span', { slot: 'title' }, menu.title || '未命名菜单')
])
let icon = null
if (menu.icon) icon = <i class={ `fa fa-${menu.icon}` }/>
else if (menu.iconSvg) icon = <d2-icon-svg name={ menu.iconSvg }/>
else icon = <i class="fa fa-file-o"/>
return <el-menu-item
key={ menu.path }
index={ menu.path }>
{ icon }
<span slot="title">{ menu.title || '未命名菜单' }</span>
</el-menu-item>
}

// 创建 el-submenu
/**
* @description 创建子菜单
* @param {Function} h createElement
* @param {Object} menu 菜单项
*/
export function elSubmenu (h, menu) {
return h('el-submenu', { key: menu.path, props: { index: menu.path } }, [
...menu.icon ? [
h('i', { slot: 'title', attrs: { class: `fa fa-${menu.icon}` } })
] : [],
...menu.icon === undefined & !menu.iconSvg ? [
h('i', { slot: 'title', attrs: { class: 'fa fa-folder-o' } })
] : [],
...menu.iconSvg ? [
h('d2-icon-svg', { slot: 'title', props: { name: menu.iconSvg } })
] : [],
h('span', { slot: 'title' }, menu.title || '未命名菜单'),
...menu.children.map((child, childIndex) => (child.children === undefined ? elMenuItem : elSubmenu).call(this, h, child))
])
let icon = null
if (menu.icon) icon = <i slot="title" class={ `fa fa-${menu.icon}` }/>
else if (menu.iconSvg) icon = <d2-icon-svg slot="title" name={ menu.iconSvg }/>
else icon = <i slot="title" class="fa fa-folder-o"/>
return <el-submenu
key={ menu.path }
index={ menu.path }>
{ icon }
<span slot="title">{ menu.title || '未命名菜单' }</span>
{ menu.children.map(child => createMenu.call(this, h, child)) }
</el-submenu>
}

/**
* @description 在组件中调用此方法渲染菜单项目
* @param {Function} h createElement
* @param {Object} menu 菜单项
*/
export function createMenu (h, menu) {
if (menu.children === undefined) return elMenuItem.call(this, h, menu)
return elSubmenu.call(this, h, menu)
}
80 changes: 44 additions & 36 deletions src/layout/header-aside/components/menu-header/index.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,57 @@
import { throttle } from 'lodash'
import { mapState } from 'vuex'
import menuMixin from '../mixin/menu'
import { elMenuItem, elSubmenu } from '../libs/util.menu'
import { createMenu } from '../libs/util.menu'

export default {
name: 'd2-layout-header-aside-menu-header',
mixins: [
menuMixin
],
render (h) {
return h('div', {
attrs: { flex: 'cross:center' },
class: { 'd2-theme-header-menu': true, 'is-scrollable': this.isScroll },
ref: 'page'
}, [
h('div', {
attrs: { class: 'd2-theme-header-menu__content', flex: '', 'flex-box': '1' },
ref: 'content'
}, [
h('div', {
attrs: { class: 'd2-theme-header-menu__scroll', 'flex-box': '0' },
style: { transform: `translateX(${this.currentTranslateX}px)` },
ref: 'scroll'
}, [
h('el-menu', {
props: { mode: 'horizontal', defaultActive: this.active },
on: { select: this.handleMenuSelect }
}, this.header.map(menu => (menu.children === undefined ? elMenuItem : elSubmenu).call(this, h, menu)))
])
]),
...this.isScroll ? [
h('div', {
attrs: { class: 'd2-theme-header-menu__prev', flex: 'main:center cross:center', 'flex-box': '0' },
on: { click: () => this.scroll('left') }
}, [
h('i', { attrs: { class: 'el-icon-arrow-left' } })
]),
h('div', {
attrs: { class: 'd2-theme-header-menu__next', flex: 'main:center cross:center', 'flex-box': '0' },
on: { click: () => this.scroll('right') }
}, [
h('i', { attrs: { class: 'el-icon-arrow-right' } })
])
] : []
])
return <div
flex="cross:center"
class={ { 'd2-theme-header-menu': true, 'is-scrollable': this.isScroll } }
ref="page">
<div
ref="content"
class="d2-theme-header-menu__content"
flex-box="1"
flex>
<div
class="d2-theme-header-menu__scroll"
flex-box="0"
style={ { transform: `translateX(${this.currentTranslateX}px)` } }
ref="scroll">
<el-menu
mode="horizontal"
defaultActive={ this.active }
onSelect={ this.handleMenuSelect }>
{ this.header.map(menu => createMenu.call(this, h, menu)) }
</el-menu>
</div>
</div>
{
this.isScroll
? [
<div
class="d2-theme-header-menu__prev"
flex="main:center cross:center"
flex-box="0"
onClick={ () => this.scroll('left') }>
<i class="el-icon-arrow-left"></i>
</div>,
<div
class="d2-theme-header-menu__next"
flex="main:center cross:center"
flex-box="0"
onClick={ () => this.scroll('right') }>
<i class="el-icon-arrow-right"></i>
</div>
]
: []
}
</div>
},
computed: {
...mapState('d2admin/menu', [
Expand Down
34 changes: 20 additions & 14 deletions src/layout/header-aside/components/menu-side/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { mapState } from 'vuex'
import menuMixin from '../mixin/menu'
import { elMenuItem, elSubmenu } from '../libs/util.menu'
import { createMenu } from '../libs/util.menu'
import BScroll from 'better-scroll'

export default {
Expand All @@ -9,19 +9,25 @@ export default {
menuMixin
],
render (h) {
return h('div', { attrs: { class: 'd2-layout-header-aside-menu-side' } }, [
h('el-menu', {
props: { collapse: this.asideCollapse, collapseTransition: this.asideTransition, uniqueOpened: true, defaultActive: this.$route.fullPath },
ref: 'menu',
on: { select: this.handleMenuSelect }
}, this.aside.map(menu => (menu.children === undefined ? elMenuItem : elSubmenu).call(this, h, menu))),
...this.aside.length === 0 && !this.asideCollapse ? [
h('div', { attrs: { class: 'd2-layout-header-aside-menu-empty', flex: 'dir:top main:center cross:center' } }, [
h('d2-icon', { props: { name: 'inbox' } }),
h('span', {}, '没有侧栏菜单')
])
] : []
])
return <div class="d2-layout-header-aside-menu-side">
<el-menu
collapse={ this.asideCollapse }
collapseTransition={ this.asideTransition }
uniqueOpened={ true }
defaultActive={ this.$route.fullPath }
ref="menu"
onSelect={ this.handleMenuSelect }>
{ this.aside.map(menu => createMenu.call(this, h, menu)) }
</el-menu>
{
this.aside.length === 0 && !this.asideCollapse
? <div class="d2-layout-header-aside-menu-empty" flex="dir:top main:center cross:center">
<d2-icon name="inbox"></d2-icon>
<span>没有侧栏菜单</span>
</div>
: null
}
</div>
},
data () {
return {
Expand Down

1 comment on commit 0e54179

@siitry
Copy link

@siitry siitry commented on 0e54179 Jun 9, 2020

Choose a reason for hiding this comment

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

6

Please sign in to comment.