-
-
Notifications
You must be signed in to change notification settings - Fork 622
Expand file tree
/
Copy pathuseForm.tsx
More file actions
285 lines (267 loc) · 7.87 KB
/
useForm.tsx
File metadata and controls
285 lines (267 loc) · 7.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
'use client'
import { FormApi, functionalUpdate, mergeAndUpdate } from '@tanstack/form-core'
import { useStore } from '@tanstack/react-store'
import { useMemo, useRef, useState } from 'react'
import { Field } from './useField'
import { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect'
import { useFormId } from './useFormId'
import type {
AnyFormApi,
AnyFormState,
FormAsyncValidateOrFn,
FormOptions,
FormState,
FormValidateOrFn,
} from '@tanstack/form-core'
import type { FunctionComponent, PropsWithChildren, ReactNode } from 'react'
import type { FieldComponent } from './useField'
import type { NoInfer } from '@tanstack/react-store'
/**
* Fields that are added onto the `FormAPI` from `@tanstack/form-core` and returned from `useForm`
*/
export interface ReactFormApi<
in out TFormData,
in out TOnMount extends undefined | FormValidateOrFn<TFormData>,
in out TOnChange extends undefined | FormValidateOrFn<TFormData>,
in out TOnChangeAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
in out TOnBlur extends undefined | FormValidateOrFn<TFormData>,
in out TOnBlurAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
in out TOnSubmit extends undefined | FormValidateOrFn<TFormData>,
in out TOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
in out TOnDynamic extends undefined | FormValidateOrFn<TFormData>,
in out TOnDynamicAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
in out TOnServer extends undefined | FormAsyncValidateOrFn<TFormData>,
in out TSubmitMeta,
> {
/**
* A React component to render form fields. With this, you can render and manage individual form fields.
*/
Field: FieldComponent<
TFormData,
TOnMount,
TOnChange,
TOnChangeAsync,
TOnBlur,
TOnBlurAsync,
TOnSubmit,
TOnSubmitAsync,
TOnDynamic,
TOnDynamicAsync,
TOnServer,
TSubmitMeta
>
/**
* 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.
*/
Subscribe: <
TSelected = NoInfer<
FormState<
TFormData,
TOnMount,
TOnChange,
TOnChangeAsync,
TOnBlur,
TOnBlurAsync,
TOnSubmit,
TOnSubmitAsync,
TOnDynamic,
TOnDynamicAsync,
TOnServer
>
>,
>(props: {
selector?: (
state: NoInfer<
FormState<
TFormData,
TOnMount,
TOnChange,
TOnChangeAsync,
TOnBlur,
TOnBlurAsync,
TOnSubmit,
TOnSubmitAsync,
TOnDynamic,
TOnDynamicAsync,
TOnServer
>
>,
) => TSelected
children: ((state: NoInfer<TSelected>) => ReactNode) | ReactNode
}) => ReturnType<FunctionComponent>
}
/**
* An extended version of the `FormApi` class that includes React-specific functionalities from `ReactFormApi`
*/
export type ReactFormExtendedApi<
TFormData,
TOnMount extends undefined | FormValidateOrFn<TFormData>,
TOnChange extends undefined | FormValidateOrFn<TFormData>,
TOnChangeAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
TOnBlur extends undefined | FormValidateOrFn<TFormData>,
TOnBlurAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
TOnSubmit extends undefined | FormValidateOrFn<TFormData>,
TOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
TOnDynamic extends undefined | FormValidateOrFn<TFormData>,
TOnDynamicAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
TOnServer extends undefined | FormAsyncValidateOrFn<TFormData>,
TSubmitMeta,
> = FormApi<
TFormData,
TOnMount,
TOnChange,
TOnChangeAsync,
TOnBlur,
TOnBlurAsync,
TOnSubmit,
TOnSubmitAsync,
TOnDynamic,
TOnDynamicAsync,
TOnServer,
TSubmitMeta
> &
ReactFormApi<
TFormData,
TOnMount,
TOnChange,
TOnChangeAsync,
TOnBlur,
TOnBlurAsync,
TOnSubmit,
TOnSubmitAsync,
TOnDynamic,
TOnDynamicAsync,
TOnServer,
TSubmitMeta
>
function LocalSubscribe({
form,
selector,
children,
}: PropsWithChildren<{
form: AnyFormApi
selector: (state: AnyFormState) => AnyFormState
}>): ReturnType<FunctionComponent> {
const data = useStore(form.store, selector)
return <>{functionalUpdate(children, data)}</>
}
/**
* A custom React Hook that returns an extended instance of the `FormApi` class.
*
* 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
*/
export function useForm<
TFormData,
TOnMount extends undefined | FormValidateOrFn<TFormData>,
TOnChange extends undefined | FormValidateOrFn<TFormData>,
TOnChangeAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
TOnBlur extends undefined | FormValidateOrFn<TFormData>,
TOnBlurAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
TOnSubmit extends undefined | FormValidateOrFn<TFormData>,
TOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
TOnDynamic extends undefined | FormValidateOrFn<TFormData>,
TOnDynamicAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
TOnServer extends undefined | FormAsyncValidateOrFn<TFormData>,
TSubmitMeta,
>(
opts?: FormOptions<
TFormData,
TOnMount,
TOnChange,
TOnChangeAsync,
TOnBlur,
TOnBlurAsync,
TOnSubmit,
TOnSubmitAsync,
TOnDynamic,
TOnDynamicAsync,
TOnServer,
TSubmitMeta
>,
) {
const fallbackFormId = useFormId()
const [prevFormId, setPrevFormId] = useState<string>(opts?.formId as never)
const [formApi, setFormApi] = useState(() => {
return new FormApi<
TFormData,
TOnMount,
TOnChange,
TOnChangeAsync,
TOnBlur,
TOnBlurAsync,
TOnSubmit,
TOnSubmitAsync,
TOnDynamic,
TOnDynamicAsync,
TOnServer,
TSubmitMeta
>({ ...opts, formId: opts?.formId ?? fallbackFormId })
})
if (prevFormId !== opts?.formId) {
const formId = opts?.formId ?? fallbackFormId
setFormApi(new FormApi({ ...opts, formId }))
setPrevFormId(formId)
}
const extendedFormApi = useMemo(() => {
const extendedApi: ReactFormExtendedApi<
TFormData,
TOnMount,
TOnChange,
TOnChangeAsync,
TOnBlur,
TOnBlurAsync,
TOnSubmit,
TOnSubmitAsync,
TOnDynamic,
TOnDynamicAsync,
TOnServer,
TSubmitMeta
> = {
...formApi,
handleSubmit: ((...props: never[]) => {
return formApi._handleSubmit(...props)
}) as typeof formApi.handleSubmit,
// We must add all `get`ters from `core`'s `FormApi` here, as otherwise the spread operator won't catch those
get formId(): string {
return formApi._formId
},
get state() {
return formApi.store.state
},
get options() {
return formApi.options
},
} as never
extendedApi.Field = function APIField(props) {
return <Field {...props} form={formApi} />
}
extendedApi.Subscribe = function Subscribe(props: any) {
return (
<LocalSubscribe
form={formApi}
selector={props.selector}
children={props.children}
/>
)
}
return extendedApi
}, [formApi])
useIsomorphicLayoutEffect(formApi.mount, [])
/**
* formApi.update should not have any side effects. Think of it like a `useRef`
* that we need to keep updated every render with the most up-to-date information.
*/
useIsomorphicLayoutEffect(() => {
formApi.update(opts)
})
const hasRan = useRef(false)
useIsomorphicLayoutEffect(() => {
if (!hasRan.current) return
if (!opts?.transform) return
mergeAndUpdate(formApi, opts.transform as never)
}, [formApi, opts?.transform])
useIsomorphicLayoutEffect(() => {
hasRan.current = true
})
return extendedFormApi
}