Skip to content

Commit 4a49d2e

Browse files
committed
docs: form composition update
1 parent e79b4f2 commit 4a49d2e

File tree

5 files changed

+66
-40
lines changed

5 files changed

+66
-40
lines changed

docs/framework/react/guides/form-composition.md

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ function App() {
103103
}
104104
```
105105

106-
This not only allows you to reuse the UI of your shared component, but retains the type-safety you'd expect from TanStack Form: Mistyping `name` will result in a TypeScript error.
106+
This not only allows you to reuse the UI of your shared component, but retains the type-safety you'd expect from TanStack Form: Mistyping `firstName` will result in a TypeScript error.
107107

108108
#### A note on performance
109109

@@ -160,6 +160,68 @@ function App() {
160160
}
161161
```
162162

163+
### Extending custom appForm
164+
165+
It is quite common for platform teams to ship pre built appForms. It can be exported from a library in a monorepo or as a standalone package on npm.
166+
167+
```tsx weyland-yutan-corp/forms.tsx
168+
import { createFormHook, createFormHookContexts } from '@tanstack/react-form'
169+
170+
// fields
171+
import { UserIdField } from './FieldComponents/UserIdField'
172+
173+
// components
174+
import { SubmitButton } from './FormComponents/SubmitButton'
175+
176+
export const { fieldContext, formContext, useFieldContext, useFormContext } =
177+
createFormHookContexts()
178+
179+
const ProfileForm = createFormHook({
180+
fieldContext,
181+
formContext,
182+
fieldComponents: { UserIdField },
183+
formComponents: { SubmitButton },
184+
})
185+
186+
export default ProfileForm
187+
```
188+
189+
There is a situation that you might have a field exclusive to a downstream dev team, in such a case you can extend the AppForm like so.
190+
191+
1 - Create new AppForm fields
192+
193+
```tsx AppForm.tsx
194+
// imported from the same AppForm you want to extend
195+
import { useFieldContext } from 'weyland-yutan-corp/forms'
196+
197+
export function CustomTextField({ label }: { label: string }) {
198+
const field = useFieldContext<string>()
199+
return (
200+
<div>
201+
<label>{/* rest of component */}</label>
202+
</div>
203+
)
204+
}
205+
```
206+
207+
2 - Extend the AppForm
208+
209+
```tsx AppForm.tsx
210+
// notice the same import as above
211+
import ProfileForm from 'Weyland-Yutan-corp/forms'
212+
213+
import { CustomTextField } from './FieldComponents/CustomTextField'
214+
import { CustomSubmit } from './FormComponents/CustomSubmit'
215+
216+
export const { useAppForm } = ProfileForm.extendForm({
217+
fieldComponents: { CustomTextField },
218+
// Ts will error since the parent appForm already has a component called CustomSubmit
219+
formComponents: { CustomSubmit },
220+
})
221+
```
222+
223+
This way you can add extra fields that are unique to your team without bloating the upstream AppForm.
224+
163225
## Breaking big forms into smaller pieces
164226

165227
Sometimes forms get very large; it's just how it goes sometimes. While TanStack Form supports large forms well, it's never fun to work with hundreds or thousands of lines of code in single files.

examples/react/composition/src/AppForm/AppForm.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ import { SubmitButton } from './FormComponents/SubmitButton'
1313
export const { fieldContext, formContext, useFieldContext, useFormContext } =
1414
createFormHookContexts()
1515

16-
const appForm = createFormHook({
16+
const { useAppForm } = createFormHook({
1717
fieldContext,
1818
formContext,
1919
fieldComponents: { TextField: TextField },
2020
formComponents: { SubmitButton },
2121
})
2222

23-
export default appForm
23+
export default useAppForm

examples/react/composition/src/ExtendedAppForm/ExtendedAppForm.tsx

Lines changed: 0 additions & 14 deletions
This file was deleted.

examples/react/composition/src/ExtendedAppForm/FieldComponents/CustomTextField.tsx

Lines changed: 0 additions & 22 deletions
This file was deleted.

examples/react/composition/src/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { createRoot } from 'react-dom/client'
44
import { TanStackDevtools } from '@tanstack/react-devtools'
55
import { formDevtoolsPlugin } from '@tanstack/react-form-devtools'
66

7-
import { useAppForm } from './ExtendedAppForm/ExtendedAppForm'
7+
import useAppForm from './AppForm/AppForm'
88

99
export default function App() {
1010
const form = useAppForm({

0 commit comments

Comments
 (0)