Open History to see the last commit
1<script setup lang="ts">
2import { useForm } from '@tanstack/vue-form'
3β
4const props = defineProps<{ items: Array<string> }>()
5β
6const form = useForm({
7 defaultValues: {
8 items: props.items,
9 },
10 onSubmit: ({ value }) => {
11 console.log(value)
12 alert(`Submitted ${value.items.length} items.`)
13 },
14})
15</script>
16β
17<template>
18 <form.Subscribe
19 :selector="(state) => state.values.items.length"
20 v-slot="amount"
21 >
22 <h2>Item amount: {{ amount.toLocaleString() }}</h2>
23 </form.Subscribe>
24β
25 <form @submit.prevent.stop="form.handleSubmit()">
26 <button type="submit">Submit</button>
27 <button type="button" @click="form.pushFieldValue('items', 'New Field')">
28 Create item
29 </button>
30β
31 <!-- ArrayField only rerenders this list when its structure changes. -->
32 <form.ArrayField name="items" v-slot="{ field: array }">
33 <ul>
34 <li v-for="(_, index) in array.value" :key="index">
35 <form.Field :name="`items[${index}]`" v-slot="{ field }">
36 <label>
37 <span>Field {{ index }}</span>
38 <input
39 :name="field.name"
40 :value="field.value"
41 @blur="field.handleBlur"
42 @input="
43 field.handleChange(($event.target as HTMLInputElement).value)
44 "
45 />
46 </label>
47 </form.Field>
48 </li>
49 </ul>
50 </form.ArrayField>
51 </form>
52</template>