Skip to content

Commit ff0838f

Browse files
committed
feat(tab-bar): support value prop
1 parent 99fd215 commit ff0838f

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

src/components/tab-bar/tab-bar.vue

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
<cube-tab
55
v-for="(item, index) in data"
66
:label="item.label"
7+
:value="item.value"
78
:icon="item.icon"
8-
:key="item.label">
9+
:key="item.value || item.label">
910
</cube-tab>
1011
</slot>
1112
<div v-if="showSlider" ref="slider" class="cube-tab-bar-slider"></div>
@@ -54,6 +55,11 @@
5455
default: true
5556
}
5657
},
58+
watch: {
59+
value () {
60+
this._updateSliderStyle()
61+
}
62+
},
5763
created () {
5864
this.tabs = []
5965
},
@@ -81,14 +87,14 @@
8187
const index = this.tabs.indexOf(tab)
8288
if (index > -1) this.tabs.splice(index, 1)
8389
},
84-
trigger (label) {
90+
trigger (value) {
8591
// emit click event as long as tab is clicked
86-
this.$emit(EVENT_CLICK, label)
92+
this.$emit(EVENT_CLICK, value)
8793
// only when value changed, emit change & input event
88-
if (label !== this.value) {
94+
if (value !== this.value) {
8995
const changedEvents = [EVENT_INPUT, EVENT_CHANGE]
9096
changedEvents.forEach((eventType) => {
91-
this.$emit(eventType, label)
97+
this.$emit(eventType, value)
9298
})
9399
}
94100
},
@@ -116,7 +122,7 @@
116122
let width = 0
117123
let index = 0
118124
if (this.tabs.length > 0) {
119-
index = findIndex(this.tabs, (tab) => tab.label === this.value)
125+
index = findIndex(this.tabs, (tab) => tab.value === this.value)
120126
width = this.tabs[index].$el.clientWidth
121127
}
122128
return {
@@ -141,11 +147,6 @@
141147
clearTimeout(this._resizeTimer)
142148
window.removeEventListener('resize', this._resizeHandler)
143149
}
144-
},
145-
watch: {
146-
value () {
147-
this._updateSliderStyle()
148-
}
149150
}
150151
}
151152
</script>

src/components/tab-bar/tab.vue

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@
2121
type: [String, Number],
2222
required: true
2323
},
24+
value: {
25+
type: [String, Number],
26+
default() {
27+
return this.label
28+
}
29+
},
2430
icon: {
2531
type: String,
2632
default: ''
@@ -34,12 +40,12 @@
3440
},
3541
computed: {
3642
isActive () {
37-
return this.$parent.value === this.label
43+
return this.$parent.value === this.value
3844
}
3945
},
4046
methods: {
4147
handleClick (item) {
42-
this.$parent.trigger(this.label)
48+
this.$parent.trigger(this.value)
4349
}
4450
}
4551
}

0 commit comments

Comments
 (0)