@tanstack/preact-form 1.33.3 → 2.0.0-alpha.1

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 (51) hide show
  1. package/dist/AppForm/Components.lib.js +3 -2
  2. package/dist/AppForm/PreactAppFormApi.public.d.ts +0 -1
  3. package/dist/AppForm/appFormOptions.public.d.ts +0 -1
  4. package/dist/AppForm/componentMap.public.d.ts +0 -1
  5. package/dist/AppForm/createFormHook.public.d.ts +0 -1
  6. package/dist/AppForm/createFormHook.public.js +2 -2
  7. package/dist/AppForm/createFormHookTypes.public.d.ts +110 -3
  8. package/dist/AppForm/fieldComponentHelpers.lib.js +3 -2
  9. package/dist/AppForm/getFormHookHelpers.public.d.ts +1 -2
  10. package/dist/AppForm/initializeAppForm.lib.js +2 -1
  11. package/dist/FieldGroup/FieldGroupApi.public.d.ts +0 -1
  12. package/dist/FieldGroup/withFields.lib.js +27 -63
  13. package/dist/FieldGroup/withFields.public.d.ts +26 -24
  14. package/dist/FieldGroup/withFields.public.js +4 -15
  15. package/dist/PreactForm/Components.lib.js +6 -4
  16. package/dist/PreactForm/Components.public.d.ts +2 -3
  17. package/dist/PreactForm/PreactFormApi.lib.js +2 -1
  18. package/dist/PreactForm/fieldSubscriptions.lib.js +4 -2
  19. package/dist/PreactForm/formApiTypes.public.d.ts +25 -2
  20. package/dist/PreactForm/formType.public.d.ts +30 -2
  21. package/dist/PreactForm/useForm.public.d.ts +0 -1
  22. package/dist/Subscribe.public.d.ts +0 -1
  23. package/dist/index.d.ts +2 -2
  24. package/dist/index.js +2 -2
  25. package/dist/preactTypes.public.d.ts +0 -2
  26. package/package.json +5 -6
  27. package/src/AppForm/Components.lib.tsx +0 -160
  28. package/src/AppForm/PreactAppFormApi.public.tsx +0 -17
  29. package/src/AppForm/appFormOptions.public.ts +0 -59
  30. package/src/AppForm/componentMap.public.ts +0 -19
  31. package/src/AppForm/contexts.lib.ts +0 -28
  32. package/src/AppForm/createFormHook.public.ts +0 -37
  33. package/src/AppForm/createFormHookTypes.public.ts +0 -34
  34. package/src/AppForm/fieldComponentHelpers.lib.tsx +0 -30
  35. package/src/AppForm/getFormHookHelpers.public.ts +0 -114
  36. package/src/AppForm/initializeAppForm.lib.ts +0 -22
  37. package/src/FieldGroup/FieldGroupApi.public.ts +0 -91
  38. package/src/FieldGroup/withFields.lib.ts +0 -191
  39. package/src/FieldGroup/withFields.public.ts +0 -221
  40. package/src/PreactForm/Components.lib.tsx +0 -166
  41. package/src/PreactForm/Components.public.ts +0 -383
  42. package/src/PreactForm/PreactFormApi.lib.tsx +0 -48
  43. package/src/PreactForm/fieldSubscriptions.lib.ts +0 -32
  44. package/src/PreactForm/formApiTypes.public.ts +0 -29
  45. package/src/PreactForm/formType.public.ts +0 -52
  46. package/src/PreactForm/useField.lib.ts +0 -52
  47. package/src/PreactForm/useForm.public.ts +0 -34
  48. package/src/Subscribe.public.tsx +0 -56
  49. package/src/index.ts +0 -18
  50. package/src/preactTypes.public.ts +0 -6
  51. package/src/types.public.ts +0 -2
@@ -1,7 +1,6 @@
1
1
  import { AnyPreactFormComponentMap, DefaultPreactFormComponentMap } from "../AppForm/componentMap.public.js";
2
2
  import { PreactFormApi } from "./formApiTypes.public.js";
3
3
  import { FormOptions, FormValidators, ToFormErrorTypes } from "@tanstack/form-core";
4
-
5
4
  //#region src/PreactForm/useForm.public.d.ts
6
5
  type UseFormHook<in out TComponents extends AnyPreactFormComponentMap> = <TFormData, const TFormValidators extends FormValidators<TFormData>, TSubmitReturn>(options: FormOptions<TFormData, TFormValidators, TSubmitReturn>) => PreactFormApi<TFormData, ToFormErrorTypes<TFormValidators, TSubmitReturn>, TComponents>;
