Open History to see the last commit
1import { FormApi, functionalUpdate } from '@tanstack/form-core'
2import { useStore } from '@tanstack/react-store'
3import React, { useState } from 'react'
4import { Field, type FieldComponent, type UseField, useField } from './useField'
5import { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect'
6import type { NoInfer } from '@tanstack/react-store'
7import type { FormOptions, FormState, Validator } from '@tanstack/form-core'
8import type { NodeType } from './types'
9β
10/**
11 * Fields that are added onto the `FormAPI` from `@tanstack/form-core` and returned from `useForm`
12 */
13export interface ReactFormApi<
14 TFormData,
15 TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,
16> {
17 /**
18 * A React component to render form fields. With this, you can render and manage individual form fields.
19 */
20 Field: FieldComponent<TFormData, TFormValidator>
21 /**
22 * A custom React hook that provides functionalities related to individual form fields. It gives you access to field values, errors, and allows you to set or update field values.
23 */
24 useField: UseField<TFormData, TFormValidator>
25 /**
26 * A `useStore` hook that connects to the internal store of the form. It can be used to access the form's current state or any other related state information. You can optionally pass in a selector function to cherry-pick specific parts of the state
27 */
28 useStore: <TSelected = NoInfer<FormState<TFormData>>>(
29 selector?: (state: NoInfer<FormState<TFormData>>) => TSelected,
30 ) => TSelected
31 /**
32 * A `Subscribe` function that allows you to listen and react to changes in the form's state. It's especially useful when you need to execute side effects or render specific components in response to state updates.
33 */
34 Subscribe: <TSelected = NoInfer<FormState<TFormData>>>(props: {
35 /**
36 TypeScript versions <=5.0.4 have a bug that prevents
37 the type of the `TSelected` generic from being inferred
38 from the return type of this method.
39β
40 In these versions, `TSelected` will fall back to the default
41 type (or `unknown` if that's not defined).
42β
43 @see {@link https://github.com/TanStack/form/pull/606/files#r1506715714 | This discussion on GitHub for the details}
44 @see {@link https://github.com/microsoft/TypeScript/issues/52786 | The bug report in `microsoft/TypeScript`}
45 */
46 selector?: (state: NoInfer<FormState<TFormData>>) => TSelected
47 children: ((state: NoInfer<TSelected>) => NodeType) | NodeType
48 }) => NodeType
49}
50β
51/**
52 * A custom React Hook that returns an extended instance of the `FormApi` class.
53 *
54 * This API encapsulates all the necessary functionalities related to the form. It allows you to manage form state, handle submissions, and interact with form fields
55 */
56export function useForm<
57 TFormData,
58 TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,
59>(opts?: FormOptions<TFormData, TFormValidator>) {
60 const [formApi] = useState(() => {
61 const api = new FormApi<TFormData, TFormValidator>(opts)
62β
63 const extendedApi: typeof api & ReactFormApi<TFormData, TFormValidator> =
64 api as never
65 extendedApi.Field = function APIField(props) {
66 return (<Field {...props} form={api} />) as never
67 }
68 // eslint-disable-next-line react-hooks/rules-of-hooks
69 extendedApi.useField = (props) => useField({ ...props, form: api })
70 extendedApi.useStore = (selector) => {
71 // eslint-disable-next-line react-hooks/rules-of-hooks
72 return useStore(api.store as any, selector as any) as any
73 }
74 extendedApi.Subscribe = (props) => {
75 return functionalUpdate(
76 props.children,
77 // eslint-disable-next-line react-hooks/rules-of-hooks
78 useStore(api.store as any, props.selector as any),
79 ) as any
80 }
81β
82 return extendedApi
83 })
84β
85 useIsomorphicLayoutEffect(formApi.mount, [])
86β
87 formApi.useStore((state) => state.isSubmitting)
88β
89 /**
90 * formApi.update should not have any side effects. Think of it like a `useRef`
91 * that we need to keep updated every render with the most up-to-date information.
92 */
93 useIsomorphicLayoutEffect(() => {
94 formApi.update(opts)
95 })
96β
97 return formApi
98}