mirror of
https://github.com/tenrok/vue-form-wizard.git
synced 2026-06-23 23:00:34 +03:00
Minor refactorings
Simplify some code and extract some methods to a helpers file
This commit is contained in:
@@ -6,15 +6,9 @@
|
|||||||
font-size: $font-size-base;
|
font-size: $font-size-base;
|
||||||
font-weight: $font-weight-bold;
|
font-weight: $font-weight-bold;
|
||||||
padding: $padding-base-vertical $padding-base-horizontal;
|
padding: $padding-base-vertical $padding-base-horizontal;
|
||||||
|
min-width: 140px;
|
||||||
&:hover,
|
&:hover,
|
||||||
&:focus {
|
&:focus {
|
||||||
outline: 0 !important;
|
outline: 0 !important;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
.vue-form-wizard {
|
|
||||||
.navbar .navbar-nav > li > a.wizard-btn.wizard-btn-wd,
|
|
||||||
.wizard-btn-wd {
|
|
||||||
min-width: 140px;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -74,6 +74,7 @@
|
|||||||
<script>
|
<script>
|
||||||
import WizardButton from './WizardButton.vue'
|
import WizardButton from './WizardButton.vue'
|
||||||
import WizardStep from './WizardStep.vue'
|
import WizardStep from './WizardStep.vue'
|
||||||
|
import {isPromise, findElementAndFocus, getFocusedTabIndex} from './helpers'
|
||||||
export default{
|
export default{
|
||||||
name: 'form-wizard',
|
name: 'form-wizard',
|
||||||
components: {
|
components: {
|
||||||
@@ -151,7 +152,6 @@
|
|||||||
data () {
|
data () {
|
||||||
return {
|
return {
|
||||||
activeTabIndex: 0,
|
activeTabIndex: 0,
|
||||||
isLastStep: false,
|
|
||||||
currentPercentage: 0,
|
currentPercentage: 0,
|
||||||
maxStep: 0,
|
maxStep: 0,
|
||||||
loading: false,
|
loading: false,
|
||||||
@@ -171,6 +171,9 @@
|
|||||||
tabCount () {
|
tabCount () {
|
||||||
return this.tabs.length
|
return this.tabs.length
|
||||||
},
|
},
|
||||||
|
isLastStep () {
|
||||||
|
return this.activeTabIndex === this.tabCount - 1
|
||||||
|
},
|
||||||
displayPrevButton () {
|
displayPrevButton () {
|
||||||
return this.activeTabIndex !== 0
|
return this.activeTabIndex !== 0
|
||||||
},
|
},
|
||||||
@@ -268,39 +271,16 @@
|
|||||||
if (this.activeTabIndex < this.tabCount - 1) {
|
if (this.activeTabIndex < this.tabCount - 1) {
|
||||||
this.changeTab(this.activeTabIndex, this.activeTabIndex + 1)
|
this.changeTab(this.activeTabIndex, this.activeTabIndex + 1)
|
||||||
} else {
|
} else {
|
||||||
this.isLastStep = true
|
|
||||||
this.$emit('on-complete')
|
this.$emit('on-complete')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.beforeTabChange(this.activeTabIndex, cb)
|
this.beforeTabChange(this.activeTabIndex, cb)
|
||||||
},
|
},
|
||||||
getActiveElementId () {
|
|
||||||
return document.activeElement.id
|
|
||||||
},
|
|
||||||
focusNextTab () {
|
|
||||||
let activeId = this.getActiveElementId()
|
|
||||||
let tabIndex = this.tabs.findIndex(tab => tab.tabId === activeId)
|
|
||||||
if (tabIndex !== -1 && tabIndex < this.tabs.length - 1) {
|
|
||||||
let toFocus = this.tabs[tabIndex + 1].tabId
|
|
||||||
let elem = document.getElementById(toFocus)
|
|
||||||
elem.focus()
|
|
||||||
}
|
|
||||||
},
|
|
||||||
focusPrevTab () {
|
|
||||||
let activeId = this.getActiveElementId()
|
|
||||||
let tabIndex = this.tabs.findIndex(tab => tab.tabId === activeId)
|
|
||||||
if (tabIndex !== -1 && tabIndex > 0) {
|
|
||||||
let toFocus = this.tabs[tabIndex - 1].tabId
|
|
||||||
let elem = document.getElementById(toFocus)
|
|
||||||
elem.focus()
|
|
||||||
}
|
|
||||||
},
|
|
||||||
prevTab () {
|
prevTab () {
|
||||||
let cb = () => {
|
let cb = () => {
|
||||||
if (this.activeTabIndex > 0) {
|
if (this.activeTabIndex > 0) {
|
||||||
this.setValidationError(null)
|
this.setValidationError(null)
|
||||||
this.changeTab(this.activeTabIndex, this.activeTabIndex - 1)
|
this.changeTab(this.activeTabIndex, this.activeTabIndex - 1)
|
||||||
this.isLastStep = false
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (this.validateOnBack) {
|
if (this.validateOnBack) {
|
||||||
@@ -309,6 +289,20 @@
|
|||||||
cb()
|
cb()
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
focusNextTab () {
|
||||||
|
let tabIndex = getFocusedTabIndex(this.tabs)
|
||||||
|
if (tabIndex !== -1 && tabIndex < this.tabs.length - 1) {
|
||||||
|
let toFocusId = this.tabs[tabIndex + 1].tabId
|
||||||
|
findElementAndFocus(toFocusId)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
focusPrevTab () {
|
||||||
|
let tabIndex = getFocusedTabIndex(this.tabs)
|
||||||
|
if (tabIndex !== -1 && tabIndex > 0) {
|
||||||
|
let toFocusId = this.tabs[tabIndex - 1].tabId
|
||||||
|
findElementAndFocus(toFocusId)
|
||||||
|
}
|
||||||
|
},
|
||||||
setLoading (value) {
|
setLoading (value) {
|
||||||
this.loading = value
|
this.loading = value
|
||||||
this.$emit('on-loading', value)
|
this.$emit('on-loading', value)
|
||||||
@@ -320,7 +314,7 @@
|
|||||||
validateBeforeChange (promiseFn, callback) {
|
validateBeforeChange (promiseFn, callback) {
|
||||||
this.setValidationError(null)
|
this.setValidationError(null)
|
||||||
// we have a promise
|
// we have a promise
|
||||||
if (promiseFn.then && typeof promiseFn.then === 'function') {
|
if (isPromise(promiseFn)) {
|
||||||
this.setLoading(true)
|
this.setLoading(true)
|
||||||
promiseFn.then((res) => {
|
promiseFn.then((res) => {
|
||||||
this.setLoading(false)
|
this.setLoading(false)
|
||||||
@@ -377,18 +371,6 @@
|
|||||||
this.$router.push(tab.route)
|
this.$router.push(tab.route)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
checkStep () {
|
|
||||||
if (this.activeTabIndex === this.tabCount - 1) {
|
|
||||||
this.isLastStep = true
|
|
||||||
} else {
|
|
||||||
this.isLastStep = false
|
|
||||||
}
|
|
||||||
},
|
|
||||||
increaseMaxStep () {
|
|
||||||
if (this.activeTabIndex > this.maxStep) {
|
|
||||||
this.maxStep = this.activeTabIndex
|
|
||||||
}
|
|
||||||
},
|
|
||||||
checkRouteChange (route) {
|
checkRouteChange (route) {
|
||||||
let matchingTabIndex = -1
|
let matchingTabIndex = -1
|
||||||
let matchingTab = this.tabs.find((tab, index) => {
|
let matchingTab = this.tabs.find((tab, index) => {
|
||||||
@@ -420,7 +402,6 @@
|
|||||||
},
|
},
|
||||||
activateTabAndCheckStep (index) {
|
activateTabAndCheckStep (index) {
|
||||||
this.activateTab(index)
|
this.activateTab(index)
|
||||||
this.checkStep()
|
|
||||||
if (index > this.maxStep) {
|
if (index > this.maxStep) {
|
||||||
this.maxStep = index
|
this.maxStep = index
|
||||||
}
|
}
|
||||||
@@ -441,7 +422,7 @@
|
|||||||
this.initializeTabs()
|
this.initializeTabs()
|
||||||
},
|
},
|
||||||
watch: {
|
watch: {
|
||||||
'$route.path': function (newRoute) {
|
'$route.path' (newRoute) {
|
||||||
this.checkRouteChange(newRoute)
|
this.checkRouteChange(newRoute)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
<template>
|
<template>
|
||||||
<button class="wizard-btn btn-fill wizard-btn-wd" tabindex="-1">
|
<button class="wizard-btn" tabindex="-1">
|
||||||
<slot></slot>
|
<slot></slot>
|
||||||
</button>
|
</button>
|
||||||
</template>
|
</template>
|
||||||
<script>
|
<script>
|
||||||
export default {}
|
export default {}
|
||||||
</script>
|
</script>
|
||||||
<style>
|
<style>
|
||||||
</style>
|
</style>
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
export function getFocusedElementId () {
|
||||||
|
return document.activeElement.id
|
||||||
|
}
|
||||||
|
export function getFocusedTabIndex (tabs = []) {
|
||||||
|
let activeId = getFocusedElementId()
|
||||||
|
let tabIndex = tabs.findIndex(tab => tab.tabId === activeId)
|
||||||
|
return tabIndex
|
||||||
|
}
|
||||||
|
export function findElementAndFocus (elemId) {
|
||||||
|
let elem = document.getElementById(elemId)
|
||||||
|
elem.focus()
|
||||||
|
}
|
||||||
|
export function isPromise (func) {
|
||||||
|
return func.then && typeof func.then === 'function'
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user