/
Tab.vue
94 lines (91 loc) · 2.08 KB
/
Tab.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<template>
<div class="tab-pane" :class="{fade: transition > 0}" role="tabpanel">
<slot></slot>
<portal :to="_uid.toString()">
<slot name="title"/>
</portal>
</div>
</template>
<script>
import {spliceIfExist} from '../../utils/arrayUtils'
import {addClass, removeClass} from '../../utils/domUtils'
import {Portal} from 'portal-vue'
const ACTIVE_CLASS = 'active'
const IN_CLASS = 'in'
export default {
components: {Portal},
props: {
title: {
type: String,
default: 'Tab Title'
},
htmlTitle: {
type: Boolean,
default: false
},
disabled: {
type: Boolean,
default: false
},
'tab-classes': {
type: Object,
default: () => { return {} }
},
group: String,
pullRight: {
type: Boolean,
default: false
},
hidden: {
type: Boolean,
default: false
}
},
data () {
return {
active: true,
transition: 150
}
},
watch: {
active (active) {
if (active) {
setTimeout(() => {
addClass(this.$el, ACTIVE_CLASS)
this.$el.offsetHeight
addClass(this.$el, IN_CLASS)
try {
this.$parent.$emit('after-change', this.$parent.activeIndex)
} catch (e) {
throw new Error('<tab> parent must be <tabs>.')
}
}, this.transition)
} else {
removeClass(this.$el, IN_CLASS)
setTimeout(() => {
removeClass(this.$el, ACTIVE_CLASS)
}, this.transition)
}
}
},
created () {
try {
this.$parent.tabs.push(this)
} catch (e) {
throw new Error('<tab> parent must be <tabs>.')
}
},
beforeDestroy () {
let tabs = this.$parent && this.$parent.tabs
spliceIfExist(tabs, this)
},
methods: {
show () {
this.$nextTick(() => {
addClass(this.$el, ACTIVE_CLASS)
addClass(this.$el, IN_CLASS)
})
}
}
}
</script>