|
| 1 | +--- |
| 2 | +id: arrays |
| 3 | +title: Arrays |
| 4 | +--- |
| 5 | + |
| 6 | +TanStack Form supports arrays as values in a form, including sub-object values inside of an array. |
| 7 | + |
| 8 | +# Basic Usage |
| 9 | + |
| 10 | +To use an array, you can use `field.api.state.value` on an array value: |
| 11 | + |
| 12 | +```typescript |
| 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 | +}) |
| 27 | +export 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 | + }) |
| 36 | +} |
| 37 | +``` |
| 38 | + |
| 39 | +This will generate the mapped JSX every time you run `pushValue` on `field`: |
| 40 | + |
| 41 | +```html |
| 42 | +<button (click)="people.api.pushValue(defaultPerson)" type="button"> |
| 43 | + Add person |
| 44 | +</button> |
| 45 | +``` |
| 46 | + |
| 47 | +Finally, you can use a subfield like so: |
| 48 | + |
| 49 | +```html |
| 50 | +<ng-container |
| 51 | + [tanstackField]="form" |
| 52 | + [name]="'people[' + $index + '].name'" |
| 53 | + #person="field" |
| 54 | +> |
| 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 | + |
| 69 | +## Full Example |
| 70 | + |
| 71 | +```typescript |
| 72 | +@Component({ |
| 73 | + selector: 'app-root', |
| 74 | + standalone: true, |
| 75 | + imports: [TanStackField], |
| 76 | + template: ` |
| 77 | + <form (submit)="handleSubmit($event)"> |
| 78 | + <div> |
| 79 | + <ng-container [tanstackField]="form" name="people" #people="field"> |
| 80 | + <div> |
| 81 | + @for (_ of people.api.state.value; track $index) { |
| 82 | + <ng-container |
| 83 | + [tanstackField]="form" |
| 84 | + [name]="'people[' + $index + '].name'" |
| 85 | + #person="field" |
| 86 | + > |
| 87 | + <div> |
| 88 | + <label> |
| 89 | + <div>Name for person {{ $index }}</div> |
| 90 | + <input |
| 91 | + [value]="person.api.state.value" |
| 92 | + (input)=" |
| 93 | + person.api.handleChange($any($event).target.value) |
| 94 | + " |
| 95 | + /> |
| 96 | + </label> |
| 97 | + </div> |
| 98 | + </ng-container> |
| 99 | + } |
| 100 | + </div> |
| 101 | + <button (click)="people.api.pushValue(defaultPerson)" type="button"> |
| 102 | + Add person |
| 103 | + </button> |
| 104 | + </ng-container> |
| 105 | + </div> |
| 106 | + <button type="submit" [disabled]="!canSubmit()"> |
| 107 | + {{ isSubmitting() ? '...' : 'Submit' }} |
| 108 | + </button> |
| 109 | + </form> |
| 110 | + `, |
| 111 | +}) |
| 112 | +export class AppComponent { |
| 113 | + defaultPerson = { name: '', age: 0 } |
| 114 | + |
| 115 | + form = injectForm({ |
| 116 | + defaultValues: { |
| 117 | + people: [] as Array<{ name: string; age: number }>, |
| 118 | + }, |
| 119 | + onSubmit({ value }) { |
| 120 | + alert(JSON.stringify(value)) |
| 121 | + }, |
| 122 | + }) |
| 123 | + |
| 124 | + canSubmit = injectStore(this.form, (state) => state.canSubmit) |
| 125 | + isSubmitting = injectStore(this.form, (state) => state.isSubmitting) |
| 126 | + |
| 127 | + handleSubmit(event: SubmitEvent) { |
| 128 | + event.preventDefault() |
| 129 | + event.stopPropagation() |
| 130 | + void this.form.handleSubmit() |
| 131 | + } |
| 132 | +} |
| 133 | +``` |
0 commit comments