MCPcopy Index your code
hub / github.com/TanStack/form / App

Function App

examples/react/array/src/index.tsx:15–78  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

13const defaultPeople: { people: Array<Person> } = { people: [] }
14
15function App() {
16 const form = useForm({
17 defaultValues: defaultPeople,
18 onSubmit({ value }) {
19 alert(JSON.stringify(value))
20 },
21 })
22
23 return (
24 <div>
25 <form
26 onSubmit={(e) => {
27 e.preventDefault()
28 e.stopPropagation()
29 form.handleSubmit()
30 }}
31 >
32 <form.Field name="people" mode="array">
33 {(field) => {
34 return (
35 <div>
36 {field.state.value.map((_, i) => {
37 return (
38 <form.Field key={i} name={`people[${i}].name`}>
39 {(subField) => {
40 return (
41 <div>
42 <label>
43 <div>Name for person {i}</div>
44 <input
45 value={subField.state.value}
46 onChange={(e) =>
47 subField.handleChange(e.target.value)
48 }
49 />
50 </label>
51 </div>
52 )
53 }}
54 </form.Field>
55 )
56 })}
57 <button
58 onClick={() => field.pushValue({ name: '', age: 0 })}
59 type="button"
60 >
61 Add person
62 </button>
63 </div>
64 )
65 }}
66 </form.Field>
67 <form.Subscribe
68 selector={(state) => [state.canSubmit, state.isSubmitting]}
69 children={([canSubmit, isSubmitting]) => (
70 <button type="submit" disabled={!canSubmit}>
71 {isSubmitting ? '...' : 'Submit'}
72 </button>

Callers

nothing calls this directly

Calls 2

useFormFunction · 0.90
handleSubmitMethod · 0.65

Tested by

no test coverage detected