forked from TanStack/form
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.svelte
More file actions
55 lines (50 loc) · 1.33 KB
/
App.svelte
File metadata and controls
55 lines (50 loc) · 1.33 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
<script lang="ts">
import { createForm } from '@tanstack/svelte-form'
const form = createForm(() => ({
defaultValues: {
people: [] as Array<{ age: number; name: string }>,
},
onSubmit: ({ value }) => alert(JSON.stringify(value)),
}))
</script>
<form
id="form"
onsubmit={(e) => {
e.preventDefault()
e.stopPropagation()
form.handleSubmit()
}}
>
<h1>TanStack Form - Svelte Demo</h1>
<form.Field name="people">
{#snippet children(field)}
<div>
{#each field.state.value as person, i}
<form.Field name={`people[${i}].name`}>
{#snippet children(subField)}
<div>
<label>
<div>Name for person {i}</div>
<input
value={person.name}
oninput={(e: Event) => {
const target = e.target as HTMLInputElement
subField.handleChange(target.value)
}}
/>
</label>
</div>
{/snippet}
</form.Field>
{/each}
<button
onclick={() => field.pushValue({ name: '', age: 0 })}
type="button"
>
Add person
</button>
</div>
{/snippet}
</form.Field>
<button type="submit"> Submit </button>
</form>