Skip to content
Source
Sign inSign up
TanStack/form
Open History to see the last commit

arrays.md

160 lines4.1 KB

1---
2id: arrays
3title: Arrays
4---
5​
6TanStack Form supports arrays as values in a form, including sub-object values inside of an array.
7​
8## Basic Usage
9​
10To use an array, you can use `field.api.state.value` on an array value:
11​
12```angular-ts
13@Component({
14 selector: 'app-root',
15 standalone: true,
16 imports: [TanStackField],
17 template: `
18 <ng-container [tanstackField]="form" name="people" #people="field">
19 <div>
20 @for (_ of people.api.state.value; track $index) {
21 <!-- ... -->
22 }
23 </div>
24 </ng-container>
25 `,
26})
27export class AppComponent {
28 form = injectForm({
29 defaultValues: {
30 people: [] as Array<{ name: string; age: number }>,
31 },
32 onSubmit({ value }) {
33 alert(JSON.stringify(value))
34 },
35 })
37```
38​
39This will generate the mapped JSX every time you run `pushValue` on `field`:
40​
41```angular-html
42<button (click)="people.api.pushValue(defaultPerson)" type="button">
43 Add person
44</button>
45```
46​
47Finally, you can use a subfield like so:
48​
49```angular-html
50 <ng-container
51 [tanstackField]="form"
52 [name]="getPeopleName($index)"
53 #person="field"
55 <div>
56 <label>
57 <div>Name for person {{ $index }}</div>
58 <input
59 [value]="person.api.state.value"
60 (input)="
61 person.api.handleChange($any($event).target.value)
62 "
63 />
64 </label>
65 </div>
66</ng-container>
67```
68​
69Where `getPeopleName` is a method on the class like so:
70​
71```typescript
72export class AppComponent {
73 getPeopleName = (idx: number) => `people[${idx}].name` as const
74​
75 // ...
77```
78​
79> While it's unfortunate that you need to use a function to get the field name, it's a requirement for how our strict TypeScript types work.
81> See, if we did the following:
83> ```angular-html
84> <ng-container [tanstackField]="form" [name]="'people[' + $index + '].name'"></ng-container>
85> ```
87> We'd be running into a TypeScript issue where `"one" + "two"` is `string` rather than the required `"onetwo"` type
89> Moreover, while Angular supports template literals in the template, they may not contain dynamic interpolation within them, such as our `$index` argument.
90​
91> It's possible that we've missed something! If you can think of a better fix for this problem, [drop us a line on our GitHub discussions](https://github.com/TanStack/form/discussions).
92​
93## Full Example
94​
95```angular-ts
96@Component({
97 selector: 'app-root',
98 standalone: true,
99 imports: [TanStackField],
100 template: `
101 <form (submit)="handleSubmit($event)">
102 <div>
103 <ng-container [tanstackField]="form" name="people" #people="field">
104 <div>
105 @for (_ of people.api.state.value; track $index) {
106 <ng-container
107 [tanstackField]="form"
108 [name]="getPeopleName($index)"
109 #person="field"
110 >
111 <div>
112 <label>
113 <div>Name for person {{ $index }}</div>
114 <input
115 [value]="person.api.state.value"
116 (input)="
117 person.api.handleChange($any($event).target.value)
118 "
119 />
120 </label>
121 </div>
122 </ng-container>
123 }
124 </div>
125 <button (click)="people.api.pushValue(defaultPerson)" type="button">
126 Add person
127 </button>
128 </ng-container>
129 </div>
130 <button type="submit" [disabled]="!canSubmit()">
131 {{ isSubmitting() ? '...' : 'Submit' }}
132 </button>
133 </form>
134 `,
135})
136export class AppComponent {
137 defaultPerson = { name: '', age: 0 }
138​
139 form = injectForm({
140 defaultValues: {
141 people: [] as Array<{ name: string; age: number }>,
142 },
143 onSubmit({ value }) {
144 alert(JSON.stringify(value))
145 },
146 })
147​
148​
149 getPeopleName = (idx: number) => `people[${idx}].name` as const;
150​
151 canSubmit = injectStore(this.form, (state) => state.canSubmit)
152 isSubmitting = injectStore(this.form, (state) => state.isSubmitting)
153​
154 handleSubmit(event: SubmitEvent) {
155 event.preventDefault()
156 event.stopPropagation()
157 this.form.handleSubmit()
158 }
160```