|
11 | 11 | <div class="mt-3"> |
12 | 12 | <div v-if="question.type === 'select'"> |
13 | 13 | <select |
| 14 | + :value="modelValue" |
| 15 | + @change="emits('update:modelValue', $event.target.value)" |
14 | 16 | class="mt-1 block w-full py-2 px-3 border border-gray-300 bg-white rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm" |
15 | 17 | > |
16 | | - <option v-for="option in question.data.options" :key="option.uuid"> |
| 18 | + <option value="">Please Select</option> |
| 19 | + <option v-for="option in question.data.options" :key="option.uuid" :value="option.text"> |
17 | 20 | {{ option.text }} |
18 | 21 | </option> |
19 | 22 | </select> |
|
27 | 30 | <input |
28 | 31 | :id="option.uuid" |
29 | 32 | :name="'question' + question.id" |
| 33 | + :value="option.text" |
| 34 | + @change="emits('update:modelValue', $event.target.value)" |
30 | 35 | type="radio" |
31 | 36 | class="focus:ring-indigo-500 h-4 w-4 text-indigo-600 border-gray-300" |
32 | 37 | /> |
|
46 | 51 | > |
47 | 52 | <input |
48 | 53 | :id="option.uuid" |
| 54 | + v-model="model[option.text]" |
| 55 | + @change="onCheckboxChange" |
49 | 56 | type="checkbox" |
50 | 57 | class="focus:ring-indigo-500 h-4 w-4 text-indigo-600 border-gray-300 rounded" |
51 | 58 | /> |
|
60 | 67 | <div v-else-if="question.type === 'text'"> |
61 | 68 | <input |
62 | 69 | type="text" |
| 70 | + :value="modelValue" |
| 71 | + @input="emits('update:modelValue', $event.target.value)" |
63 | 72 | class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md" |
64 | 73 | /> |
65 | 74 | </div> |
66 | 75 | <div v-else-if="question.type === 'textarea'"> |
67 | 76 | <textarea |
68 | | - :value="value" |
69 | | - @change="emits('input', $event.target.value)" |
| 77 | + :value="modelValue" |
| 78 | + @input="emits('update:modelValue', $event.target.value)" |
70 | 79 | class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md" |
71 | 80 | ></textarea> |
72 | 81 | </div> |
73 | | - <pre>{{value}}</pre> |
74 | 82 | </div> |
75 | 83 | </fieldset> |
76 | 84 | <hr class="mb-4" /> |
77 | 85 | </template> |
78 | 86 |
|
79 | 87 | <script setup> |
80 | | -
|
81 | | -const { question, index, value } = defineProps({ |
| 88 | +import { ref } from "vue"; |
| 89 | +const { question, index, modelValue } = defineProps({ |
82 | 90 | question: Object, |
83 | 91 | index: Number, |
84 | | - value: String |
| 92 | + modelValue: [String, Array], |
85 | 93 | }); |
86 | | -const emits = defineEmits(['input', 'update']); |
| 94 | +const emits = defineEmits(["update:modelValue"]); |
| 95 | +
|
| 96 | +let model; |
| 97 | +if (question.type === "checkbox") { |
| 98 | + model = ref({}); |
| 99 | +} |
87 | 100 |
|
88 | 101 | function shouldHaveOptions() { |
89 | 102 | return ["select", "radio", "checkbox"].includes(question.type); |
90 | 103 | } |
| 104 | +
|
| 105 | +function onCheckboxChange($event) { |
| 106 | + const selectedOptions = []; |
| 107 | + for (let uuid in model.value) { |
| 108 | + if (model.value[uuid]) { |
| 109 | + selectedOptions.push(uuid); |
| 110 | + } |
| 111 | + } |
| 112 | + emits("update:modelValue", selectedOptions); |
| 113 | +} |
91 | 114 | </script> |
92 | 115 |
|
93 | 116 | <style></style> |
0 commit comments