-
-
Notifications
You must be signed in to change notification settings - Fork 355
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): docs for reusable fields & reusable field utility type
- Loading branch information
Harry Whorlow
committed
Dec 20, 2024
1 parent
91eb9ab
commit 3aeddc2
Showing
2 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
--- | ||
id: reusable-fields | ||
title: Creating reusable fields | ||
--- | ||
|
||
As TanStack form is a headless library, we provide you the core building blocks and then give you the freedom to build on top of them. This page introduces the concept of creating reusable field-components for your form, allowing you to create fields that you can reuse throughout your app. | ||
|
||
## Basic Usage | ||
|
||
To create a reusable fields, you can do the following. | ||
|
||
```tsx | ||
import { useForm, Validator, InferValidFormKeys } from '@tanstack/react-form'; | ||
|
||
export default function GenericTextField< | ||
TForm, | ||
TName extends InferValidFormKeys<TForm, string>, | ||
TFormValidator extends Validator<TForm, unknown> | undefined, | ||
>({ name, form }: { | ||
name: TName; | ||
form: ReturnType<typeof useForm<TForm, TFormValidator> | ||
> }): JSX.Element { | ||
return ( | ||
<form.Field name={name}> | ||
{({ handleChange, handleBlur, state }) => ( | ||
<input | ||
value={state.value} | ||
onChange={(e) => handleChange(e.target.value as any)} | ||
onBlur={handleBlur} | ||
/> | ||
)} | ||
</form.Field> | ||
); | ||
} | ||
``` | ||
|
||
In this setup the GenericTextField will only accept names of fields that have a valid value type, in this case a string, as shown here. | ||
|
||
```tsx | ||
TName extends InferValidFormKeys<TForm, string>, | ||
``` | ||
|
||
Deep values can also be inferred using this method from the parent form. | ||
|
||
```tsx | ||
function App() { | ||
const form = useForm({ | ||
defaultValues: { name: '', id: 0, interests: {hobbies: 'climbing'} }, | ||
onSubmit: ({ value }) => { | ||
console.log(value); | ||
}, | ||
}); | ||
|
||
return <GenericTextField form={form} name="interests.hobbies" />; | ||
} | ||
``` | ||
|
||
## Full Example | ||
|
||
```tsx | ||
import { useForm, Validator, InferValidFormKeys } from '@tanstack/react-form'; | ||
|
||
export default function GenericTextField< | ||
TForm, | ||
TName extends InferValidFormKeys<TForm, string>, | ||
TFormValidator extends Validator<TForm, unknown> | undefined, | ||
>({ name, form }: { | ||
name: TName; | ||
form: ReturnType<typeof useForm<TForm, TFormValidator> | ||
> }): JSX.Element { | ||
return ( | ||
<form.Field name={name}> | ||
{({ handleChange, handleBlur, state }) => ( | ||
<input | ||
value={state.value} | ||
onChange={(e) => handleChange(e.target.value as any)} | ||
onBlur={handleBlur} | ||
/> | ||
)} | ||
</form.Field> | ||
); | ||
} | ||
|
||
function App() { | ||
const form = useForm({ | ||
defaultValues: { name: '', id: 0 }, | ||
onSubmit: ({ value }) => { | ||
console.log(value); | ||
}, | ||
}); | ||
|
||
return <GenericTextField form={form} name="name" />; | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters