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

FieldGroupDefinition.md

103 lines2.7 KB

1---
2id: FieldGroupDefinition
3title: FieldGroupDefinition
4---
5​
6Defined in: [packages/react-form/src/FieldGroup/withFields.public.ts:118](https://github.com/TanStack/form/blob/main/packages/react-form/src/FieldGroup/withFields.public.ts#L118)
7​
8## Type Parameters
9​
10### TFields
11​
12`TFields` *extends* `FieldGroupFields`
13​
14### TFieldComponents
15​
16`TFieldComponents` *extends* [`ReactComponentTree`](../type-aliases/ReactComponentTree.md)
17​
18## Properties
19​
20### bindComponent
21​
22```ts
23bindComponent: FieldGroupWithFieldsFn<ReactFieldGroup<TFields, TFieldComponents>>;
24```
25​
26Defined in: [packages/react-form/src/FieldGroup/withFields.public.ts:191](https://github.com/TanStack/form/blob/main/packages/react-form/src/FieldGroup/withFields.public.ts#L191)
27​
28Binds a component's field-group API prop to concrete paths in a parent
29form.
30​
31The returned component accepts the original component props except for the
32injected field-group API prop and adds a `form` prop. When every virtual
33field name is already a compatible form path, those paths bind
34automatically when the bindings prop is omitted. A complete bindings map
35can still reroute them. Otherwise, the injected prop name is reused for the
36required virtual-to-concrete field binding map.
37​
38#### Example
39​
40```tsx
41const passwordFieldGroup = defineFieldGroup(({ strict }) => ({
42 password: strict<string>(),
43 confirmPassword: strict<string>(),
44}))
45​
46interface PasswordFieldsProps {
47 fields: typeof passwordFieldGroup.fields
48 legend: string
50​
51function PasswordFields({ fields, legend }: PasswordFieldsProps) {
52 return (
53 <fieldset>
54 <legend>{legend}</legend>
55 <fields.Field name="password">
56 {(field) => (
57 <input
58 type="password"
59 value={field.value}
60 onChange={(event) => field.handleChange(event.target.value)}
61 />
62 )}
63 </fields.Field>
64 <fields.Field name="confirmPassword">
65 {(field) => (
66 <input
67 type="password"
68 value={field.value}
69 onChange={(event) => field.handleChange(event.target.value)}
70 />
71 )}
72 </fields.Field>
73 </fieldset>
74 )
76​
77const PasswordSection = passwordFieldGroup.bindComponent(
78 PasswordFields,
79 'fields',
81​
82<PasswordSection
83 form={form}
84 legend="Choose a password"
85 fields={{
86 password: 'account.password',
87 confirmPassword: 'account.confirmPassword',
88 }}
89/>
90```
91​
92***
93​
94### fields
95​
96```ts
97fields: ReactFieldGroup<TFields, TFieldComponents>;
98```
99​
100Defined in: [packages/react-form/src/FieldGroup/withFields.public.ts:126](https://github.com/TanStack/form/blob/main/packages/react-form/src/FieldGroup/withFields.public.ts#L126)
101​
102The virtual field-group API injected into the component passed to
103`bindComponent`.