|
74 | 74 | <script> |
75 | 75 | import WizardButton from './WizardButton.vue' |
76 | 76 | import WizardStep from './WizardStep.vue' |
| 77 | + import {isPromise, findElementAndFocus, getFocusedTabIndex} from './helpers' |
77 | 78 | export default{ |
78 | 79 | name: 'form-wizard', |
79 | 80 | components: { |
|
151 | 152 | data () { |
152 | 153 | return { |
153 | 154 | activeTabIndex: 0, |
154 | | - isLastStep: false, |
155 | 155 | currentPercentage: 0, |
156 | 156 | maxStep: 0, |
157 | 157 | loading: false, |
|
171 | 171 | tabCount () { |
172 | 172 | return this.tabs.length |
173 | 173 | }, |
| 174 | + isLastStep () { |
| 175 | + return this.activeTabIndex === this.tabCount - 1 |
| 176 | + }, |
174 | 177 | displayPrevButton () { |
175 | 178 | return this.activeTabIndex !== 0 |
176 | 179 | }, |
|
268 | 271 | if (this.activeTabIndex < this.tabCount - 1) { |
269 | 272 | this.changeTab(this.activeTabIndex, this.activeTabIndex + 1) |
270 | 273 | } else { |
271 | | - this.isLastStep = true |
272 | 274 | this.$emit('on-complete') |
273 | 275 | } |
274 | 276 | } |
275 | 277 | this.beforeTabChange(this.activeTabIndex, cb) |
276 | 278 | }, |
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 | | - }, |
298 | 279 | prevTab () { |
299 | 280 | let cb = () => { |
300 | 281 | if (this.activeTabIndex > 0) { |
301 | 282 | this.setValidationError(null) |
302 | 283 | this.changeTab(this.activeTabIndex, this.activeTabIndex - 1) |
303 | | - this.isLastStep = false |
304 | 284 | } |
305 | 285 | } |
306 | 286 | if (this.validateOnBack) { |
|
309 | 289 | cb() |
310 | 290 | } |
311 | 291 | }, |
| 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 | + }, |
312 | 306 | setLoading (value) { |
313 | 307 | this.loading = value |
314 | 308 | this.$emit('on-loading', value) |
|
320 | 314 | validateBeforeChange (promiseFn, callback) { |
321 | 315 | this.setValidationError(null) |
322 | 316 | // we have a promise |
323 | | - if (promiseFn.then && typeof promiseFn.then === 'function') { |
| 317 | + if (isPromise(promiseFn)) { |
324 | 318 | this.setLoading(true) |
325 | 319 | promiseFn.then((res) => { |
326 | 320 | this.setLoading(false) |
|
377 | 371 | this.$router.push(tab.route) |
378 | 372 | } |
379 | 373 | }, |
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 | | - }, |
392 | 374 | checkRouteChange (route) { |
393 | 375 | let matchingTabIndex = -1 |
394 | 376 | let matchingTab = this.tabs.find((tab, index) => { |
|
420 | 402 | }, |
421 | 403 | activateTabAndCheckStep (index) { |
422 | 404 | this.activateTab(index) |
423 | | - this.checkStep() |
424 | 405 | if (index > this.maxStep) { |
425 | 406 | this.maxStep = index |
426 | 407 | } |
|
441 | 422 | this.initializeTabs() |
442 | 423 | }, |
443 | 424 | watch: { |
444 | | - '$route.path': function (newRoute) { |
| 425 | + '$route.path' (newRoute) { |
445 | 426 | this.checkRouteChange(newRoute) |
446 | 427 | } |
447 | 428 | } |
|
0 commit comments