Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 36 additions & 34 deletions src/components/Questions/QuestionMultiple.vue
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@
class="question__input"
:label="placeholderOtherAnswer"
:required="otherAnswer !== undefined"
:value.sync="otherAnswerText" />
:model-value="cachedOtherAnswerText"
@update:model-value="onOtherAnswerTextChange" />
</div>
</fieldset>
</template>
Expand Down Expand Up @@ -287,39 +288,6 @@ export default {
v.startsWith(QUESTION_EXTRASETTINGS_OTHER_PREFIX),
)
},

/**
* The text value of the "other" answer
*/
otherAnswerText: {
get() {
return this.cachedOtherAnswerText
},
/**
* Called when the value of the "other" anwer is changed input
* @param {string} value the new text of the "other" answer
*/
set(value) {
this.cachedOtherAnswerText = 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,
],
)
},
},
},

watch: {
Expand Down Expand Up @@ -483,6 +451,40 @@ export default {
onAllowOtherAnswerChange(allowOtherAnswer) {
return this.onExtraSettingsChange({ allowOtherAnswer })
},

/**
* Handles the change event for the "Other" answer text input.
*
* @param {string} value - The new value entered for the "Other" answer.
*
* This method performs the following actions:
* 1. Updates the cached value of the "Other" answer text (`cachedOtherAnswerText`).
* 2. Prefixes the input value with a predefined constant (`QUESTION_EXTRASETTINGS_OTHER_PREFIX`).
* 3. Emits an `update:values` event with the updated list of values:
* - If `isUnique` is true, the emitted values will only include the prefixed "Other" answer.
* - If `isUnique` is false, the emitted values will include all existing values
* (excluding any that start with the "Other" prefix) and the new prefixed "Other" answer.
*/
onOtherAnswerTextChange(value) {
this.cachedOtherAnswerText = 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,
],
)
},
},
}
</script>
Expand Down