Skip to content

Commit 3f8a759

Browse files
committed
#4 Support async validation functions
1 parent ac05291 commit 3f8a759

File tree

6 files changed

+66
-16
lines changed

6 files changed

+66
-16
lines changed

dev/App.vue

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<template>
22
<div>
3-
<vue-tabs class="card">
3+
<vue-tabs class="card" @on-error="handleError">
44
<!--<template slot="tab" scope="props">
55
<li :class="{active:props.tab.active}" >
66
<a href="" @click.prevent="props.clickHandler(props.index)" style="border-radius:50%">
77
{{props.tab.title}}
88
</a>
99
</li>
1010
</template>-->
11-
<v-tab title="First tab" icon="ti-bag">
11+
<v-tab title="First tab" icon="ti-bag" :before-change="validateFirstTab">
1212
<div>adsdaasd</div>
1313
<div>adsdaasd</div>
1414
<div>adsdaasd</div>
@@ -24,6 +24,7 @@
2424
Third tab
2525
</v-tab>
2626

27+
<div v-if="error">{{error}}</div>
2728
</vue-tabs>
2829
</div>
2930
</template>
@@ -32,9 +33,19 @@
3233
3334
export default {
3435
name: 'app',
36+
data(){
37+
return {
38+
error: '',
39+
}
40+
},
3541
methods: {
36-
onComplete () {
37-
alert('Yay!')
42+
handleError(error){
43+
this.error = error
44+
},
45+
validateFirstTab(){
46+
return new Promise((resolve, reject) => {
47+
resolve(true)
48+
})
3849
}
3950
}
4051
}

dist/[object Object]

Lines changed: 0 additions & 1 deletion
This file was deleted.

dist/vue-tabs.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/TabContent.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
},
3737
data () {
3838
return {
39-
active: false
39+
active: false,
40+
validationError: null
4041
}
4142
}
4243
}

src/components/VueTabs.vue

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,17 +110,59 @@
110110
},
111111
methods: {
112112
navigateToTab (index) {
113-
if (this.beforeTabChange(this.activeTabIndex)) {
113+
this.beforeTabChange(this.activeTabIndex, () => {
114114
this.changeTab(this.activeTabIndex, index)
115+
})
116+
},
117+
setLoading (value) {
118+
this.loading = value
119+
this.$emit('on-loading', value)
120+
},
121+
setValidationError (tab, error) {
122+
this.tabs[this.activeTabIndex].validationError = error
123+
this.$emit('on-error', error)
124+
if (error && tab.$emit) {
125+
tab.$emit('on-error', error)
115126
}
116127
},
117-
118-
beforeTabChange (index) {
128+
validateBeforeChange (promiseFn, tab, callback) {
129+
this.setValidationError(tab, null)
130+
// we have a promise
131+
if (promiseFn.then && typeof promiseFn.then === 'function') {
132+
this.setLoading(true)
133+
promiseFn.then((res) => {
134+
this.setLoading(false)
135+
let validationResult = res === true
136+
this.executeBeforeChange(validationResult, callback)
137+
}).catch((error) => {
138+
this.setLoading(false)
139+
this.setValidationError(tab, error)
140+
})
141+
// we have a simple function
142+
} else {
143+
let validationResult = promiseFn === true
144+
this.executeBeforeChange(validationResult, callback)
145+
}
146+
},
147+
executeBeforeChange (validationResult, callback) {
148+
this.$emit('on-validate', validationResult, this.activeTabIndex)
149+
if (validationResult) {
150+
callback()
151+
} else {
152+
this.tabs[this.activeTabIndex].validationError = 'error'
153+
}
154+
},
155+
beforeTabChange (index, callback) {
156+
if (this.loading) {
157+
return
158+
}
119159
let oldTab = this.tabs[index]
120160
if (oldTab && oldTab.beforeChange !== undefined) {
121-
return oldTab.beforeChange()
161+
let tabChangeRes = oldTab.beforeChange()
162+
this.validateBeforeChange(tabChangeRes, oldTab, callback)
163+
} else {
164+
callback()
122165
}
123-
return true
124166
},
125167
changeTab (oldIndex, newIndex) {
126168
if (newIndex < 0 || newIndex >= this.tabCount) {

webpack.build.config.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,7 @@ module.exports = [
7676
raw: true
7777
}),*/
7878
new ExtractTextPlugin({filename:"vue-tabs.min.css", allChunks: true, fallback:"style-loader" }),
79-
new StatsPlugin( {filename:"./stats.json",
80-
chunkModules: true
81-
//exclude: [/node_modules[\\\/]react/]
82-
}),
79+
new StatsPlugin('stats.json'),
8380
],
8481

8582
module: {

0 commit comments

Comments
 (0)