@jcoreio/zod-forms 1.0.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 (521) hide show
  1. package/FieldPath.d.ts +26 -0
  2. package/FieldPath.d.ts.map +1 -0
  3. package/FieldPath.js +155 -0
  4. package/FieldPath.js.map +1 -0
  5. package/FieldPath.mjs +138 -0
  6. package/FieldPath.mjs.map +1 -0
  7. package/FormAction.d.ts +15 -0
  8. package/FormAction.d.ts.map +1 -0
  9. package/FormAction.js +6 -0
  10. package/FormAction.js.map +1 -0
  11. package/FormAction.mjs +2 -0
  12. package/FormAction.mjs.map +1 -0
  13. package/FormContext.d.ts +39 -0
  14. package/FormContext.d.ts.map +1 -0
  15. package/FormContext.js +10 -0
  16. package/FormContext.js.map +1 -0
  17. package/FormContext.mjs +3 -0
  18. package/FormContext.mjs.map +1 -0
  19. package/FormState.d.ts +31 -0
  20. package/FormState.d.ts.map +1 -0
  21. package/FormState.js +6 -0
  22. package/FormState.js.map +1 -0
  23. package/FormState.mjs +2 -0
  24. package/FormState.mjs.map +1 -0
  25. package/FormStateContext.d.ts +8 -0
  26. package/FormStateContext.d.ts.map +1 -0
  27. package/FormStateContext.js +10 -0
  28. package/FormStateContext.js.map +1 -0
  29. package/FormStateContext.mjs +3 -0
  30. package/FormStateContext.mjs.map +1 -0
  31. package/LICENSE.md +21 -0
  32. package/README.md +154 -0
  33. package/actions/addHandlers.d.ts +15 -0
  34. package/actions/addHandlers.d.ts.map +1 -0
  35. package/actions/addHandlers.js +16 -0
  36. package/actions/addHandlers.js.map +1 -0
  37. package/actions/addHandlers.mjs +7 -0
  38. package/actions/addHandlers.mjs.map +1 -0
  39. package/actions/arrayActions.d.ts +113 -0
  40. package/actions/arrayActions.d.ts.map +1 -0
  41. package/actions/arrayActions.js +146 -0
  42. package/actions/arrayActions.js.map +1 -0
  43. package/actions/arrayActions.mjs +120 -0
  44. package/actions/arrayActions.mjs.map +1 -0
  45. package/actions/initialize.d.ts +13 -0
  46. package/actions/initialize.d.ts.map +1 -0
  47. package/actions/initialize.js +16 -0
  48. package/actions/initialize.js.map +1 -0
  49. package/actions/initialize.mjs +7 -0
  50. package/actions/initialize.mjs.map +1 -0
  51. package/actions/removeHandlers.d.ts +10 -0
  52. package/actions/removeHandlers.d.ts.map +1 -0
  53. package/actions/removeHandlers.js +16 -0
  54. package/actions/removeHandlers.js.map +1 -0
  55. package/actions/removeHandlers.mjs +7 -0
  56. package/actions/removeHandlers.mjs.map +1 -0
  57. package/actions/setMeta.d.ts +9 -0
  58. package/actions/setMeta.d.ts.map +1 -0
  59. package/actions/setMeta.js +14 -0
  60. package/actions/setMeta.js.map +1 -0
  61. package/actions/setMeta.mjs +8 -0
  62. package/actions/setMeta.mjs.map +1 -0
  63. package/actions/setMounted.d.ts +6 -0
  64. package/actions/setMounted.d.ts.map +1 -0
  65. package/actions/setMounted.js +13 -0
  66. package/actions/setMounted.js.map +1 -0
  67. package/actions/setMounted.mjs +7 -0
  68. package/actions/setMounted.mjs.map +1 -0
  69. package/actions/setRawValue.d.ts +9 -0
  70. package/actions/setRawValue.d.ts.map +1 -0
  71. package/actions/setRawValue.js +14 -0
  72. package/actions/setRawValue.js.map +1 -0
  73. package/actions/setRawValue.mjs +8 -0
  74. package/actions/setRawValue.mjs.map +1 -0
  75. package/actions/setSubmitStatus.d.ts +14 -0
  76. package/actions/setSubmitStatus.d.ts.map +1 -0
  77. package/actions/setSubmitStatus.js +16 -0
  78. package/actions/setSubmitStatus.js.map +1 -0
  79. package/actions/setSubmitStatus.mjs +7 -0
  80. package/actions/setSubmitStatus.mjs.map +1 -0
  81. package/actions/setValue.d.ts +12 -0
  82. package/actions/setValue.d.ts.map +1 -0
  83. package/actions/setValue.js +18 -0
  84. package/actions/setValue.js.map +1 -0
  85. package/actions/setValue.mjs +9 -0
  86. package/actions/setValue.mjs.map +1 -0
  87. package/actions/submit.d.ts +5 -0
  88. package/actions/submit.d.ts.map +1 -0
  89. package/actions/submit.js +12 -0
  90. package/actions/submit.js.map +1 -0
  91. package/actions/submit.mjs +6 -0
  92. package/actions/submit.mjs.map +1 -0
  93. package/actions/submitSucceeded.d.ts +5 -0
  94. package/actions/submitSucceeded.d.ts.map +1 -0
  95. package/actions/submitSucceeded.js +12 -0
  96. package/actions/submitSucceeded.js.map +1 -0
  97. package/actions/submitSucceeded.mjs +6 -0
  98. package/actions/submitSucceeded.mjs.map +1 -0
  99. package/createFormMiddleware.d.ts +6 -0
  100. package/createFormMiddleware.d.ts.map +1 -0
  101. package/createFormMiddleware.js +186 -0
  102. package/createFormMiddleware.js.map +1 -0
  103. package/createFormMiddleware.mjs +53 -0
  104. package/createFormMiddleware.mjs.map +1 -0
  105. package/createFormProvider.d.ts +7 -0
  106. package/createFormProvider.d.ts.map +1 -0
  107. package/createFormProvider.js +85 -0
  108. package/createFormProvider.js.map +1 -0
  109. package/createFormProvider.mjs +63 -0
  110. package/createFormProvider.mjs.map +1 -0
  111. package/createFormReducer.d.ts +9 -0
  112. package/createFormReducer.d.ts.map +1 -0
  113. package/createFormReducer.js +95 -0
  114. package/createFormReducer.js.map +1 -0
  115. package/createFormReducer.mjs +84 -0
  116. package/createFormReducer.mjs.map +1 -0
  117. package/createSelectFieldErrorMap.d.ts +32 -0
  118. package/createSelectFieldErrorMap.d.ts.map +1 -0
  119. package/createSelectFieldErrorMap.js +43 -0
  120. package/createSelectFieldErrorMap.js.map +1 -0
  121. package/createSelectFieldErrorMap.mjs +24 -0
  122. package/createSelectFieldErrorMap.mjs.map +1 -0
  123. package/createSelectFormStatus.d.ts +89 -0
  124. package/createSelectFormStatus.d.ts.map +1 -0
  125. package/createSelectFormStatus.js +54 -0
  126. package/createSelectFormStatus.js.map +1 -0
  127. package/createSelectFormStatus.mjs +28 -0
  128. package/createSelectFormStatus.mjs.map +1 -0
  129. package/createSelectFormValues.d.ts +45 -0
  130. package/createSelectFormValues.d.ts.map +1 -0
  131. package/createSelectFormValues.js +24 -0
  132. package/createSelectFormValues.js.map +1 -0
  133. package/createSelectFormValues.mjs +10 -0
  134. package/createSelectFormValues.mjs.map +1 -0
  135. package/createZodForm.d.ts +33 -0
  136. package/createZodForm.d.ts.map +1 -0
  137. package/createZodForm.js +51 -0
  138. package/createZodForm.js.map +1 -0
  139. package/createZodForm.mjs +46 -0
  140. package/createZodForm.mjs.map +1 -0
  141. package/index.d.ts +14 -0
  142. package/index.d.ts.map +1 -0
  143. package/index.js +76 -0
  144. package/index.js.map +1 -0
  145. package/index.mjs +11 -0
  146. package/index.mjs.map +1 -0
  147. package/initFormState.d.ts +4 -0
  148. package/initFormState.d.ts.map +1 -0
  149. package/initFormState.js +22 -0
  150. package/initFormState.js.map +1 -0
  151. package/initFormState.mjs +16 -0
  152. package/initFormState.mjs.map +1 -0
  153. package/package.json +49 -0
  154. package/reducers/addHandlers.d.ts +24 -0
  155. package/reducers/addHandlers.d.ts.map +1 -0
  156. package/reducers/addHandlers.js +24 -0
  157. package/reducers/addHandlers.js.map +1 -0
  158. package/reducers/addHandlers.mjs +21 -0
  159. package/reducers/addHandlers.mjs.map +1 -0
  160. package/reducers/arrayInsert.d.ts +7 -0
  161. package/reducers/arrayInsert.d.ts.map +1 -0
  162. package/reducers/arrayInsert.js +16 -0
  163. package/reducers/arrayInsert.js.map +1 -0
  164. package/reducers/arrayInsert.mjs +12 -0
  165. package/reducers/arrayInsert.mjs.map +1 -0
  166. package/reducers/arrayInsertRaw.d.ts +7 -0
  167. package/reducers/arrayInsertRaw.d.ts.map +1 -0
  168. package/reducers/arrayInsertRaw.js +22 -0
  169. package/reducers/arrayInsertRaw.js.map +1 -0
  170. package/reducers/arrayInsertRaw.mjs +14 -0
  171. package/reducers/arrayInsertRaw.mjs.map +1 -0
  172. package/reducers/arrayMove.d.ts +8 -0
  173. package/reducers/arrayMove.d.ts.map +1 -0
  174. package/reducers/arrayMove.js +25 -0
  175. package/reducers/arrayMove.js.map +1 -0
  176. package/reducers/arrayMove.mjs +16 -0
  177. package/reducers/arrayMove.mjs.map +1 -0
  178. package/reducers/arrayPop.d.ts +7 -0
  179. package/reducers/arrayPop.d.ts.map +1 -0
  180. package/reducers/arrayPop.js +13 -0
  181. package/reducers/arrayPop.js.map +1 -0
  182. package/reducers/arrayPop.mjs +5 -0
  183. package/reducers/arrayPop.mjs.map +1 -0
  184. package/reducers/arrayPush.d.ts +7 -0
  185. package/reducers/arrayPush.d.ts.map +1 -0
  186. package/reducers/arrayPush.js +15 -0
  187. package/reducers/arrayPush.js.map +1 -0
  188. package/reducers/arrayPush.mjs +11 -0
  189. package/reducers/arrayPush.mjs.map +1 -0
  190. package/reducers/arrayPushRaw.d.ts +7 -0
  191. package/reducers/arrayPushRaw.d.ts.map +1 -0
  192. package/reducers/arrayPushRaw.js +17 -0
  193. package/reducers/arrayPushRaw.js.map +1 -0
  194. package/reducers/arrayPushRaw.mjs +9 -0
  195. package/reducers/arrayPushRaw.mjs.map +1 -0
  196. package/reducers/arrayRemove.d.ts +7 -0
  197. package/reducers/arrayRemove.d.ts.map +1 -0
  198. package/reducers/arrayRemove.js +21 -0
  199. package/reducers/arrayRemove.js.map +1 -0
  200. package/reducers/arrayRemove.mjs +13 -0
  201. package/reducers/arrayRemove.mjs.map +1 -0
  202. package/reducers/arrayRemoveAll.d.ts +7 -0
  203. package/reducers/arrayRemoveAll.d.ts.map +1 -0
  204. package/reducers/arrayRemoveAll.js +14 -0
  205. package/reducers/arrayRemoveAll.js.map +1 -0
  206. package/reducers/arrayRemoveAll.mjs +8 -0
  207. package/reducers/arrayRemoveAll.mjs.map +1 -0
  208. package/reducers/arrayShift.d.ts +7 -0
  209. package/reducers/arrayShift.d.ts.map +1 -0
  210. package/reducers/arrayShift.js +14 -0
  211. package/reducers/arrayShift.js.map +1 -0
  212. package/reducers/arrayShift.mjs +8 -0
  213. package/reducers/arrayShift.mjs.map +1 -0
  214. package/reducers/arraySplice.d.ts +7 -0
  215. package/reducers/arraySplice.d.ts.map +1 -0
  216. package/reducers/arraySplice.js +20 -0
  217. package/reducers/arraySplice.js.map +1 -0
  218. package/reducers/arraySplice.mjs +14 -0
  219. package/reducers/arraySplice.mjs.map +1 -0
  220. package/reducers/arraySpliceRaw.d.ts +7 -0
  221. package/reducers/arraySpliceRaw.d.ts.map +1 -0
  222. package/reducers/arraySpliceRaw.js +26 -0
  223. package/reducers/arraySpliceRaw.js.map +1 -0
  224. package/reducers/arraySpliceRaw.mjs +18 -0
  225. package/reducers/arraySpliceRaw.mjs.map +1 -0
  226. package/reducers/arraySwap.d.ts +8 -0
  227. package/reducers/arraySwap.d.ts.map +1 -0
  228. package/reducers/arraySwap.js +27 -0
  229. package/reducers/arraySwap.js.map +1 -0
  230. package/reducers/arraySwap.mjs +18 -0
  231. package/reducers/arraySwap.mjs.map +1 -0
  232. package/reducers/arrayUnshift.d.ts +7 -0
  233. package/reducers/arrayUnshift.d.ts.map +1 -0
  234. package/reducers/arrayUnshift.js +15 -0
  235. package/reducers/arrayUnshift.js.map +1 -0
  236. package/reducers/arrayUnshift.mjs +11 -0
  237. package/reducers/arrayUnshift.mjs.map +1 -0
  238. package/reducers/arrayUnshiftRaw.d.ts +7 -0
  239. package/reducers/arrayUnshiftRaw.d.ts.map +1 -0
  240. package/reducers/arrayUnshiftRaw.js +17 -0
  241. package/reducers/arrayUnshiftRaw.js.map +1 -0
  242. package/reducers/arrayUnshiftRaw.mjs +9 -0
  243. package/reducers/arrayUnshiftRaw.mjs.map +1 -0
  244. package/reducers/initialize.d.ts +46 -0
  245. package/reducers/initialize.d.ts.map +1 -0
  246. package/reducers/initialize.js +46 -0
  247. package/reducers/initialize.js.map +1 -0
  248. package/reducers/initialize.mjs +38 -0
  249. package/reducers/initialize.mjs.map +1 -0
  250. package/reducers/removeHandlers.d.ts +24 -0
  251. package/reducers/removeHandlers.d.ts.map +1 -0
  252. package/reducers/removeHandlers.js +24 -0
  253. package/reducers/removeHandlers.js.map +1 -0
  254. package/reducers/removeHandlers.mjs +21 -0
  255. package/reducers/removeHandlers.mjs.map +1 -0
  256. package/reducers/setMeta.d.ts +6 -0
  257. package/reducers/setMeta.d.ts.map +1 -0
  258. package/reducers/setMeta.js +28 -0
  259. package/reducers/setMeta.js.map +1 -0
  260. package/reducers/setMeta.mjs +21 -0
  261. package/reducers/setMeta.mjs.map +1 -0
  262. package/reducers/setRawValue.d.ts +27 -0
  263. package/reducers/setRawValue.d.ts.map +1 -0
  264. package/reducers/setRawValue.js +35 -0
  265. package/reducers/setRawValue.js.map +1 -0
  266. package/reducers/setRawValue.mjs +26 -0
  267. package/reducers/setRawValue.mjs.map +1 -0
  268. package/reducers/setSubmitStatus.d.ts +24 -0
  269. package/reducers/setSubmitStatus.d.ts.map +1 -0
  270. package/reducers/setSubmitStatus.js +18 -0
  271. package/reducers/setSubmitStatus.js.map +1 -0
  272. package/reducers/setSubmitStatus.mjs +12 -0
  273. package/reducers/setSubmitStatus.mjs.map +1 -0
  274. package/reducers/setValue.d.ts +28 -0
  275. package/reducers/setValue.d.ts.map +1 -0
  276. package/reducers/setValue.js +41 -0
  277. package/reducers/setValue.js.map +1 -0
  278. package/reducers/setValue.mjs +32 -0
  279. package/reducers/setValue.mjs.map +1 -0
  280. package/reducers/submitSucceeded.d.ts +23 -0
  281. package/reducers/submitSucceeded.d.ts.map +1 -0
  282. package/reducers/submitSucceeded.js +22 -0
  283. package/reducers/submitSucceeded.js.map +1 -0
  284. package/reducers/submitSucceeded.mjs +13 -0
  285. package/reducers/submitSucceeded.mjs.map +1 -0
  286. package/reducers/util/getInverseArrayElementSchema.d.ts +3 -0
  287. package/reducers/util/getInverseArrayElementSchema.d.ts.map +1 -0
  288. package/reducers/util/getInverseArrayElementSchema.js +14 -0
  289. package/reducers/util/getInverseArrayElementSchema.js.map +1 -0
  290. package/reducers/util/getInverseArrayElementSchema.mjs +8 -0
  291. package/reducers/util/getInverseArrayElementSchema.mjs.map +1 -0
  292. package/reducers/util/updateRawArray.d.ts +7 -0
  293. package/reducers/util/updateRawArray.d.ts.map +1 -0
  294. package/reducers/util/updateRawArray.js +14 -0
  295. package/reducers/util/updateRawArray.js.map +1 -0
  296. package/reducers/util/updateRawArray.mjs +8 -0
  297. package/reducers/util/updateRawArray.mjs.map +1 -0
  298. package/src/FieldPath.ts +215 -0
  299. package/src/FormAction.ts +26 -0
  300. package/src/FormContext.ts +44 -0
  301. package/src/FormState.ts +36 -0
  302. package/src/FormStateContext.ts +13 -0
  303. package/src/actions/addHandlers.ts +23 -0
  304. package/src/actions/arrayActions.ts +183 -0
  305. package/src/actions/initialize.ts +16 -0
  306. package/src/actions/removeHandlers.ts +13 -0
  307. package/src/actions/setMeta.ts +17 -0
  308. package/src/actions/setMounted.ts +8 -0
  309. package/src/actions/setRawValue.ts +17 -0
  310. package/src/actions/setSubmitStatus.ts +26 -0
  311. package/src/actions/setValue.ts +21 -0
  312. package/src/actions/submit.ts +7 -0
  313. package/src/actions/submitSucceeded.ts +7 -0
  314. package/src/createFormMiddleware.ts +74 -0
  315. package/src/createFormProvider.tsx +101 -0
  316. package/src/createFormReducer.ts +91 -0
  317. package/src/createSelectFieldErrorMap.ts +47 -0
  318. package/src/createSelectFormStatus.ts +41 -0
  319. package/src/createSelectFormValues.ts +16 -0
  320. package/src/createZodForm.ts +57 -0
  321. package/src/index.ts +32 -0
  322. package/src/initFormState.ts +18 -0
  323. package/src/reducers/addHandlers.ts +21 -0
  324. package/src/reducers/arrayInsert.ts +19 -0
  325. package/src/reducers/arrayInsertRaw.ts +22 -0
  326. package/src/reducers/arrayMove.ts +36 -0
  327. package/src/reducers/arrayPop.ts +16 -0
  328. package/src/reducers/arrayPush.ts +20 -0
  329. package/src/reducers/arrayPushRaw.ts +18 -0
  330. package/src/reducers/arrayRemove.ts +22 -0
  331. package/src/reducers/arrayRemoveAll.ts +15 -0
  332. package/src/reducers/arrayShift.ts +15 -0
  333. package/src/reducers/arraySplice.ts +17 -0
  334. package/src/reducers/arraySpliceRaw.ts +30 -0
  335. package/src/reducers/arraySwap.ts +29 -0
  336. package/src/reducers/arrayUnshift.ts +16 -0
  337. package/src/reducers/arrayUnshiftRaw.ts +18 -0
  338. package/src/reducers/initialize.ts +47 -0
  339. package/src/reducers/removeHandlers.ts +21 -0
  340. package/src/reducers/setMeta.ts +26 -0
  341. package/src/reducers/setRawValue.ts +40 -0
  342. package/src/reducers/setSubmitStatus.ts +15 -0
  343. package/src/reducers/setValue.ts +49 -0
  344. package/src/reducers/submitSucceeded.ts +17 -0
  345. package/src/reducers/util/getInverseArrayElementSchema.ts +9 -0
  346. package/src/reducers/util/updateRawArray.ts +25 -0
  347. package/src/useArrayField.ts +161 -0
  348. package/src/useField.ts +152 -0
  349. package/src/useFormContext.ts +11 -0
  350. package/src/useFormDispatch.ts +13 -0
  351. package/src/useFormSelector.ts +9 -0
  352. package/src/useFormStatus.ts +14 -0
  353. package/src/useFormValues.ts +8 -0
  354. package/src/useHtmlField.ts +235 -0
  355. package/src/useInitialize.ts +14 -0
  356. package/src/useSubmit.ts +29 -0
  357. package/src/util/PathInSchema.ts +7 -0
  358. package/src/util/PathInType.ts +26 -0
  359. package/src/util/SchemaAt.ts +68 -0
  360. package/src/util/acceptsArray.ts +72 -0
  361. package/src/util/acceptsBigint.ts +71 -0
  362. package/src/util/acceptsNumber.ts +71 -0
  363. package/src/util/bindActionsToField.ts +22 -0
  364. package/src/util/get.ts +10 -0
  365. package/src/util/getArrayElementSchema.ts +74 -0
  366. package/src/util/maybeParse.ts +9 -0
  367. package/src/util/parsePathstring.ts +119 -0
  368. package/src/util/pathstring.ts +32 -0
  369. package/src/util/set.ts +25 -0
  370. package/src/util/setAdd.ts +6 -0
  371. package/src/util/setDelete.ts +6 -0
  372. package/useArrayField.d.ts +34 -0
  373. package/useArrayField.d.ts.map +1 -0
  374. package/useArrayField.js +99 -0
  375. package/useArrayField.js.map +1 -0
  376. package/useArrayField.mjs +75 -0
  377. package/useArrayField.mjs.map +1 -0
  378. package/useField.d.ts +34 -0
  379. package/useField.d.ts.map +1 -0
  380. package/useField.js +98 -0
  381. package/useField.js.map +1 -0
  382. package/useField.mjs +75 -0
  383. package/useField.mjs.map +1 -0
  384. package/useFormContext.d.ts +4 -0
  385. package/useFormContext.d.ts.map +1 -0
  386. package/useFormContext.js +17 -0
  387. package/useFormContext.js.map +1 -0
  388. package/useFormContext.mjs +10 -0
  389. package/useFormContext.mjs.map +1 -0
  390. package/useFormDispatch.d.ts +7 -0
  391. package/useFormDispatch.d.ts.map +1 -0
  392. package/useFormDispatch.js +10 -0
  393. package/useFormDispatch.js.map +1 -0
  394. package/useFormDispatch.mjs +4 -0
  395. package/useFormDispatch.mjs.map +1 -0
  396. package/useFormSelector.d.ts +6 -0
  397. package/useFormSelector.d.ts.map +1 -0
  398. package/useFormSelector.js +10 -0
  399. package/useFormSelector.js.map +1 -0
  400. package/useFormSelector.mjs +4 -0
  401. package/useFormSelector.mjs.map +1 -0
  402. package/useFormStatus.d.ts +13 -0
  403. package/useFormStatus.d.ts.map +1 -0
  404. package/useFormStatus.js +15 -0
  405. package/useFormStatus.js.map +1 -0
  406. package/useFormStatus.mjs +10 -0
  407. package/useFormStatus.mjs.map +1 -0
  408. package/useFormValues.d.ts +8 -0
  409. package/useFormValues.d.ts.map +1 -0
  410. package/useFormValues.js +14 -0
  411. package/useFormValues.js.map +1 -0
  412. package/useFormValues.mjs +9 -0
  413. package/useFormValues.mjs.map +1 -0
  414. package/useHtmlField.d.ts +41 -0
  415. package/useHtmlField.d.ts.map +1 -0
  416. package/useHtmlField.js +161 -0
  417. package/useHtmlField.js.map +1 -0
  418. package/useHtmlField.mjs +148 -0
  419. package/useHtmlField.mjs.map +1 -0
  420. package/useInitialize.d.ts +5 -0
  421. package/useInitialize.d.ts.map +1 -0
  422. package/useInitialize.js +18 -0
  423. package/useInitialize.js.map +1 -0
  424. package/useInitialize.mjs +11 -0
  425. package/useInitialize.mjs.map +1 -0
  426. package/useSubmit.d.ts +5 -0
  427. package/useSubmit.d.ts.map +1 -0
  428. package/useSubmit.js +53 -0
  429. package/useSubmit.js.map +1 -0
  430. package/useSubmit.mjs +39 -0
  431. package/useSubmit.mjs.map +1 -0
  432. package/util/PathInSchema.d.ts +5 -0
  433. package/util/PathInSchema.d.ts.map +1 -0
  434. package/util/PathInSchema.js +6 -0
  435. package/util/PathInSchema.js.map +1 -0
  436. package/util/PathInSchema.mjs +2 -0
  437. package/util/PathInSchema.mjs.map +1 -0
  438. package/util/PathInType.d.ts +11 -0
  439. package/util/PathInType.d.ts.map +1 -0
  440. package/util/PathInType.js +6 -0
  441. package/util/PathInType.js.map +1 -0
  442. package/util/PathInType.mjs +2 -0
  443. package/util/PathInType.mjs.map +1 -0
  444. package/util/SchemaAt.d.ts +5 -0
  445. package/util/SchemaAt.d.ts.map +1 -0
  446. package/util/SchemaAt.js +6 -0
  447. package/util/SchemaAt.js.map +1 -0
  448. package/util/SchemaAt.mjs +2 -0
  449. package/util/SchemaAt.mjs.map +1 -0
  450. package/util/acceptsArray.d.ts +3 -0
  451. package/util/acceptsArray.d.ts.map +1 -0
  452. package/util/acceptsArray.js +89 -0
  453. package/util/acceptsArray.js.map +1 -0
  454. package/util/acceptsArray.mjs +105 -0
  455. package/util/acceptsArray.mjs.map +1 -0
  456. package/util/acceptsBigint.d.ts +3 -0
  457. package/util/acceptsBigint.d.ts.map +1 -0
  458. package/util/acceptsBigint.js +88 -0
  459. package/util/acceptsBigint.js.map +1 -0
  460. package/util/acceptsBigint.mjs +104 -0
  461. package/util/acceptsBigint.mjs.map +1 -0
  462. package/util/acceptsNumber.d.ts +3 -0
  463. package/util/acceptsNumber.d.ts.map +1 -0
  464. package/util/acceptsNumber.js +88 -0
  465. package/util/acceptsNumber.js.map +1 -0
  466. package/util/acceptsNumber.mjs +104 -0
  467. package/util/acceptsNumber.mjs.map +1 -0
  468. package/util/bindActionsToField.d.ts +7 -0
  469. package/util/bindActionsToField.d.ts.map +1 -0
  470. package/util/bindActionsToField.js +22 -0
  471. package/util/bindActionsToField.js.map +1 -0
  472. package/util/bindActionsToField.mjs +4 -0
  473. package/util/bindActionsToField.mjs.map +1 -0
  474. package/util/get.d.ts +2 -0
  475. package/util/get.d.ts.map +1 -0
  476. package/util/get.js +12 -0
  477. package/util/get.js.map +1 -0
  478. package/util/get.mjs +4 -0
  479. package/util/get.mjs.map +1 -0
  480. package/util/getArrayElementSchema.d.ts +3 -0
  481. package/util/getArrayElementSchema.d.ts.map +1 -0
  482. package/util/getArrayElementSchema.js +87 -0
  483. package/util/getArrayElementSchema.js.map +1 -0
  484. package/util/getArrayElementSchema.mjs +102 -0
  485. package/util/getArrayElementSchema.mjs.map +1 -0
  486. package/util/maybeParse.d.ts +3 -0
  487. package/util/maybeParse.d.ts.map +1 -0
  488. package/util/maybeParse.js +11 -0
  489. package/util/maybeParse.js.map +1 -0
  490. package/util/maybeParse.mjs +5 -0
  491. package/util/maybeParse.mjs.map +1 -0
  492. package/util/parsePathstring.d.ts +40 -0
  493. package/util/parsePathstring.d.ts.map +1 -0
  494. package/util/parsePathstring.js +55 -0
  495. package/util/parsePathstring.js.map +1 -0
  496. package/util/parsePathstring.mjs +35 -0
  497. package/util/parsePathstring.mjs.map +1 -0
  498. package/util/pathstring.d.ts +13 -0
  499. package/util/pathstring.d.ts.map +1 -0
  500. package/util/pathstring.js +15 -0
  501. package/util/pathstring.js.map +1 -0
  502. package/util/pathstring.mjs +7 -0
  503. package/util/pathstring.mjs.map +1 -0
  504. package/util/set.d.ts +2 -0
  505. package/util/set.d.ts.map +1 -0
  506. package/util/set.js +33 -0
  507. package/util/set.js.map +1 -0
  508. package/util/set.mjs +23 -0
  509. package/util/set.mjs.map +1 -0
  510. package/util/setAdd.d.ts +2 -0
  511. package/util/setAdd.d.ts.map +1 -0
  512. package/util/setAdd.js +13 -0
  513. package/util/setAdd.js.map +1 -0
  514. package/util/setAdd.mjs +7 -0
  515. package/util/setAdd.mjs.map +1 -0
  516. package/util/setDelete.d.ts +2 -0
  517. package/util/setDelete.d.ts.map +1 -0
  518. package/util/setDelete.js +13 -0
  519. package/util/setDelete.js.map +1 -0
  520. package/util/setDelete.mjs +7 -0
  521. package/util/setDelete.mjs.map +1 -0
