|
| 1 | +<script lang="ts"> |
| 2 | + import { z } from 'zod'; |
| 3 | + import { page } from '$app/stores'; |
| 4 | + import { superForm } from '$lib/client'; |
| 5 | +
|
| 6 | + const postSchema = z.object({ |
| 7 | + questions: z |
| 8 | + .object({ |
| 9 | + text: z.string(), |
| 10 | + generated: z.boolean() |
| 11 | + }) |
| 12 | + .array() |
| 13 | + .min(1, { |
| 14 | + message: 'Must have at least one question' |
| 15 | + }) |
| 16 | + }); |
| 17 | +
|
| 18 | + const { form, errors, message, enhance, constraints } = superForm< |
| 19 | + typeof postSchema |
| 20 | + >($page.data.form, { |
| 21 | + taintedMessage: 'Are you sure you want to leave?', |
| 22 | + validators: postSchema, |
| 23 | + resetForm: true |
| 24 | + //dataType: 'json' |
| 25 | + }); |
| 26 | +
|
| 27 | + let tooManyQuestions = false; |
| 28 | + let query = ''; |
| 29 | +
|
| 30 | + function addQuestion() { |
| 31 | + if (!query) { |
| 32 | + console.log('return'); |
| 33 | + return; |
| 34 | + } |
| 35 | + if ($form.questions.length >= 10) { |
| 36 | + tooManyQuestions = true; |
| 37 | + return; |
| 38 | + } |
| 39 | + $form.questions = [ |
| 40 | + ...$form.questions, |
| 41 | + { text: query, generated: false } |
| 42 | + ]; |
| 43 | + query = ''; |
| 44 | + console.log('addQuestion', $form); |
| 45 | + } |
| 46 | +
|
| 47 | + function removeQuestion(index: number) { |
| 48 | + $form.questions.splice(index, 1); |
| 49 | + $form.questions = $form.questions; |
| 50 | + if ($form.questions.length <= 10) { |
| 51 | + tooManyQuestions = false; |
| 52 | + } |
| 53 | + } |
| 54 | +</script> |
| 55 | + |
| 56 | +<h1>Welcome to SvelteKit</h1> |
| 57 | + |
| 58 | +<form method="POST" class="sm:p-4 md:p-8"> |
| 59 | + <div class="mt-2 flex flex-row"> |
| 60 | + <input |
| 61 | + bind:value={query} |
| 62 | + type="text" |
| 63 | + class="block w-full rounded-md border-0 bg-white/5 py-1.5 text-white shadow-sm ring-1 ring-inset ring-white/10 focus:ring-2 focus:ring-inset focus:ring-indigo-500 sm:text-sm sm:leading-6" |
| 64 | + /> |
| 65 | + <button |
| 66 | + type="button" |
| 67 | + on:click={addQuestion} |
| 68 | + class="m-2 h-7 w-7 cursor-pointer" |
| 69 | + /> |
| 70 | + {#each $form.questions as _, index} |
| 71 | + <div class="col-span-full"> |
| 72 | + <div class="my-2 flex flex-col"> |
| 73 | + <label for="questions" class="font-dmsans text-sm text-white" |
| 74 | + >Question {index}</label |
| 75 | + > |
| 76 | + <div class="mt-2 flex flex-row"> |
| 77 | + <input |
| 78 | + type="text" |
| 79 | + id={`q_${index}`} |
| 80 | + bind:value={$form.questions[index].text} |
| 81 | + name="questions" |
| 82 | + class="block w-full rounded-md border-0 bg-white/5 py-1.5 text-white shadow-sm ring-1 ring-inset ring-white/10 focus:ring-2 focus:ring-inset focus:ring-indigo-500 sm:text-sm sm:leading-6" |
| 83 | + /> |
| 84 | + {#if $errors.questions?.[index].text}{/if} |
| 85 | + <button |
| 86 | + type="button" |
| 87 | + class="m-2 h-7 w-7 cursor-pointer text-red-600" |
| 88 | + on:click={() => removeQuestion(index)} |
| 89 | + /> |
| 90 | + </div> |
| 91 | + </div> |
| 92 | + </div> |
| 93 | + {/each} |
| 94 | + </div> |
| 95 | + <button |
| 96 | + type="submit" |
| 97 | + class="rounded-md bg-indigo-500 px-3 py-2 text-sm font-semibold text-white shadow-sm hover:bg-indigo-400 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-500" |
| 98 | + >Save</button |
| 99 | + > |
| 100 | +</form> |
0 commit comments