Skip to content
This repository was archived by the owner on Jun 1, 2023. It is now read-only.

Commit 7c64ff4

Browse files
committed
Minor refactorings
Simplify some code and extract some methods to a helpers file
1 parent 02173ee commit 7c64ff4

File tree

4 files changed

+38
-48
lines changed

4 files changed

+38
-48
lines changed

src/assets/form-wizard/_buttons.scss

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,9 @@
66
font-size: $font-size-base;
77
font-weight: $font-weight-bold;
88
padding: $padding-base-vertical $padding-base-horizontal;
9+
min-width: 140px;
910
&:hover,
1011
&:focus {
1112
outline: 0 !important;
1213
}
1314
}
14-
15-
.vue-form-wizard {
16-
.navbar .navbar-nav > li > a.wizard-btn.wizard-btn-wd,
17-
.wizard-btn-wd {
18-
min-width: 140px;
19-
}
20-
}

src/components/FormWizard.vue

Lines changed: 20 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
<script>
7575
import WizardButton from './WizardButton.vue'
7676
import WizardStep from './WizardStep.vue'
77+
import {isPromise, findElementAndFocus, getFocusedTabIndex} from './helpers'
7778
export default{
7879
name: 'form-wizard',
7980
components: {
@@ -151,7 +152,6 @@
151152
data () {
152153
return {
153154
activeTabIndex: 0,
154-
isLastStep: false,
155155
currentPercentage: 0,
156156
maxStep: 0,
157157
loading: false,
@@ -171,6 +171,9 @@
171171
tabCount () {
172172
return this.tabs.length
173173
},
174+
isLastStep () {
175+
return this.activeTabIndex === this.tabCount - 1
176+
},
174177
displayPrevButton () {
175178
return this.activeTabIndex !== 0
176179
},
@@ -268,39 +271,16 @@
268271
if (this.activeTabIndex < this.tabCount - 1) {
269272
this.changeTab(this.activeTabIndex, this.activeTabIndex + 1)
270273
} else {
271-
this.isLastStep = true
272274
this.$emit('on-complete')
273275
}
274276
}
275277
this.beforeTabChange(this.activeTabIndex, cb)
276278
},
277-
getActiveElementId () {
278-
return document.activeElement.id
279-
},
280-
focusNextTab () {
281-
let activeId = this.getActiveElementId()
282-
let tabIndex = this.tabs.findIndex(tab => tab.tabId === activeId)
283-
if (tabIndex !== -1 && tabIndex < this.tabs.length - 1) {
284-
let toFocus = this.tabs[tabIndex + 1].tabId
285-
let elem = document.getElementById(toFocus)
286-
elem.focus()
287-
}
288-
},
289-
focusPrevTab () {
290-
let activeId = this.getActiveElementId()
291-
let tabIndex = this.tabs.findIndex(tab => tab.tabId === activeId)
292-
if (tabIndex !== -1 && tabIndex > 0) {
293-
let toFocus = this.tabs[tabIndex - 1].tabId
294-
let elem = document.getElementById(toFocus)
295-
elem.focus()
296-
}
297-
},
298279
prevTab () {
299280
let cb = () => {
300281
if (this.activeTabIndex > 0) {
301282
this.setValidationError(null)
302283
this.changeTab(this.activeTabIndex, this.activeTabIndex - 1)
303-
this.isLastStep = false
304284
}
305285
}
306286
if (this.validateOnBack) {
@@ -309,6 +289,20 @@
309289
cb()
310290
}
311291
},
292+
focusNextTab () {
293+
let tabIndex = getFocusedTabIndex(this.tabs)
294+
if (tabIndex !== -1 && tabIndex < this.tabs.length - 1) {
295+
let toFocusId = this.tabs[tabIndex + 1].tabId
296+
findElementAndFocus(toFocusId)
297+
}
298+
},
299+
focusPrevTab () {
300+
let tabIndex = getFocusedTabIndex(this.tabs)
301+
if (tabIndex !== -1 && tabIndex > 0) {
302+
let toFocusId = this.tabs[tabIndex - 1].tabId
303+
findElementAndFocus(toFocusId)
304+
}
305+
},
312306
setLoading (value) {
313307
this.loading = value
314308
this.$emit('on-loading', value)
@@ -320,7 +314,7 @@
320314
validateBeforeChange (promiseFn, callback) {
321315
this.setValidationError(null)
322316
// we have a promise
323-
if (promiseFn.then && typeof promiseFn.then === 'function') {
317+
if (isPromise(promiseFn)) {
324318
this.setLoading(true)
325319
promiseFn.then((res) => {
326320
this.setLoading(false)
@@ -377,18 +371,6 @@
377371
this.$router.push(tab.route)
378372
}
379373
},
380-
checkStep () {
381-
if (this.activeTabIndex === this.tabCount - 1) {
382-
this.isLastStep = true
383-
} else {
384-
this.isLastStep = false
385-
}
386-
},
387-
increaseMaxStep () {
388-
if (this.activeTabIndex > this.maxStep) {
389-
this.maxStep = this.activeTabIndex
390-
}
391-
},
392374
checkRouteChange (route) {
393375
let matchingTabIndex = -1
394376
let matchingTab = this.tabs.find((tab, index) => {
@@ -420,7 +402,6 @@
420402
},
421403
activateTabAndCheckStep (index) {
422404
this.activateTab(index)
423-
this.checkStep()
424405
if (index > this.maxStep) {
425406
this.maxStep = index
426407
}
@@ -441,7 +422,7 @@
441422
this.initializeTabs()
442423
},
443424
watch: {
444-
'$route.path': function (newRoute) {
425+
'$route.path' (newRoute) {
445426
this.checkRouteChange(newRoute)
446427
}
447428
}

src/components/WizardButton.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<template>
2-
<button class="wizard-btn btn-fill wizard-btn-wd" tabindex="-1">
2+
<button class="wizard-btn" tabindex="-1">
33
<slot></slot>
44
</button>
55
</template>
66
<script>
7-
export default {}
7+
export default {}
88
</script>
99
<style>
1010
</style>

src/components/helpers.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export function getFocusedElementId () {
2+
return document.activeElement.id
3+
}
4+
export function getFocusedTabIndex (tabs = []) {
5+
let activeId = getFocusedElementId()
6+
let tabIndex = tabs.findIndex(tab => tab.tabId === activeId)
7+
return tabIndex
8+
}
9+
export function findElementAndFocus (elemId) {
10+
let elem = document.getElementById(elemId)
11+
elem.focus()
12+
}
13+
export function isPromise (func) {
14+
return func.then && typeof func.then === 'function'
15+
}

0 commit comments

Comments
 (0)