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

form-composition.md

178 lines5.0 KB

1---
2id: form-composition
3title: Form Composition
4---
5​
6A common criticism of TanStack Form is its verbosity out-of-the-box. While this _can_ be useful for educational purposes - helping enforce understanding our APIs - it's not ideal in production use cases.
7​
8As a result, while basic usage of `[tanstackField]` enables the most powerful and flexible usage of TanStack Form, we provide APIs that wrap it and make your application code less verbose.
9​
10## Pre-bound Field Components
11​
12If you've ever used TanStack Form in Angular to bind more than one input, you'll have quickly realized how much goes into each input:
13​
14```angular-ts
15import { Component } from '@angular/core'
16import { TanStackField, injectForm, injectStore } from '@tanstack/angular-form'
17​
18@Component({
19 selector: 'app-root',
20 standalone: true,
21 imports: [TanStackField],
22 template: `
23 <div>
24 <ng-container
25 [tanstackField]="form"
26 name="firstName"
27 #firstName="field"
28 >
29 <label [for]="firstName.api.name">First Name:</label>
30 <input
31 [id]="firstName.api.name"
32 [name]="firstName.api.name"
33 [value]="firstName.api.state.value"
34 (blur)="firstName.api.handleBlur()"
35 (input)="firstName.api.handleChange($any($event).target.value)"
36 />
37 @if (firstName.api.state.meta.isTouched) {
38 @for (error of firstName.api.state.meta.errors; track $index) {
39 <div style="color: red">
40 {{ error }}
41 </div>
42 }
43 }
44 @if (firstName.api.state.meta.isValidating) {
45 <p>Validating...</p>
46 }
47 </ng-container>
48 </div>
49 <div>
50 <ng-container
51 [tanstackField]="form"
52 name="lastName"
53 #lastName="field"
54 >
55 <label [for]="lastName.api.name">Last Name:</label>
56 <input
57 [id]="lastName.api.name"
58 [name]="lastName.api.name"
59 [value]="lastName.api.state.value"
60 (blur)="lastName.api.handleBlur()"
61 (input)="lastName.api.handleChange($any($event).target.value)"
62 />
63 @if (lastName.api.state.meta.isTouched) {
64 @for (error of lastName.api.state.meta.errors; track $index) {
65 <div style="color: red">
66 {{ error }}
67 </div>
68 }
69 }
70 @if (lastName.api.state.meta.isValidating) {
71 <p>Validating...</p>
72 }
73 </ng-container>
74 </div>
75 `,
76})
77export class AppComponent {
78 form = injectForm({
79 defaultValues: {
80 firstName: '',
81 lastName: '',
82 },
83 onSubmit({ value }) {
84 // Do something with form data
85 console.log(value)
86 },
87 })
89```
90​
91This is functionally correct, but introduces a _lot_ of repeated templating behavior over and over. Instead, let's move the error handling, label to input binding, and other repeated logic into a component:
92​
93```angular-ts
94import {injectField} from '@tanstack/angular-form'
95​
96@Component({
97 selector: 'app-text-field',
98 standalone: true,
99 template: `
100 <label [for]="field.api.name">{{ label() }}</label>
101 <input
102 [id]="field.api.name"
103 [name]="field.api.name"
104 [value]="field.api.state.value"
105 (blur)="field.api.handleBlur()"
106 (input)="field.api.handleChange($any($event).target.value)"
107 />
108 @if (field.api.state.meta.isTouched) {
109 @for (error of field.api.state.meta.errors; track $index) {
110 <div style="color: red">
111 {{ error }}
112 </div>
113 }
114 }
115 @if (field.api.state.meta.isValidating) {
116 <p>Validating...</p>
117 }
118 `,
119})
120export class AppTextField {
121 label = input.required<string>()
122 // This API requires another part to it from the parent component
123 field = injectField<string>()
125```
126​
127> `injectField` accepts a single generic to define the `field.state.value` type.
129> As a result, a numerical text field would be represented as `injectField<number>`, for example.
130​
131Now, we can use the `TanStackAppField` directive (`tanstack-app-field`) to `provide` the expected field associated with this input:
132​
133```angular-ts
134import { Component } from '@angular/core'
135import {
136 TanStackAppField,
137 TanStackField,
138 injectForm,
139} from '@tanstack/angular-form'
140​
141@Component({
142 selector: 'app-root',
143 standalone: true,
144 imports: [TanStackField, TanStackAppField, AppTextField],
145 template: `
146 <div>
147 <app-text-field
148 label="First name:"
149 tanstack-app-field
150 [tanstackField]="form"
151 name="firstName"
152 />
153 </div>
154 <div>
155 <app-text-field
156 label="Last name:"
157 tanstack-app-field
158 [tanstackField]="form"
159 name="lastName"
160 />
161 </div>
162 `,
163})
164export class AppComponent {
165 form = injectForm({
166 defaultValues: {
167 firstName: '',
168 lastName: '',
169 },
170 onSubmit({ value }) {
171 // Do something with form data
172 console.log(value)
173 },
174 })
176```
177​
178> Here, the `tanstack-app-field` directive is taking the properties from `[tanstackField]` and `provide`ing them down to the `app-text-field` so that they can be more easily consumed as a component.