7
6
  declare const useForm: UseFormHook<DefaultPreactFormComponentMap>;
@@ -1,6 +1,5 @@
1
1
  import { CrossVersionPreactNode } from "./preactTypes.public.js";
2
2
  import { Atom, ReadonlyAtom, ReadonlyStore, Store } from "@tanstack/preact-store";
3
-
4
3
  //#region src/Subscribe.public.d.ts
5
4
  type SubscribeSource<TValue> = Atom<TValue> | ReadonlyAtom<TValue> | Store<TValue> | ReadonlyStore<TValue>;
6
5
  /**
package/dist/index.d.ts CHANGED
@@ -6,7 +6,7 @@ import { UseFormHook, useForm } from "./PreactForm/useForm.public.js";
6
6
  import { AppFormOptions, AppFormOptionsApi } from "./AppForm/appFormOptions.public.js";
7
7
  import { PreactFormType } from "./PreactForm/formType.public.js";
8
8
  import { AnyFieldGroupApi, FieldGroupApi, FieldGroupArrayFieldComponent, FieldGroupFieldComponent, FieldGroupFormState, FieldGroupSubscribeComponent, FieldGroupSubscribeProps } from "./FieldGroup/FieldGroupApi.public.js";
9
- import { AnyFieldGroupFieldSlot, DefineFieldsFn, FieldGroupDefinition, FieldGroupFieldBindingForSlot, FieldGroupFieldBindings, FieldGroupFieldBindingsOf, FieldGroupFieldComponentsOf, FieldGroupFieldData, FieldGroupFieldNameForSlot, FieldGroupFieldNames, FieldGroupFieldSlot, FieldGroupFieldSlotAllows, FieldGroupFieldSlotMode, FieldGroupFieldSlotModeOf, FieldGroupFieldSlotValue, FieldGroupFields, FieldGroupFieldsOf, FieldGroupFieldsPropName, FieldGroupForm, FieldGroupHelper, FieldGroupHelpers, FieldGroupWithFieldsFn, LooseFieldGroupFieldSlot, StrictFieldGroupFieldSlot, getFieldGroupHelpers } from "./FieldGroup/withFields.public.js";
9
+ import { AnyFieldGroupFieldSlot, DefineFieldGroupFn, FieldGroupDefinition, FieldGroupFieldBindingForSlot, FieldGroupFieldBindings, FieldGroupFieldBindingsOf, FieldGroupFieldComponentsOf, FieldGroupFieldData, FieldGroupFieldNameForSlot, FieldGroupFieldNames, FieldGroupFieldSlot, FieldGroupFieldSlotAllows, FieldGroupFieldSlotMode, FieldGroupFieldSlotModeOf, FieldGroupFieldSlotValue, FieldGroupFields, FieldGroupFieldsOf, FieldGroupFieldsPropName, FieldGroupForm, FieldGroupHelper, FieldGroupWithFieldsFn, LooseFieldGroupFieldSlot, PreactFieldGroup, StrictFieldGroupFieldSlot, defineFieldGroup } from "./FieldGroup/withFields.public.js";
10
10
  import "./types.public.js";
11
11
  import { Subscribe, SubscribeProps, SubscribeSource } from "./Subscribe.public.js";
12
12
  import { AppFormComponent, PreactAppFormApi } from "./AppForm/PreactAppFormApi.public.js";
@@ -15,4 +15,4 @@ import { createFormHook } from "./AppForm/createFormHook.public.js";
15
15
  import { FormHookHelpers, getFormHookHelpers } from "./AppForm/getFormHookHelpers.public.js";
16
16
  export * from "@tanstack/form-core";
17
17
  export * from "@tanstack/preact-store";
18
- export { AnyFieldGroupApi, AnyFieldGroupFieldSlot, AnyPreactFormApi, AnyPreactFormComponentMap, AppFormComponent, AppFormHookResult, AppFormOptions, AppFormOptionsApi, CrossVersionPreactNode, DefaultPreactFormComponentMap, DefineFieldsFn, FieldGroupApi, FieldGroupArrayFieldComponent, FieldGroupDefinition, FieldGroupFieldBindingForSlot, FieldGroupFieldBindings, FieldGroupFieldBindingsOf, FieldGroupFieldComponent, FieldGroupFieldComponentsOf, FieldGroupFieldData, FieldGroupFieldNameForSlot, FieldGroupFieldNames, FieldGroupFieldSlot, FieldGroupFieldSlotAllows, FieldGroupFieldSlotMode, FieldGroupFieldSlotModeOf, FieldGroupFieldSlotValue, FieldGroupFields, FieldGroupFieldsOf, FieldGroupFieldsPropName, FieldGroupForm, FieldGroupFormState, FieldGroupHelper, FieldGroupHelpers, FieldGroupSubscribeComponent, FieldGroupSubscribeProps, FieldGroupWithFieldsFn, FormHookHelpers, LooseFieldGroupFieldSlot, PreactAppFormApi, PreactFieldApi, PreactFormApi, PreactFormArrayFieldComponent, PreactFormComponentMap, PreactFormFieldComponent, PreactFormFieldProps, PreactFormGroupApi, PreactFormGroupArrayFieldComponent, PreactFormGroupComponent, PreactFormGroupFieldComponent, PreactFormGroupProps, PreactFormGroupSubscribeComponent, PreactFormGroupSubscribeProps, PreactFormSubscribeComponent, PreactFormSubscribeProps, PreactFormType, PreactTanStackFormComponents, StrictFieldGroupFieldSlot, Subscribe, SubscribeProps, SubscribeSource, UseAppFormHook, UseFormHook, createFormHook, getFieldGroupHelpers, getFormHookHelpers, useForm };
18
+ export { AnyFieldGroupApi, AnyFieldGroupFieldSlot, AnyPreactFormApi, AnyPreactFormComponentMap, AppFormComponent, AppFormHookResult, AppFormOptions, AppFormOptionsApi, CrossVersionPreactNode, DefaultPreactFormComponentMap, DefineFieldGroupFn, FieldGroupApi, FieldGroupArrayFieldComponent, FieldGroupDefinition, FieldGroupFieldBindingForSlot, FieldGroupFieldBindings, FieldGroupFieldBindingsOf, FieldGroupFieldComponent, FieldGroupFieldComponentsOf, FieldGroupFieldData, FieldGroupFieldNameForSlot, FieldGroupFieldNames, FieldGroupFieldSlot, FieldGroupFieldSlotAllows, FieldGroupFieldSlotMode, FieldGroupFieldSlotModeOf, FieldGroupFieldSlotValue, FieldGroupFields, FieldGroupFieldsOf, FieldGroupFieldsPropName, FieldGroupForm, FieldGroupFormState, FieldGroupHelper, FieldGroupSubscribeComponent, FieldGroupSubscribeProps, FieldGroupWithFieldsFn, FormHookHelpers, LooseFieldGroupFieldSlot, PreactAppFormApi, PreactFieldApi, PreactFieldGroup, PreactFormApi, PreactFormArrayFieldComponent, PreactFormComponentMap, PreactFormFieldComponent, PreactFormFieldProps, PreactFormGroupApi, PreactFormGroupArrayFieldComponent, PreactFormGroupComponent, PreactFormGroupFieldComponent, PreactFormGroupProps, PreactFormGroupSubscribeComponent, PreactFormGroupSubscribeProps, PreactFormSubscribeComponent, PreactFormSubscribeProps, PreactFormType, PreactTanStackFormComponents, StrictFieldGroupFieldSlot, Subscribe, SubscribeProps, SubscribeSource, UseAppFormHook, UseFormHook, createFormHook, defineFieldGroup, getFormHookHelpers, useForm };
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { Subscribe } from "./Subscribe.public.js";
2
2
  import { useForm } from "./PreactForm/useForm.public.js";
3
- import { getFieldGroupHelpers } from "./FieldGroup/withFields.public.js";
3
+ import { defineFieldGroup } from "./FieldGroup/withFields.public.js";
4
4
  import { createFormHook } from "./AppForm/createFormHook.public.js";
5
5
  import { getFormHookHelpers } from "./AppForm/getFormHookHelpers.public.js";
6
6
 
@@ -8,4 +8,4 @@ export * from "@tanstack/form-core"
8
8
 
9
9
  export * from "@tanstack/preact-store"
10
10
 
11
- export { Subscribe, createFormHook, getFieldGroupHelpers, getFormHookHelpers, useForm };
11
+ export { Subscribe, createFormHook, defineFieldGroup, getFormHookHelpers, useForm };
@@ -1,7 +1,5 @@
1
1
  import { FunctionComponent } from "preact/compat";
2
-
3
2
  //#region src/preactTypes.public.d.ts
4
-
5
3
  /**
6
4
  * Keeps component children compatible across supported Preact versions.
7
5
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/preact-form",
3
- "version": "1.33.3",
3
+ "version": "2.0.0-alpha.1",
4
4
  "description": "Powerful, type-safe forms for Preact.",
5
5
  "author": "tannerlinsley",
6
6
  "license": "MIT",
@@ -31,15 +31,14 @@
31
31
  "node": ">=18"
32
32
  },
33
33
  "files": [
34
- "dist",
35
- "src"
34
+ "dist"
36
35
  ],
37
36
  "dependencies": {
38
- "@tanstack/preact-store": "^0.13.1",
39
- "@tanstack/form-core": "1.33.3"
37
+ "@tanstack/preact-store": "^0.13.2",
38
+ "@tanstack/form-core": "2.0.0-alpha.1"
40
39
  },
41
40
  "devDependencies": {
42
- "@eslint-react/eslint-plugin": "^5.18.1",
41
+ "@eslint-react/eslint-plugin": "^5.18.3",
43
42
  "@preact/preset-vite": "^2.10.2",
44
43
  "@testing-library/preact": "^3.2.4",
45
44
  "preact": "^10.26.4",
@@ -1,160 +0,0 @@
1
- import Preact from 'preact/compat'
2
-
3
- import { InternalFormGroupApi } from '@tanstack/form-core/internals'
4
- import { attachPreactFormComponents } from '../PreactForm/Components.lib'
5
- import { useField } from '../PreactForm/useField.lib'
6
- import { useValueFieldSubscription } from '../PreactForm/fieldSubscriptions.lib'
7
- import { Subscribe } from '../Subscribe.public'
8
- import { FieldContext, FormContext } from './contexts.lib'
9
- import type { AnyInternalFormApi } from '@tanstack/form-core/internals'
10
- import type { FunctionComponent } from 'preact/compat'
11
- import type {
12
- AppFormComponent,
13
- PreactAppFormApi,
14
- } from './PreactAppFormApi.public'
15
- import type { AnyPreactFormComponentMap } from './componentMap.public'
16
- import type { CrossVersionPreactNode } from '../preactTypes.public'
17
- import type {
18
- PreactFormFieldProps,
19
- PreactFormGroupProps,
20
- } from '../PreactForm/Components.public'
21
- import type { InternalPreactFormApi } from '../PreactForm/PreactFormApi.lib'
22
-
23
- type AnyPreactAppFormApi = PreactAppFormApi<any, any, AnyPreactFormComponentMap>
24
-
25
- export function attachPreactAppFormComponents(
26
- form: AnyInternalFormApi,
27
- formComponents: Record<string, FunctionComponent<any>>,
28
- fieldComponents: Record<string, FunctionComponent<any>>,
29
- ): AnyPreactAppFormApi {
30
- const resultForm = attachPreactFormComponents(
31
- form,
32
- fieldComponents,
33
- ) as never as AnyPreactAppFormApi
34
- resultForm.AppForm = createAppForm(form)
35
- resultForm.Field = createFieldWithContext(
36
- form,
37
- fieldComponents,
38
- ) as AnyPreactAppFormApi['Field']
39
- resultForm.FormGroup = createFormGroupWithContext(
40
- resultForm as any,
41
- ) as AnyPreactAppFormApi['FormGroup']
42
-
43
- return Object.assign(resultForm, formComponents)
44
- }
45
-
46
- function createAppForm(form: AnyInternalFormApi): AppFormComponent {
47
- const AppForm: FunctionComponent<{
48
- children: Exclude<CrossVersionPreactNode, Promise<any>>
49
- }> = function AppFormComponent(props) {
50
- // eslint-disable-next-line @eslint-react/no-context-provider
51
- return <FormContext.Provider value={form as never} {...props} />
52
- }
53
-
54
- AppForm.displayName = 'TanStackForm.AppForm'
55
-
56
- return AppForm
57
- }
58
-
59
- type AnyFieldComponent = FunctionComponent<
60
- PreactFormFieldProps<any, any, any, any, never, any, any, any>
61
- >
62
-
63
- function createFieldWithContext(
64
- form: AnyInternalFormApi,
65
- fieldComponents: Record<string, FunctionComponent<any>>,
66
- ) {
67
- const TanStackFormField: AnyFieldComponent = (props) => {
68
- const fieldApi = useField({ ...props, form }, fieldComponents)
69
- const field = useValueFieldSubscription(fieldApi)
70
-
71
- return (
72
- // eslint-disable-next-line @eslint-react/no-context-provider
73
- <FieldContext.Provider value={field}>
74
- {props.children(field) as never}
75
- </FieldContext.Provider>
76
- )
77
- }
78
-
79
- TanStackFormField.displayName = 'TanStackForm.Field'
80
-
81
- return TanStackFormField
82
- }
83
-
84
- type AnyFormGroupComponent = FunctionComponent<
85
- PreactFormGroupProps<any, any, any, any, any, any>
86
- >
87
-
88
- function createFormGroupWithContext(
89
- form: InternalPreactFormApi,
90
- ): AnyFormGroupComponent {
91
- const TanStackFormGroup: AnyFormGroupComponent = (props) => {
92
- const groupRef =
93
- Preact.useRef<InternalFormGroupApi<any, any, any, any, any>>(null)
94
-
95
- if (!groupRef.current) {
96
- groupRef.current = attachAppFormGroupComponents(
97
- new InternalFormGroupApi({ ...props, form } as never),
98
- form,
99
- )
100
- }
101
-
102
- Preact.useEffect(() => groupRef.current?.update({ ...props, form }))
103
-
104
- Preact.useEffect(() => {
105
- const group = groupRef.current!
106
- group.mount()
107
- return () => group._cleanup()
108
- }, [])
109
-
110
- return props.children(groupRef.current as never)
111
- }
112
-
113
- TanStackFormGroup.displayName = 'TanStackForm.FormGroup'
114
-
115
- return TanStackFormGroup
116
- }
117
-
118
- function attachAppFormGroupComponents(
119
- group: InternalFormGroupApi<any, any, any, any, any>,
120
- form: InternalPreactFormApi,
121
- ) {
122
- type GroupWithComponents = InternalFormGroupApi<any, any, any, any, any> & {
123
- Field: FunctionComponent<any>
124
- ArrayField: FunctionComponent<any>
125
- Subscribe: FunctionComponent<any>
126
- }
127
-
128
- const resultGroup: GroupWithComponents = group as never
129
-
130
- resultGroup.Field = function Field(props) {
131
- return (
132
- <form.Field
133
- {...(group._getFormFieldOptions(props, (base, overrides) => ({
134
- ...base,
135
- ...overrides,
136
- })) as any)}
137
- />
138
- )
139
- }
140
- resultGroup.Field.displayName = 'TanStackForm.FormGroup.Field'
141
-
142
- resultGroup.ArrayField = function ArrayField(props) {
143
- return (
144
- <form.ArrayField
145
- {...(group._getFormFieldOptions(props, (base, overrides) => ({
146
- ...base,
147
- ...overrides,
148
- })) as any)}
149
- />
150
- )
151
- }
152
- resultGroup.ArrayField.displayName = 'TanStackForm.FormGroup.ArrayField'
153
-
154
- resultGroup.Subscribe = (props) => {
155
- return <Subscribe source={group.atom} {...props} />
156
- }
157
- resultGroup.Subscribe.displayName = 'TanStackForm.FormGroup.Subscribe'
158
-
159
- return resultGroup
160
- }
@@ -1,17 +0,0 @@
1
- import type { FormErrorTypes } from '@tanstack/form-core'
2
- import type { PreactFormApi } from '../PreactForm/formApiTypes.public'
3
- import type { FunctionComponent } from 'preact/compat'
4
- import type { CrossVersionPreactNode } from '../preactTypes.public'
5
- import type { AnyPreactFormComponentMap } from './componentMap.public'
6
-
7
- export type AppFormComponent = FunctionComponent<{
8
- children: Exclude<CrossVersionPreactNode, Promise<any>>
9
- }>
10
-
11
- export type PreactAppFormApi<
12
- TFormData,
13
- TFormErrorTypes extends FormErrorTypes,
14
- TComponents extends AnyPreactFormComponentMap,
15
- > = PreactFormApi<TFormData, TFormErrorTypes, TComponents> & {
16
- AppForm: AppFormComponent
17
- }
@@ -1,59 +0,0 @@
1
- import type {
2
- FormOptions,
3
- FormValidatorData,
4
- FormValidators,
5
- InferUnion,
6
- NullableSchemaData,
7
- } from '@tanstack/form-core'
8
- import type { AnyPreactFormComponentMap } from './componentMap.public'
9
-
10
- declare const componentsSymbol: unique symbol
11
-
12
- export interface AppFormOptions<
13
- in out TFormData,
14
- in out TFormValidators extends FormValidators<TFormData>,
15
- in out TSubmitReturn,
16
- out TComponents extends AnyPreactFormComponentMap,
17
- > extends FormOptions<TFormData, TFormValidators, TSubmitReturn> {
18
- [componentsSymbol]: TComponents
19
- }
20
-
21
- export interface AppFormOptionsApi<
22
- in out TComponents extends AnyPreactFormComponentMap,
23
- > {
24
- <
25
- TFormData,
26
- const TFormValidators extends FormValidators<TFormData>,
27
- TSubmitReturn,
28
- >(
29
- options: FormOptions<TFormData, TFormValidators, TSubmitReturn>,
30
- ): AppFormOptions<TFormData, TFormValidators, TSubmitReturn, TComponents>
31
-
32
- strictSchema: <
33
- const TFormValidators extends FormValidators<any>,
34
- // Not quite sure why, but using FormValidatorData directly in the generic breaks things.
35
- // Probably something recursive going on that resolves it to `never`?
36
- TFormData extends FormValidatorData<TFormValidators>,
37
- TSubmitReturn,
38
- >(
39
- options: FormOptions<TFormData, TFormValidators, TSubmitReturn>,
40
- ) => AppFormOptions<
41
- FormValidatorData<TFormValidators>,
42
- TFormValidators,
43
- TSubmitReturn,
44
- TComponents
45
- >
46
-
47
- looseSchema: <
48
- const TFormValidators extends FormValidators<any>,
49
- const TFormData extends NullableSchemaData<TFormValidators>,
50
- TSubmitReturn,
51
- >(
52
- options: FormOptions<TFormData, TFormValidators, TSubmitReturn>,
53
- ) => AppFormOptions<
54
- InferUnion<TFormData, FormValidatorData<TFormValidators>>,
55
- TFormValidators,
56
- TSubmitReturn,
57
- TComponents
58
- >
59
- }
@@ -1,19 +0,0 @@
1
- import type { FunctionComponent } from 'preact/compat'
2
-
3
- export interface PreactFormComponentMap<
4
- in out TFormComponents extends Record<string, FunctionComponent<any>>,
5
- in out TFieldComponents extends Record<string, FunctionComponent<any>>,
6
- > {
7
- formComponents: TFormComponents
8
- fieldComponents: TFieldComponents
9
- }
10
-
11
- export type AnyPreactFormComponentMap = PreactFormComponentMap<
12
- Record<string, FunctionComponent<any>>,
13
- Record<string, FunctionComponent<any>>
14
- >
15
-
16
- export type DefaultPreactFormComponentMap = PreactFormComponentMap<
17
- Record<never, never>,
18
- Record<never, never>
19
- >
@@ -1,28 +0,0 @@
1
- import { createContext, useContext } from 'preact/compat'
2
- import type { AnyInternalFieldApi } from '@tanstack/form-core/internals'
3
- import type { InternalPreactFormApi } from '../PreactForm/PreactFormApi.lib'
4
-
5
- export const FormContext = createContext<InternalPreactFormApi | null>(null)
6
- export const FieldContext = createContext<AnyInternalFieldApi | null>(null)
7
-
8
- export function useFieldContext(): AnyInternalFieldApi {
9
- const field = useContext(FieldContext)
10
- if (field === null) {
11
- throw new Error(
12
- 'TanStack Form: Field components must be used within a `form.Field` component.',
13
- )
14
- }
15
-
16
- return field
17
- }
18
-
19
- export function useFormContext(): InternalPreactFormApi {
20
- const form = useContext(FormContext)
21
- if (form === null) {
22
- throw new Error(
23
- 'TanStack Form: Form components must be used within a `form.AppForm` component.',
24
- )
25
- }
26
-
27
- return form
28
- }
@@ -1,37 +0,0 @@
1
- import { useInternalForm } from '../PreactForm/PreactFormApi.lib'
2
- import { getFieldGroupHelpers } from '../FieldGroup/withFields.public'
3
- import { createAppFormInitializer } from './initializeAppForm.lib'
4
- import { useFormContext } from './contexts.lib'
5
- import type { AppFormOptionsApi } from './appFormOptions.public'
6
- import type { AnyPreactFormComponentMap } from './componentMap.public'
7
- import type {
8
- AppFormHookResult,
9
- UseAppFormHook,
10
- } from './createFormHookTypes.public'
11
- import type { FormOptions } from '@tanstack/form-core'
12
-
13
- const appFormOptions = ((opts) => {
14
- return opts
15
- }) as AppFormOptionsApi<any>
16
-
17
- appFormOptions.strictSchema = (opts) => opts as never
18
- appFormOptions.looseSchema = (opts) => opts as never
19
-
20
- export function createFormHook<
21
- const TComponents extends AnyPreactFormComponentMap,
22
- >(createOptions: TComponents): AppFormHookResult<TComponents> {
23
- const initializeAppForm = createAppFormInitializer(createOptions)
24
-
25
- function useExtendedForm(hookOptions: FormOptions<any, any, any>) {
26
- const form = useInternalForm(hookOptions, initializeAppForm)
27
- return form
28
- }
29
- const useAppForm = useExtendedForm as never as UseAppFormHook<TComponents>
30
-
31
- return {
32
- useFormContext: useFormContext as never,
33
- appFormOptions,
34
- getAppFieldGroupHelpers: getFieldGroupHelpers,
35
- useAppForm,
36
- }
37
- }
@@ -1,34 +0,0 @@
1
- import type { AppFormOptionsApi } from './appFormOptions.public'
2
- import type { AnyPreactFormComponentMap } from './componentMap.public'
3
- import type { PreactAppFormApi } from './PreactAppFormApi.public'
4
- import type { FieldGroupHelpers } from '../FieldGroup/withFields.public'
5
- import type {
6
- FormOptions,
7
- FormValidators,
8
- ToFormErrorTypes,
9
- } from '@tanstack/form-core'
10
-
11
- export type UseAppFormHook<
12
- in out TComponents extends AnyPreactFormComponentMap,
13
- > = <
14
- TFormData,
15
- TFormValidators extends FormValidators<TFormData>,
16
- TSubmitReturn,
17
- >(
18
- options: FormOptions<TFormData, TFormValidators, TSubmitReturn>,
19
- ) => PreactAppFormApi<
20
- TFormData,
21
- ToFormErrorTypes<TFormValidators, TSubmitReturn>,
22
- TComponents
23
- >
24
-
25
- export interface AppFormHookResult<
26
- in out TComponents extends AnyPreactFormComponentMap,
27
- > {
28
- appFormOptions: AppFormOptionsApi<TComponents>
29
- getAppFieldGroupHelpers: () => FieldGroupHelpers<
30
- TComponents['fieldComponents']
31
- >
32
- useAppForm: UseAppFormHook<TComponents>
33
- useFormContext: () => PreactAppFormApi<any, any, TComponents>
34
- }
@@ -1,30 +0,0 @@
1
- import { useFieldContext } from './contexts.lib'
2
- import type { FunctionComponent } from 'preact/compat'
3
- import type { CrossVersionPreactNode } from '../preactTypes.public'
4
-
5
- type AnyFieldComponent = (props: any) => CrossVersionPreactNode
6
-
7
- export function wrapField(
8
- Component: AnyFieldComponent,
9
- fieldPropKey: string,
10
- ): AnyFieldComponent {
11
- const Wrapper: FunctionComponent<any> = function TanStackFormFieldWrapper(
12
- props,
13
- ) {
14
- const field = useFieldContext()
15
-
16
- const newProps = { ...props, [fieldPropKey]: field }
17
-
18
- return <Component {...newProps} />
19
- }
20
-
21
- Wrapper.displayName = 'TanStackForm.FieldComponent'
22
-
23
- return Wrapper as never
24
- }
25
-
26
- export function brandComponentFactory(): (
27
- component: AnyFieldComponent,
28
- ) => AnyFieldComponent {
29
- return (component) => component
30
- }
@@ -1,114 +0,0 @@
1
- import { brandComponentFactory, wrapField } from './fieldComponentHelpers.lib'
2
- import type { CrossVersionPreactNode } from '../preactTypes.public'
3
- import type { FieldWithValue } from '@tanstack/form-core'
4
-
5
- type ExactFieldBrand<out TValue> = {
6
- readonly __tanstackFieldExactType: TValue
7
- }
8
-
9
- type AcceptsFieldBrand<out TAcceptedValue> = {
10
- readonly __tanstackFieldAcceptsType: TAcceptedValue
11
- }
12
-
13
- type AnyFieldComponent = (props: any) => CrossVersionPreactNode
14
-
15
- type PropsOf<TComponent> = TComponent extends (
16
- props: infer TProps,
17
- ) => CrossVersionPreactNode
18
- ? TProps
19
- : never
20
-
21
- type FieldValuePropKeys<TProps> = {
22
- [K in keyof TProps]-?: TProps[K] extends FieldWithValue<any> ? K : never
23
- }[keyof TProps]
24
-
25
- type FieldValueFromProp<TProp> =
26
- TProp extends FieldWithValue<infer TValue> ? TValue : never
27
-
28
- type FieldValueFromComponentProp<
29
- TProps,
30
- TFieldPropKey extends keyof TProps,
31
- > = FieldValueFromProp<TProps[TFieldPropKey]>
32
-
33
- type PublicPropsReplacingField<
34
- TProps,
35
- TFieldPropKey extends keyof TProps,
36
- > = Omit<TProps, TFieldPropKey>
37
-
38
- type StrictInjectedFieldComponent<
39
- TComponent extends AnyFieldComponent,
40
- TProps = PropsOf<TComponent>,
41
- TFieldPropKey extends FieldValuePropKeys<TProps> = FieldValuePropKeys<TProps>,
42
- > = ((
43
- props: PublicPropsReplacingField<TProps, TFieldPropKey>,
44
- ) => CrossVersionPreactNode) &
45
- ExactFieldBrand<FieldValueFromComponentProp<TProps, TFieldPropKey>>
46
-
47
- type LooseInjectedFieldComponent<
48
- TComponent extends AnyFieldComponent,
49
- TProps = PropsOf<TComponent>,
50
- TFieldPropKey extends FieldValuePropKeys<TProps> = FieldValuePropKeys<TProps>,
51
- > = ((
52
- props: PublicPropsReplacingField<TProps, TFieldPropKey>,
53
- ) => CrossVersionPreactNode) &
54
- AcceptsFieldBrand<FieldValueFromComponentProp<TProps, TFieldPropKey>>
55
-
56
- type StrictBrandedFieldComponent<
57
- TComponent extends AnyFieldComponent,
58
- TValue,
59
- > = TComponent & ExactFieldBrand<TValue>
60
-
61
- type LooseBrandedFieldComponent<
62
- TComponent extends AnyFieldComponent,
63
- TAcceptedValue,
64
- > = TComponent & AcceptsFieldBrand<TAcceptedValue>
65
-
66
- interface FieldComponentHelper {
67
- strict: <
68
- TComponent extends AnyFieldComponent,
69
- TProps = PropsOf<TComponent>,
70
- TFieldPropKey extends FieldValuePropKeys<TProps> =
71
- FieldValuePropKeys<TProps>,
72
- >(
73
- Component: TComponent,
74
- fieldPropKey: TFieldPropKey,
75
- ) => StrictInjectedFieldComponent<TComponent, TProps, TFieldPropKey>
76
-
77
- loose: <
78
- TComponent extends AnyFieldComponent,
79
- TProps = PropsOf<TComponent>,
80
- TFieldPropKey extends FieldValuePropKeys<TProps> =
81
- FieldValuePropKeys<TProps>,
82
- >(
83
- Component: TComponent,
84
- fieldPropKey: TFieldPropKey,
85
- ) => LooseInjectedFieldComponent<TComponent, TProps, TFieldPropKey>
86
- }
87
-
88
- interface FieldBrandHelper {
89
- strict: <TValue>() => <TComponent extends AnyFieldComponent>(
90
- Component: TComponent,
91
- ) => StrictBrandedFieldComponent<TComponent, TValue>
92
-
93
- loose: <TValue>() => <TComponent extends AnyFieldComponent>(
94
- Component: TComponent,
95
- ) => LooseBrandedFieldComponent<TComponent, TValue>
96
- }
97
-
98
- export interface FormHookHelpers {
99
- fieldBrand: FieldBrandHelper
100
- fieldComponent: FieldComponentHelper
101
- }
102
-
103
- export function getFormHookHelpers(): FormHookHelpers {
104
- return {
105
- fieldComponent: {
106
- loose: wrapField as FieldComponentHelper['loose'],
107
- strict: wrapField as FieldComponentHelper['strict'],
108
- },
109
- fieldBrand: {
110
- loose: brandComponentFactory as FieldBrandHelper['loose'],
111
- strict: brandComponentFactory as FieldBrandHelper['strict'],
112
- },
113
- }
114
- }
@@ -1,22 +0,0 @@
1
- import { InternalFormApi } from '@tanstack/form-core/internals'
2
- import { attachPreactAppFormComponents } from './Components.lib'
3
- import type { FormOptions } from '@tanstack/form-core'
4
- import type { InternalPreactFormApi } from '../PreactForm/PreactFormApi.lib'
5
- import type { AnyPreactFormComponentMap } from './componentMap.public'
6
-
7
- export function createAppFormInitializer<
8
- TComponents extends AnyPreactFormComponentMap,
9
- >(
10
- createOptions: TComponents,
11
- ): (options: FormOptions<any, any, any>) => InternalPreactFormApi {
12
- return (options) => {
13
- const form = new InternalFormApi(options)
14
- const extendedForm = attachPreactAppFormComponents(
15
- form,
16
- createOptions.formComponents,
17
- createOptions.fieldComponents,
18
- )
19
-
20
- return extendedForm as never
21
- }
22
- }