|
| 1 | +<script lang="ts"> |
| 2 | + import { page } from '$app/stores'; |
| 3 | + import { superForm } from '$lib/client'; |
| 4 | + import { createEventDispatcher, onDestroy } from 'svelte'; |
| 5 | + import SuperDebug from '$lib/client/SuperDebug.svelte'; |
| 6 | + import { get } from 'svelte/store'; |
| 7 | +
|
| 8 | + const dispatch = createEventDispatcher(); |
| 9 | +
|
| 10 | + const frm = superForm($page.data.form); |
| 11 | + const { form, errors, message, enhance, reset } = frm; |
| 12 | +
|
| 13 | + page.subscribe(($page) => { |
| 14 | + console.log( |
| 15 | + '🚀 ~ file: Form.svelte:18 ~ page.subscribe ~ page:', |
| 16 | + $page.data.form.data |
| 17 | + ); |
| 18 | + }); |
| 19 | +</script> |
| 20 | + |
| 21 | +<SuperDebug label="Form" data={$form} /> |
| 22 | + |
| 23 | +{#if $message} |
| 24 | + <div |
| 25 | + class="status" |
| 26 | + class:error={$page.status >= 400} |
| 27 | + class:success={$page.status == 200} |
| 28 | + > |
| 29 | + {$message} |
| 30 | + </div> |
| 31 | +{/if} |
| 32 | + |
| 33 | +<form method="POST" use:enhance> |
| 34 | + <label> |
| 35 | + Name<br /> |
| 36 | + <input name="name" data-invalid={$errors.name} bind:value={$form.name} /> |
| 37 | + {#if $errors.name}<span class="invalid">{$errors.name}</span>{/if} |
| 38 | + </label> |
| 39 | + |
| 40 | + <label> |
| 41 | + Email<br /> |
| 42 | + <input |
| 43 | + name="email" |
| 44 | + type="email" |
| 45 | + data-invalid={$errors.email} |
| 46 | + bind:value={$form.email} |
| 47 | + /> |
| 48 | + {#if $errors.email}<span class="invalid">{$errors.email}</span>{/if} |
| 49 | + </label> |
| 50 | + |
| 51 | + <button disabled>Submit</button> |
| 52 | + <button |
| 53 | + type="button" |
| 54 | + on:click={() => { |
| 55 | + reset(); |
| 56 | + console.log('Resetted:', get(frm.form)); |
| 57 | + setTimeout(() => dispatch('cancel'), 500); |
| 58 | + }}>Cancel</button |
| 59 | + > |
| 60 | +</form> |
| 61 | + |
| 62 | +<hr /> |
| 63 | +<p> |
| 64 | + <a target="_blank" href="https://superforms.vercel.app/api" |
| 65 | + >API Reference</a |
| 66 | + > |
| 67 | +</p> |
| 68 | + |
| 69 | +<style> |
| 70 | + .invalid { |
| 71 | + color: red; |
| 72 | + } |
| 73 | +
|
| 74 | + .status { |
| 75 | + color: white; |
| 76 | + padding: 4px; |
| 77 | + padding-left: 8px; |
| 78 | + border-radius: 2px; |
| 79 | + font-weight: 500; |
| 80 | + } |
| 81 | +
|
| 82 | + .status.success { |
| 83 | + background-color: seagreen; |
| 84 | + } |
| 85 | +
|
| 86 | + .status.error { |
| 87 | + background-color: #ff2a02; |
| 88 | + } |
| 89 | +
|
| 90 | + input { |
| 91 | + background-color: #ddd; |
| 92 | + } |
| 93 | +
|
| 94 | + a { |
| 95 | + text-decoration: underline; |
| 96 | + } |
| 97 | +
|
| 98 | + hr { |
| 99 | + margin-top: 4rem; |
| 100 | + } |
| 101 | +
|
| 102 | + form { |
| 103 | + padding-top: 1rem; |
| 104 | + padding-bottom: 1rem; |
| 105 | + } |
| 106 | +</style> |
0 commit comments