Skip to content

Commit b20fd38

Browse files
authored
Fix/on quick select (#144)
Fixed issue with QuestionType.MultipleChoice when quickly switching choices or when quickly pressing enter.
1 parent 88edb25 commit b20fd38

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

src/components/Question.vue

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@
144144
data() {
145145
return {
146146
QuestionType: QuestionType,
147-
dataValue: null
147+
dataValue: null,
148+
debounced: false
148149
}
149150
},
150151
mounted() {
@@ -188,8 +189,24 @@
188189
* Emits "answer" event and calls "onEnter" method on Enter press
189190
*/
190191
onEnter($event) {
192+
this.checkAnswer(this.emitAnswer)
193+
},
194+
195+
onTab($event) {
196+
this.checkAnswer(this.emitAnswerTab)
197+
},
198+
199+
checkAnswer(fn) {
191200
const q = this.$refs.questionComponent
192201
202+
if (q.isValid() && this.question.nextStepOnAnswer && !this.question.multiple) {
203+
this.debounce(() => fn(q), 350)
204+
} else {
205+
fn(q)
206+
}
207+
},
208+
209+
emitAnswer(q) {
193210
if (q) {
194211
if (!q.focused) {
195212
this.$emit('answer', q)
@@ -199,16 +216,24 @@
199216
}
200217
},
201218
202-
onTab($event) {
203-
const q = this.$refs.questionComponent
204-
219+
emitAnswerTab(q) {
205220
if (q && this.question.type !== QuestionType.Date) {
206221
this.returnFocus()
207222
this.$emit('answer', q)
208223
209224
q.onEnter()
210225
}
211226
},
227+
228+
debounce(fn, delay) {
229+
let debounceTimer
230+
this.debounced = true
231+
232+
return (() => {
233+
clearTimeout(debounceTimer)
234+
debounceTimer = setTimeout(fn, delay)
235+
})()
236+
},
212237
213238
/**
214239
* Check if the "OK" button should be shown.

src/components/QuestionTypes/MultipleChoiceType.vue

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,12 @@
167167
} else {
168168
this.dataValue = option.selected ? option.choiceValue() : null
169169
}
170-
171-
this.setAnswer(this.dataValue)
172-
170+
173171
if (this.isValid() && this.question.nextStepOnAnswer && !this.question.multiple) {
174-
setTimeout(() => this.$emit('next'), 350)
172+
this.$emit('next')
175173
}
174+
175+
this.setAnswer(this.dataValue)
176176
},
177177
178178
_removeAnswer(value) {

0 commit comments

Comments
 (0)