|
1 | 1 | <script lang="ts"> |
2 | | - import { Button } from '$comp/ui/button'; |
3 | 2 | import * as Dialog from '$comp/ui/dialog'; |
| 3 | + import * as Form from '$comp/ui/form'; |
4 | 4 | import { Input } from '$comp/ui/input'; |
5 | | - import { Label } from '$comp/ui/label'; |
| 5 | + import { defaults, superForm } from 'sveltekit-superforms'; |
| 6 | + import { classvalidatorClient } from 'sveltekit-superforms/adapters'; |
| 7 | +
|
| 8 | + import { ReferenceLinkForm } from '../models'; |
6 | 9 |
|
7 | 10 | interface Props { |
8 | 11 | open: boolean; |
9 | 12 | save: (url: string) => Promise<void>; |
10 | 13 | } |
11 | 14 |
|
12 | | - // TODO: Use form validation |
13 | 15 | let { open = $bindable(), save }: Props = $props(); |
14 | | - let value = $state(''); |
15 | 16 |
|
16 | | - async function onSubmit() { |
17 | | - await save(value); |
18 | | - open = false; |
19 | | - } |
| 17 | + const form = superForm(defaults(new ReferenceLinkForm(), classvalidatorClient(ReferenceLinkForm)), { |
| 18 | + dataType: 'json', |
| 19 | + async onUpdate({ form }) { |
| 20 | + if (!form.valid) { |
| 21 | + return; |
| 22 | + } |
| 23 | +
|
| 24 | + await save(form.data.url); |
| 25 | + open = false; |
| 26 | + }, |
| 27 | + SPA: true, |
| 28 | + validators: classvalidatorClient(ReferenceLinkForm) |
| 29 | + }); |
| 30 | +
|
| 31 | + const { enhance, form: formData } = form; |
20 | 32 | </script> |
21 | 33 |
|
22 | 34 | <Dialog.Root bind:open> |
23 | 35 | <Dialog.Content class="sm:max-w-[425px]"> |
24 | | - <Dialog.Header> |
25 | | - <Dialog.Title>Add Reference Link</Dialog.Title> |
26 | | - <Dialog.Description>Add a reference link to an external resource.</Dialog.Description> |
27 | | - </Dialog.Header> |
28 | | - <div class="grid gap-4 py-4"> |
29 | | - <div class="grid grid-cols-5 items-center gap-6"> |
30 | | - <Label for="url" class="text-right">Url</Label> |
31 | | - <Input id="url" name="url" type="url" bind:value placeholder="Please enter a valid URL" class="col-span-4" required /> |
| 36 | + <form method="POST" use:enhance> |
| 37 | + <Dialog.Header> |
| 38 | + <Dialog.Title>Add Reference Link</Dialog.Title> |
| 39 | + <Dialog.Description>Add a reference link to an external resource.</Dialog.Description> |
| 40 | + </Dialog.Header> |
| 41 | + |
| 42 | + <div class="py-4"> |
| 43 | + <Form.Field {form} name="url"> |
| 44 | + <Form.Control> |
| 45 | + {#snippet children({ props })} |
| 46 | + <Form.Label>Reference Link</Form.Label> |
| 47 | + <Input {...props} bind:value={$formData.url} type="url" placeholder="Please enter a valid URL" autocomplete="url" required /> |
| 48 | + {/snippet} |
| 49 | + </Form.Control> |
| 50 | + <Form.Description /> |
| 51 | + <Form.FieldErrors /> |
| 52 | + </Form.Field> |
32 | 53 | </div> |
33 | | - </div> |
34 | | - <Dialog.Footer> |
35 | | - <Button type="submit" onclick={onSubmit}>Save Reference Link</Button> |
36 | | - </Dialog.Footer> |
| 54 | + |
| 55 | + <Dialog.Footer> |
| 56 | + <Form.Button>Save Reference Link</Form.Button> |
| 57 | + </Dialog.Footer> |
| 58 | + </form> |
37 | 59 | </Dialog.Content> |
38 | 60 | </Dialog.Root> |
0 commit comments