Open History to see the last commit
1As a form grows, keeping every field in one component can make the code harder
2to navigate and maintain. You can move related fields into smaller components
3while keeping one form instance, but each new component boundary needs a type
4for the form or field it receives.
5β
6## Split a large form into sections
7β
8First define the form's reusable options with `formOptions`. In this example,
9the same module also exports a named form type derived from those options. The
10parent can add page-specific options, such as `onSubmit`, when it creates the
11form, while extracted sections can import the same form type.
12β
13<!-- ::start:framework -->
14β
15# React
16β
17React's `ReactFormType` derives the form prop from the shared options. The
18result keeps the field names and values available to `form.Field` in the
19extracted component.
20β
21<!-- ::start:tabs variant="files" -->
22β
23```ts file="shared-form.ts"
24import { formOptions } from '@tanstack/react-form'
25import type { ReactFormType } from '@tanstack/react-form'
26β
27export const profileFormOptions = formOptions({
28 defaultValues: {
29 name: '',
30 address: {
31 street: '',
32 city: '',
33 },
34 },
35})
36β
37export type ProfileForm = ReactFormType<typeof profileFormOptions>
38```
39β
40```tsx file="AddressFields.tsx"
41import type { ProfileForm } from './shared-form'
42β
43interface AddressFieldsProps {
44 form: ProfileForm
45}
46β
47export function AddressFields({ form }: AddressFieldsProps) {
48 return (
49 <>
50 <form.Field name="address.street">
51 {(field) => (
52 <input
53 aria-label="Street"
54 value={field.value}
55 onChange={(event) => field.handleChange(event.target.value)}
56 />
57 )}
58 </form.Field>
59 <form.Field name="address.city">
60 {(field) => (
61 <input
62 aria-label="City"
63 value={field.value}
64 onChange={(event) => field.handleChange(event.target.value)}
65 />
66 )}
67 </form.Field>
68 </>
69 )
70}
71```
72β
73```tsx file="ProfileForm.tsx"
74import { useForm } from '@tanstack/react-form'
75import { AddressFields } from './AddressFields'
76import { profileFormOptions } from './shared-form'
77β
78export function ProfileForm() {
79 const form = useForm({
80 ...profileFormOptions,
81 onSubmit: ({ value }) => console.log(value),
82 })
83β
84 return <AddressFields form={form} />
85}
86```
87β
88<!-- ::end:tabs -->
89β
90> [!NOTE] Sharing between forms
91> If the section should work with multiple forms instead of only one, use [field groups](./framework/react/guides/field-groups).
92β
93# Preact
94β
95Preact's `PreactFormType` derives the form prop from the shared options. The
96result keeps the field names and values available to `form.Field` in the
97extracted component.
98β
99<!-- ::start:tabs variant="files" -->
100β
101```ts file="shared-form.ts"
102import { formOptions } from '@tanstack/preact-form'
103import type { PreactFormType } from '@tanstack/preact-form'
104β
105export const profileFormOptions = formOptions({
106 defaultValues: {
107 name: '',
108 address: {
109 street: '',
110 city: '',
111 },
112 },
113})
114β
115export type ProfileForm = PreactFormType<typeof profileFormOptions>
116```
117β
118```tsx file="AddressFields.tsx"
119import type { ProfileForm } from './shared-form'
120β
121interface AddressFieldsProps {
122 form: ProfileForm
123}
124β
125export function AddressFields({ form }: AddressFieldsProps) {
126 return (
127 <>
128 <form.Field name="address.street">
129 {(field) => (
130 <input
131 aria-label="Street"
132 value={field.value}
133 onInput={(event) => field.handleChange(event.currentTarget.value)}
134 />
135 )}
136 </form.Field>
137 <form.Field name="address.city">
138 {(field) => (
139 <input
140 aria-label="City"
141 value={field.value}
142 onInput={(event) => field.handleChange(event.currentTarget.value)}
143 />
144 )}
145 </form.Field>
146 </>
147 )
148}
149```
150β
151```tsx file="ProfileForm.tsx"
152import { useForm } from '@tanstack/preact-form'
153import { AddressFields } from './AddressFields'
154import { profileFormOptions } from './shared-form'
155β
156export function ProfileForm() {
157 const form = useForm({
158 ...profileFormOptions,
159 onSubmit: ({ value }) => console.log(value),
160 })
161β
162 return <AddressFields form={form} />
163}
164```
165β
166<!-- ::end:tabs -->
167β
168# Vue
169β
170Vue's `VueFormType` derives the form prop from the shared options. The result
171keeps the field names and values available to `form.Field` in the extracted
172component.
173β
174<!-- ::start:tabs variant="files" -->
175β
176```ts file="shared-form.ts"
177import { formOptions } from '@tanstack/vue-form'
178import type { VueFormType } from '@tanstack/vue-form'
179β
180export const profileFormOptions = formOptions({
181 defaultValues: {
182 name: '',
183 address: {
184 street: '',
185 city: '',
186 },
187 },
188})
189β
190export type ProfileForm = VueFormType<typeof profileFormOptions>
191```
192β
193```vue file="AddressFields.vue"
194<script setup lang="ts">
195import { computed } from 'vue'
196import type { ProfileForm } from './shared-form'
197β
198const props = defineProps<{
199 form: ProfileForm
200}>()
201β
202const form = computed(() => props.form)
203</script>
204β
205<template>
206 <form.Field name="address.street" v-slot="{ field }">
207 <input
208 aria-label="Street"
209 :value="field.value"
210 @input="field.handleChange(($event.target as HTMLInputElement).value)"
211 />
212 </form.Field>
213 <form.Field name="address.city" v-slot="{ field }">
214 <input
215 aria-label="City"
216 :value="field.value"
217 @input="field.handleChange(($event.target as HTMLInputElement).value)"
218 />
219 </form.Field>
220</template>
221```
222β
223```vue file="ProfileForm.vue"
224<script setup lang="ts">
225import { useForm } from '@tanstack/vue-form'
226import AddressFields from './AddressFields.vue'
227import { profileFormOptions } from './shared-form'
228β
229const form = useForm({
230 ...profileFormOptions,
231 onSubmit: ({ value }) => console.log(value),
232})
233</script>
234β
235<template>
236 <AddressFields :form="form" />
237</template>
238```
239β
240<!-- ::end:tabs -->
241β
242# Angular
243β
244Angular's `AngularFormType` derives the form input from the shared options. The
245result keeps the field names and values available to `TanStackField` in the
246extracted component.
247β
248<!-- ::start:tabs variant="files" -->
249β
250```ts file="shared-form.ts"
251import { formOptions } from '@tanstack/angular-form'
252import type { AngularFormType } from '@tanstack/angular-form'
253β
254export const profileFormOptions = formOptions({
255 defaultValues: {
256 name: '',
257 address: {
258 street: '',
259 city: '',
260 },
261 },
262})
263β
264export type ProfileForm = AngularFormType<typeof profileFormOptions>
265```
266β
267```ts file="address-fields.component.ts"
268import { ChangeDetectionStrategy, Component, input } from '@angular/core'
269import { TanStackField } from '@tanstack/angular-form'
270import type { ProfileForm } from './shared-form'
271β
272@Component({
273 selector: 'app-address-fields',
274 standalone: true,
275 changeDetection: ChangeDetectionStrategy.OnPush,
276 imports: [TanStackField],
277 template: `
278 <ng-container
279 [tanstackField]="form()"
280 name="address.street"
281 #street="field"
282 >
283 <input
284 aria-label="Street"
285 [value]="street.api.value"
286 (input)="street.api.handleChange($any($event).target.value)"
287 />
288 </ng-container>
289 <ng-container [tanstackField]="form()" name="address.city" #city="field">
290 <input
291 aria-label="City"
292 [value]="city.api.value"
293 (input)="city.api.handleChange($any($event).target.value)"
294 />
295 </ng-container>
296 `,
297})
298export class AddressFieldsComponent {
299 form = input.required<ProfileForm>()
300}
301```
302β
303```ts file="profile-form.component.ts"
304import { ChangeDetectionStrategy, Component } from '@angular/core'
305import { injectForm } from '@tanstack/angular-form'
306import { AddressFieldsComponent } from './address-fields.component'
307import { profileFormOptions } from './shared-form'
308β
309@Component({
310 selector: 'app-profile-form',
311 standalone: true,
312 changeDetection: ChangeDetectionStrategy.OnPush,
313 imports: [AddressFieldsComponent],
314 template: `<app-address-fields [form]="form" />`,
315})
316export class ProfileFormComponent {
317 form = injectForm({
318 ...profileFormOptions,
319 onSubmit: ({ value }) => console.log(value),
320 })
321}
322```
323β
324<!-- ::end:tabs -->
325β
326# Solid
327β
328Solid's `SolidFormType` derives the form prop from the shared options. The
329result keeps the field names and values available to `form.Field` in the
330extracted component.
331β
332<!-- ::start:tabs variant="files" -->
333β
334```ts file="shared-form.ts"
335import { formOptions } from '@tanstack/solid-form'
336import type { SolidFormType } from '@tanstack/solid-form'
337β
338export const profileFormOptions = formOptions({
339 defaultValues: {
340 name: '',
341 address: {
342 street: '',
343 city: '',
344 },
345 },
346})
347β
348export type ProfileForm = SolidFormType<typeof profileFormOptions>
349```
350β
351```tsx file="AddressFields.tsx"
352import type { ProfileForm } from './shared-form'
353β
354interface AddressFieldsProps {
355 form: ProfileForm
356}
357β
358export function AddressFields(props: AddressFieldsProps) {
359 return (
360 <>
361 <props.form.Field name="address.street">
362 {(field) => (
363 <input
364 aria-label="Street"
365 value={field().value}
366 onInput={(event) => field().handleChange(event.currentTarget.value)}
367 />
368 )}
369 </props.form.Field>
370 <props.form.Field name="address.city">
371 {(field) => (
372 <input
373 aria-label="City"
374 value={field().value}
375 onInput={(event) => field().handleChange(event.currentTarget.value)}
376 />
377 )}
378 </props.form.Field>
379 </>
380 )
381}
382```
383β
384```tsx file="ProfileForm.tsx"
385import { createForm } from '@tanstack/solid-form'
386import { AddressFields } from './AddressFields'
387import { profileFormOptions } from './shared-form'
388β
389export function ProfileForm() {
390 const form = createForm(() => ({
391 ...profileFormOptions,
392 onSubmit: ({ value }) => console.log(value),
393 }))
394β
395 return <AddressFields form={form} />
396}
397```
398β
399<!-- ::end:tabs -->
400β
401# Lit
402β
403Litβs `LitFormType` derives a controller type from the shared options. The
404result keeps the field names and values available to the controller's `field`
405method in the extracted render function.
406β
407<!-- ::start:tabs variant="files" -->
408β
409```ts file="shared-form.ts"
410import { formOptions } from '@tanstack/lit-form'
411import type { LitFormType } from '@tanstack/lit-form'
412β
413export const profileFormOptions = formOptions({
414 defaultValues: {
415 name: '',
416 address: {
417 street: '',
418 city: '',
419 },
420 },
421})
422β
423export type ProfileForm = LitFormType<typeof profileFormOptions>
424```
425β
426```ts file="address-fields.ts"
427import { html } from 'lit'
428import type { ProfileForm } from './shared-form'
429β
430export function addressFields(form: ProfileForm) {
431 return html`
432 ${form.field(
433 { name: 'address.street' },
434 (field) => html`
435 <input
436 aria-label="Street"
437 .value=${field.value}
438 @input=${(event: InputEvent) =>
439 field.handleChange((event.currentTarget as HTMLInputElement).value)}
440 />
441 `,
442 )}
443 ${form.field(
444 { name: 'address.city' },
445 (field) => html`
446 <input
447 aria-label="City"
448 .value=${field.value}
449 @input=${(event: InputEvent) =>
450 field.handleChange((event.currentTarget as HTMLInputElement).value)}
451 />
452 `,
453 )}
454 `
455}
456```
457β
458```ts file="profile-form.ts"
459import { LitElement, html } from 'lit'
460import { customElement } from 'lit/decorators.js'
461import { TanStackFormController } from '@tanstack/lit-form'
462import { addressFields } from './address-fields'
463import { profileFormOptions } from './shared-form'
464β
465@customElement('profile-form')
466export class ProfileForm extends LitElement {
467 private form = new TanStackFormController(this, {
468 ...profileFormOptions,
469 onSubmit: ({ value }) => console.log(value),
470 })
471β
472 render() {
473 return html`${addressFields(this.form)}`
474 }
475}
476```
477β
478<!-- ::end:tabs -->
479β
480# Svelte
481β
482Svelte's `SvelteFormType` derives the form prop from the shared options. The
483result keeps the field names and values available to `form.Field` in the
484extracted component.
485β
486<!-- ::start:tabs variant="files" -->
487β
488```ts file="shared-form.ts"
489import { formOptions } from '@tanstack/svelte-form'
490import type { SvelteFormType } from '@tanstack/svelte-form'
491β
492export const profileFormOptions = formOptions({
493 defaultValues: {
494 name: '',
495 address: {
496 street: '',
497 city: '',
498 },
499 },
500})
501β
502export type ProfileForm = SvelteFormType<typeof profileFormOptions>
503```
504β
505```svelte file="AddressFields.svelte"
506<script lang="ts">
507 import type { ProfileForm } from './shared-form.js'
508β
509 const { form }: { form: ProfileForm } = $props()
510</script>
511β
512<form.Field name="address.street">
513 {#snippet children(field)}
514 <input
515 aria-label="Street"
516 value={field.value}
517 oninput={(event) => field.handleChange(event.currentTarget.value)}
518 />
519 {/snippet}
520</form.Field>
521<form.Field name="address.city">
522 {#snippet children(field)}
523 <input
524 aria-label="City"
525 value={field.value}
526 oninput={(event) => field.handleChange(event.currentTarget.value)}
527 />
528 {/snippet}
529</form.Field>
530```
531β
532```svelte file="ProfileForm.svelte"
533<script lang="ts">
534 import { createForm } from '@tanstack/svelte-form'
535 import AddressFields from './AddressFields.svelte'
536 import { profileFormOptions } from './shared-form.js'
537β
538 const form = createForm(() => ({
539 ...profileFormOptions,
540 onSubmit: ({ value }) => console.log(value),
541 }))
542</script>
543β
544<AddressFields {form} />
545```
546β
547<!-- ::end:tabs -->
548β
549<!-- ::end:framework -->
550β
551## Extract a field component by value type
552β
553After splitting the form into sections, you may still have repeated controls
554for values such as strings, numbers, or dates. These controls don't need to
555know the form's complete data shape or the path of a particular field. They
556only need the type of the value they read and update.
557β
558`FieldWithValue<T>` describes a field by the value type a reusable component
559handles. This preserves type checking when the component reads or updates the
560value without tying it to a particular field path or form shape.
561β
562<!-- ::start:framework -->
563β
564# React
565β
566Type the React component's field prop as `FieldWithValue<string>`, then pass it
567the field from the `form.Field` render prop.
568β
569<!-- ::start:tabs variant="files" -->
570β
571```tsx file="TextField.tsx"
572import type { FieldWithValue } from '@tanstack/react-form'
573β
574interface TextFieldProps {
575 field: FieldWithValue<string>
576 label: string
577}
578β
579export function TextField({ field, label }: TextFieldProps) {
580 return (
581 <label>
582 <span>{label}</span>
583 <input
584 name={field.name}
585 value={field.value}
586 onBlur={field.handleBlur}
587 onChange={(event) => field.handleChange(event.target.value)}
588 />
589 </label>
590 )
591}
592```
593β
594```tsx file="NameField.tsx"
595import { TextField } from './TextField'
596import type { ProfileForm } from './shared-form'
597β
598export function NameField({ form }: { form: ProfileForm }) {
599 return (
600 <form.Field name="name">
601 {(field) => <TextField field={field} label="Name" />}
602 </form.Field>
603 )
604}
605```
606β
607<!-- ::end:tabs -->
608β
609# Preact
610β
611Type the Preact component's field prop as `FieldWithValue<string>`, then pass it
612the field from the `form.Field` render prop.
613β
614<!-- ::start:tabs variant="files" -->
615β
616```tsx file="TextField.tsx"
617import type { FieldWithValue } from '@tanstack/preact-form'
618β
619interface TextFieldProps {
620 field: FieldWithValue<string>
621 label: string
622}
623β
624export function TextField({ field, label }: TextFieldProps) {
625 return (
626 <label>
627 <span>{label}</span>
628 <input
629 name={field.name}
630 value={field.value}
631 onBlur={field.handleBlur}
632 onInput={(event) => field.handleChange(event.currentTarget.value)}
633 />
634 </label>
635 )
636}
637```
638β
639```tsx file="NameField.tsx"
640import { TextField } from './TextField'
641import type { ProfileForm } from './shared-form'
642β
643export function NameField({ form }: { form: ProfileForm }) {
644 return (
645 <form.Field name="name">
646 {(field) => <TextField field={field} label="Name" />}
647 </form.Field>
648 )
649}
650```
651β
652<!-- ::end:tabs -->
653β
654# Vue
655β
656Type the Vue component's field prop as `FieldWithValue<string>`, then pass it
657the field from the `form.Field` slot.
658β
659<!-- ::start:tabs variant="files" -->
660β
661```vue file="TextField.vue"
662<script setup lang="ts">
663import type { FieldWithValue } from '@tanstack/vue-form'
664β
665defineProps<{
666 field: FieldWithValue<string>
667 label: string
668}>()
669</script>
670β
671<template>
672 <label>
673 <span>{{ label }}</span>
674 <input
675 :name="field.name"
676 :value="field.value"
677 @blur="field.handleBlur"
678 @input="field.handleChange(($event.target as HTMLInputElement).value)"
679 />
680 </label>
681</template>
682```
683β
684```vue file="NameField.vue"
685<script setup lang="ts">
686import { computed } from 'vue'
687import TextField from './TextField.vue'
688import type { ProfileForm } from './shared-form'
689β
690const props = defineProps<{ form: ProfileForm }>()
691const form = computed(() => props.form)
692</script>
693β
694<template>
695 <form.Field name="name" v-slot="{ field }">
696 <TextField :field="field" label="Name" />
697 </form.Field>
698</template>
699```
700β
701<!-- ::end:tabs -->
702β
703# Angular
704β
705Angular field components can declare their value type through
706`injectField<string>()`. The `tanstack-app-field` directive provides the field
707to that component, so it doesn't need a form input of its own.
708β
709<!-- ::start:tabs variant="files" -->
710β
711```ts file="text-field.component.ts"
712import { ChangeDetectionStrategy, Component, input } from '@angular/core'
713import { injectField } from '@tanstack/angular-form'
714β
715@Component({
716 selector: 'app-text-field',
717 standalone: true,
718 changeDetection: ChangeDetectionStrategy.OnPush,
719 template: `
720 <label>
721 <span>{{ label() }}</span>
722 <input
723 [name]="field.api.name"
724 [value]="field.api.value"
725 (blur)="field.api.handleBlur()"
726 (input)="field.api.handleChange($any($event).target.value)"
727 />
728 </label>
729 `,
730})
731export class TextFieldComponent {
732 label = input.required<string>()
733 field = injectField<string>()
734}
735```
736β
737```ts file="name-field.component.ts"
738import { ChangeDetectionStrategy, Component, input } from '@angular/core'
739import { TanStackAppField, TanStackField } from '@tanstack/angular-form'
740import { TextFieldComponent } from './text-field.component'
741import type { ProfileForm } from './shared-form'
742β
743@Component({
744 selector: 'app-name-field',
745 standalone: true,
746 changeDetection: ChangeDetectionStrategy.OnPush,
747 imports: [TanStackAppField, TanStackField, TextFieldComponent],
748 template: `
749 <app-text-field
750 tanstack-app-field
751 [tanstackField]="form()"
752 name="name"
753 label="Name"
754 />
755 `,
756})
757export class NameFieldComponent {
758 form = input.required<ProfileForm>()
759}
760```
761β
762<!-- ::end:tabs -->
763β
764# Solid
765β
766Solid's field render prop is an accessor, so the component accepts an
767`Accessor<FieldWithValue<string>>`.
768β
769<!-- ::start:tabs variant="files" -->
770β
771```tsx file="TextField.tsx"
772import type { Accessor } from 'solid-js'
773import type { FieldWithValue } from '@tanstack/solid-form'
774β
775interface TextFieldProps {
776 field: Accessor<FieldWithValue<string>>
777 label: string
778}
779β
780export function TextField(props: TextFieldProps) {
781 return (
782 <label>
783 <span>{props.label}</span>
784 <input
785 name={props.field().name}
786 value={props.field().value}
787 onBlur={props.field().handleBlur}
788 onInput={(event) =>
789 props.field().handleChange(event.currentTarget.value)
790 }
791 />
792 </label>
793 )
794}
795```
796β
797```tsx file="NameField.tsx"
798import { TextField } from './TextField'
799import type { ProfileForm } from './shared-form'
800β
801export function NameField(props: { form: ProfileForm }) {
802 return (
803 <props.form.Field name="name">
804 {(field) => <TextField field={field} label="Name" />}
805 </props.form.Field>
806 )
807}
808```
809β
810<!-- ::end:tabs -->
811β
812# Lit
813β
814Type the Lit render helper's field parameter as `FieldWithValue<string>`, then
815call it from the controller's `field` render callback.
816β
817<!-- ::start:tabs variant="files" -->
818β
819```ts file="text-field.ts"
820import { html } from 'lit'
821import type { FieldWithValue } from '@tanstack/lit-form'
822β
823export function textField(field: FieldWithValue<string>, label: string) {
824 return html`
825 <label>
826 <span>${label}</span>
827 <input
828 name=${field.name}
829 .value=${field.value}
830 @blur=${() => field.handleBlur()}
831 @input=${(event: InputEvent) =>
832 field.handleChange((event.currentTarget as HTMLInputElement).value)}
833 />
834 </label>
835 `
836}
837```
838β
839```ts file="name-field.ts"
840import { textField } from './text-field'
841import type { ProfileForm } from './shared-form'
842β
843export function nameField(form: ProfileForm) {
844 return form.field({ name: 'name' }, (field) => textField(field, 'Name'))
845}
846```
847β
848<!-- ::end:tabs -->
849β
850# Svelte
851β
852Type the Svelte component's field prop as `FieldWithValue<string>`, then pass it
853the field from the `form.Field` snippet.
854β
855<!-- ::start:tabs variant="files" -->
856β
857```svelte file="TextField.svelte"
858<script lang="ts">
859 import type { FieldWithValue } from '@tanstack/svelte-form'
860β
861 interface Props {
862 field: FieldWithValue<string>
863 label: string
864 }
865β
866 const { field, label }: Props = $props()
867</script>
868β
869<label>
870 <span>{label}</span>
871 <input
872 name={field.name}
873 value={field.value}
874 onblur={field.handleBlur}
875 oninput={(event) => field.handleChange(event.currentTarget.value)}
876 />
877</label>
878```
879β
880```svelte file="NameField.svelte"
881<script lang="ts">
882 import TextField from './TextField.svelte'
883 import type { ProfileForm } from './shared-form.js'
884β
885 const { form }: { form: ProfileForm } = $props()
886</script>
887β
888<form.Field name="name">
889 {#snippet children(field)}
890 <TextField {field} label="Name" />
891 {/snippet}
892</form.Field>
893```
894β
895<!-- ::end:tabs -->
896β
897<!-- ::end:framework -->
898β
899## Accept any form or field
900β
901Some extracted controls behave the same regardless of the form's data. An
902error display only needs field metadata, while a submit or reset button may
903only need form state and methods. Giving these controls the concrete
904`ProfileForm` type would couple them to details they don't use.
905β
906Use `AnyFieldApi` when a control doesn't depend on the field's value type. For
907form-level controls, use the adapter's `Any*FormApi` type. These types
908deliberately remove information about values and field paths, so use a concrete
909type when a component reads either one.
910β
911<!-- ::start:framework -->
912β
913# React
914β
915React provides `AnyReactFormApi` for form-level components. Pass the field from
916`form.Field` to `FieldErrors`, and pass the form instance to `SubmitButton`.
917β
918<!-- ::start:tabs variant="files" -->
919β
920```tsx file="FieldErrors.tsx"
921import type { AnyFieldApi } from '@tanstack/react-form'
922β
923export function FieldErrors({ field }: { field: AnyFieldApi }) {
924 return (
925 <small role="alert" aria-live="polite">
926 {field.errors.map((error) => error.message).join(', ')}
927 </small>
928 )
929}
930```
931β
932```tsx file="SubmitButton.tsx"
933import type { AnyReactFormApi } from '@tanstack/react-form'
934β
935export function SubmitButton({ form }: { form: AnyReactFormApi }) {
936 return (
937 <form.Subscribe
938 selector={(state) => [state.canSubmit, state.isSubmitting] as const}
939 >
940 {([canSubmit, isSubmitting]) => (
941 <button type="submit" disabled={!canSubmit || isSubmitting}>
942 {isSubmitting ? 'Submitting...' : 'Submit'}
943 </button>
944 )}
945 </form.Subscribe>
946 )
947}
948```
949β
950<!-- ::end:tabs -->
951β
952# Preact
953β
954Preact provides `AnyPreactFormApi` for form-level components. Pass the field
955from `form.Field` to `FieldErrors`, and pass the form instance to
956`SubmitButton`.
957β
958<!-- ::start:tabs variant="files" -->
959β
960```tsx file="FieldErrors.tsx"
961import type { AnyFieldApi } from '@tanstack/preact-form'
962β
963export function FieldErrors({ field }: { field: AnyFieldApi }) {
964 return (
965 <small role="alert" aria-live="polite">
966 {field.errors.map((error) => error.message).join(', ')}
967 </small>
968 )
969}
970```
971β
972```tsx file="SubmitButton.tsx"
973import type { AnyPreactFormApi } from '@tanstack/preact-form'
974β
975export function SubmitButton({ form }: { form: AnyPreactFormApi }) {
976 return (
977 <form.Subscribe
978 selector={(state) => [state.canSubmit, state.isSubmitting] as const}
979 >
980 {([canSubmit, isSubmitting]) => (
981 <button type="submit" disabled={!canSubmit || isSubmitting}>
982 {isSubmitting ? 'Submitting...' : 'Submit'}
983 </button>
984 )}
985 </form.Subscribe>
986 )
987}
988```
989β
990<!-- ::end:tabs -->
991β
992# Vue
993β
994Vue provides `AnyVueFormApi` for form-level components. Pass the field from the
995`form.Field` slot to `FieldErrors`, and pass the form instance to
996`SubmitButton`.
997β
998<!-- ::start:tabs variant="files" -->
999β
1000```vue file="FieldErrors.vue"
1001<script setup lang="ts">
1002import type { AnyFieldApi } from '@tanstack/vue-form'
1003β
1004defineProps<{ field: AnyFieldApi }>()
1005</script>
1006β
1007<template>
1008 <small role="alert" aria-live="polite">
1009 {{ field.errors.map((error) => error.message).join(', ') }}
1010 </small>
1011</template>
1012```
1013β
1014```vue file="SubmitButton.vue"
1015<script setup lang="ts">
1016import { computed } from 'vue'
1017import type { AnyVueFormApi } from '@tanstack/vue-form'
1018β
1019const props = defineProps<{ form: AnyVueFormApi }>()
1020const form = computed(() => props.form)
1021</script>
1022β
1023<template>
1024 <form.Subscribe
1025 :selector="(state) => [state.canSubmit, state.isSubmitting] as const"
1026 v-slot="[canSubmit, isSubmitting]"
1027 >
1028 <button type="submit" :disabled="!canSubmit || isSubmitting">
1029 {{ isSubmitting ? 'Submitting...' : 'Submit' }}
1030 </button>
1031 </form.Subscribe>
1032</template>
1033```
1034β
1035<!-- ::end:tabs -->
1036β
1037# Angular
1038β
1039For a field control that doesn't read the value, use `injectField<unknown>()`
1040to receive the field and react to its metadata. Angular provides
1041`AnyAngularFormApi` when a separate component only needs form methods.
1042β
1043<!-- ::start:tabs variant="files" -->
1044β
1045```ts file="field-errors.component.ts"
1046import { ChangeDetectionStrategy, Component } from '@angular/core'
1047import { injectField } from '@tanstack/angular-form'
1048β
1049@Component({
1050 selector: 'app-field-errors',
1051 standalone: true,
1052 changeDetection: ChangeDetectionStrategy.OnPush,
1053 template: `
1054 <small role="alert" aria-live="polite">
1055 @for (error of field.api.errors; track error) {
1056 {{ error.message }}
1057 }
1058 </small>
1059 `,
1060})
1061export class FieldErrorsComponent {
1062 field = injectField<unknown>()
1063}
1064```
1065β
1066```ts file="reset-button.component.ts"
1067import { ChangeDetectionStrategy, Component, input } from '@angular/core'
1068import type { AnyAngularFormApi } from '@tanstack/angular-form'
1069β
1070@Component({
1071 selector: 'app-reset-button',
1072 standalone: true,
1073 changeDetection: ChangeDetectionStrategy.OnPush,
1074 template: `<button type="button" (click)="form().reset()">Reset</button>`,
1075})
1076export class ResetButtonComponent {
1077 form = input.required<AnyAngularFormApi>()
1078}
1079```
1080β
1081<!-- ::end:tabs -->
1082β
1083# Solid
1084β
1085Solid provides `AnySolidFormApi` for form-level components. Its field render
1086prop remains an accessor, so `FieldErrors` accepts an
1087`Accessor<AnyFieldApi>`.
1088β
1089<!-- ::start:tabs variant="files" -->
1090β
1091```tsx file="FieldErrors.tsx"
1092import type { Accessor } from 'solid-js'
1093import type { AnyFieldApi } from '@tanstack/solid-form'
1094β
1095export function FieldErrors(props: { field: Accessor<AnyFieldApi> }) {
1096 return (
1097 <small role="alert" aria-live="polite">
1098 {props
1099 .field()
1100 .errors.map((error) => error.message)
1101 .join(', ')}
1102 </small>
1103 )
1104}
1105```
1106β
1107```tsx file="SubmitButton.tsx"
1108import type { AnySolidFormApi } from '@tanstack/solid-form'
1109β
1110export function SubmitButton(props: { form: AnySolidFormApi }) {
1111 return (
1112 <props.form.Subscribe
1113 selector={(state) => [state.canSubmit, state.isSubmitting] as const}
1114 >
1115 {(state) => (
1116 <button type="submit" disabled={!state()[0] || state()[1]}>
1117 {state()[1] ? 'Submitting...' : 'Submit'}
1118 </button>
1119 )}
1120 </props.form.Subscribe>
1121 )
1122}
1123```
1124β
1125<!-- ::end:tabs -->
1126β
1127# Lit
1128β
1129Lit doesn't expose an `AnyLitFormApi` controller type. Use `AnyFieldApi` for a
1130field render helper, or pass the controller's core `form.api` object to a
1131helper typed with `AnyFormApi`.
1132β
1133<!-- ::start:tabs variant="files" -->
1134β
1135```ts file="field-errors.ts"
1136import { html } from 'lit'
1137import type { AnyFieldApi } from '@tanstack/lit-form'
1138β
1139export function fieldErrors(field: AnyFieldApi) {
1140 return html`
1141 <small role="alert" aria-live="polite">
1142 ${field.errors.map((error) => error.message).join(', ')}
1143 </small>
1144 `
1145}
1146```
1147β
1148```ts file="reset-button.ts"
1149import { html } from 'lit'
1150import type { AnyFormApi } from '@tanstack/lit-form'
1151β
1152export function resetButton(form: AnyFormApi) {
1153 return html`
1154 <button type="button" @click=${() => form.reset()}>Reset</button>
1155 `
1156}
1157```
1158β
1159<!-- ::end:tabs -->
1160β
1161# Svelte
1162β
1163Svelte provides `AnySvelteFormApi` for form-level components. Pass the field
1164from the `form.Field` snippet to `FieldErrors`, and pass the form instance to
1165`SubmitButton`.
1166β
1167<!-- ::start:tabs variant="files" -->
1168β
1169```svelte file="FieldErrors.svelte"
1170<script lang="ts">
1171 import type { AnyFieldApi } from '@tanstack/svelte-form'
1172β
1173 const { field }: { field: AnyFieldApi } = $props()
1174</script>
1175β
1176<small role="alert" aria-live="polite">
1177 {field.errors.map((error) => error.message).join(', ')}
1178</small>
1179```
1180β
1181```svelte file="SubmitButton.svelte"
1182<script lang="ts">
1183 import type { AnySvelteFormApi } from '@tanstack/svelte-form'
1184β
1185 const { form }: { form: AnySvelteFormApi } = $props()
1186</script>
1187β
1188<form.Subscribe
1189 selector={(state) => [state.canSubmit, state.isSubmitting] as const}
1190>
1191 {#snippet children([canSubmit, isSubmitting])}
1192 <button type="submit" disabled={!canSubmit || isSubmitting}>
1193 {isSubmitting ? 'Submitting...' : 'Submit'}
1194 </button>
1195 {/snippet}
1196</form.Subscribe>
1197```
1198β
1199<!-- ::end:tabs -->
1200β
1201<!-- ::end:framework -->