Skip to content

Commit bb83da8

Browse files
author
Harry Whorlow
committed
feat(react): UseFormReturnType utility type
1 parent bb46a92 commit bb83da8

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

docs/framework/react/guides/reusable-fields.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ As TanStack form is a headless library, we provide you the core building blocks
1010
To create a reusable fields, you can do the following.
1111

1212
```tsx
13-
import { useForm, Validator, InferValidFormKeys } from '@tanstack/react-form';
13+
import { InferValidFormKeys } from '@tanstack/react-form';
1414

1515
export default function GenericTextField<
1616
TForm,
17+
TFormValidator
1718
TName extends InferValidFormKeys<TForm, string>,
18-
TFormValidator extends Validator<TForm, unknown> | undefined,
1919
>({ name, form }: {
2020
name: TName;
21-
form: ReturnType<typeof useForm<TForm, TFormValidator>
21+
form: UseFormReturnType<TForm, TFormValidator>,
2222
> }): JSX.Element {
2323
return (
2424
<form.Field name={name}>
@@ -45,7 +45,7 @@ Deep values can also be inferred using this method from the parent form.
4545
```tsx
4646
function App() {
4747
const form = useForm({
48-
defaultValues: { name: '', id: 0, interests: {hobbies: 'climbing'} },
48+
defaultValues: { name: '', id: 0, interests: {hobbies: ''} },
4949
onSubmit: ({ value }) => {
5050
console.log(value);
5151
},
@@ -58,15 +58,15 @@ function App() {
5858
## Full Example
5959

6060
```tsx
61-
import { useForm, Validator, InferValidFormKeys } from '@tanstack/react-form';
61+
import { InferValidFormKeys } from '@tanstack/react-form';
6262

6363
export default function GenericTextField<
6464
TForm,
65+
TFormValidator
6566
TName extends InferValidFormKeys<TForm, string>,
66-
TFormValidator extends Validator<TForm, unknown> | undefined,
6767
>({ name, form }: {
6868
name: TName;
69-
form: ReturnType<typeof useForm<TForm, TFormValidator>
69+
form: UseFormReturnType<TForm, TFormValidator>
7070
> }): JSX.Element {
7171
return (
7272
<form.Field name={name}>
@@ -83,7 +83,7 @@ export default function GenericTextField<
8383

8484
function App() {
8585
const form = useForm({
86-
defaultValues: { name: '', id: 0, interests: {hobbies: 'climbing'} },
86+
defaultValues: { name: '', id: 0, interests: {hobbies: ''} },
8787
onSubmit: ({ value }) => {
8888
console.log(value);
8989
},

packages/react-form/src/types.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type {
55
Validator,
66
} from '@tanstack/form-core'
77
import type { FunctionComponent } from 'react'
8+
import { useForm } from './useForm'
89

910
/**
1011
* The field options.
@@ -28,3 +29,12 @@ export type UseFieldOptions<
2829
> & {
2930
mode?: 'value' | 'array'
3031
}
32+
33+
/**
34+
* The return type use useForm with pre-populated generics
35+
*/
36+
export type UseFormReturnType<TForm, TFormValidator> = TFormValidator extends
37+
| Validator<TForm, unknown>
38+
| undefined
39+
? ReturnType<typeof useForm<TForm, TFormValidator>>
40+
: never

0 commit comments

Comments
 (0)