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

FieldComponentHelper.md

161 lines3.9 KB

1---
2id: FieldComponentHelper
3title: FieldComponentHelper
4---
5​
6Defined in: [packages/react-form/src/AppForm/getFormHookHelpers.public.ts:68](https://github.com/TanStack/form/blob/main/packages/react-form/src/AppForm/getFormHookHelpers.public.ts#L68)
7​
8Wraps components that accept a field API prop so App Form can supply that
9prop from field context.
10​
11## Properties
12​
13### loose
14​
15```ts
16loose: <TComponent, TProps, TFieldPropKey>(Component, fieldPropKey) => LooseInjectedFieldComponent<TComponent, TProps, TFieldPropKey>;
17```
18​
19Defined in: [packages/react-form/src/AppForm/getFormHookHelpers.public.ts:147](https://github.com/TanStack/form/blob/main/packages/react-form/src/AppForm/getFormHookHelpers.public.ts#L147)
20​
21Wraps a component for fields whose value type is assignable to the value
22type accepted by its field API prop.
23​
24The returned component omits `fieldPropKey` from its public props and reads
25that field API from the nearest App Form field context. Use this mode for a
26component that intentionally supports a broader range of field values.
27​
28#### Type Parameters
29​
30##### TComponent
31​
32`TComponent` *extends* `AnyFieldComponent`
33​
34Library-managed. Do not specify explicitly.
35​
36##### TProps
37​
38`TProps` = `PropsOf`\<`TComponent`\>
39​
40Library-managed. Do not specify explicitly.
41​
42##### TFieldPropKey
43​
44`TFieldPropKey` *extends* `string` \| `number` \| `symbol` = `FieldValuePropKeys`\<`TProps`\>
45​
46Library-managed. Do not specify explicitly.
47​
48#### Parameters
49​
50##### Component
51​
52`TComponent`
53​
54The component whose field API prop should be injected.
55​
56##### fieldPropKey
57​
58`TFieldPropKey`
59​
60The prop that accepts the current field API.
61​
62#### Returns
63​
64`LooseInjectedFieldComponent`\<`TComponent`, `TProps`, `TFieldPropKey`\>
65​
66#### Example
67​
68```tsx
69import { getFormHookHelpers } from '@tanstack/react-form'
70import type { AnyFieldApi } from '@tanstack/react-form'
71​
72function FieldErrors({ field }: { field: AnyFieldApi }) {
73 return <span>{field.errors.map((error) => error.message).join(', ')}</span>
75​
76const { fieldComponent } = getFormHookHelpers()
77const Errors = fieldComponent.loose(FieldErrors, 'field')
78```
79​
80***
81​
82### strict
83​
84```ts
85strict: <TComponent, TProps, TFieldPropKey>(Component, fieldPropKey) => StrictInjectedFieldComponent<TComponent, TProps, TFieldPropKey>;
86```
87​
88Defined in: [packages/react-form/src/AppForm/getFormHookHelpers.public.ts:110](https://github.com/TanStack/form/blob/main/packages/react-form/src/AppForm/getFormHookHelpers.public.ts#L110)
89​
90Wraps a component for fields whose value type exactly matches the value
91type of its field API prop.
92​
93The returned component omits `fieldPropKey` from its public props and reads
94that field API from the nearest App Form field context. It must be rendered
95beneath an App Form or field-group `Field` or `ArrayField` component.
96​
97#### Type Parameters
98​
99##### TComponent
100​
101`TComponent` *extends* `AnyFieldComponent`
102​
103Library-managed. Do not specify explicitly.
104​
105##### TProps
106​
107`TProps` = `PropsOf`\<`TComponent`\>
108​
109Library-managed. Do not specify explicitly.
110​
111##### TFieldPropKey
112​
113`TFieldPropKey` *extends* `string` \| `number` \| `symbol` = `FieldValuePropKeys`\<`TProps`\>
114​
115Library-managed. Do not specify explicitly.
116​
117#### Parameters
118​
119##### Component
120​
121`TComponent`
122​
123The component whose field API prop should be injected.
124​
125##### fieldPropKey
126​
127`TFieldPropKey`
128​
129The prop that accepts the current field API.
130​
131#### Returns
132​
133`StrictInjectedFieldComponent`\<`TComponent`, `TProps`, `TFieldPropKey`\>
134​
135#### Example
136​
137```tsx
138import { getFormHookHelpers } from '@tanstack/react-form'
139import type { FieldWithValue } from '@tanstack/react-form'
140​
141function TextInput({
142 field,
143 label,
144}: {
145 field: FieldWithValue<string>
146 label: string
147}) {
148 return (
149 <label>
150 {label}
151 <input
152 value={field.value}
153 onChange={(event) => field.handleChange(event.target.value)}
154 />
155 </label>
156 )
158​
159const { fieldComponent } = getFormHookHelpers()
160const TextField = fieldComponent.strict(TextInput, 'field')
161```