@tanstack/react-form 0.11.0 → 0.13.0

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 (132) hide show
  1. package/dist/cjs/createFormFactory.d.ts +11 -0
  2. package/{build/legacy → dist/cjs}/formContext.d.ts +4 -7
  3. package/dist/cjs/index.cjs +181 -0
  4. package/dist/cjs/index.cjs.map +1 -0
  5. package/dist/cjs/index.d.cts +8 -0
  6. package/dist/cjs/index.d.ts +8 -0
  7. package/dist/cjs/index.js +181 -0
  8. package/dist/cjs/tests/createFormFactory.test.d.ts +1 -0
  9. package/dist/cjs/tests/useField.test-d.d.ts +1 -0
  10. package/dist/cjs/tests/useField.test.d.ts +1 -0
  11. package/dist/cjs/tests/useForm.test-d.d.ts +1 -0
  12. package/dist/cjs/tests/useForm.test.d.ts +1 -0
  13. package/dist/cjs/tests/utils.d.ts +1 -0
  14. package/dist/cjs/types.d.ts +4 -0
  15. package/dist/cjs/useField.d.ts +28 -0
  16. package/{build/legacy → dist/cjs}/useForm.d.ts +7 -9
  17. package/dist/cjs/useIsomorphicEffectOnce.d.ts +5 -0
  18. package/dist/cjs/useIsomorphicLayoutEffect.d.ts +3 -0
  19. package/dist/cjs/useTransform.d.ts +5 -0
  20. package/dist/cjs/validateFormData.d.ts +14 -0
  21. package/dist/mjs/createFormFactory.d.ts +11 -0
  22. package/{build/modern → dist/mjs}/formContext.d.ts +4 -7
  23. package/dist/mjs/index.d.mts +8 -0
  24. package/dist/mjs/index.d.ts +8 -0
  25. package/dist/mjs/index.js +170 -0
  26. package/dist/mjs/index.mjs +170 -0
  27. package/dist/mjs/index.mjs.map +1 -0
  28. package/dist/mjs/tests/createFormFactory.test.d.ts +1 -0
  29. package/dist/mjs/tests/useField.test-d.d.ts +1 -0
  30. package/dist/mjs/tests/useField.test.d.ts +1 -0
  31. package/dist/mjs/tests/useForm.test-d.d.ts +1 -0
  32. package/dist/mjs/tests/useForm.test.d.ts +1 -0
  33. package/dist/mjs/tests/utils.d.ts +1 -0
  34. package/dist/mjs/types.d.ts +4 -0
  35. package/dist/mjs/useField.d.ts +28 -0
  36. package/{build/modern → dist/mjs}/useForm.d.ts +7 -9
  37. package/dist/mjs/useIsomorphicEffectOnce.d.ts +5 -0
  38. package/dist/mjs/useIsomorphicLayoutEffect.d.ts +3 -0
  39. package/dist/mjs/useTransform.d.ts +5 -0
  40. package/dist/mjs/validateFormData.d.ts +14 -0
  41. package/package.json +21 -24
  42. package/src/createFormFactory.ts +10 -0
  43. package/src/formContext.ts +3 -3
  44. package/src/index.ts +7 -1
  45. package/src/tests/useField.test-d.tsx +2 -2
  46. package/src/tests/useForm.test.tsx +2 -3
  47. package/src/useField.tsx +15 -3
  48. package/src/useForm.tsx +5 -1
  49. package/src/useIsomorphicEffectOnce.ts +38 -0
  50. package/src/useIsomorphicLayoutEffect.ts +1 -1
  51. package/src/useTransform.ts +16 -0
  52. package/src/validateFormData.ts +70 -0
  53. package/build/legacy/createFormFactory.cjs +0 -42
  54. package/build/legacy/createFormFactory.cjs.map +0 -1
  55. package/build/legacy/createFormFactory.d.cts +0 -12
  56. package/build/legacy/createFormFactory.d.ts +0 -12
  57. package/build/legacy/createFormFactory.js +0 -17
  58. package/build/legacy/createFormFactory.js.map +0 -1
  59. package/build/legacy/formContext.cjs +0 -51
  60. package/build/legacy/formContext.cjs.map +0 -1
  61. package/build/legacy/formContext.d.cts +0 -13
  62. package/build/legacy/formContext.js +0 -15
  63. package/build/legacy/formContext.js.map +0 -1
  64. package/build/legacy/index.cjs +0 -46
  65. package/build/legacy/index.cjs.map +0 -1
  66. package/build/legacy/index.d.cts +0 -7
  67. package/build/legacy/index.d.ts +0 -7
  68. package/build/legacy/index.js +0 -15
  69. package/build/legacy/index.js.map +0 -1
  70. package/build/legacy/types.cjs +0 -19
  71. package/build/legacy/types.cjs.map +0 -1
  72. package/build/legacy/types.d.cts +0 -7
  73. package/build/legacy/types.d.ts +0 -7
  74. package/build/legacy/types.js +0 -1
  75. package/build/legacy/types.js.map +0 -1
  76. package/build/legacy/useField.cjs +0 -88
  77. package/build/legacy/useField.cjs.map +0 -1
  78. package/build/legacy/useField.d.cts +0 -27
  79. package/build/legacy/useField.d.ts +0 -27
  80. package/build/legacy/useField.js +0 -52
  81. package/build/legacy/useField.js.map +0 -1
  82. package/build/legacy/useForm.cjs +0 -73
  83. package/build/legacy/useForm.cjs.map +0 -1
  84. package/build/legacy/useForm.d.cts +0 -21
  85. package/build/legacy/useForm.js +0 -38
  86. package/build/legacy/useForm.js.map +0 -1
  87. package/build/legacy/useIsomorphicLayoutEffect.cjs +0 -35
  88. package/build/legacy/useIsomorphicLayoutEffect.cjs.map +0 -1
  89. package/build/legacy/useIsomorphicLayoutEffect.d.cts +0 -5
  90. package/build/legacy/useIsomorphicLayoutEffect.d.ts +0 -5
  91. package/build/legacy/useIsomorphicLayoutEffect.js +0 -10
  92. package/build/legacy/useIsomorphicLayoutEffect.js.map +0 -1
  93. package/build/modern/createFormFactory.cjs +0 -42
  94. package/build/modern/createFormFactory.cjs.map +0 -1
  95. package/build/modern/createFormFactory.d.cts +0 -12
  96. package/build/modern/createFormFactory.d.ts +0 -12
  97. package/build/modern/createFormFactory.js +0 -17
  98. package/build/modern/createFormFactory.js.map +0 -1
  99. package/build/modern/formContext.cjs +0 -51
  100. package/build/modern/formContext.cjs.map +0 -1
  101. package/build/modern/formContext.d.cts +0 -13
  102. package/build/modern/formContext.js +0 -15
  103. package/build/modern/formContext.js.map +0 -1
  104. package/build/modern/index.cjs +0 -46
  105. package/build/modern/index.cjs.map +0 -1
  106. package/build/modern/index.d.cts +0 -7
  107. package/build/modern/index.d.ts +0 -7
  108. package/build/modern/index.js +0 -15
  109. package/build/modern/index.js.map +0 -1
  110. package/build/modern/types.cjs +0 -19
  111. package/build/modern/types.cjs.map +0 -1
  112. package/build/modern/types.d.cts +0 -7
  113. package/build/modern/types.d.ts +0 -7
  114. package/build/modern/types.js +0 -1
  115. package/build/modern/types.js.map +0 -1
  116. package/build/modern/useField.cjs +0 -88
  117. package/build/modern/useField.cjs.map +0 -1
  118. package/build/modern/useField.d.cts +0 -27
  119. package/build/modern/useField.d.ts +0 -27
  120. package/build/modern/useField.js +0 -52
  121. package/build/modern/useField.js.map +0 -1
  122. package/build/modern/useForm.cjs +0 -73
  123. package/build/modern/useForm.cjs.map +0 -1
  124. package/build/modern/useForm.d.cts +0 -21
  125. package/build/modern/useForm.js +0 -38
  126. package/build/modern/useForm.js.map +0 -1
  127. package/build/modern/useIsomorphicLayoutEffect.cjs +0 -35
  128. package/build/modern/useIsomorphicLayoutEffect.cjs.map +0 -1
  129. package/build/modern/useIsomorphicLayoutEffect.d.cts +0 -5
  130. package/build/modern/useIsomorphicLayoutEffect.d.ts +0 -5
  131. package/build/modern/useIsomorphicLayoutEffect.js +0 -10
  132. package/build/modern/useIsomorphicLayoutEffect.js.map +0 -1
