-
Notifications
You must be signed in to change notification settings - Fork 182
/
Copy pathQuestionEditor.vue
345 lines (319 loc) · 8.18 KB
/
QuestionEditor.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
<template>
<!-- Question index -->
<div class="flex items-center justify-between">
<h3 class="text-lg font-bold">
{{ index + 1 }}. {{ model.question }}
</h3>
<div class="flex items-center">
<!-- Add new question -->
<button
type="button"
@click="addQuestion()"
class="
flex
items-center
text-xs
py-1
px-3
mr-2
rounded-sm
text-white
bg-gray-600
hover:bg-gray-700
"
>
<svg
xmlns="http://www.w3.org/2000/svg"
class="h-4 w-4"
viewBox="0 0 20 20"
fill="currentColor"
>
<path
fill-rule="evenodd"
d="M10 5a1 1 0 011 1v3h3a1 1 0 110 2h-3v3a1 1 0 11-2 0v-3H6a1 1 0 110-2h3V6a1 1 0 011-1z"
clip-rule="evenodd"
/>
</svg>
Add
</button>
<!--/ Add new question -->
<!-- Delete question -->
<button
type="button"
@click="deleteQuestion()"
class="
flex
items-center
text-xs
py-1
px-3
rounded-sm
border border-transparent
text-red-500
hover:border-red-600
"
>
<svg
xmlns="http://www.w3.org/2000/svg"
class="h-4 w-4"
viewBox="0 0 20 20"
fill="currentColor"
>
<path
fill-rule="evenodd"
d="M9 2a1 1 0 00-.894.553L7.382 4H4a1 1 0 000 2v10a2 2 0 002 2h8a2 2 0 002-2V6a1 1 0 100-2h-3.382l-.724-1.447A1 1 0 0011 2H9zM7 8a1 1 0 012 0v6a1 1 0 11-2 0V8zm5-1a1 1 0 00-1 1v6a1 1 0 102 0V8a1 1 0 00-1-1z"
clip-rule="evenodd"
/>
</svg>
Delete
</button>
<!--/ Delete question -->
</div>
</div>
<!--/ Question index -->
<div class="grid gap-3 grid-cols-12">
<!-- Question -->
<div class="mt-3 col-span-9">
<label
:for="'question_text_' + model.data"
class="block text-sm font-medium text-gray-700"
>Question Text</label
>
<input
type="text"
:name="'question_text_' + model.data"
v-model="model.question"
@change="dataChange"
:id="'question_text_' + model.data"
class="
mt-1
focus:ring-indigo-500 focus:border-indigo-500
block
w-full
shadow-sm
sm:text-sm
border-gray-300
rounded-md
"
/>
</div>
<!--/ Question -->
<!-- Question Type -->
<div class="mt-3 col-span-3">
<label for="question_type" class="block text-sm font-medium text-gray-700"
>Select Question Type</label
>
<select
id="question_type"
name="question_type"
v-model="model.type"
@change="typeChange"
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
"
>
<option v-for="type in questionTypes" :key="type" :value="type">
{{ upperCaseFirst(type) }}
</option>
</select>
</div>
<!--/ Question Type -->
</div>
<!-- Question Description -->
<div class="mt-3 col-span-9">
<label
:for="'question_description_' + model.id"
class="block text-sm font-medium text-gray-700"
>Description</label
>
<textarea
:name="'question_description_' + model.id"
v-model="model.description"
@change="dataChange"
:id="'question_description_' + model.id"
class="
mt-1
focus:ring-indigo-500 focus:border-indigo-500
block
w-full
shadow-sm
sm:text-sm
border-gray-300
rounded-md
"
/>
</div>
<!--/ Question Description -->
<!-- Data -->
<div>
<div v-if="shouldHaveOptions()" class="mt-2">
<h4 class="text-sm font-semibold mb-1 flex justify-between items-center">
Options
<!-- Add new option -->
<button
type="button"
@click="addOption()"
class="
flex
items-center
text-xs
py-1
px-2
rounded-sm
text-white
bg-gray-600
hover:bg-gray-700
"
>
<svg
xmlns="http://www.w3.org/2000/svg"
class="h-4 w-4"
viewBox="0 0 20 20"
fill="currentColor"
>
<path
fill-rule="evenodd"
d="M10 5a1 1 0 011 1v3h3a1 1 0 110 2h-3v3a1 1 0 11-2 0v-3H6a1 1 0 110-2h3V6a1 1 0 011-1z"
clip-rule="evenodd"
/>
</svg>
Add Option
</button>
<!--/ Add new option -->
</h4>
<div
v-if="!model.data.options.length"
class="text-xs text-gray-600 text-center py-3"
>
You don't have any options defined
</div>
<!-- Option list -->
<div
v-for="(option, index) in model.data.options"
:key="option.uuid"
class="flex items-center mb-1"
>
<span class="w-6 text-sm"> {{ index + 1 }}. </span>
<input
type="text"
tabindex="1"
v-model="option.text"
@change="dataChange"
class="
w-full
rounded-sm
py-1
px-2
text-xs
border border-gray-300
focus:border-indigo-500
"
/>
<!-- Delete Option -->
<button
type="button"
@click="removeOption(option)"
class="
h-6
w-6
rounded-full
flex
items-center
justify-center
border border-transparent
transition-colors
hover:border-red-100
"
>
<svg
xmlns="http://www.w3.org/2000/svg"
class="h-3 w-3 text-red-500"
viewBox="0 0 20 20"
fill="currentColor"
>
<path
fill-rule="evenodd"
d="M9 2a1 1 0 00-.894.553L7.382 4H4a1 1 0 000 2v10a2 2 0 002 2h8a2 2 0 002-2V6a1 1 0 100-2h-3.382l-.724-1.447A1 1 0 0011 2H9zM7 8a1 1 0 012 0v6a1 1 0 11-2 0V8zm5-1a1 1 0 00-1 1v6a1 1 0 102 0V8a1 1 0 00-1-1z"
clip-rule="evenodd"
/>
</svg>
</button>
<!--/ Delete Option -->
</div>
<!--/ Option list -->
</div>
</div>
<!--/ Data -->
<hr class="my-4" />
</template>
<script setup>
import { v4 as uuidv4 } from "uuid";
import { computed, ref } from "@vue/reactivity";
import store from "../../store";
const props = defineProps({
question: Object,
index: Number,
});
const emit = defineEmits(["change", "addQuestion", "deleteQuestion"]);
// Re-create the whole question data to avoid unintentional reference change
const model = ref(JSON.parse(JSON.stringify(props.question)));
// Get question types from vuex
const questionTypes = computed(() => store.state.questionTypes);
function upperCaseFirst(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
}
function getOptions() {
return model.value.data.options;
}
function setOptions(options) {
model.value.data.options = options;
}
// Check if the question should have options
function shouldHaveOptions() {
return ["select", "radio", "checkbox"].includes(model.value.type);
}
// Add option
function addOption() {
setOptions([
...getOptions(),
{ uuid: uuidv4(), text: "" },
]);
dataChange();
}
// Remove option
function removeOption(op) {
setOptions(getOptions().filter((opt) => opt !== op));
dataChange();
}
function typeChange() {
if (shouldHaveOptions()) {
setOptions(getOptions() || []);
}
dataChange();
}
// Emit the data change
function dataChange() {
const data = model.value;
if (!shouldHaveOptions()) {
delete data.data.options;
}
emit("change", data);
}
function addQuestion() {
emit("addQuestion", props.index + 1);
}
function deleteQuestion() {
emit("deleteQuestion", props.question);
}
</script>
<style></style>