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

ui-libraries.md

87 lines2.3 KB

1---
2id: ui-libraries
3title: UI Libraries
4---
5​
6TanStack Form is headless. UI library integration consists of adapting a
7field's value, blur callback, change callback, invalid state, and errors to the
8component's prop names.
9​
10## Text inputs
11​
12Many Solid text-input components accept DOM-like props:
13​
14```tsx
15<form.Field name="name">
16 {(field) => (
17 <TextInput
18 name={field().name}
19 label="Name"
20 value={field().value}
21 onBlur={field().handleBlur}
22 onInput={(event) => field().handleChange(event.currentTarget.value)}
23 error={field()
24 .errors.map((error) => error.message)
25 .join(', ')}
26 />
27 )}
28</form.Field>
29```
30​
31Pass `value` instead of using a component's one-time default-value prop so the
32component reflects form state.
33​
34## Checkboxes and switches
35​
36DOM-style checkbox components expose `event.currentTarget.checked`:
37​
38```tsx
39<form.Field name="acceptedTerms">
40 {(field) => (
41 <Checkbox
42 checked={field().value}
43 onBlur={field().handleBlur}
44 onChange={(event) => field().handleChange(event.currentTarget.checked)}
45 aria-invalid={field().meta.isInvalid}
46 />
47 )}
48</form.Field>
49```
50​
51Some libraries call back with a boolean, a details object, or a union such as
52`boolean | 'indeterminate'`. Normalize that value at the boundary:
53​
54```tsx
55<Checkbox
56 checked={field().value}
57 onCheckedChange={(checked) => field().handleChange(checked === true)}
58/>
59```
60​
61## Selects, date pickers, and custom controls
62​
63The integration does not require a native event. Pass the value produced by the
64component to `field().handleChange`:
65​
66```tsx
67<DatePicker
68 value={field().value}
69 onValueChange={(nextDate) => field().handleChange(nextDate)}
70 onBlur={field().handleBlur}
71/>
72```
73​
74Choose a serializable form value appropriate for your application, then adapt
75the UI library's richer object at the component boundary if necessary.
76​
77## Reuse the adapter
78​
79If the same wiring appears throughout the application, extract a Solid
80component that accepts `Accessor<FieldWithValue<T>>` or register it with
81`createFormHook`.
82​
83For the helper types used when passing a form or field to an extracted
84component, see [Splitting forms](../../../splitting-forms).
85​
86The repository's `examples/solid/large-form` example demonstrates reusable
87field and form components with the v2 Solid adapter.