@@ -49,7 +49,7 @@ it('should type onChange properly', () => {
49
49
  return null
50
50
  },
51
51
  }}
52
- children={(field) => null}
52
+ children={() => null}
53
53
  />
54
54
  <form.Field
55
55
  name="age"
@@ -59,7 +59,7 @@ it('should type onChange properly', () => {
59
59
  return null
60
60
  },
61
61
  }}
62
- children={(field) => null}
62
+ children={() => null}
63
63
  />
64
64
  </form.Provider>
65
65
  )
@@ -178,6 +178,7 @@ describe('useForm', () => {
178
178
  },
179
179
  },
180
180
  })
181
+ const onChangeError = form.useStore((s) => s.errorMap.onChange)
181
182
  return (
182
183
  <form.Provider>
183
184
  <form.Field
@@ -192,9 +193,7 @@ describe('useForm', () => {
192
193
  />
193
194
  )}
194
195
  />
195
- <form.Subscribe selector={(state) => state.errorMap}>
196
- {(errorMap) => <p>{errorMap.onChange}</p>}
197
- </form.Subscribe>
196
+ <p>{onChangeError}</p>
198
197
  </form.Provider>
199
198
  )
200
199
  }
package/src/useField.tsx CHANGED
@@ -1,4 +1,4 @@
1
- import React, { useState } from 'react'
1
+ import React, { useRef, useState } from 'rehackt'
2
2
  import { useStore } from '@tanstack/react-store'
