2
0
mirror of https://github.com/tenrok/vue-form-wizard.git synced 2026-06-24 09:50:33 +03:00

#1 Handle promise based before-change functions.

This commit is contained in:
cristi
2017-04-23 17:02:01 +03:00
parent a16c4051fe
commit 1a9e86c9c1
2 changed files with 59 additions and 49 deletions
+6 -17
View File
@@ -5,32 +5,18 @@
color="#e74c3c" color="#e74c3c"
class="card"> class="card">
<tab-content title="Personal details" <tab-content title="Personal details"
:before-change="validate" :before-change="validateAsync"
icon="ti-user"> icon="ti-user">
My first tab content My first tab content
</tab-content> </tab-content>
<tab-content title="Additional Info" <tab-content title="Additional Info"
:before-change="validate"
icon="ti-settings"> icon="ti-settings">
My second tab content My second tab content
</tab-content> </tab-content>
<tab-content title="Last step" <tab-content title="Last step"
icon="ti-check"> icon="ti-check">
Yuhuuu! This seems pretty damn simple Yuhuuu! This seems pretty damn simple
</tab-content> </tab-content>
</form-wizard> </form-wizard>
@@ -45,12 +31,15 @@
onComplete () { onComplete () {
alert('Yay!') alert('Yay!')
}, },
validate () { validateAsync () {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
setTimeout(() => { setTimeout(() => {
resolve(true) resolve(true)
}, 1000) }, 1000)
}) })
},
validate () {
return true
} }
} }
} }
+53 -32
View File
@@ -43,7 +43,7 @@
<template> <template>
<span @click="prevTab" v-if="displayPrevButton"> <span @click="prevTab" v-if="displayPrevButton">
<slot name="prev"> <slot name="prev">
<button type="button" class="btn btn-default btn-wd" :style="fillButtonStyle"> <button type="button" class="btn btn-default btn-wd" :style="fillButtonStyle" :disabled="loading">
{{backButtonText}} {{backButtonText}}
</button> </button>
</slot> </slot>
@@ -63,7 +63,7 @@
<template> <template>
<span @click="nextTab" class="pull-right" v-if="!isLastStep"> <span @click="nextTab" class="pull-right" v-if="!isLastStep">
<slot name="next"> <slot name="next">
<button type="button" class="btn btn-fill btn-wd btn-next" :style="fillButtonStyle"> <button type="button" class="btn btn-fill btn-wd btn-next" :style="fillButtonStyle" :disabled="loading">
{{nextButtonText}} {{nextButtonText}}
</button> </button>
</slot> </slot>
@@ -130,6 +130,7 @@
isLastStep: false, isLastStep: false,
currentPercentage: 0, currentPercentage: 0,
maxStep: 0, maxStep: 0,
loading: false,
tabs: [] tabs: []
} }
}, },
@@ -196,22 +197,45 @@
}, },
navigateToTab (index) { navigateToTab (index) {
if (index <= this.maxStep) { if (index <= this.maxStep) {
this.beforeTabChange(this.activeTabIndex).then((res) => { let cb = () => {
res && this.changeTab(this.activeTabIndex, index) this.changeTab(this.activeTabIndex, index)
}) }
this.beforeTabChange(this.activeTabIndex, cb)
} }
}, },
beforeTabChange (index) { setLoading (value) {
this.loading = value
this.$emit('on-loading', value)
},
validateTabChangePromise (promiseFn, callback) {
// we have a promise
if (promiseFn.then && typeof promiseFn.then === 'function') {
this.setLoading(true)
promiseFn.then((res) => {
this.setLoading(false)
if (res) {
callback()
}
}).catch(() => {
this.setLoading(false)
})
// we have a simple function
} else {
if (promiseFn) {
callback()
}
}
},
beforeTabChange (index, callback) {
if (this.loading) {
return
}
let oldTab = this.tabs[index] let oldTab = this.tabs[index]
if (oldTab && oldTab.beforeChange !== undefined) { if (oldTab && oldTab.beforeChange !== undefined) {
let tabChangeRes = oldTab.beforeChange() let tabChangeRes = oldTab.beforeChange()
if (tabChangeRes.then && typeof tabChangeRes.then === 'function') { this.validateTabChangePromise(tabChangeRes, callback)
return tabChangeRes
} else {
return Promise.resolve(tabChangeRes)
}
} else { } else {
return Promise.resolve(true) callback()
} }
}, },
changeTab (oldIndex, newIndex) { changeTab (oldIndex, newIndex) {
@@ -241,28 +265,26 @@
this.maxStep = this.activeTabIndex this.maxStep = this.activeTabIndex
} }
}, },
nextTab () { nextTab () {
this.beforeTabChange(this.activeTabIndex).then((res) => { let cb = () => {
if (res) { if (this.activeTabIndex < this.tabCount - 1) {
if (this.activeTabIndex < this.tabCount - 1) { this.changeTab(this.activeTabIndex, this.activeTabIndex + 1)
this.activeTabIndex++ this.increaseMaxStep()
this.increaseMaxStep() } else {
this.checkStep() this.isLastStep = true
} else { this.$emit('finished')
this.isLastStep = true
this.$emit('finished')
}
} }
}) }
this.beforeTabChange(this.activeTabIndex, cb)
}, },
prevTab () { prevTab () {
this.beforeTabChange(this.activeTabIndex).then((res) => { let cb = () => {
if (res && this.activeTabIndex > 0) { if (this.activeTabIndex > 0) {
this.activeTabIndex-- this.activeTabIndex--
this.isLastStep = false this.isLastStep = false
} }
}) }
this.beforeTabChange(this.activeTabIndex, cb)
}, },
finish () { finish () {
this.$emit('on-complete') this.$emit('on-complete')
@@ -284,11 +306,10 @@
}, },
watch: { watch: {
activeTabIndex: function (newVal, oldVal) { activeTabIndex: function (newVal, oldVal) {
this.beforeTabChange(oldVal).then((res) => { let cb = () => {
if (res) { this.changeTab(oldVal, newVal)
this.changeTab(oldVal, newVal) }
} this.beforeTabChange(oldVal, cb)
})
} }
} }
} }