Skip to content

Commit bb31c78

Browse files
authored
Refactor(QuestionMultiple): update "Other" answer handling to use v-model and add change handler
Signed-off-by: GitHub <noreply@github.com>
1 parent 6610506 commit bb31c78

1 file changed

Lines changed: 38 additions & 29 deletions

File tree

src/components/Questions/QuestionMultiple.vue

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@
106106
class="question__input"
107107
:label="placeholderOtherAnswer"
108108
:required="otherAnswer !== undefined"
109-
:value.sync="otherAnswerText" />
109+
:model-value="otherAnswerText"
110+
@update:model-value="onOtherAnswerTextChange" />
110111
</div>
111112
</fieldset>
112113
</template>
@@ -291,34 +292,8 @@ export default {
291292
/**
292293
* The text value of the "other" answer
293294
*/
294-
otherAnswerText: {
295-
get() {
296-
return this.cachedOtherAnswerText
297-
},
298-
/**
299-
* Called when the value of the "other" anwer is changed input
300-
* @param {string} value the new text of the "other" answer
301-
*/
302-
set(value) {
303-
this.cachedOtherAnswerText = value
304-
// Prefix the value
305-
const prefixedValue = `${QUESTION_EXTRASETTINGS_OTHER_PREFIX}${value}`
306-
// emit the values and add the "other" answer
307-
this.$emit(
308-
'update:values',
309-
this.isUnique
310-
? [prefixedValue]
311-
: [
312-
...this.values.filter(
313-
(v) =>
314-
!v.startsWith(
315-
QUESTION_EXTRASETTINGS_OTHER_PREFIX,
316-
),
317-
),
318-
prefixedValue,
319-
],
320-
)
321-
},
295+
otherAnswerText() {
296+
return this.cachedOtherAnswerText
322297
},
323298
},
324299
@@ -483,6 +458,40 @@ export default {
483458
onAllowOtherAnswerChange(allowOtherAnswer) {
484459
return this.onExtraSettingsChange({ allowOtherAnswer })
485460
},
461+
462+
/**
463+
* Handles the change event for the "Other" answer text input.
464+
*
465+
* @param {string} value - The new value entered for the "Other" answer.
466+
*
467+
* This method performs the following actions:
468+
* 1. Updates the cached value of the "Other" answer text (`cachedOtherAnswerText`).
469+
* 2. Prefixes the input value with a predefined constant (`QUESTION_EXTRASETTINGS_OTHER_PREFIX`).
470+
* 3. Emits an `update:values` event with the updated list of values:
471+
* - If `isUnique` is true, the emitted values will only include the prefixed "Other" answer.
472+
* - If `isUnique` is false, the emitted values will include all existing values
473+
* (excluding any that start with the "Other" prefix) and the new prefixed "Other" answer.
474+
*/
475+
onOtherAnswerTextChange(value) {
476+
this.cachedOtherAnswerText = value
477+
// Prefix the value
478+
const prefixedValue = `${QUESTION_EXTRASETTINGS_OTHER_PREFIX}${value}`
479+
// emit the values and add the "other" answer
480+
this.$emit(
481+
'update:values',
482+
this.isUnique
483+
? [prefixedValue]
484+
: [
485+
...this.values.filter(
486+
(v) =>
487+
!v.startsWith(
488+
QUESTION_EXTRASETTINGS_OTHER_PREFIX,
489+
),
490+
),
491+
prefixedValue,
492+
],
493+
)
494+
},
486495
},
487496
}
488497
</script>

0 commit comments

Comments
 (0)