3
3
  import type {
4
4
  DeepKeys,
@@ -10,6 +10,7 @@ import { FieldApi, functionalUpdate } from '@tanstack/form-core'
10
10
  import { useFormContext, formContext } from './formContext'
11
11
  import type { UseFieldOptions } from './types'
12
12
  import { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect'
13
+ import { useIsomorphicEffectOnce } from './useIsomorphicEffectOnce'
13
14
 
14
15
  declare module '@tanstack/form-core' {
15
16
  // eslint-disable-next-line no-shadow
@@ -103,8 +104,19 @@ export function useField<
103
104
  }
104
105
  : undefined,
105
106
  )
106
- // Instantiates field meta and removes it when unrendered
107
- useIsomorphicLayoutEffect(() => fieldApi.mount(), [fieldApi])
107
+ const unmountFn = useRef<(() => void) | null>(null)
108
+
109
+ useIsomorphicEffectOnce(() => {
110
+ return () => {
111
+ unmountFn.current?.()
112
+ }
113
+ })
114
+
115
+ // We have to mount it right as soon as it renders, otherwise we get:
116
+ // https://github.com/TanStack/form/issues/523
117
+ if (!unmountFn.current) {
118
+ unmountFn.current = fieldApi.mount()
119
+ }
108
120
 
109
121
  return fieldApi as never
110
122
  }
package/src/useForm.tsx CHANGED
@@ -2,7 +2,11 @@ import type { FormState, FormOptions, Validator } from '@tanstack/form-core'
2
2
  import { FormApi, functionalUpdate } from '@tanstack/form-core'
3
3
  import type { NoInfer } from '@tanstack/react-store'
4
4
  import { useStore } from '@tanstack/react-store'
5
- import React, { type PropsWithChildren, type ReactNode, useState } from 'react'
5
+ import React, {
6
+ type PropsWithChildren,
7
+ type ReactNode,
8
+ useState,
9
+ } from 'rehackt'
6
10
  import { type UseField, type FieldComponent, Field, useField } from './useField'
7
11
  import { formContext } from './formContext'
