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 bb46a92 commit 616d520
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
6 changes: 3 additions & 3 deletions docs/framework/react/guides/reusable-fields.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default function GenericTextField<
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 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 616d520

Please sign in to comment.