@@ -0,0 +1,16 @@
1
+ import z from 'zod'
2
+ import { createStructuredSelector } from 'reselect'
3
+ import { FormState } from './FormState'
4
+
5
+ export type SelectFormValues<T extends z.ZodTypeAny> = ReturnType<
6
+ typeof createSelectFormValues<T>
7
+ >
8
+
9
+ export function createSelectFormValues<T extends z.ZodTypeAny>() {
10
+ return createStructuredSelector({
11
+ values: (state: FormState<T>) => state.values,
12
+ rawValues: (state: FormState<T>) => state.rawValues,
13
+ initialValues: (state: FormState<T>) => state.initialValues,
14
+ rawInitialValues: (state: FormState<T>) => state.rawInitialValues,
15
+ })
16
+ }
@@ -0,0 +1,57 @@
1
+ import z from 'zod'
2
+ import { invert } from 'zod-invertible'
3
+ import { FieldPath } from './FieldPath'
4
+ import { createFormProvider } from './createFormProvider'
5
+ import { useField, TypedUseField } from './useField'
6
+ import { useHtmlField, TypedUseHtmlField } from './useHtmlField'
7
+ import { useFormContext } from './useFormContext'
8
+ import { useFormStatus } from './useFormStatus'
9
+ import { useFormValues } from './useFormValues'
10
+ import { useSubmit } from './useSubmit'
11
+ import { useInitialize } from './useInitialize'
12
+ import { createSelectFormStatus } from './createSelectFormStatus'
13
+ import { createSelectFieldErrorMap } from './createSelectFieldErrorMap'
14
+ import { createSelectFormValues } from './createSelectFormValues'
15
+ import { TypedUseArrayField, useArrayField } from './useArrayField'
16
+
17
+ export type ZodForm<T extends z.ZodTypeAny> = ReturnType<
18
+ typeof createZodForm<T>
19
+ >
20
+
21
+ export function createZodForm<T extends z.ZodTypeAny>({
22
+ schema,
23
+ }: {
24
+ schema: T
25
+ }) {
26
+ const root = FieldPath.root(schema)
27
+ const inverseSchema = invert(schema)
28
+
29
+ const selectFormStatus = createSelectFormStatus()
30
+ const selectFieldErrorMap = createSelectFieldErrorMap()
31
+ const selectFormValues = createSelectFormValues<T>()
32
+
33
+ const FormProvider = createFormProvider({
34
+ root,
35
+ schema,
36
+ inverseSchema,
37
+ selectFormStatus,
38
+ selectFieldErrorMap,
39
+ selectFormValues,
40
+ })
41
+
42
+ const get: (typeof root)['get'] = root.get.bind(root)
43
+
44
+ return {
45
+ root,
46
+ get,
47
+ FormProvider,
48
+ useFormContext: useFormContext<T>,
49
+ useFormStatus,
50
+ useFormValues: useFormValues<T>,
51
+ useInitialize: useInitialize<T>,
52
+ useSubmit: useSubmit<T>,
53
+ useArrayField: useArrayField as TypedUseArrayField<T>,
54
+ useField: useField as TypedUseField<T>,
55
+ useHtmlField: useHtmlField as TypedUseHtmlField<T>,
56
+ }
57
+ }
package/src/index.ts ADDED
@@ -0,0 +1,32 @@
1
+ export { createZodForm, type ZodForm } from './createZodForm'
2
+ export {
3
+ type FieldMeta,
4
+ type SubmitHandler,
5
+ type SubmitSuccededHandler,
6
+ type SubmitFailedHandler,
7
+ } from './FormState'
8
+ export {
9
+ FieldPath,
10
+ type FieldPathForValue,
11
+ type FieldPathForRawValue,
12
+ } from './FieldPath'
13
+ export { useField, type UseFieldProps, type TypedUseField } from './useField'
14
+ export {
15
+ useArrayField,
16
+ type UseArrayFieldProps,
17
+ type TypedUseArrayField,
18
+ } from './useArrayField'
19
+ export { type FormContextProps } from './FormContext'
20
+ export { useFormContext } from './useFormContext'
21
+ export { type FormStatus } from './createSelectFormStatus'
22
+ export { useFormStatus } from './useFormStatus'
23
+ export { useFormValues } from './useFormValues'
24
+ export {
25
+ useHtmlField,
26
+ type TypedUseHtmlField,
27
+ type UseHtmlFieldProps,
28
+ type HtmlFieldInputProps,
29
+ type ValidUseHtmlFieldProps,
30
+ } from './useHtmlField'
31
+ export { useInitialize } from './useInitialize'
32
+ export { useSubmit } from './useSubmit'
@@ -0,0 +1,18 @@
1
+ import z from 'zod'
2
+ import { FormState } from './FormState'
3
+
4
+ export function initFormState<T extends z.ZodTypeAny>(): FormState<T> {
5
+ return {
6
+ mounted: true,
7
+ initialized: false,
8
+ fieldMeta: {},
9
+ rawInitialValues: undefined,
10
+ initialValues: undefined,
11
+ submitting: false,
12
+ submitFailed: false,
13
+ submitSucceeded: false,
14
+ onSubmit: new Set(),
15
+ onSubmitSucceeded: new Set(),
16
+ onSubmitFailed: new Set(),
17
+ }
18
+ }
@@ -0,0 +1,21 @@
1
+ import z from 'zod'
2
+ import { FormState } from '../FormState'
3
+ import { AddHandlersAction } from '../actions/addHandlers'
4
+ import { setAdd } from '../util/setAdd'
5
+
6
+ export function addHandlersReducer<T extends z.ZodTypeAny>(
7
+ state: FormState<T>,
8
+ action: AddHandlersAction<T>
9
+ ) {
10
+ const { onSubmit, onSubmitSucceeded, onSubmitFailed } = action
11
+ return {
12
+ ...state,
13
+ ...(onSubmit && { onSubmit: setAdd(state.onSubmit, onSubmit) }),
14
+ ...(onSubmitSucceeded && {
15
+ onSubmitSucceeded: setAdd(state.onSubmitSucceeded, onSubmitSucceeded),
16
+ }),
17
+ ...(onSubmitFailed && {
18
+ onSubmitFailed: setAdd(state.onSubmitFailed, onSubmitFailed),
19
+ }),
20
+ }
21
+ }
@@ -0,0 +1,19 @@
1
+ import z from 'zod'
2
+ import { FormState } from '../FormState'
3
+ import { arrayInsertRaw, ArrayInsertAction } from '../actions/arrayActions'
4
+ import { Reducer } from 'redux'
5
+ import { FormAction } from '../FormAction'
6
+ import { getInverseArrayElementSchema } from './util/getInverseArrayElementSchema'
7
+
8
+ export function arrayInsertReducer<T extends z.ZodTypeAny>(
9
+ reducer: Reducer<FormState<T>, FormAction<T>>,
10
+ state: FormState<T>,
11
+ action: ArrayInsertAction
12
+ ) {
13
+ const { field, index, value } = action
14
+ const inverseSchema = getInverseArrayElementSchema(field.schema)
15
+ return reducer(
16
+ state,
17
+ arrayInsertRaw(field, index, inverseSchema.parse(value))
18
+ )
19
+ }
@@ -0,0 +1,22 @@
1
+ import z from 'zod'
2
+ import { FormState } from '../FormState'
3
+ import { ArrayInsertRawAction } from '../actions/arrayActions'
4
+ import { Reducer } from 'redux'
5
+ import { FormAction } from '../FormAction'
6
+ import { updateRawArray } from './util/updateRawArray'
7
+
8
+ export function arrayInsertRawReducer<T extends z.ZodTypeAny>(
9
+ reducer: Reducer<FormState<T>, FormAction<T>>,
10
+ state: FormState<T>,
11
+ action: ArrayInsertRawAction
12
+ ) {
13
+ const { field, index, rawValue } = action
14
+ return updateRawArray(reducer, state, field, (array) =>
15
+ insert(array ?? [], index, rawValue)
16
+ )
17
+ }
18
+
19
+ function insert<T>(array: T[], index: number, value: T) {
20
+ if (index < 0 || index > array.length) throw new Error(`index out of range`)
21
+ return [...array.slice(0, index), value, ...array.slice(index)]
22
+ }
@@ -0,0 +1,36 @@
1
+ import z from 'zod'
2
+ import { FormState } from '../FormState'
3
+ import { ArrayMoveAction } from '../actions/arrayActions'
4
+ import { Reducer } from 'redux'
5
+ import { FormAction } from '../FormAction'
6
+ import { updateRawArray } from './util/updateRawArray'
7
+
8
+ export function arrayMoveReducer<T extends z.ZodTypeAny>(
9
+ reducer: Reducer<FormState<T>, FormAction<T>>,
10
+ state: FormState<T>,
11
+ action: ArrayMoveAction
12
+ ) {
13
+ const { field, from, to } = action
14
+ if (from === to) return state
15
+ return updateRawArray(reducer, state, field, (array) =>
16
+ array ? move(array, from, to) : array
17
+ )
18
+ }
19
+
20
+ export function move<T>(array: T[], from: number, to: number): T[] {
21
+ if (from < 0 || from >= array.length) throw new Error(`from out of range`)
22
+ if (to < 0 || to >= array.length) throw new Error(`to out of range`)
23
+ return from < to
24
+ ? [
25
+ ...array.slice(0, from),
26
+ ...array.slice(from + 1, to),
27
+ array[from],
28
+ ...array.slice(to + 1),
29
+ ]
30
+ : [
31
+ ...array.slice(0, to),
32
+ array[from],
33
+ ...array.slice(to + 1, from),
34
+ ...array.slice(from + 1),
35
+ ]
36
+ }
@@ -0,0 +1,16 @@
1
+ import z from 'zod'
2
+ import { FormState } from '../FormState'
3
+ import { ArrayPopAction } from '../actions/arrayActions'
4
+ import { Reducer } from 'redux'
5
+ import { FormAction } from '../FormAction'
6
+ import { updateRawArray } from './util/updateRawArray'
7
+
8
+ export function arrayPopReducer<T extends z.ZodTypeAny>(
9
+ reducer: Reducer<FormState<T>, FormAction<T>>,
10
+ state: FormState<T>,
11
+ action: ArrayPopAction
12
+ ) {
13
+ return updateRawArray(reducer, state, action.field, (array) =>
14
+ array?.slice(0, array.length - 1)
15
+ )
16
+ }
@@ -0,0 +1,20 @@
1
+ import z from 'zod'
2
+ import { FormState } from '../FormState'
3
+ import { ArrayPushAction } from '../actions/arrayActions'
4
+ import { Reducer } from 'redux'
5
+ import { FormAction } from '../FormAction'
6
+ import { setValue } from '../actions/setValue'
7
+ import { get } from '../util/get'
8
+
9
+ export function arrayPushReducer<T extends z.ZodTypeAny>(
10
+ reducer: Reducer<FormState<T>, FormAction<T>>,
11
+ state: FormState<T>,
12
+ action: ArrayPushAction
13
+ ) {
14
+ const { field, value } = action
15
+ const array = get(state.rawValues, field.path)
16
+ return reducer(
17
+ state,
18
+ setValue(field.get([Array.isArray(array) ? array.length : 0]), value)
19
+ )
20
+ }
@@ -0,0 +1,18 @@
1
+ import z from 'zod'
2
+ import { FormState } from '../FormState'
3
+ import { ArrayPushRawAction } from '../actions/arrayActions'
4
+ import { Reducer } from 'redux'
5
+ import { FormAction } from '../FormAction'
6
+ import { updateRawArray } from './util/updateRawArray'
7
+
8
+ export function arrayPushRawReducer<T extends z.ZodTypeAny>(
9
+ reducer: Reducer<FormState<T>, FormAction<T>>,
10
+ state: FormState<T>,
11
+ action: ArrayPushRawAction
12
+ ) {
13
+ const { field, rawValue } = action
14
+ return updateRawArray(reducer, state, field, (array) => [
15
+ ...(array ?? []),
16
+ rawValue,
17
+ ])
18
+ }
@@ -0,0 +1,22 @@
1
+ import z from 'zod'
2
+ import { FormState } from '../FormState'
3
+ import { ArrayRemoveAction } from '../actions/arrayActions'
4
+ import { Reducer } from 'redux'
5
+ import { FormAction } from '../FormAction'
6
+ import { updateRawArray } from './util/updateRawArray'
7
+
8
+ export function arrayRemoveReducer<T extends z.ZodTypeAny>(
9
+ reducer: Reducer<FormState<T>, FormAction<T>>,
10
+ state: FormState<T>,
11
+ action: ArrayRemoveAction
12
+ ) {
13
+ const { field, index } = action
14
+ return updateRawArray(reducer, state, field, (array) =>
15
+ array ? remove(array, index) : array
16
+ )
17
+ }
18
+
19
+ function remove<T>(array: T[], index: number): T[] {
20
+ if (index < 0 || index >= array.length) return array
21
+ return [...array.slice(0, index), ...array.slice(index + 1)]
22
+ }
@@ -0,0 +1,15 @@
1
+ import z from 'zod'
2
+ import { FormState } from '../FormState'
3
+ import { ArrayRemoveAllAction } from '../actions/arrayActions'
4
+ import { Reducer } from 'redux'
5
+ import { FormAction } from '../FormAction'
6
+ import { updateRawArray } from './util/updateRawArray'
7
+
8
+ export function arrayRemoveAllReducer<T extends z.ZodTypeAny>(
9
+ reducer: Reducer<FormState<T>, FormAction<T>>,
10
+ state: FormState<T>,
11
+ action: ArrayRemoveAllAction
12
+ ) {
13
+ const { field } = action
14
+ return updateRawArray(reducer, state, field, (array) => (array ? [] : array))
15
+ }
@@ -0,0 +1,15 @@
1
+ import z from 'zod'
2
+ import { FormState } from '../FormState'
3
+ import { ArrayShiftAction } from '../actions/arrayActions'
4
+ import { Reducer } from 'redux'
5
+ import { FormAction } from '../FormAction'
6
+ import { updateRawArray } from './util/updateRawArray'
7
+
8
+ export function arrayShiftReducer<T extends z.ZodTypeAny>(
9
+ reducer: Reducer<FormState<T>, FormAction<T>>,
10
+ state: FormState<T>,
11
+ action: ArrayShiftAction
12
+ ) {
13
+ const { field } = action
14
+ return updateRawArray(reducer, state, field, (array) => array?.slice(1))
15
+ }
@@ -0,0 +1,17 @@
1
+ import z from 'zod'
2
+ import { FormState } from '../FormState'
3
+ import { ArraySpliceAction, arraySpliceRaw } from '../actions/arrayActions'
4
+ import { Reducer } from 'redux'
5
+ import { FormAction } from '../FormAction'
6
+ import { getInverseArrayElementSchema } from './util/getInverseArrayElementSchema'
7
+
8
+ export function arraySpliceReducer<T extends z.ZodTypeAny>(
9
+ reducer: Reducer<FormState<T>, FormAction<T>>,
10
+ state: FormState<T>,
11
+ action: ArraySpliceAction
12
+ ) {
13
+ const { field, index, deleteCount, values } = action
14
+ const inverseSchema = getInverseArrayElementSchema(field.schema)
15
+ const rawValues = values.map((v) => inverseSchema.parse(v))
16
+ return reducer(state, arraySpliceRaw(field, index, deleteCount, rawValues))
17
+ }
@@ -0,0 +1,30 @@
1
+ import z from 'zod'
2
+ import { FormState } from '../FormState'
3
+ import { ArraySpliceRawAction } from '../actions/arrayActions'
4
+ import { Reducer } from 'redux'
5
+ import { FormAction } from '../FormAction'
6
+ import { updateRawArray } from './util/updateRawArray'
7
+
8
+ export function arraySpliceRawReducer<T extends z.ZodTypeAny>(
9
+ reducer: Reducer<FormState<T>, FormAction<T>>,
10
+ state: FormState<T>,
11
+ action: ArraySpliceRawAction
12
+ ) {
13
+ const { field, index, deleteCount, rawValues } = action
14
+ return updateRawArray(reducer, state, field, (array) =>
15
+ splice(array ?? [], index, deleteCount, rawValues)
16
+ )
17
+ }
18
+
19
+ function splice<T>(
20
+ array: T[],
21
+ index: number,
22
+ deleteCount: number,
23
+ values: T[]
24
+ ) {
25
+ if (index < 0 || index > array.length) throw new Error(`index out of range`)
26
+ if (!deleteCount && !values.length) return array
27
+ const result = [...array]
28
+ result.splice(index, deleteCount, ...values)
29
+ return result
30
+ }
@@ -0,0 +1,29 @@
1
+ import z from 'zod'
2
+ import { FormState } from '../FormState'
3
+ import { ArraySwapAction } from '../actions/arrayActions'
4
+ import { Reducer } from 'redux'
5
+ import { FormAction } from '../FormAction'
6
+ import { updateRawArray } from './util/updateRawArray'
7
+
8
+ export function arraySwapReducer<T extends z.ZodTypeAny>(
9
+ reducer: Reducer<FormState<T>, FormAction<T>>,
10
+ state: FormState<T>,
11
+ action: ArraySwapAction
12
+ ) {
13
+ const { field, indexA, indexB } = action
14
+ return updateRawArray(reducer, state, field, (array) =>
15
+ array ? swap(array, indexA, indexB) : array
16
+ )
17
+ }
18
+
19
+ export function swap<T>(array: T[], indexA: number, indexB: number) {
20
+ if (indexA < 0 || indexA >= array.length)
21
+ throw new Error(`indexA out of range`)
22
+ if (indexB < 0 || indexB >= array.length)
23
+ throw new Error(`indexB out of range`)
24
+
25
+ const result = [...array]
26
+ result[indexA] = array[indexB]
27
+ result[indexB] = array[indexA]
28
+ return result
29
+ }
@@ -0,0 +1,16 @@
1
+ import z from 'zod'
2
+ import { FormState } from '../FormState'
3
+ import { ArrayUnshiftAction, arrayUnshiftRaw } from '../actions/arrayActions'
4
+ import { Reducer } from 'redux'
5
+ import { FormAction } from '../FormAction'
6
+ import { getInverseArrayElementSchema } from './util/getInverseArrayElementSchema'
7
+
8
+ export function arrayUnshiftReducer<T extends z.ZodTypeAny>(
9
+ reducer: Reducer<FormState<T>, FormAction<T>>,
10
+ state: FormState<T>,
11
+ action: ArrayUnshiftAction
12
+ ) {
13
+ const { field, value } = action
14
+ const inverseSchema = getInverseArrayElementSchema(field.schema)
15
+ return reducer(state, arrayUnshiftRaw(field, inverseSchema.parse(value)))
16
+ }
@@ -0,0 +1,18 @@
1
+ import z from 'zod'
2
+ import { FormState } from '../FormState'
3
+ import { ArrayUnshiftRawAction } from '../actions/arrayActions'
4
+ import { Reducer } from 'redux'
5
+ import { FormAction } from '../FormAction'
6
+ import { updateRawArray } from './util/updateRawArray'
7
+
8
+ export function arrayUnshiftRawReducer<T extends z.ZodTypeAny>(
9
+ reducer: Reducer<FormState<T>, FormAction<T>>,
10
+ state: FormState<T>,
11
+ action: ArrayUnshiftRawAction
12
+ ) {
13
+ const { field, rawValue } = action
14
+ return updateRawArray(reducer, state, field, (array) => [
15
+ rawValue,
16
+ ...(array ?? []),
17
+ ])
18
+ }
@@ -0,0 +1,47 @@
1
+ import z from 'zod'
2
+ import { FormState } from '../FormState'
3
+ import { InitializeAction } from '../actions/initialize'
4
+
5
+ export const createInitializeReducer = <T extends z.ZodTypeAny>({
6
+ schema,
7
+ inverseSchema,
8
+ }: {
9
+ schema: T
10
+ inverseSchema: z.ZodType<z.input<T>, any, z.output<T>>
11
+ }) =>
12
+ function initializeReducer(state: FormState<T>, action: InitializeAction<T>) {
13
+ const { keepSubmitSucceeded } = action
14
+ try {
15
+ const rawValues =
16
+ action.rawValues ??
17
+ (action.values ? inverseSchema.parse(action.values) : undefined)
18
+ const values =
19
+ action.values ??
20
+ (action.rawValues ? schema.parse(action.rawValues) : undefined)
21
+ return {
22
+ ...state,
23
+ validationError: undefined,
24
+ initialized: true,
25
+ submitting: false,
26
+ submitFailed: false,
27
+ submitSucceeded: keepSubmitSucceeded ? state.submitSucceeded : false,
28
+ rawValues,
29
+ values,
30
+ rawInitialValues: rawValues,
31
+ initialValues: values,
32
+ }
33
+ } catch (error) {
34
+ return {
35
+ ...state,
36
+ validationError: error,
37
+ initialized: true,
38
+ submitting: false,
39
+ submitFailed: false,
40
+ submitSucceeded: keepSubmitSucceeded ? state.submitSucceeded : false,
41
+ rawValues: action.rawValues,
42
+ values: action.values,
43
+ rawInitialValues: action.rawValues,
44
+ initialValues: action.values,
45
+ }
46
+ }
47
+ }
@@ -0,0 +1,21 @@
1
+ import z from 'zod'
2
+ import { FormState } from '../FormState'
3
+ import { RemoveHandlersAction } from '../actions/removeHandlers'
4
+ import { setDelete } from '../util/setDelete'
5
+
6
+ export function removeHandlersReducer<T extends z.ZodTypeAny>(
7
+ state: FormState<T>,
8
+ action: RemoveHandlersAction<T>
9
+ ) {
10
+ const { onSubmit, onSubmitSucceeded, onSubmitFailed } = action
11
+ return {
12
+ ...state,
13
+ ...(onSubmit && { onSubmit: setDelete(state.onSubmit, onSubmit) }),
14
+ ...(onSubmitSucceeded && {
15
+ onSubmitSucceeded: setDelete(state.onSubmitSucceeded, onSubmitSucceeded),
16
+ }),
17
+ ...(onSubmitFailed && {
18
+ onSubmitFailed: setDelete(state.onSubmitFailed, onSubmitFailed),
19
+ }),
20
+ }
21
+ }
@@ -0,0 +1,26 @@
1
+ import z from 'zod'
2
+ import { FormState } from '../FormState'
3
+ import { SetMetaAction } from '../actions/setMeta'
4
+ import { FieldPath } from '../FieldPath'
5
+
6
+ export function setMetaReducer<T extends z.ZodTypeAny>(
7
+ state: FormState<T>,
8
+ action: SetMetaAction<FieldPath>
9
+ ) {
10
+ const { field, meta } = action
11
+ const oldMeta = state.fieldMeta[field.pathstring]
12
+ if (
13
+ Object.entries(meta).every(([key, value]) =>
14
+ Object.is(value, (oldMeta as any)?.[key])
15
+ )
16
+ ) {
17
+ return state
18
+ }
19
+ return {
20
+ ...state,
21
+ fieldMeta: {
22
+ ...state.fieldMeta,
23
+ [field.pathstring]: { ...oldMeta, ...meta },
24
+ },
25
+ }
26
+ }
@@ -0,0 +1,40 @@
1
+ import z from 'zod'
2
+ import { FormState } from '../FormState'
3
+ import { SetRawValueAction } from '../actions/setRawValue'
4
+ import { set } from '../util/set'
5
+ import { FieldPath } from '../FieldPath'
6
+
7
+ export const createSetRawValueReducer = <T extends z.ZodTypeAny>({
8
+ schema,
9
+ }: {
10
+ schema: T
11
+ }) =>
12
+ function setRawValueReducer<Field extends FieldPath>(
13
+ state: FormState<T>,
14
+ action: SetRawValueAction<Field>
15
+ ) {
16
+ const newRawValues = set(
17
+ state.rawValues,
18
+ action.field.path,
19
+ action.rawValue
20
+ )
21
+ if (newRawValues === state.rawValues) return state
22
+ try {
23
+ const newValues = schema.parse(newRawValues)
24
+ return {
25
+ ...state,
26
+ submitError: undefined,
27
+ validationError: undefined,
28
+ rawValues: newRawValues,
29
+ values: newValues,
30
+ }
31
+ } catch (error) {
32
+ return {
33
+ ...state,
34
+ submitError: undefined,
35
+ validationError: error,
36
+ rawValues: newRawValues,
37
+ values: undefined,
38
+ }
39
+ }
40
+ }
@@ -0,0 +1,15 @@
1
+ import z from 'zod'
2
+ import { FormState } from '../FormState'
3
+ import { SetSubmitStatusAction } from '../actions/setSubmitStatus'
4
+
5
+ export function setSubmitStatusReducer<T extends z.ZodTypeAny>(
6
+ state: FormState<T>,
7
+ action: SetSubmitStatusAction<T>
8
+ ) {
9
+ const {
10
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
11
+ type,
12
+ ...status
13
+ } = action
14
+ return { ...state, ...status }
15
+ }
@@ -0,0 +1,49 @@
1
+ import z from 'zod'
2
+ import { FormState } from '../FormState'
3
+ import { SetValueAction } from '../actions/setValue'
4
+ import { set } from '../util/set'
5
+ import { FieldPath } from '../FieldPath'
6
+ import { invert } from 'zod-invertible'
7
+
8
+ export const createSetValueReducer = <T extends z.ZodTypeAny>({
9
+ schema,
10
+ inverseSchema,
11
+ }: {
12
+ schema: T
13
+ inverseSchema: z.ZodType<z.input<T>, any, z.output<T>>
14
+ }) =>
15
+ function setValueReducer<Field extends FieldPath>(
16
+ state: FormState<T>,
17
+ action: SetValueAction<Field>
18
+ ) {
19
+ const newValues = set(state.values, action.field.path, action.value)
20
+ try {
21
+ const newRawValues = inverseSchema.parse(newValues)
22
+ schema.parse(newRawValues)
23
+ return {
24
+ ...state,
25
+ submitError: undefined,
26
+ validationError: undefined,
27
+ rawValues: newRawValues,
28
+ values: newValues,
29
+ }
30
+ } catch (error) {
31
+ const newRawParsed = invert(action.field.schema).safeParse(action.value)
32
+ const rawValues = newRawParsed.success
33
+ ? set(state.rawValues, action.field.path, newRawParsed.data)
34
+ : state.rawValues
35
+ const newParsed = schema.safeParse(rawValues)
36
+ const result = {
37
+ ...state,
38
+ submitError: undefined,
39
+ validationError: !newRawParsed.success
40
+ ? newRawParsed.error
41
+ : newParsed.success
42
+ ? undefined
43
+ : newParsed.error,
44
+ rawValues,
45
+ values: newParsed.success ? newParsed.data : state.values,
46
+ }
47
+ return result
48
+ }
49
+ }