-
-
Notifications
You must be signed in to change notification settings - Fork 355
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(core): docs for reusable fields & reusable field utility type #1084
Open
harry-whorlow
wants to merge
5
commits into
TanStack:main
Choose a base branch
from
harry-whorlow:reusable-fields
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+137
−1
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
57c6fb3
feat(core): docs for reusable fields & reusable field utility type
8872329
ci: apply automated fixes and generate docs
autofix-ci[bot] bb46a92
fix(core): match default values between snippets and full example
bb83da8
feat(react): UseFormReturnType utility type
524d024
fix(core): cleaned up generic slots
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,92 @@ | ||
--- | ||
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 { InferValidFormKeys, UseFormReturnType } from '@tanstack/react-form'; | ||
|
||
export default function GenericTextField< | ||
TForm, | ||
TFormValidator | ||
>({ name, form }: { | ||
name: InferValidFormKeys<TForm, string>; | ||
form: UseFormReturnType<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> | ||
); | ||
} | ||
``` | ||
|
||
With the InferValidFormKeys the GenericTextField component's name prop will only accept the names of fields that have a valid value type, in this case a string, as shown here. | ||
|
||
```tsx | ||
name: 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: ''} }, | ||
onSubmit: ({ value }) => { | ||
console.log(value); | ||
}, | ||
}); | ||
|
||
return <GenericTextField form={form} name="interests.hobbies" />; | ||
} | ||
``` | ||
|
||
## Full Example | ||
|
||
```tsx | ||
import { InferValidFormKeys, UseFormReturnType } from '@tanstack/react-form'; | ||
|
||
export default function GenericTextField< | ||
TForm, | ||
TFormValidator | ||
>({ name, form }: { | ||
name: InferValidFormKeys<TForm, string>; | ||
form: UseFormReturnType<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, interests: {hobbies: ''} }, | ||
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
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,22 @@ | ||
--- | ||
id: InferValidFormKeys | ||
title: InferValidFormKeys | ||
--- | ||
|
||
# Type Alias: InferValidFormKeys\<TForm, TFieldType\> | ||
|
||
```ts | ||
type InferValidFormKeys<TForm, TFieldType>: { [K in DeepKeys<TForm>]: DeepValue<TForm, K> extends TFieldType ? K : never }[DeepKeys<TForm>]; | ||
``` | ||
|
||
Infers the form keys of valid fields | ||
|
||
## Type Parameters | ||
|
||
• **TForm** | ||
|
||
• **TFieldType** | ||
|
||
## Defined in | ||
|
||
[packages/form-core/src/util-types.ts:151](https://github.com/TanStack/form/blob/main/packages/form-core/src/util-types.ts#L151) |
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
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 |
---|---|---|
|
@@ -4,7 +4,7 @@ import type { | |
FieldApiOptions, | ||
Validator, | ||
} from '@tanstack/form-core' | ||
import type { FunctionComponent } from 'react' | ||
import { ReactFormExtendedApi } from './useForm' | ||
|
||
/** | ||
* The field options. | ||
|
@@ -28,3 +28,12 @@ export type UseFieldOptions< | |
> & { | ||
mode?: 'value' | 'array' | ||
} | ||
|
||
/** | ||
* The return type use useForm with pre-populated generics | ||
*/ | ||
export type UseFormReturnType<TForm, TFormValidator> = TFormValidator extends | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would love to get rid of the TFormValidator slot, since TFormValidator is just Validator<TForm, unknown> | undefined. But so far my efforts have been fruitless. |
||
| Validator<TForm, unknown> | ||
| undefined | ||
? ReactFormExtendedApi<TForm, TFormValidator> | ||
: never |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another improvement would be to get Ts to infer the handleChange value so the update is type safe.