2
0
mirror of https://github.com/tenrok/vue-form-wizard.git synced 2026-06-25 08:40:33 +03:00

#1 Modify beforeTabChange to support promises (wip)

This commit is contained in:
cristi
2017-04-22 11:58:58 +03:00
parent 732d612747
commit a16c4051fe
3 changed files with 75 additions and 39 deletions
+1 -1
View File
File diff suppressed because one or more lines are too long
+23
View File
@@ -5,16 +5,32 @@
color="#e74c3c" color="#e74c3c"
class="card"> class="card">
<tab-content title="Personal details" <tab-content title="Personal details"
:before-change="validate"
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"
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>
@@ -28,6 +44,13 @@
methods: { methods: {
onComplete () { onComplete () {
alert('Yay!') alert('Yay!')
},
validate () {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(true)
}, 1000)
})
} }
} }
} }
+24 -11
View File
@@ -17,7 +17,8 @@
<div class="icon-circle" :class="{checked:isChecked(index),square_shape:isStepSquare, tab_shape:isTabShape}" <div class="icon-circle" :class="{checked:isChecked(index),square_shape:isStepSquare, tab_shape:isTabShape}"
:style="isChecked(index)? stepCheckedStyle : {}"> :style="isChecked(index)? stepCheckedStyle : {}">
<transition :name="transition" mode="out-in"> <transition :name="transition" mode="out-in">
<div v-if="tab.active" class="icon-container" :class="{square_shape:isStepSquare, tab_shape:isTabShape}" :style="tab.active ? iconActiveStyle: {}"> <div v-if="tab.active" class="icon-container" :class="{square_shape:isStepSquare, tab_shape:isTabShape}"
:style="tab.active ? iconActiveStyle: {}">
<i v-if="tab.icon" :class="tab.icon" class="icon"></i> <i v-if="tab.icon" :class="tab.icon" class="icon"></i>
<i v-else class="icon">{{index + 1}}</i> <i v-else class="icon">{{index + 1}}</i>
</div> </div>
@@ -194,16 +195,24 @@
return index <= this.maxStep return index <= this.maxStep
}, },
navigateToTab (index) { navigateToTab (index) {
if (index <= this.maxStep && this.beforeTabChange(this.activeTabIndex)) { if (index <= this.maxStep) {
this.changeTab(this.activeTabIndex, index) this.beforeTabChange(this.activeTabIndex).then((res) => {
res && this.changeTab(this.activeTabIndex, index)
})
} }
}, },
beforeTabChange (index) { beforeTabChange (index) {
let oldTab = this.tabs[index] let oldTab = this.tabs[index]
if (oldTab && oldTab.beforeChange !== undefined) { if (oldTab && oldTab.beforeChange !== undefined) {
return oldTab.beforeChange() let tabChangeRes = oldTab.beforeChange()
if (tabChangeRes.then && typeof tabChangeRes.then === 'function') {
return tabChangeRes
} else {
return Promise.resolve(tabChangeRes)
}
} else {
return Promise.resolve(true)
} }
return true
}, },
changeTab (oldIndex, newIndex) { changeTab (oldIndex, newIndex) {
let oldTab = this.tabs[oldIndex] let oldTab = this.tabs[oldIndex]
@@ -234,8 +243,8 @@
}, },
nextTab () { nextTab () {
if (!this.beforeTabChange(this.activeTabIndex)) return this.beforeTabChange(this.activeTabIndex).then((res) => {
if (res) {
if (this.activeTabIndex < this.tabCount - 1) { if (this.activeTabIndex < this.tabCount - 1) {
this.activeTabIndex++ this.activeTabIndex++
this.increaseMaxStep() this.increaseMaxStep()
@@ -244,14 +253,16 @@
this.isLastStep = true this.isLastStep = true
this.$emit('finished') this.$emit('finished')
} }
}
})
}, },
prevTab () { prevTab () {
if (!this.beforeTabChange(this.activeTabIndex)) return this.beforeTabChange(this.activeTabIndex).then((res) => {
if (res && this.activeTabIndex > 0) {
if (this.activeTabIndex > 0) {
this.activeTabIndex-- this.activeTabIndex--
this.isLastStep = false this.isLastStep = false
} }
})
}, },
finish () { finish () {
this.$emit('on-complete') this.$emit('on-complete')
@@ -273,9 +284,11 @@
}, },
watch: { watch: {
activeTabIndex: function (newVal, oldVal) { activeTabIndex: function (newVal, oldVal) {
if (this.beforeTabChange(oldVal)) { this.beforeTabChange(oldVal).then((res) => {
if (res) {
this.changeTab(oldVal, newVal) this.changeTab(oldVal, newVal)
} }
})
} }
} }
} }