@tanstack/preact-form 0.0.1 → 1.29.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +35 -45
  3. package/dist/cjs/createFormHook.cjs +101 -0
  4. package/dist/cjs/createFormHook.cjs.map +1 -0
  5. package/dist/cjs/createFormHook.d.cts +80 -0
  6. package/dist/cjs/index.cjs +24 -0
  7. package/dist/cjs/index.cjs.map +1 -0
  8. package/dist/cjs/index.d.cts +8 -0
  9. package/dist/cjs/types.d.cts +17 -0
  10. package/dist/cjs/useField.cjs +109 -0
  11. package/dist/cjs/useField.cjs.map +1 -0
  12. package/dist/cjs/useField.d.cts +58 -0
  13. package/dist/cjs/useFieldGroup.cjs +48 -0
  14. package/dist/cjs/useFieldGroup.cjs.map +1 -0
  15. package/dist/cjs/useFieldGroup.d.cts +30 -0
  16. package/dist/cjs/useForm.cjs +74 -0
  17. package/dist/cjs/useForm.cjs.map +1 -0
  18. package/dist/cjs/useForm.d.cts +30 -0
  19. package/dist/cjs/useFormId.cjs +6 -0
  20. package/dist/cjs/useFormId.cjs.map +1 -0
  21. package/dist/cjs/useFormId.d.cts +2 -0
  22. package/dist/cjs/useIsomorphicLayoutEffect.cjs +6 -0
  23. package/dist/cjs/useIsomorphicLayoutEffect.cjs.map +1 -0
  24. package/dist/cjs/useIsomorphicLayoutEffect.d.cts +2 -0
  25. package/dist/cjs/useStore.cjs +24 -0
  26. package/dist/cjs/useStore.cjs.map +1 -0
  27. package/dist/cjs/useStore.d.cts +13 -0
  28. package/dist/esm/createFormHook.d.ts +80 -0
  29. package/dist/esm/createFormHook.js +101 -0
  30. package/dist/esm/createFormHook.js.map +1 -0
  31. package/dist/esm/index.d.ts +8 -0
  32. package/dist/esm/index.js +18 -0
  33. package/dist/esm/index.js.map +1 -0
  34. package/dist/esm/types.d.ts +17 -0
  35. package/dist/esm/useField.d.ts +58 -0
  36. package/dist/esm/useField.js +109 -0
  37. package/dist/esm/useField.js.map +1 -0
  38. package/dist/esm/useFieldGroup.d.ts +30 -0
  39. package/dist/esm/useFieldGroup.js +48 -0
  40. package/dist/esm/useFieldGroup.js.map +1 -0
  41. package/dist/esm/useForm.d.ts +30 -0
  42. package/dist/esm/useForm.js +74 -0
  43. package/dist/esm/useForm.js.map +1 -0
  44. package/dist/esm/useFormId.d.ts +2 -0
  45. package/dist/esm/useFormId.js +6 -0
  46. package/dist/esm/useFormId.js.map +1 -0
  47. package/dist/esm/useIsomorphicLayoutEffect.d.ts +2 -0
  48. package/dist/esm/useIsomorphicLayoutEffect.js +6 -0
  49. package/dist/esm/useIsomorphicLayoutEffect.js.map +1 -0
  50. package/dist/esm/useStore.d.ts +13 -0
  51. package/dist/esm/useStore.js +24 -0
  52. package/dist/esm/useStore.js.map +1 -0
  53. package/package.json +64 -8
  54. package/src/createFormHook.tsx +603 -0
  55. package/src/index.ts +10 -0
  56. package/src/types.ts +143 -0
  57. package/src/useField.tsx +779 -0
  58. package/src/useFieldGroup.tsx +259 -0
  59. package/src/useForm.tsx +281 -0
  60. package/src/useFormId.ts +3 -0
  61. package/src/useIsomorphicLayoutEffect.ts +4 -0
  62. package/src/useStore.ts +44 -0
