From ed3b3ab816ffc15af2c69796da15cb527a14296a Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Tue, 21 Nov 2023 03:44:06 +0100 Subject: [PATCH] fix: Make sure "other" answer is set correctly so it will be saved in LocalStorage Signed-off-by: Ferdinand Thiessen --- src/components/Questions/QuestionMultiple.vue | 70 ++++++++++--------- 1 file changed, 37 insertions(+), 33 deletions(-) diff --git a/src/components/Questions/QuestionMultiple.vue b/src/components/Questions/QuestionMultiple.vue index 69b64bc671..95111743b5 100644 --- a/src/components/Questions/QuestionMultiple.vue +++ b/src/components/Questions/QuestionMultiple.vue @@ -52,7 +52,7 @@
{{ t('forms', 'Other:') }} - +
@@ -96,7 +97,6 @@
  • item.startsWith(this.QUESTION_EXTRASETTINGS_OTHER_PREFIX)) - return checkedOtherAnswer[0] !== undefined - }, - }, - - watch: { - inputOtherAnswer() { - if (this.isUnique) { - this.onChange(this.valueOtherAnswer) - return - } - - const values = this.values.filter(item => !item.startsWith(this.QUESTION_EXTRASETTINGS_OTHER_PREFIX)) - if (this.inputOtherAnswer !== '') { - values.push(this.valueOtherAnswer) + /** + * Text of the "other" answer field + */ + otherAnswerText() { + if (this.otherAnswer) { + return this.otherAnswer.slice(QUESTION_EXTRASETTINGS_OTHER_PREFIX.length) } + return '' + }, - this.onChange(values) + /** + * The full "other" answer including prefix, undefined if no "other answer" + */ + otherAnswer() { + return this.values.find((v) => v.startsWith(QUESTION_EXTRASETTINGS_OTHER_PREFIX)) }, }, @@ -228,6 +220,20 @@ export default { this.$emit('update:values', this.isUnique ? [value] : value) }, + /** + * Called when the value of the "other" anwer is changed input + * @param {string} value the new text of the "other" answer + */ + onChangeOther(value) { + // Prefix the value + const prefixedValue = `${QUESTION_EXTRASETTINGS_OTHER_PREFIX}${value}` + // emit the values and add the "other" answer + this.$emit( + 'update:values', + this.isUnique ? [prefixedValue] : [...this.values.filter((v) => !v.startsWith(QUESTION_EXTRASETTINGS_OTHER_PREFIX)), prefixedValue], + ) + }, + /** * Is the provided answer required ? * This is needed for checkboxes as html5 @@ -311,12 +317,10 @@ export default { options.push({ id: GenRandomId(), questionId: this.id, - text: this.inputValue, + text: '', local: true, }) - this.inputValue = '' - // Update question this.updateOptions(options)