diff --git a/src/App.vue b/src/App.vue
index 0ef3688..d99951c 100644
--- a/src/App.vue
+++ b/src/App.vue
@@ -5,32 +5,18 @@
color="#e74c3c"
class="card">
My first tab content
-
-
-
-
-
My second tab content
-
-
-
-
-
Yuhuuu! This seems pretty damn simple
-
-
-
-
-
@@ -45,12 +31,15 @@
onComplete () {
alert('Yay!')
},
- validate () {
+ validateAsync () {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(true)
}, 1000)
})
+ },
+ validate () {
+ return true
}
}
}
diff --git a/src/components/FormWizard.vue b/src/components/FormWizard.vue
index d80dec8..89dd90a 100644
--- a/src/components/FormWizard.vue
+++ b/src/components/FormWizard.vue
@@ -43,7 +43,7 @@
-
@@ -63,7 +63,7 @@
-
+
{{nextButtonText}}
@@ -130,6 +130,7 @@
isLastStep: false,
currentPercentage: 0,
maxStep: 0,
+ loading: false,
tabs: []
}
},
@@ -196,22 +197,45 @@
},
navigateToTab (index) {
if (index <= this.maxStep) {
- this.beforeTabChange(this.activeTabIndex).then((res) => {
- res && this.changeTab(this.activeTabIndex, index)
- })
+ let cb = () => {
+ 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]
if (oldTab && oldTab.beforeChange !== undefined) {
let tabChangeRes = oldTab.beforeChange()
- if (tabChangeRes.then && typeof tabChangeRes.then === 'function') {
- return tabChangeRes
- } else {
- return Promise.resolve(tabChangeRes)
- }
+ this.validateTabChangePromise(tabChangeRes, callback)
} else {
- return Promise.resolve(true)
+ callback()
}
},
changeTab (oldIndex, newIndex) {
@@ -241,28 +265,26 @@
this.maxStep = this.activeTabIndex
}
},
-
nextTab () {
- this.beforeTabChange(this.activeTabIndex).then((res) => {
- if (res) {
- if (this.activeTabIndex < this.tabCount - 1) {
- this.activeTabIndex++
- this.increaseMaxStep()
- this.checkStep()
- } else {
- this.isLastStep = true
- this.$emit('finished')
- }
+ let cb = () => {
+ if (this.activeTabIndex < this.tabCount - 1) {
+ this.changeTab(this.activeTabIndex, this.activeTabIndex + 1)
+ this.increaseMaxStep()
+ } else {
+ this.isLastStep = true
+ this.$emit('finished')
}
- })
+ }
+ this.beforeTabChange(this.activeTabIndex, cb)
},
prevTab () {
- this.beforeTabChange(this.activeTabIndex).then((res) => {
- if (res && this.activeTabIndex > 0) {
+ let cb = () => {
+ if (this.activeTabIndex > 0) {
this.activeTabIndex--
this.isLastStep = false
}
- })
+ }
+ this.beforeTabChange(this.activeTabIndex, cb)
},
finish () {
this.$emit('on-complete')
@@ -284,11 +306,10 @@
},
watch: {
activeTabIndex: function (newVal, oldVal) {
- this.beforeTabChange(oldVal).then((res) => {
- if (res) {
- this.changeTab(oldVal, newVal)
- }
- })
+ let cb = () => {
+ this.changeTab(oldVal, newVal)
+ }
+ this.beforeTabChange(oldVal, cb)
}
}
}