Skip to content

Commit ca6f9f7

Browse files
committed
Allow jumping to previous questions, add API function for jumping to any question ID
1 parent dfdc5b2 commit ca6f9f7

File tree

1 file changed

+52
-2
lines changed

1 file changed

+52
-2
lines changed

src/components/FlowForm.vue

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,8 @@
418418
let
419419
index = 0,
420420
serialIndex = 0,
421-
nextId
421+
nextId,
422+
activeIndex = this.activeQuestionIndex
422423
423424
do {
424425
let question = this.questionModels[index]
@@ -432,13 +433,20 @@
432433
++index
433434
} else if (question.answered) {
434435
nextId = question.getJumpId()
436+
435437
if (nextId) {
436438
if (nextId === '_submit') {
437439
index = this.questionModels.length
438440
} else {
439441
for (let i = 0; i < this.questionModels.length; i++) {
440442
if (this.questionModels[i].id === nextId) {
441-
index = i
443+
if (i < index) {
444+
question.answered = false
445+
activeIndex = i
446+
++index
447+
} else {
448+
index = i
449+
}
442450
break
443451
}
444452
}
@@ -454,6 +462,7 @@
454462
} while (index < this.questionModels.length)
455463
456464
this.questionListActivePath = questions
465+
this.goToQuestion(activeIndex)
457466
},
458467
459468
/**
@@ -667,13 +676,54 @@
667676
*/
668677
goToNextQuestion() {
669678
this.blurFocus()
679+
670680
if (this.isNextQuestionAvailable()) {
671681
this.emitEnter()
672682
}
673683
674684
this.reverse = false
675685
},
676686
687+
/**
688+
* Jumps to question with specific index.
689+
*/
690+
goToQuestion(index) {
691+
if (isNaN(+index)) {
692+
let questionIndex = this.activeQuestionIndex
693+
694+
this.questionListActivePath.forEach((question, _index) => {
695+
if (question.id === index) {
696+
questionIndex = _index
697+
}
698+
})
699+
700+
index = questionIndex;
701+
}
702+
703+
if (index !== this.activeQuestionIndex) {
704+
this.blurFocus()
705+
706+
if (!this.submitted && index <= this.questionListActivePath.length - 1) {
707+
// Check if we can actually jump to the wanted question.
708+
do {
709+
const previousQuestion = index > 0 ? this.questionListActivePath[index - 1] : null
710+
711+
if (previousQuestion === null || previousQuestion.answered) {
712+
break
713+
}
714+
715+
--index
716+
}
717+
while (index > 0)
718+
719+
this.reverse = index < this.activeQuestionIndex
720+
this.activeQuestionIndex = index
721+
722+
this.checkTimer()
723+
}
724+
}
725+
},
726+
677727
/**
678728
* Removes focus from the currently focused DOM element.
679729
*/

0 commit comments

Comments
 (0)