@@ -0,0 +1,259 @@
1
+ import { useState } from 'preact/hooks'
2
+ import { FieldGroupApi, functionalUpdate } from '@tanstack/form-core'
3
+ import { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect'
4
+ import { useStore } from './useStore'
5
+ import type {
6
+ AnyFieldGroupApi,
7
+ DeepKeysOfType,
8
+ FieldGroupState,
9
+ FieldsMap,
10
+ FormAsyncValidateOrFn,
11
+ FormValidateOrFn,
12
+ } from '@tanstack/form-core'
13
+ import type { AppFieldExtendedReactFormApi } from './createFormHook'
14
+ import type { ComponentChild, ComponentType, FunctionComponent } from 'preact'
15
+ import type { LensFieldComponent } from './useField'
16
+ import type { PropsWithChildren } from './types'
17
+
18
+ function LocalSubscribe({
19
+ lens,
20
+ selector,
21
+ children,
22
+ }: PropsWithChildren<{
23
+ lens: AnyFieldGroupApi
24
+ selector: (state: FieldGroupState<any>) => FieldGroupState<any>
25
+ }>): ReturnType<FunctionComponent> {
26
+ const data = useStore(lens.store, selector)
27
+
28
+ return <>{functionalUpdate(children, data)}</>
29
+ }
30
+
31
+ /**
32
+ * @private
33
+ */
34
+ export type AppFieldExtendedReactFieldGroupApi<
35
+ TFormData,
36
+ TFieldGroupData,
37
+ TFields extends
38
+ | DeepKeysOfType<TFormData, TFieldGroupData | null | undefined>
39
+ | FieldsMap<TFormData, TFieldGroupData>,
40
+ TOnMount extends undefined | FormValidateOrFn<TFormData>,
41
+ TOnChange extends undefined | FormValidateOrFn<TFormData>,
42
+ TOnChangeAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
43
+ TOnBlur extends undefined | FormValidateOrFn<TFormData>,
44
+ TOnBlurAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
45
+ TOnSubmit extends undefined | FormValidateOrFn<TFormData>,
46
+ TOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
47
+ TOnDynamic extends undefined | FormValidateOrFn<TFormData>,
48
+ TOnDynamicAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
49
+ TOnServer extends undefined | FormAsyncValidateOrFn<TFormData>,
50
+ TSubmitMeta,
51
+ TFieldComponents extends Record<string, ComponentType<any>>,
52
+ TFormComponents extends Record<string, ComponentType<any>>,
53
+ > = FieldGroupApi<
54
+ TFormData,
55
+ TFieldGroupData,
56
+ TFields,
57
+ TOnMount,
58
+ TOnChange,
59
+ TOnChangeAsync,
60
+ TOnBlur,
61
+ TOnBlurAsync,
62
+ TOnSubmit,
63
+ TOnSubmitAsync,
64
+ TOnDynamic,
65
+ TOnDynamicAsync,
66
+ TOnServer,
67
+ TSubmitMeta
68
+ > &
69
+ NoInfer<TFormComponents> & {
70
+ AppField: LensFieldComponent<
71
+ TFieldGroupData,
72
+ TSubmitMeta,
73
+ NoInfer<TFieldComponents>
74
+ >
75
+ AppForm: ComponentType<PropsWithChildren<{}>>
76
+ /**
77
+ * A React component to render form fields. With this, you can render and manage individual form fields.
78
+ */
79
+ Field: LensFieldComponent<TFieldGroupData, TSubmitMeta>
80
+
81
+ /**
82
+ * 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.
83
+ */
84
+ Subscribe: <TSelected = NoInfer<FieldGroupState<TFieldGroupData>>>(props: {
85
+ selector?: (state: NoInfer<FieldGroupState<TFieldGroupData>>) => TSelected
86
+ children: ((state: NoInfer<TSelected>) => ComponentChild) | ComponentChild
87
+ }) => ReturnType<FunctionComponent>
88
+ }
89
+
90
+ export function useFieldGroup<
91
+ TFormData,
92
+ TFieldGroupData,
93
+ TFields extends
94
+ | DeepKeysOfType<TFormData, TFieldGroupData | null | undefined>
95
+ | FieldsMap<TFormData, TFieldGroupData>,
96
+ TOnMount extends undefined | FormValidateOrFn<TFormData>,
97
+ TOnChange extends undefined | FormValidateOrFn<TFormData>,
98
+ TOnChangeAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
99
+ TOnBlur extends undefined | FormValidateOrFn<TFormData>,
100
+ TOnBlurAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
101
+ TOnSubmit extends undefined | FormValidateOrFn<TFormData>,
102
+ TOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
103
+ TOnDynamic extends undefined | FormValidateOrFn<TFormData>,
104
+ TOnDynamicAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
105
+ TOnServer extends undefined | FormAsyncValidateOrFn<TFormData>,
106
+ TComponents extends Record<string, ComponentType<any>>,
107
+ TFormComponents extends Record<string, ComponentType<any>>,
108
+ TSubmitMeta = never,
109
+ >(opts: {
110
+ form:
111
+ | AppFieldExtendedReactFormApi<
112
+ TFormData,
113
+ TOnMount,
114
+ TOnChange,
115
+ TOnChangeAsync,
116
+ TOnBlur,
117
+ TOnBlurAsync,
118
+ TOnSubmit,
119
+ TOnSubmitAsync,
120
+ TOnDynamic,
121
+ TOnDynamicAsync,
122
+ TOnServer,
123
+ TSubmitMeta,
124
+ TComponents,
125
+ TFormComponents
126
+ >
127
+ | AppFieldExtendedReactFieldGroupApi<
128
+ // Since this only occurs if you nest it within other form lenses, it can be more
129
+ // lenient with the types.
130
+ unknown,
131
+ TFormData,
132
+ string | FieldsMap<unknown, TFormData>,
133
+ any,
134
+ any,
135
+ any,
136
+ any,
137
+ any,
138
+ any,
139
+ any,
140
+ any,
141
+ any,
142
+ any,
143
+ TSubmitMeta,
144
+ TComponents,
145
+ TFormComponents
146
+ >
147
+ fields: TFields
148
+ defaultValues?: TFieldGroupData
149
+ onSubmitMeta?: TSubmitMeta
150
+ formComponents: TFormComponents
151
+ }): AppFieldExtendedReactFieldGroupApi<
152
+ TFormData,
153
+ TFieldGroupData,
154
+ TFields,
155
+ TOnMount,
156
+ TOnChange,
157
+ TOnChangeAsync,
158
+ TOnBlur,
159
+ TOnBlurAsync,
160
+ TOnSubmit,
161
+ TOnSubmitAsync,
162
+ TOnDynamic,
163
+ TOnDynamicAsync,
164
+ TOnServer,
165
+ TSubmitMeta,
166
+ TComponents,
167
+ TFormComponents
168
+ > {
169
+ const [formLensApi] = useState(() => {
170
+ const api = new FieldGroupApi(opts)
171
+ const form =
172
+ opts.form instanceof FieldGroupApi
173
+ ? (opts.form.form as AppFieldExtendedReactFormApi<
174
+ TFormData,
175
+ TOnMount,
176
+ TOnChange,
177
+ TOnChangeAsync,
178
+ TOnBlur,
179
+ TOnBlurAsync,
180
+ TOnSubmit,
181
+ TOnSubmitAsync,
182
+ TOnDynamic,
183
+ TOnDynamicAsync,
184
+ TOnServer,
185
+ TSubmitMeta,
186
+ TComponents,
187
+ TFormComponents
188
+ >)
189
+ : opts.form
190
+
191
+ const extendedApi: AppFieldExtendedReactFieldGroupApi<
192
+ TFormData,
193
+ TFieldGroupData,
194
+ TFields,
195
+ TOnMount,
196
+ TOnChange,
197
+ TOnChangeAsync,
198
+ TOnBlur,
199
+ TOnBlurAsync,
200
+ TOnSubmit,
201
+ TOnSubmitAsync,
202
+ TOnDynamic,
203
+ TOnDynamicAsync,
204
+ TOnServer,
205
+ TSubmitMeta,
206
+ TComponents,
207
+ TFormComponents
208
+ > = api as never
209
+
210
+ extendedApi.AppForm = function AppForm(appFormProps) {
211
+ return <form.AppForm {...appFormProps} />
212
+ }
213
+
214
+ extendedApi.AppField = function AppField(props) {
215
+ return (
216
+ <form.AppField {...(formLensApi.getFormFieldOptions(props) as any)} />
217
+ )
218
+ }
219
+
220
+ extendedApi.Field = function Field(props) {
221
+ return <form.Field {...(formLensApi.getFormFieldOptions(props) as any)} />
222
+ }
223
+
224
+ extendedApi.Subscribe = function Subscribe(props: any) {
225
+ return (
226
+ <LocalSubscribe
227
+ lens={formLensApi}
228
+ selector={props.selector}
229
+ children={props.children}
230
+ />
231
+ )
232
+ }
233
+
234
+ return Object.assign(extendedApi, {
235
+ ...opts.formComponents,
236
+ }) as AppFieldExtendedReactFieldGroupApi<
237
+ TFormData,
238
+ TFieldGroupData,
239
+ TFields,
240
+ TOnMount,
241
+ TOnChange,
242
+ TOnChangeAsync,
243
+ TOnBlur,
244
+ TOnBlurAsync,
245
+ TOnSubmit,
246
+ TOnSubmitAsync,
247
+ TOnDynamic,
248
+ TOnDynamicAsync,
249
+ TOnServer,
250
+ TSubmitMeta,
251
+ TComponents,
252
+ TFormComponents
253
+ >
254
+ })
255
+
256
+ useIsomorphicLayoutEffect(formLensApi.mount, [formLensApi])
257
+
258
+ return formLensApi
259
+ }
@@ -0,0 +1,281 @@
1
+ import { FormApi, functionalUpdate, mergeAndUpdate } from '@tanstack/form-core'
2
+ import { useMemo, useRef, useState } from 'preact/hooks'
3
+ import { Field } from './useField'
4
+ import { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect'
5
+ import { useFormId } from './useFormId'
6
+ import { useStore } from './useStore'
7
+ import type {
8
+ AnyFormApi,
9
+ AnyFormState,
10
+ FormAsyncValidateOrFn,
11
+ FormOptions,
12
+ FormState,
13
+ FormValidateOrFn,
14
+ } from '@tanstack/form-core'
15
+ import type { ComponentChild, FunctionComponent } from 'preact'
16
+ import type { FieldComponent } from './useField'
17
+ import type { PropsWithChildren } from './types'
18
+ import type { NoInfer } from './useStore'
19
+
20
+ /**
21
+ * Fields that are added onto the `FormAPI` from `@tanstack/form-core` and returned from `useForm`
22
+ */
23
+ export interface ReactFormApi<
24
+ in out TFormData,
25
+ in out TOnMount extends undefined | FormValidateOrFn<TFormData>,
26
+ in out TOnChange extends undefined | FormValidateOrFn<TFormData>,
27
+ in out TOnChangeAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
28
+ in out TOnBlur extends undefined | FormValidateOrFn<TFormData>,
29
+ in out TOnBlurAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
30
+ in out TOnSubmit extends undefined | FormValidateOrFn<TFormData>,
31
+ in out TOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
32
+ in out TOnDynamic extends undefined | FormValidateOrFn<TFormData>,
33
+ in out TOnDynamicAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
34
+ in out TOnServer extends undefined | FormAsyncValidateOrFn<TFormData>,
35
+ in out TSubmitMeta,
36
+ > {
37
+ /**
38
+ * A React component to render form fields. With this, you can render and manage individual form fields.
39
+ */
40
+ Field: FieldComponent<
41
+ TFormData,
42
+ TOnMount,
43
+ TOnChange,
44
+ TOnChangeAsync,
45
+ TOnBlur,
46
+ TOnBlurAsync,
47
+ TOnSubmit,
48
+ TOnSubmitAsync,
49
+ TOnDynamic,
50
+ TOnDynamicAsync,
51
+ TOnServer,
52
+ TSubmitMeta
53
+ >
54
+ /**
55
+ * 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.
56
+ */
57
+ Subscribe: <
58
+ TSelected = NoInfer<
59
+ FormState<
60
+ TFormData,
61
+ TOnMount,
62
+ TOnChange,
63
+ TOnChangeAsync,
64
+ TOnBlur,
65
+ TOnBlurAsync,
66
+ TOnSubmit,
67
+ TOnSubmitAsync,
68
+ TOnDynamic,
69
+ TOnDynamicAsync,
70
+ TOnServer
71
+ >
72
+ >,
73
+ >(props: {
74
+ selector?: (
75
+ state: NoInfer<
76
+ FormState<
77
+ TFormData,
78
+ TOnMount,
79
+ TOnChange,
80
+ TOnChangeAsync,
81
+ TOnBlur,
82
+ TOnBlurAsync,
83
+ TOnSubmit,
84
+ TOnSubmitAsync,
85
+ TOnDynamic,
86
+ TOnDynamicAsync,
87
+ TOnServer
88
+ >
89
+ >,
90
+ ) => TSelected
91
+ children: ((state: NoInfer<TSelected>) => ComponentChild) | ComponentChild
92
+ }) => ReturnType<FunctionComponent>
93
+ }
94
+
95
+ /**
96
+ * An extended version of the `FormApi` class that includes React-specific functionalities from `ReactFormApi`
97
+ */
98
+ export type ReactFormExtendedApi<
99
+ TFormData,
100
+ TOnMount extends undefined | FormValidateOrFn<TFormData>,
101
+ TOnChange extends undefined | FormValidateOrFn<TFormData>,
102
+ TOnChangeAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
103
+ TOnBlur extends undefined | FormValidateOrFn<TFormData>,
104
+ TOnBlurAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
105
+ TOnSubmit extends undefined | FormValidateOrFn<TFormData>,
106
+ TOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
107
+ TOnDynamic extends undefined | FormValidateOrFn<TFormData>,
108
+ TOnDynamicAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
109
+ TOnServer extends undefined | FormAsyncValidateOrFn<TFormData>,
110
+ TSubmitMeta,
111
+ > = FormApi<
112
+ TFormData,
113
+ TOnMount,
114
+ TOnChange,
115
+ TOnChangeAsync,
116
+ TOnBlur,
117
+ TOnBlurAsync,
118
+ TOnSubmit,
119
+ TOnSubmitAsync,
120
+ TOnDynamic,
121
+ TOnDynamicAsync,
122
+ TOnServer,
123
+ TSubmitMeta
124
+ > &
125
+ ReactFormApi<
126
+ TFormData,
127
+ TOnMount,
128
+ TOnChange,
129
+ TOnChangeAsync,
130
+ TOnBlur,
131
+ TOnBlurAsync,
132
+ TOnSubmit,
133
+ TOnSubmitAsync,
134
+ TOnDynamic,
135
+ TOnDynamicAsync,
136
+ TOnServer,
137
+ TSubmitMeta
138
+ >
139
+
140
+ function LocalSubscribe({
141
+ form,
142
+ selector,
143
+ children,
144
+ }: PropsWithChildren<{
145
+ form: AnyFormApi
146
+ selector: (state: AnyFormState) => AnyFormState
147
+ }>): ReturnType<FunctionComponent> {
148
+ const data = useStore(form.store, selector)
149
+
150
+ return <>{functionalUpdate(children, data)}</>
151
+ }
152
+
153
+ /**
154
+ * A custom React Hook that returns an extended instance of the `FormApi` class.
155
+ *
156
+ * 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
157
+ */
158
+ export function useForm<
159
+ TFormData,
160
+ TOnMount extends undefined | FormValidateOrFn<TFormData>,
161
+ TOnChange extends undefined | FormValidateOrFn<TFormData>,
162
+ TOnChangeAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
163
+ TOnBlur extends undefined | FormValidateOrFn<TFormData>,
164
+ TOnBlurAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
165
+ TOnSubmit extends undefined | FormValidateOrFn<TFormData>,
166
+ TOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
167
+ TOnDynamic extends undefined | FormValidateOrFn<TFormData>,
168
+ TOnDynamicAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
169
+ TOnServer extends undefined | FormAsyncValidateOrFn<TFormData>,
170
+ TSubmitMeta,
171
+ >(
172
+ opts?: FormOptions<
173
+ TFormData,
174
+ TOnMount,
175
+ TOnChange,
176
+ TOnChangeAsync,
177
+ TOnBlur,
178
+ TOnBlurAsync,
179
+ TOnSubmit,
180
+ TOnSubmitAsync,
181
+ TOnDynamic,
182
+ TOnDynamicAsync,
183
+ TOnServer,
184
+ TSubmitMeta
185
+ >,
186
+ ) {
187
+ const fallbackFormId = useFormId()
188
+ const [prevFormId, setPrevFormId] = useState<string>(opts?.formId as never)
189
+
190
+ const [formApi, setFormApi] = useState(() => {
191
+ return new FormApi<
192
+ TFormData,
193
+ TOnMount,
194
+ TOnChange,
195
+ TOnChangeAsync,
196
+ TOnBlur,
197
+ TOnBlurAsync,
198
+ TOnSubmit,
199
+ TOnSubmitAsync,
200
+ TOnDynamic,
201
+ TOnDynamicAsync,
202
+ TOnServer,
203
+ TSubmitMeta
204
+ >({ ...opts, formId: opts?.formId ?? fallbackFormId })
205
+ })
206
+
207
+ if (prevFormId !== opts?.formId) {
208
+ const formId = opts?.formId ?? fallbackFormId
209
+ setFormApi(new FormApi({ ...opts, formId }))
210
+ setPrevFormId(formId)
211
+ }
212
+
213
+ const extendedFormApi = useMemo(() => {
214
+ const extendedApi: ReactFormExtendedApi<
215
+ TFormData,
216
+ TOnMount,
217
+ TOnChange,
218
+ TOnChangeAsync,
219
+ TOnBlur,
220
+ TOnBlurAsync,
221
+ TOnSubmit,
222
+ TOnSubmitAsync,
223
+ TOnDynamic,
224
+ TOnDynamicAsync,
225
+ TOnServer,
226
+ TSubmitMeta
227
+ > = {
228
+ ...formApi,
229
+ handleSubmit: ((...props: never[]) => {
230
+ return formApi._handleSubmit(...props)
231
+ }) as typeof formApi.handleSubmit,
232
+ // We must add all `get`ters from `core`'s `FormApi` here, as otherwise the spread operator won't catch those
233
+ get formId(): string {
234
+ return formApi._formId
235
+ },
236
+ get state() {
237
+ return formApi.store.state
238
+ },
239
+ } as never
240
+
241
+ extendedApi.Field = function APIField(props) {
242
+ return <Field {...props} form={formApi} />
243
+ }
244
+
245
+ extendedApi.Subscribe = function Subscribe(props: any) {
246
+ return (
247
+ <LocalSubscribe
248
+ form={formApi}
249
+ selector={props.selector}
250
+ children={props.children}
251
+ />
252
+ )
253
+ }
254
+
255
+ return extendedApi
256
+ }, [formApi])
257
+
258
+ useIsomorphicLayoutEffect(formApi.mount, [])
259
+
260
+ /**
261
+ * formApi.update should not have any side effects. Think of it like a `useRef`
262
+ * that we need to keep updated every render with the most up-to-date information.
263
+ */
264
+ useIsomorphicLayoutEffect(() => {
265
+ formApi.update(opts)
266
+ })
267
+
268
+ const hasRan = useRef(false)
269
+
270
+ useIsomorphicLayoutEffect(() => {
271
+ if (!hasRan.current) return
272
+ if (!opts?.transform) return
273
+ mergeAndUpdate(formApi, opts.transform as never)
274
+ }, [formApi, opts?.transform])
275
+
276
+ useIsomorphicLayoutEffect(() => {
277
+ hasRan.current = true
278
+ })
279
+
280
+ return extendedFormApi
281
+ }
@@ -0,0 +1,3 @@
1
+ import { useId } from 'preact/hooks'
2
+
3
+ export const useFormId = useId
@@ -0,0 +1,4 @@
1
+ import { useEffect, useLayoutEffect } from 'preact/hooks'
2
+
3
+ export const useIsomorphicLayoutEffect =
4
+ typeof window !== 'undefined' ? useLayoutEffect : useEffect
@@ -0,0 +1,44 @@
1
+ import { useStore as usePreactStore } from '@tanstack/preact-store'
2
+
3
+ export type NoInfer<T> = [T][T extends any ? 0 : never]
4
+
5
+ type EqualityFn<T> = (objA: T, objB: T) => boolean
6
+
7
+ interface UseStoreOptions<T> {
8
+ equal?: EqualityFn<T>
9
+ }
10
+
11
+ type StoreLike<TState> = {
12
+ subscribe: (
13
+ listener: (...args: Array<unknown>) => void,
14
+ ) => { unsubscribe: () => void } | (() => void)
15
+ readonly state: TState
16
+ }
17
+
18
+ function bridgeStore<TState>(store: StoreLike<TState>) {
19
+ return {
20
+ get state() {
21
+ return store.state
22
+ },
23
+ // @tanstack/preact-store currently calls subscribe unbound.
24
+ subscribe: (listener: () => void) => {
25
+ const subscription = store.subscribe(listener)
26
+
27
+ return typeof subscription === 'function'
28
+ ? subscription
29
+ : () => subscription.unsubscribe()
30
+ },
31
+ }
32
+ }
33
+
34
+ export function useStore<TState, TSelected = NoInfer<TState>>(
35
+ store: StoreLike<TState>,
36
+ selector?: (state: NoInfer<TState>) => TSelected,
37
+ options?: UseStoreOptions<TSelected>,
38
+ ): TSelected {
39
+ return usePreactStore(
40
+ bridgeStore(store) as never,
41
+ selector as never,
42
+ options as never,
43
+ ) as TSelected
44
+ }