-
Notifications
You must be signed in to change notification settings - Fork 872
/
Tabs.vue
198 lines (193 loc) · 4.43 KB
/
Tabs.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<template>
<component :is="layoutComponent">
<template slot="nav">
<div class="nav-wrapper">
<ul class="nav"
role="tablist"
:class="
[type ? `nav-pills-${type}`: '',
pills ? 'nav-pills': 'nav-tabs',
{'nav-pills-icons': icons},
{'nav-fill': fill},
{'nav-pills-circle': circle},
{'justify-content-center': centered},
tabNavClasses
]">
<li v-for="tab in tabs"
class="nav-item"
:key="tab.id || tab.title">
<a data-toggle="tab"
role="tab"
class="nav-link"
:href="`#${tab.id || tab.title}`"
@click.prevent="activateTab(tab)"
:aria-selected="tab.active"
:class="{active: tab.active}">
<tab-item-content :tab="tab">
</tab-item-content>
</a>
</li>
</ul>
</div>
</template>
<div slot="content" class="tab-content"
:class="[tabContentClasses]">
<slot v-bind="slotData"></slot>
</div>
</component>
</template>
<script>
import PillsLayout from "./PillsLayout";
import TabsLayout from "./TabsLayout";
export default {
name: "tabs",
components: {
TabsLayout,
PillsLayout,
TabItemContent: {
props: ["tab"],
render(h) {
return h("div", [this.tab.$slots.title || this.tab.title]);
}
}
},
props: {
type: {
type: String,
default: "",
validator: value => {
let acceptedValues = [
"",
"primary",
"info",
"success",
"warning",
"danger"
];
return acceptedValues.indexOf(value) !== -1;
},
description: "Tabs type (primary|info|danger|default|warning|success)"
},
pills: {
type: Boolean,
default: true,
description: "Whether tabs are pills"
},
circle: {
type: Boolean,
default: false,
description: "Whether tabs are circle"
},
fill: {
type: Boolean,
default: true,
description: "Whether to fill each tab"
},
activeTab: {
type: String,
default: "",
description: "Default active tab name"
},
tabNavWrapperClasses: {
type: [String, Object],
default: "",
description: "Tab Nav wrapper (div) css classes"
},
tabNavClasses: {
type: [String, Object],
default: "",
description: "Tab Nav (ul) css classes"
},
tabContentClasses: {
type: [String, Object],
default: "",
description: "Tab content css classes"
},
icons: {
type: Boolean,
description: "Whether tabs should be of icon type (small no text)"
},
centered: {
type: Boolean,
description: "Whether tabs are centered"
},
value: {
type: String,
description: "Initial value (active tab)"
}
},
provide() {
return {
addTab: this.addTab,
removeTab: this.removeTab
};
},
data() {
return {
tabs: [],
activeTabIndex: 0
};
},
computed: {
layoutComponent() {
return this.pills ? "pills-layout" : "tabs-layout";
},
slotData() {
return {
activeTabIndex: this.activeTabIndex,
tabs: this.tabs
};
}
},
methods: {
findAndActivateTab(title) {
let tabToActivate = this.tabs.find(t => t.title === title);
if (tabToActivate) {
this.activateTab(tabToActivate);
}
},
activateTab(tab) {
if (this.handleClick) {
this.handleClick(tab);
}
this.deactivateTabs();
tab.active = true;
this.activeTabIndex = this.tabs.findIndex(t => t.active);
},
deactivateTabs() {
this.tabs.forEach(tab => {
tab.active = false;
});
},
addTab(tab) {
if (this.activeTab === tab.name) {
tab.active = true;
}
this.tabs.push(tab);
},
removeTab(tab) {
const tabs = this.tabs;
const index = tabs.indexOf(tab);
if (index > -1) {
tabs.splice(index, 1);
}
}
},
mounted() {
this.$nextTick(() => {
if (this.value) {
this.findAndActivateTab(this.value);
} else {
if (this.tabs.length > 0) {
this.activateTab(this.tabs[0]);
}
}
});
},
watch: {
value(newVal) {
this.findAndActivateTab(newVal);
}
}
};
</script>