Skip to content

Commit bbd917d

Browse files
committed
feat: multi-tab content menu
1 parent 03d1233 commit bbd917d

File tree

1 file changed

+98
-1
lines changed

1 file changed

+98
-1
lines changed

src/components/MultiTab/MultiTab.vue

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1+
<!--
12
<template>
23
<div style="margin: -23px -24px 24px -24px">
4+
&lt;!&ndash;<a-dropdown :trigger="['contextmenu']" overlayClassName="multi-tab-menu-wrapper">
5+
<a-menu slot="overlay">
6+
<a-menu-item key="1">1st menu item</a-menu-item>
7+
<a-menu-item key="2">2nd menu item</a-menu-item>
8+
<a-menu-item key="3">3rd menu item</a-menu-item>
9+
</a-menu>
10+
</a-dropdown>&ndash;&gt;
311
<a-tabs
412
hideAdd
513
v-model="activeKey"
@@ -9,9 +17,13 @@
917
>
1018
<a-tab-pane v-for="page in pages" :style="{ height: 0 }" :tab="page.meta.title" :key="page.fullPath" :closable="pages.length > 1">
1119
</a-tab-pane>
20+
<template slot="renderTabBar" slot-scope="props, DefaultTabBar">
21+
<component :is="DefaultTabBar" {...props} />
22+
</template>
1223
</a-tabs>
1324
</div>
1425
</template>
26+
-->
1527

1628
<script>
1729
export default {
@@ -27,16 +39,81 @@ export default {
2739
created () {
2840
this.pages.push(this.$route)
2941
this.fullPathList.push(this.$route.fullPath)
42+
this.selectedLastPath()
3043
},
3144
methods: {
3245
onEdit (targetKey, action) {
46+
console.log('onEdit', targetKey, action)
3347
this[action](targetKey)
3448
},
3549
remove (targetKey) {
3650
this.pages = this.pages.filter(page => page.fullPath !== targetKey)
3751
this.fullPathList = this.fullPathList.filter(path => path !== targetKey)
3852
// 跳转到最后一个还存在的标签页
53+
this.selectedLastPath()
54+
},
55+
selectedLastPath () {
3956
this.activeKey = this.fullPathList[this.fullPathList.length - 1]
57+
},
58+
59+
// content menu
60+
closeThat (e) {
61+
console.log('close that', e)
62+
this.remove(e)
63+
},
64+
closeLeft (e) {
65+
// TODO
66+
console.log('close left', e)
67+
const index = this.fullPathList.indexOf(e)
68+
if (index > -1) {
69+
this.fullPathList.splice(index, -1)
70+
}
71+
},
72+
closeRight (e) {
73+
console.log('close right', e)
74+
},
75+
closeAll (e) {
76+
console.log('close all', e)
77+
},
78+
closeMenuClick ({ key, item, domEvent }) {
79+
console.log('key', key)
80+
console.log('item', item.$attrs['data-vkey'])
81+
const vkey = domEvent.target.getAttribute('data-vkey')
82+
switch (key) {
83+
case 'close-right':
84+
this.closeRight(vkey)
85+
break
86+
case 'close-left':
87+
this.closeLeft(vkey)
88+
break
89+
case 'close-all':
90+
this.closeAll(vkey)
91+
break
92+
default:
93+
case 'close-that':
94+
this.closeThat(vkey)
95+
break
96+
}
97+
},
98+
renderTabPaneMenu (e) {
99+
return (
100+
<a-menu {...{ on: { click: this.closeMenuClick } }}>
101+
<a-menu-item key="close-that" data-vkey={e}>关闭当前标签</a-menu-item>
102+
<a-menu-item key="close-right" data-vkey={e}>关闭右侧</a-menu-item>
103+
<a-menu-item key="close-left" data-vkey={e}>关闭左侧</a-menu-item>
104+
<a-menu-item key="close-all" data-vkey={e}>关闭全部</a-menu-item>
105+
</a-menu>
106+
)
107+
},
108+
// render
109+
renderTabPane (title, keyPath) {
110+
const menu = this.renderTabPaneMenu(keyPath)
111+
112+
return (
113+
<a-dropdown overlay={menu} trigger={['contextmenu']}>
114+
<span style={{ userSelect: 'none' }}>{ title }</span>
115+
</a-dropdown>
116+
)
40117
}
41118
},
42119
watch: {
@@ -48,8 +125,28 @@ export default {
48125
}
49126
},
50127
activeKey: function (newPathKey) {
128+
console.log('activeKey', newPathKey)
51129
this.$router.push({ path: newPathKey })
52130
}
131+
},
132+
render () {
133+
const { onEdit, $data: { pages } } = this
134+
const panes = pages.map(page => {
135+
return (<a-tab-pane style={{ height: 0 }} tab={this.renderTabPane(page.meta.title, page.fullPath)} key={page.fullPath} closable={pages.length > 1}></a-tab-pane>)
136+
})
137+
138+
return (
139+
<div style="margin: -23px -24px 24px -24px">
140+
<a-tabs
141+
hideAdd
142+
type={'editable-card'}
143+
v-model={this.activeKey}
144+
tabBarStyle={{ background: '#FFF', margin: 0, paddingLeft: '16px', paddingTop: '1px' }}
145+
{...{ on: { edit: onEdit } }}>
146+
{panes}
147+
</a-tabs>
148+
</div>
149+
)
53150
}
54151
}
55-
</script>
152+
</script>

0 commit comments

Comments
 (0)