Skip to content

Commit

Permalink
feat(react): UseFormReturnType utility type
Browse files Browse the repository at this point in the history
  • Loading branch information
Harry Whorlow committed Dec 20, 2024
1 parent 60594af commit 4256a04
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
4 changes: 2 additions & 2 deletions docs/framework/react/guides/reusable-fields.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ import { useForm, Validator, InferValidFormKeys } from '@tanstack/react-form';

export default function GenericTextField<
TForm,
TFormValidator
TName extends InferValidFormKeys<TForm, string>,
TFormValidator extends Validator<TForm, unknown> | undefined,
>({ name, form }: {
name: TName;
form: ReturnType<typeof useForm<TForm, TFormValidator>
form: UseFormReturnType<TForm, TFormValidator>
> }): JSX.Element {
return (
<form.Field name={name}>
Expand Down
10 changes: 10 additions & 0 deletions packages/react-form/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
Validator,
} from '@tanstack/form-core'
import type { FunctionComponent } from 'react'
import { useForm } from './useForm'

/**
* The field options.
Expand All @@ -28,3 +29,12 @@ export type UseFieldOptions<
> & {
mode?: 'value' | 'array'
}

/**
* The return type use useForm with pre-populated generics
*/
export type UseFormReturnType<TForm, TFormValidator> = TFormValidator extends
| Validator<TForm, unknown>
| undefined
? ReturnType<typeof useForm<TForm, TFormValidator>>
: never

0 comments on commit 4256a04

Please sign in to comment.