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

splitting-forms.md

1,201 lines29.0 KB

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
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 )
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} />
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
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 )
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} />
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>()
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 })
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
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 )
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} />
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 `
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 }
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
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 )
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 )
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
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 )
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 )
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>()
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>()
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
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 )
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 )
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 `
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'))
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 )
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 )
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 )
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 )
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]"
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 }}
1058 </small>
1059 `,
1061export class FieldErrorsComponent {
1062 field = injectField<unknown>()
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>`,
1076export class ResetButtonComponent {
1077 form = input.required<AnyAngularFormApi>()
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>
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}
1115 {(state) => (
1116 <button type="submit" disabled={!state()[0] || state()[1]}>
1117 {state()[1] ? 'Submitting...' : 'Submit'}
1118 </button>
1119 )}
1120 </props.form.Subscribe>
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>
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>
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}
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 -->