-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample.svelte
More file actions
152 lines (131 loc) · 4.29 KB
/
Copy pathExample.svelte
File metadata and controls
152 lines (131 loc) · 4.29 KB
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
<script lang="ts">
import { createForm } from '$lib'
export let canToggle = false
// is valid if > 5 characters, otherwise invalid, with random 0.5 - 1.5 second delay
async function isNameAvailable(value: string) {
await new Promise(resolve => setTimeout(resolve, Math.random() * 1000 + 500))
const valid = value.length > 5
return valid ? null : `Name '${value}' not available`
}
const form = createForm()
const email = form.field()
const age = form.field()
const name = form.field({ validator: isNameAvailable })
const random = form.field()
const radio = form.field()
let showForm = true
let showEmail = true
function onSubmit() {
// whatever
}
let drone: string
</script>
{#if canToggle}
<label class="flex items-center">
<input class="mr-2" type="checkbox" bind:checked={showForm} />
Show Form
</label>
{/if}
{#if showForm}
<form use:form on:submit|preventDefault={onSubmit}>
<label for="name" class="text-sm text-gray-500">Username</label>
<input
id="name"
class="touched:valid:text-green-700 touched:valid:border-green-700 touched:invalid:text-red-700 touched:invalid:border-red-700"
use:name
type="text"
placeholder="unique name"
required
/>
<div id={$name.id} class="m-1 text-xs text-red-700" hidden={!$name.show}>
{#if $name.valueMissing}Name is required{/if}
{#if $name.customError}Name not available{/if}
</div>
{#if canToggle}
<label class="flex items-center">
<input class="mr-2" type="checkbox" bind:checked={showEmail} />
Include Email in Form
</label>
{/if}
{#if showEmail}
<label for="email" class="mt-2 text-sm text-gray-500">Email</label>
<input
id="email"
class="touched:valid:text-green-700 touched:valid:border-green-700 touched:invalid:text-red-700 touched:invalid:border-red-700"
use:email
type="email"
placeholder="email address"
required
/>
<div id={$email.id} class="m-1 text-xs text-red-700" hidden={!$email.show}>
{#if $email.valueMissing}Email address is required{/if}
{#if $email.typeMismatch}Not a valid email address{/if}
</div>
{/if}
<label for="age" class="mt-2 text-sm text-gray-500">Age</label>
<input
id="age"
class="touched:valid:text-green-700 touched:valid:border-green-700 touched:invalid:text-red-700 touched:invalid:border-red-700"
use:age
type="number"
required
value="0"
min="18"
max="65"
/>
<div id={$age.id} class="m-1 text-xs text-red-700" hidden={!$age.show}>
{#if $age.valueMissing}You have to tell us your age{/if}
{#if $age.rangeUnderflow}You must be at least 18{/if}
{#if $age.rangeOverflow}Sorry, no pensioners!{/if}
{#if $age.stepMismatch}Whole years only{/if}
</div>
<label for="random" class="mt-2 text-sm text-gray-500">Pick a number from, 1 to 10</label>
<input
id="random"
class="touched:valid:text-green-700 touched:valid:border-green-700 touched:invalid:text-red-700 touched:invalid:border-red-700"
use:random
type="number"
required
value="0"
min="1"
max="10"
/>
<div id={$random.id} class="m-1 text-xs text-red-700" hidden={!$random.show}>{$random.message}</div>
<fieldset>
<legend>Select a maintenance drone:</legend>
<label class="flex items-center">
<input use:radio type="radio" name="drone" value="huey" required bind:group={drone} />
Huey
</label>
<label class="flex items-center">
<input use:radio type="radio" name="drone" value="dewey" required bind:group={drone} />
Dewey
</label>
<label class="flex items-center">
<input use:radio type="radio" name="drone" value="louie" required bind:group={drone} />
Louie
</label>
</fieldset>
<div id={$radio.id} class="m-1 text-xs text-red-700" hidden={!$radio.show}>{$radio.message}</div>
<button type="submit" class="block my-3 text-white bg-green-800 py-2 px-4 rounded disabled:bg-gray-400" disabled={!$form.valid}> Submit </button>
</form>
{/if}
<pre class="text-xs mt-4">
form: {JSON.stringify($form, null, 2)}
name: {JSON.stringify($name, null, 2)}
email: {JSON.stringify($email, null, 2)}
age: {JSON.stringify($age, null, 2)}
number: {JSON.stringify($random, null, 2)}
radio : {JSON.stringify($radio, null, 2)}
</pre>
<style>
label {
display: block;
}
:global(input.touched:valid) {
border-color: green;
}
:global(input.touched:invalid) {
border-color: red;
}
</style>