8
12
  import { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect'
@@ -0,0 +1,38 @@
1
+ import { useRef, useState, type EffectCallback } from 'rehackt'
2
+ import { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect'
3
+
4
+ /**
5
+ * This hook handles StrictMode and prod mode
6
+ */
7
+ export const useIsomorphicEffectOnce = (effect: EffectCallback) => {
8
+ const destroyFunc = useRef<void | (() => void)>()
9
+ const effectCalled = useRef(false)
10
+ const renderAfterCalled = useRef(false)
11
+ const [val, setVal] = useState(0)
12
+
13
+ if (effectCalled.current) {
14
+ renderAfterCalled.current = true
15
+ }
16
+
17
+ useIsomorphicLayoutEffect(() => {
18
+ // only execute the effect first time around
19
+ if (!effectCalled.current) {
20
+ destroyFunc.current = effect()
21
+ effectCalled.current = true
22
+ }
23
+
24
+ // this forces one render after the effect is run
25
+ setVal((v) => v + 1)
26
+
27
+ return () => {
28
+ // if the comp didn't render since the useEffect was called,
29
+ // we know it's the dummy React cycle
30
+ if (!renderAfterCalled.current) {
31
+ return
32
+ }
33
+ if (destroyFunc.current) {
34
+ destroyFunc.current()
35
+ }
36
+ }
37
+ }, [])
38
+ }
@@ -1,4 +1,4 @@
1
- import { useEffect, useLayoutEffect } from 'react'
1
+ import { useEffect, useLayoutEffect } from 'rehackt'
2
2
 
3
3
  export const useIsomorphicLayoutEffect =
4
4
  // @ts-ignore
@@ -0,0 +1,16 @@
1
+ import type { FormApi, Validator } from '@tanstack/form-core'
2
+
3
+ export function useTransform<
4
+ TFormData,
5
+ TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,
6
+ >(
7
+ fn: (
8
+ formBase: FormApi<TFormData, TFormValidator>,
9
+ ) => FormApi<TFormData, TFormValidator>,
10
+ deps: unknown[],
11
+ ) {
12
+ return {
13
+ fn,
14
+ deps,
15
+ }
16
+ }
@@ -0,0 +1,70 @@
1
+ import { decode } from 'decode-formdata'
2
+ import type {
3
+ FormApi,
4
+ FormOptions,
5
+ ValidationError,
6
+ Validator,
7
+ } from '@tanstack/form-core'
8
+
9
+ type OnServerValidateFn<TFormData> = (props: {
10
+ value: TFormData
11
+ }) => ValidationError
12
+
13
+ type OnServerValidateOrFn<
14
+ TFormData,
15
+ TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,
16
+ > = TFormValidator extends Validator<TFormData, infer FFN>
17
+ ? FFN | OnServerValidateFn<TFormData>
18
+ : OnServerValidateFn<TFormData>
19
+
20
+ declare module '@tanstack/form-core' {
21
+ // eslint-disable-next-line no-shadow
22
+ interface FormOptions<
23
+ TFormData,
24
+ TFormValidator extends
25
+ | Validator<TFormData, unknown>
26
+ | undefined = undefined,
27
+ > {
28
+ onServerValidate?: OnServerValidateOrFn<TFormData, TFormValidator>
29
+ }
30
+ }
31
+
32
+ export type ValidateFormData<
33
+ TFormData,
34
+ TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,
35
+ > = (
36
+ formData: FormData,
37
+ info?: Parameters<typeof decode>[1],
38
+ ) => Promise<Partial<FormApi<TFormData, TFormValidator>['state']>>
39
+
40
+ export const getValidateFormData = <
41
+ TFormData,
42
+ TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,
43
+ >(
44
+ defaultOpts?: FormOptions<TFormData, TFormValidator>,
45
+ ) =>
46
+ (async (
47
+ formData: FormData,
48
+ info?: Parameters<typeof decode>[1],
49
+ ): Promise<Partial<FormApi<TFormData, TFormValidator>['state']>> => {
50
+ const { validatorAdapter, onServerValidate } = defaultOpts || {}
51
+
52
+ const runValidator = (propsValue: { value: TFormData }) => {
53
+ if (validatorAdapter && typeof onServerValidate !== 'function') {
54
+ return validatorAdapter().validate(propsValue, onServerValidate)
55
+ }
56
+
57
+ return (onServerValidate as OnServerValidateFn<TFormData>)(propsValue)
58
+ }
59
+
60
+ const data = decode(formData, info) as never as TFormData
61
+
62
+ const onServerError = runValidator({ value: data })
63
+
64
+ return {
65
+ errorMap: {
66
+ onServer: onServerError,
67
+ },
68
+ errors: onServerError ? [onServerError] : [],
69
+ }
70
+ }) as ValidateFormData<TFormData, TFormValidator>
@@ -1,42 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
-
20
- // src/createFormFactory.ts
21
- var createFormFactory_exports = {};
22
- __export(createFormFactory_exports, {
23
- createFormFactory: () => createFormFactory
24
- });
25
- module.exports = __toCommonJS(createFormFactory_exports);
26
- var import_useField = require("./useField.cjs");
27
- var import_useForm = require("./useForm.cjs");
28
- function createFormFactory(defaultOpts) {
29
- return {
30
- useForm: (opts) => {
31
- const formOptions = Object.assign({}, defaultOpts, opts);
32
- return (0, import_useForm.useForm)(formOptions);
33
- },
34
- useField: import_useField.useField,
35
- Field: import_useField.Field
36
- };
37
- }
38
- // Annotate the CommonJS export names for ESM import in node:
39
- 0 && (module.exports = {
40
- createFormFactory
41
- });
42
- //# sourceMappingURL=createFormFactory.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../src/createFormFactory.ts"],"sourcesContent":["import type { FormApi, FormOptions, Validator } from '@tanstack/form-core'\n\nimport { type UseField, type FieldComponent, Field, useField } from './useField'\nimport { useForm } from './useForm'\n\nexport type FormFactory<\n TFormData,\n TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,\n> = {\n useForm: (\n opts?: FormOptions<TFormData, TFormValidator>,\n ) => FormApi<TFormData, TFormValidator>\n useField: UseField<TFormData>\n Field: FieldComponent<TFormData, TFormValidator>\n}\n\nexport function createFormFactory<\n TFormData,\n TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,\n>(\n defaultOpts?: FormOptions<TFormData, TFormValidator>,\n): FormFactory<TFormData, TFormValidator> {\n return {\n useForm: (opts) => {\n const formOptions = Object.assign({}, defaultOpts, opts)\n return useForm<TFormData, TFormValidator>(formOptions)\n },\n useField: useField as any,\n Field: Field as any,\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAEA,sBAAoE;AACpE,qBAAwB;AAajB,SAAS,kBAId,aACwC;AACxC,SAAO;AAAA,IACL,SAAS,CAAC,SAAS;AACjB,YAAM,cAAc,OAAO,OAAO,CAAC,GAAG,aAAa,IAAI;AACvD,iBAAO,wBAAmC,WAAW;AAAA,IACvD;AAAA,IACA,UAAU;AAAA,IACV,OAAO;AAAA,EACT;AACF;","names":[]}
@@ -1,12 +0,0 @@
1
- import { Validator, FormOptions, FormApi } from '@tanstack/form-core';
2
- import { UseField, FieldComponent } from './useField.cjs';
3
- import './types.cjs';
4
-
5
- type FormFactory<TFormData, TFormValidator extends Validator<TFormData, unknown> | undefined = undefined> = {
6
- useForm: (opts?: FormOptions<TFormData, TFormValidator>) => FormApi<TFormData, TFormValidator>;
7
- useField: UseField<TFormData>;
8
- Field: FieldComponent<TFormData, TFormValidator>;
9
- };
10
- declare function createFormFactory<TFormData, TFormValidator extends Validator<TFormData, unknown> | undefined = undefined>(defaultOpts?: FormOptions<TFormData, TFormValidator>): FormFactory<TFormData, TFormValidator>;
11
-
12
- export { FormFactory, createFormFactory };
@@ -1,12 +0,0 @@
1
- import { Validator, FormOptions, FormApi } from '@tanstack/form-core';
2
- import { UseField, FieldComponent } from './useField.js';
3
- import './types.js';
4
-
5
- type FormFactory<TFormData, TFormValidator extends Validator<TFormData, unknown> | undefined = undefined> = {
6
- useForm: (opts?: FormOptions<TFormData, TFormValidator>) => FormApi<TFormData, TFormValidator>;
7
- useField: UseField<TFormData>;
8
- Field: FieldComponent<TFormData, TFormValidator>;
9
- };
10
- declare function createFormFactory<TFormData, TFormValidator extends Validator<TFormData, unknown> | undefined = undefined>(defaultOpts?: FormOptions<TFormData, TFormValidator>): FormFactory<TFormData, TFormValidator>;
11
-
12
- export { FormFactory, createFormFactory };
@@ -1,17 +0,0 @@
1
- // src/createFormFactory.ts
2
- import { Field, useField } from "./useField.js";
3
- import { useForm } from "./useForm.js";
4
- function createFormFactory(defaultOpts) {
5
- return {
6
- useForm: (opts) => {
7
- const formOptions = Object.assign({}, defaultOpts, opts);
8
- return useForm(formOptions);
9
- },
10
- useField,
11
- Field
12
- };
13
- }
14
- export {
15
- createFormFactory
16
- };
17
- //# sourceMappingURL=createFormFactory.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../src/createFormFactory.ts"],"sourcesContent":["import type { FormApi, FormOptions, Validator } from '@tanstack/form-core'\n\nimport { type UseField, type FieldComponent, Field, useField } from './useField'\nimport { useForm } from './useForm'\n\nexport type FormFactory<\n TFormData,\n TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,\n> = {\n useForm: (\n opts?: FormOptions<TFormData, TFormValidator>,\n ) => FormApi<TFormData, TFormValidator>\n useField: UseField<TFormData>\n Field: FieldComponent<TFormData, TFormValidator>\n}\n\nexport function createFormFactory<\n TFormData,\n TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,\n>(\n defaultOpts?: FormOptions<TFormData, TFormValidator>,\n): FormFactory<TFormData, TFormValidator> {\n return {\n useForm: (opts) => {\n const formOptions = Object.assign({}, defaultOpts, opts)\n return useForm<TFormData, TFormValidator>(formOptions)\n },\n useField: useField as any,\n Field: Field as any,\n }\n}\n"],"mappings":";AAEA,SAA6C,OAAO,gBAAgB;AACpE,SAAS,eAAe;AAajB,SAAS,kBAId,aACwC;AACxC,SAAO;AAAA,IACL,SAAS,CAAC,SAAS;AACjB,YAAM,cAAc,OAAO,OAAO,CAAC,GAAG,aAAa,IAAI;AACvD,aAAO,QAAmC,WAAW;AAAA,IACvD;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;","names":[]}
@@ -1,51 +0,0 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
- var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
19
- };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
-
30
- // src/formContext.ts
31
- var formContext_exports = {};
32
- __export(formContext_exports, {
33
- formContext: () => formContext,
34
- useFormContext: () => useFormContext
35
- });
36
- module.exports = __toCommonJS(formContext_exports);
37
- var React = __toESM(require("react"), 1);
38
- var formContext = React.createContext(null);
39
- function useFormContext() {
40
- const formApi = React.useContext(formContext);
41
- if (!formApi) {
42
- throw new Error(`You are trying to use the form API outside of a form!`);
43
- }
44
- return formApi;
45
- }
46
- // Annotate the CommonJS export names for ESM import in node:
47
- 0 && (module.exports = {
48
- formContext,
49
- useFormContext
50
- });
51
- //# sourceMappingURL=formContext.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../src/formContext.ts"],"sourcesContent":["import type { FormApi, Validator } from '@tanstack/form-core'\nimport * as React from 'react'\n\nexport const formContext = React.createContext<{\n formApi: FormApi<any, Validator<any, unknown> | undefined>\n parentFieldName?: string\n} | null>(null!)\n\nexport function useFormContext() {\n const formApi = React.useContext(formContext)\n\n if (!formApi) {\n throw new Error(`You are trying to use the form API outside of a form!`)\n }\n\n return formApi\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,YAAuB;AAEhB,IAAM,cAAoB,oBAGvB,IAAK;AAER,SAAS,iBAAiB;AAC/B,QAAM,UAAgB,iBAAW,WAAW;AAE5C,MAAI,CAAC,SAAS;AACZ,UAAM,IAAI,MAAM,uDAAuD;AAAA,EACzE;AAEA,SAAO;AACT;","names":[]}
@@ -1,13 +0,0 @@
1
- import { FormApi, Validator } from '@tanstack/form-core';
2
- import * as React from 'react';
3
-
4
- declare const formContext: React.Context<{
5
- formApi: FormApi<any, Validator<any, unknown> | undefined>;
6
- parentFieldName?: string | undefined;
7
- } | null>;
8
- declare function useFormContext(): {
9
- formApi: FormApi<any, Validator<any, unknown> | undefined>;
10
- parentFieldName?: string | undefined;
11
- };
12
-
13
- export { formContext, useFormContext };
@@ -1,15 +0,0 @@
1
- // src/formContext.ts
2
- import * as React from "react";
3
- var formContext = React.createContext(null);
4
- function useFormContext() {
5
- const formApi = React.useContext(formContext);
6
- if (!formApi) {
7
- throw new Error(`You are trying to use the form API outside of a form!`);
8
- }
9
- return formApi;
10
- }
11
- export {
12
- formContext,
13
- useFormContext
14
- };
15
- //# sourceMappingURL=formContext.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../src/formContext.ts"],"sourcesContent":["import type { FormApi, Validator } from '@tanstack/form-core'\nimport * as React from 'react'\n\nexport const formContext = React.createContext<{\n formApi: FormApi<any, Validator<any, unknown> | undefined>\n parentFieldName?: string\n} | null>(null!)\n\nexport function useFormContext() {\n const formApi = React.useContext(formContext)\n\n if (!formApi) {\n throw new Error(`You are trying to use the form API outside of a form!`)\n }\n\n return formApi\n}\n"],"mappings":";AACA,YAAY,WAAW;AAEhB,IAAM,cAAoB,oBAGvB,IAAK;AAER,SAAS,iBAAiB;AAC/B,QAAM,UAAgB,iBAAW,WAAW;AAE5C,MAAI,CAAC,SAAS;AACZ,UAAM,IAAI,MAAM,uDAAuD;AAAA,EACzE;AAEA,SAAO;AACT;","names":[]}
@@ -1,46 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
-
20
- // src/index.ts
21
- var src_exports = {};
22
- __export(src_exports, {
23
- Field: () => import_useField.Field,
24
- FieldApi: () => import_form_core.FieldApi,
25
- FormApi: () => import_form_core.FormApi,
26
- createFormFactory: () => import_createFormFactory.createFormFactory,
27
- functionalUpdate: () => import_form_core.functionalUpdate,
28
- useField: () => import_useField.useField,
29
- useForm: () => import_useForm.useForm
30
- });
31
- module.exports = __toCommonJS(src_exports);
32
- var import_form_core = require("@tanstack/form-core");
33
- var import_useForm = require("./useForm.cjs");
34
- var import_useField = require("./useField.cjs");
35
- var import_createFormFactory = require("./createFormFactory.cjs");
36
- // Annotate the CommonJS export names for ESM import in node:
37
- 0 && (module.exports = {
38
- Field,
39
- FieldApi,
40
- FormApi,
41
- createFormFactory,
42
- functionalUpdate,
43
- useField,
44
- useForm
45
- });
46
- //# sourceMappingURL=index.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../src/index.ts"],"sourcesContent":["export type {\n DeepKeys,\n DeepValue,\n FieldApiOptions,\n FieldInfo,\n FieldMeta,\n FieldOptions,\n FieldState,\n FormOptions,\n FormState,\n RequiredByKey,\n Updater,\n UpdaterFn,\n ValidationCause,\n ValidationError,\n ValidationMeta,\n} from '@tanstack/form-core'\n\nexport { FormApi, FieldApi, functionalUpdate } from '@tanstack/form-core'\n\nexport { useForm } from './useForm'\n\nexport type { UseField, FieldComponent } from './useField'\nexport { useField, Field } from './useField'\n\nexport type { FormFactory } from './createFormFactory'\nexport { createFormFactory } from './createFormFactory'\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAkBA,uBAAoD;AAEpD,qBAAwB;AAGxB,sBAAgC;AAGhC,+BAAkC;","names":[]}
@@ -1,7 +0,0 @@
1
- export { DeepKeys, DeepValue, FieldApi, FieldApiOptions, FieldInfo, FieldMeta, FieldOptions, FieldState, FormApi, FormOptions, FormState, RequiredByKey, Updater, UpdaterFn, ValidationCause, ValidationError, ValidationMeta, functionalUpdate } from '@tanstack/form-core';
2
- export { useForm } from './useForm.cjs';
3
- export { Field, FieldComponent, UseField, useField } from './useField.cjs';
4
- export { FormFactory, createFormFactory } from './createFormFactory.cjs';
5
- import '@tanstack/react-store';
6
- import 'react';
7
- import './types.cjs';
@@ -1,7 +0,0 @@
1
- export { DeepKeys, DeepValue, FieldApi, FieldApiOptions, FieldInfo, FieldMeta, FieldOptions, FieldState, FormApi, FormOptions, FormState, RequiredByKey, Updater, UpdaterFn, ValidationCause, ValidationError, ValidationMeta, functionalUpdate } from '@tanstack/form-core';
2
- export { useForm } from './useForm.js';
3
- export { Field, FieldComponent, UseField, useField } from './useField.js';
4
- export { FormFactory, createFormFactory } from './createFormFactory.js';
5
- import '@tanstack/react-store';
6
- import 'react';
7
- import './types.js';
@@ -1,15 +0,0 @@
1
- // src/index.ts
2
- import { FormApi, FieldApi, functionalUpdate } from "@tanstack/form-core";
3
- import { useForm } from "./useForm.js";
4
- import { useField, Field } from "./useField.js";
5
- import { createFormFactory } from "./createFormFactory.js";
6
- export {
7
- Field,
8
- FieldApi,
9
- FormApi,
10
- createFormFactory,
11
- functionalUpdate,
12
- useField,
13
- useForm
14
- };
15
- //# sourceMappingURL=index.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../src/index.ts"],"sourcesContent":["export type {\n DeepKeys,\n DeepValue,\n FieldApiOptions,\n FieldInfo,\n FieldMeta,\n FieldOptions,\n FieldState,\n FormOptions,\n FormState,\n RequiredByKey,\n Updater,\n UpdaterFn,\n ValidationCause,\n ValidationError,\n ValidationMeta,\n} from '@tanstack/form-core'\n\nexport { FormApi, FieldApi, functionalUpdate } from '@tanstack/form-core'\n\nexport { useForm } from './useForm'\n\nexport type { UseField, FieldComponent } from './useField'\nexport { useField, Field } from './useField'\n\nexport type { FormFactory } from './createFormFactory'\nexport { createFormFactory } from './createFormFactory'\n"],"mappings":";AAkBA,SAAS,SAAS,UAAU,wBAAwB;AAEpD,SAAS,eAAe;AAGxB,SAAS,UAAU,aAAa;AAGhC,SAAS,yBAAyB;","names":[]}
@@ -1,19 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __copyProps = (to, from, except, desc) => {
7
- if (from && typeof from === "object" || typeof from === "function") {
8
- for (let key of __getOwnPropNames(from))
9
- if (!__hasOwnProp.call(to, key) && key !== except)
10
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
11
- }
12
- return to;
13
- };
14
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
15
-
16
- // src/types.ts
17
- var types_exports = {};
18
- module.exports = __toCommonJS(types_exports);
19
- //# sourceMappingURL=types.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../src/types.ts"],"sourcesContent":["import type {\n FieldOptions,\n DeepKeys,\n DeepValue,\n Validator,\n} from '@tanstack/form-core'\n\nexport type UseFieldOptions<\n TParentData,\n TName extends DeepKeys<TParentData>,\n TFieldValidator extends\n | Validator<DeepValue<TParentData, TName>, unknown>\n | undefined = undefined,\n TFormValidator extends\n | Validator<TParentData, unknown>\n | undefined = undefined,\n TData extends DeepValue<TParentData, TName> = DeepValue<TParentData, TName>,\n> = FieldOptions<TParentData, TName, TFieldValidator, TFormValidator, TData> & {\n mode?: 'value' | 'array'\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAAA;AAAA;","names":[]}
@@ -1,7 +0,0 @@
1
- import { DeepKeys, Validator, DeepValue, FieldOptions } from '@tanstack/form-core';
2
-
3
- type UseFieldOptions<TParentData, TName extends DeepKeys<TParentData>, TFieldValidator extends Validator<DeepValue<TParentData, TName>, unknown> | undefined = undefined, TFormValidator extends Validator<TParentData, unknown> | undefined = undefined, TData extends DeepValue<TParentData, TName> = DeepValue<TParentData, TName>> = FieldOptions<TParentData, TName, TFieldValidator, TFormValidator, TData> & {
4
- mode?: 'value' | 'array';
5
- };
6
-
7
- export { UseFieldOptions };
@@ -1,7 +0,0 @@
1
- import { DeepKeys, Validator, DeepValue, FieldOptions } from '@tanstack/form-core';
2
-
3
- type UseFieldOptions<TParentData, TName extends DeepKeys<TParentData>, TFieldValidator extends Validator<DeepValue<TParentData, TName>, unknown> | undefined = undefined, TFormValidator extends Validator<TParentData, unknown> | undefined = undefined, TData extends DeepValue<TParentData, TName> = DeepValue<TParentData, TName>> = FieldOptions<TParentData, TName, TFieldValidator, TFormValidator, TData> & {
4
- mode?: 'value' | 'array';
5
- };
6
-
7
- export { UseFieldOptions };
@@ -1 +0,0 @@
1
- //# sourceMappingURL=types.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
@@ -1,88 +0,0 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
- var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
19
- };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
-
30
- // src/useField.tsx
31
- var useField_exports = {};
32
- __export(useField_exports, {
33
- Field: () => Field,
34
- useField: () => useField
35
- });
36
- module.exports = __toCommonJS(useField_exports);
37
- var import_react = __toESM(require("react"), 1);
38
- var import_react_store = require("@tanstack/react-store");
39
- var import_form_core = require("@tanstack/form-core");
40
- var import_formContext = require("./formContext.cjs");
41
- var import_useIsomorphicLayoutEffect = require("./useIsomorphicLayoutEffect.cjs");
42
- function useField(opts) {
43
- const { formApi, parentFieldName } = (0, import_formContext.useFormContext)();
44
- const [fieldApi] = (0, import_react.useState)(() => {
45
- const name = (typeof opts.index === "number" ? [parentFieldName, opts.index, opts.name] : [parentFieldName, opts.name]).filter((d) => d !== void 0).join(".");
46
- const api = new import_form_core.FieldApi({
47
- ...opts,
48
- form: formApi,
49
- // TODO: Fix typings to include `index` and `parentFieldName`, if present
50
- name
51
- });
52
- api.Field = Field;
53
- return api;
54
- });
55
- (0, import_useIsomorphicLayoutEffect.useIsomorphicLayoutEffect)(() => {
56
- fieldApi.update({ ...opts, form: formApi });
57
- });
58
- (0, import_react_store.useStore)(
59
- fieldApi.store,
60
- opts.mode === "array" ? (state) => {
61
- return [state.meta, Object.keys(state.value).length];
62
- } : void 0
63
- );
64
- (0, import_useIsomorphicLayoutEffect.useIsomorphicLayoutEffect)(() => fieldApi.mount(), [fieldApi]);
65
- return fieldApi;
66
- }
67
- function Field({
68
- children,
69
- ...fieldOptions
70
- }) {
71
- const fieldApi = useField(fieldOptions);
72
- return /* @__PURE__ */ import_react.default.createElement(
73
- import_formContext.formContext.Provider,
74
- {
75
- value: {
76
- formApi: fieldApi.form,
77
- parentFieldName: fieldApi.name
78
- },
79
- children: (0, import_form_core.functionalUpdate)(children, fieldApi)
80
- }
81
- );
82
- }
83
- // Annotate the CommonJS export names for ESM import in node:
84
- 0 && (module.exports = {
85
- Field,
86
- useField
87
- });
88
- //# sourceMappingURL=useField.cjs.map