-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path+page.svelte
129 lines (105 loc) · 2.54 KB
/
+page.svelte
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
<script lang="ts">
import { page } from '$app/stores';
import { superForm } from 'sveltekit-superforms';
import SuperDebug from 'sveltekit-superforms';
import { debounce } from 'throttle-debounce';
import spinner from './spinner.svg?raw';
export let data;
const { form, errors, message, enhance } = superForm(data.form);
const {
delayed,
submit: submitCheckUsername,
enhance: submitEnhance
} = superForm(
{ username: '' },
{
invalidateAll: false,
applyAction: false,
multipleSubmits: 'abort',
onSubmit({ cancel }) {
if (!$form.username) cancel();
},
onUpdated({ form }) {
// Update the other form to show the error message
$errors.username = form.errors.username;
}
}
);
const checkUsername = debounce(200, submitCheckUsername);
</script>
<SuperDebug data={$form} />
<h3>Debounced username check</h3>
<p>Alphabet names like "alpha", "delta", "romeo" are not available.</p>
{#if $message}
<!-- eslint-disable-next-line svelte/valid-compile -->
<div class="status" class:error={$page.status >= 400} class:success={$page.status == 200}>
{$message}
</div>
{/if}
<form method="POST" action="?/post" use:enhance>
<label>
Username<br />
<input
form="check"
name="username"
aria-invalid={$errors.username ? 'true' : undefined}
bind:value={$form.username}
on:input={checkUsername}
/>
<input type="hidden" name="username" value={$form.username} />
{#if $delayed}
<!-- eslint-disable-next-line svelte/no-at-html-tags -->
{@html spinner}
{:else if $errors.username}
❌
{:else if $form.username && 'username' in $errors}
✅
{/if}
{#if $errors.username}<div class="invalid">{$errors.username[0]}</div>{/if}
</label>
<label>
Email<br />
<input
name="email"
type="email"
aria-invalid={$errors.email ? 'true' : undefined}
bind:value={$form.email}
/>
{#if $errors.email}<span class="invalid">{$errors.email}</span>{/if}
</label>
<button>Submit</button>
</form>
<form id="check" method="POST" action="?/check" use:submitEnhance></form>
<hr />
<p><a target="_blank" href="https://superforms.rocks">Created with Superforms for SvelteKit</a></p>
<style>
.invalid {
color: red;
}
.status {
color: white;
padding: 4px;
padding-left: 8px;
border-radius: 2px;
font-weight: 500;
}
.status.success {
background-color: seagreen;
}
.status.error {
background-color: #ff2a02;
}
input {
background-color: #ddd;
}
a {
text-decoration: underline;
}
hr {
margin-top: 4rem;
}
form {
padding-top: 1rem;
padding-bottom: 1rem;
}
</style>