Skip to content

Commit 88edb25

Browse files
spinnmgd722EkaterinaVu
authored
Allow ChoiceOptions to have a falsy value (#148)
Allow falsy values to be returned from ChoiceOption Co-authored-by: Mike Davlantes <rocks722@hotmail.com> Co-authored-by: EkaterinaVu <ekaterina.vujasinovic@gmail.com>
2 parents 5c98e2e + 31d8247 commit 88edb25

File tree

5 files changed

+24
-6
lines changed

5 files changed

+24
-6
lines changed

examples/questionnaire/Example.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@
282282
this.questions.forEach(question => {
283283
if (question.title) {
284284
let answer = question.answer
285-
if (typeof answer === 'object') {
285+
if (Array.isArray(answer)) {
286286
answer = answer.join(', ')
287287
}
288288

src/components/Question.vue

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,9 @@
228228
return false
229229
}
230230
231-
if (!q || !this.dataValue) {
231+
// If there is no question referenced, or dataValue is still set to its default (null).
232+
// This allows a ChoiceOption value of false, but will not allow you to use null as a value.
233+
if (!q || this.dataValue === null) {
232234
return false
233235
}
234236
@@ -241,7 +243,7 @@
241243
showInvalid() {
242244
const q = this.$refs.questionComponent
243245
244-
if (!q || !this.dataValue) {
246+
if (!q || this.dataValue === null) {
245247
return false
246248
}
247249

src/components/QuestionTypes/BaseType.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
data() {
2626
return {
2727
dirty: false,
28-
dataValue: '',
28+
dataValue: null,
2929
answer: null,
3030
enterPressed: false,
3131
allowedChars: null,

src/components/QuestionTypes/MultipleChoiceType.vue

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,20 @@
222222
stopEditOther() {
223223
this.editingOther = false
224224
}
225+
},
226+
227+
computed: {
228+
hasValue() {
229+
if (this.question.options.filter(o => o.selected).length) {
230+
return true
231+
}
232+
233+
if (this.question.allowOther) {
234+
return this.question.other && this.question.other.trim().length > 0
235+
}
236+
237+
return false
238+
}
225239
}
226240
}
227241
</script>

src/models/QuestionModel.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export const MaskPresets = Object.freeze({
3535
export class ChoiceOption {
3636
constructor(options) {
3737
this.label = ''
38-
this.value = ''
38+
this.value = null
3939
this.selected = false
4040

4141
Object.assign(this, options)
@@ -46,7 +46,9 @@ export class ChoiceOption {
4646
}
4747

4848
choiceValue() {
49-
return this.value || this.label
49+
// Returns the value if it's anything other than the default (null).
50+
// Returns label if the value has not been set.
51+
return this.value !== null ? this.value : this.label
5052
}
5153

5254
toggle() {

0 commit comments

Comments
 (0)