Skip to content

Commit 3d715b9

Browse files
committed
Fix MultipleChoiceType pre-filling issues
1 parent 6b4c0fc commit 3d715b9

File tree

3 files changed

+36
-11
lines changed

3 files changed

+36
-11
lines changed

src/components/FlowForm.vue

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,8 @@
354354
355355
q.componentInstance.question = model
356356
357+
model.resetOptions()
358+
357359
questions.push(model)
358360
})
359361
}

src/components/QuestionTypes/MultipleChoiceType.vue

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,6 @@
7070
},
7171
7272
mounted() {
73-
if (this.question.answer) {
74-
// Set initial answer when we have one
75-
this.question.options.forEach(o => {
76-
const optionValue = o.choiceValue()
77-
78-
if (this.question.answer === optionValue || (Array.isArray(this.question.answer) && this.question.answer.indexOf(optionValue) !== -1)) {
79-
this.toggleAnswer(o)
80-
}
81-
})
82-
}
83-
8473
this.addKeyListener()
8574
},
8675

src/models/QuestionModel.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ export default class QuestionModel {
127127
if (this.multiple && !Array.isArray(this.answer)) {
128128
this.answer = this.answer ? [this.answer] : []
129129
}
130+
131+
this.resetOptions()
130132
}
131133

132134
getJumpId() {
@@ -158,4 +160,36 @@ export default class QuestionModel {
158160

159161
this.index = index
160162
}
163+
164+
resetOptions() {
165+
if (this.options) {
166+
const isArray = Array.isArray(this.answer)
167+
let numSelected = 0
168+
169+
this.options.forEach(o => {
170+
const optionValue = o.choiceValue()
171+
172+
if (this.answer === optionValue || (isArray && this.answer.indexOf(optionValue) !== -1)) {
173+
o.selected = true
174+
++numSelected
175+
}
176+
})
177+
178+
if (this.allowOther) {
179+
let otherAnswer = null
180+
181+
if (isArray) {
182+
if (this.answer.length && this.answer.length !== numSelected) {
183+
otherAnswer = this.answer[this.answer.length - 1]
184+
}
185+
} else if (this.options.map(o => o.choiceValue()).indexOf(this.answer) === -1) {
186+
otherAnswer = this.answer
187+
}
188+
189+
if (otherAnswer !== null) {
190+
this.other = otherAnswer
191+
}
192+
}
193+
}
194+
}
161195
}

0 commit comments

Comments
 (0)