@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.
- package/FieldPath.d.ts +26 -0
- package/FieldPath.d.ts.map +1 -0
- package/FieldPath.js +155 -0
- package/FieldPath.js.map +1 -0
- package/FieldPath.mjs +138 -0
- package/FieldPath.mjs.map +1 -0
- package/FormAction.d.ts +15 -0
- package/FormAction.d.ts.map +1 -0
- package/FormAction.js +6 -0
- package/FormAction.js.map +1 -0
- package/FormAction.mjs +2 -0
- package/FormAction.mjs.map +1 -0
- package/FormContext.d.ts +39 -0
- package/FormContext.d.ts.map +1 -0
- package/FormContext.js +10 -0
- package/FormContext.js.map +1 -0
- package/FormContext.mjs +3 -0
- package/FormContext.mjs.map +1 -0
- package/FormState.d.ts +31 -0
- package/FormState.d.ts.map +1 -0
- package/FormState.js +6 -0
- package/FormState.js.map +1 -0
- package/FormState.mjs +2 -0
- package/FormState.mjs.map +1 -0
- package/FormStateContext.d.ts +8 -0
- package/FormStateContext.d.ts.map +1 -0
- package/FormStateContext.js +10 -0
- package/FormStateContext.js.map +1 -0
- package/FormStateContext.mjs +3 -0
- package/FormStateContext.mjs.map +1 -0
- package/LICENSE.md +21 -0
- package/README.md +154 -0
- package/actions/addHandlers.d.ts +15 -0
- package/actions/addHandlers.d.ts.map +1 -0
- package/actions/addHandlers.js +16 -0
- package/actions/addHandlers.js.map +1 -0
- package/actions/addHandlers.mjs +7 -0
- package/actions/addHandlers.mjs.map +1 -0
- package/actions/arrayActions.d.ts +113 -0
- package/actions/arrayActions.d.ts.map +1 -0
- package/actions/arrayActions.js +146 -0
- package/actions/arrayActions.js.map +1 -0
- package/actions/arrayActions.mjs +120 -0
- package/actions/arrayActions.mjs.map +1 -0
- package/actions/initialize.d.ts +13 -0
- package/actions/initialize.d.ts.map +1 -0
- package/actions/initialize.js +16 -0
- package/actions/initialize.js.map +1 -0
- package/actions/initialize.mjs +7 -0
- package/actions/initialize.mjs.map +1 -0
- package/actions/removeHandlers.d.ts +10 -0
- package/actions/removeHandlers.d.ts.map +1 -0
- package/actions/removeHandlers.js +16 -0
- package/actions/removeHandlers.js.map +1 -0
- package/actions/removeHandlers.mjs +7 -0
- package/actions/removeHandlers.mjs.map +1 -0
- package/actions/setMeta.d.ts +9 -0
- package/actions/setMeta.d.ts.map +1 -0
- package/actions/setMeta.js +14 -0
- package/actions/setMeta.js.map +1 -0
- package/actions/setMeta.mjs +8 -0
- package/actions/setMeta.mjs.map +1 -0
- package/actions/setMounted.d.ts +6 -0
- package/actions/setMounted.d.ts.map +1 -0
- package/actions/setMounted.js +13 -0
- package/actions/setMounted.js.map +1 -0
- package/actions/setMounted.mjs +7 -0
- package/actions/setMounted.mjs.map +1 -0
- package/actions/setRawValue.d.ts +9 -0
- package/actions/setRawValue.d.ts.map +1 -0
- package/actions/setRawValue.js +14 -0
- package/actions/setRawValue.js.map +1 -0
- package/actions/setRawValue.mjs +8 -0
- package/actions/setRawValue.mjs.map +1 -0
- package/actions/setSubmitStatus.d.ts +14 -0
- package/actions/setSubmitStatus.d.ts.map +1 -0
- package/actions/setSubmitStatus.js +16 -0
- package/actions/setSubmitStatus.js.map +1 -0
- package/actions/setSubmitStatus.mjs +7 -0
- package/actions/setSubmitStatus.mjs.map +1 -0
- package/actions/setValue.d.ts +12 -0
- package/actions/setValue.d.ts.map +1 -0
- package/actions/setValue.js +18 -0
- package/actions/setValue.js.map +1 -0
- package/actions/setValue.mjs +9 -0
- package/actions/setValue.mjs.map +1 -0
- package/actions/submit.d.ts +5 -0
- package/actions/submit.d.ts.map +1 -0
- package/actions/submit.js +12 -0
- package/actions/submit.js.map +1 -0
- package/actions/submit.mjs +6 -0
- package/actions/submit.mjs.map +1 -0
- package/actions/submitSucceeded.d.ts +5 -0
- package/actions/submitSucceeded.d.ts.map +1 -0
- package/actions/submitSucceeded.js +12 -0
- package/actions/submitSucceeded.js.map +1 -0
- package/actions/submitSucceeded.mjs +6 -0
- package/actions/submitSucceeded.mjs.map +1 -0
- package/createFormMiddleware.d.ts +6 -0
- package/createFormMiddleware.d.ts.map +1 -0
- package/createFormMiddleware.js +186 -0
- package/createFormMiddleware.js.map +1 -0
- package/createFormMiddleware.mjs +53 -0
- package/createFormMiddleware.mjs.map +1 -0
- package/createFormProvider.d.ts +7 -0
- package/createFormProvider.d.ts.map +1 -0
- package/createFormProvider.js +85 -0
- package/createFormProvider.js.map +1 -0
- package/createFormProvider.mjs +63 -0
- package/createFormProvider.mjs.map +1 -0
- package/createFormReducer.d.ts +9 -0
- package/createFormReducer.d.ts.map +1 -0
- package/createFormReducer.js +95 -0
- package/createFormReducer.js.map +1 -0
- package/createFormReducer.mjs +84 -0
- package/createFormReducer.mjs.map +1 -0
- package/createSelectFieldErrorMap.d.ts +32 -0
- package/createSelectFieldErrorMap.d.ts.map +1 -0
- package/createSelectFieldErrorMap.js +43 -0
- package/createSelectFieldErrorMap.js.map +1 -0
- package/createSelectFieldErrorMap.mjs +24 -0
- package/createSelectFieldErrorMap.mjs.map +1 -0
- package/createSelectFormStatus.d.ts +89 -0
- package/createSelectFormStatus.d.ts.map +1 -0
- package/createSelectFormStatus.js +54 -0
- package/createSelectFormStatus.js.map +1 -0
- package/createSelectFormStatus.mjs +28 -0
- package/createSelectFormStatus.mjs.map +1 -0
- package/createSelectFormValues.d.ts +45 -0
- package/createSelectFormValues.d.ts.map +1 -0
- package/createSelectFormValues.js +24 -0
- package/createSelectFormValues.js.map +1 -0
- package/createSelectFormValues.mjs +10 -0
- package/createSelectFormValues.mjs.map +1 -0
- package/createZodForm.d.ts +33 -0
- package/createZodForm.d.ts.map +1 -0
- package/createZodForm.js +51 -0
- package/createZodForm.js.map +1 -0
- package/createZodForm.mjs +46 -0
- package/createZodForm.mjs.map +1 -0
- package/index.d.ts +14 -0
- package/index.d.ts.map +1 -0
- package/index.js +76 -0
- package/index.js.map +1 -0
- package/index.mjs +11 -0
- package/index.mjs.map +1 -0
- package/initFormState.d.ts +4 -0
- package/initFormState.d.ts.map +1 -0
- package/initFormState.js +22 -0
- package/initFormState.js.map +1 -0
- package/initFormState.mjs +16 -0
- package/initFormState.mjs.map +1 -0
- package/package.json +49 -0
- package/reducers/addHandlers.d.ts +24 -0
- package/reducers/addHandlers.d.ts.map +1 -0
- package/reducers/addHandlers.js +24 -0
- package/reducers/addHandlers.js.map +1 -0
- package/reducers/addHandlers.mjs +21 -0
- package/reducers/addHandlers.mjs.map +1 -0
- package/reducers/arrayInsert.d.ts +7 -0
- package/reducers/arrayInsert.d.ts.map +1 -0
- package/reducers/arrayInsert.js +16 -0
- package/reducers/arrayInsert.js.map +1 -0
- package/reducers/arrayInsert.mjs +12 -0
- package/reducers/arrayInsert.mjs.map +1 -0
- package/reducers/arrayInsertRaw.d.ts +7 -0
- package/reducers/arrayInsertRaw.d.ts.map +1 -0
- package/reducers/arrayInsertRaw.js +22 -0
- package/reducers/arrayInsertRaw.js.map +1 -0
- package/reducers/arrayInsertRaw.mjs +14 -0
- package/reducers/arrayInsertRaw.mjs.map +1 -0
- package/reducers/arrayMove.d.ts +8 -0
- package/reducers/arrayMove.d.ts.map +1 -0
- package/reducers/arrayMove.js +25 -0
- package/reducers/arrayMove.js.map +1 -0
- package/reducers/arrayMove.mjs +16 -0
- package/reducers/arrayMove.mjs.map +1 -0
- package/reducers/arrayPop.d.ts +7 -0
- package/reducers/arrayPop.d.ts.map +1 -0
- package/reducers/arrayPop.js +13 -0
- package/reducers/arrayPop.js.map +1 -0
- package/reducers/arrayPop.mjs +5 -0
- package/reducers/arrayPop.mjs.map +1 -0
- package/reducers/arrayPush.d.ts +7 -0
- package/reducers/arrayPush.d.ts.map +1 -0
- package/reducers/arrayPush.js +15 -0
- package/reducers/arrayPush.js.map +1 -0
- package/reducers/arrayPush.mjs +11 -0
- package/reducers/arrayPush.mjs.map +1 -0
- package/reducers/arrayPushRaw.d.ts +7 -0
- package/reducers/arrayPushRaw.d.ts.map +1 -0
- package/reducers/arrayPushRaw.js +17 -0
- package/reducers/arrayPushRaw.js.map +1 -0
- package/reducers/arrayPushRaw.mjs +9 -0
- package/reducers/arrayPushRaw.mjs.map +1 -0
- package/reducers/arrayRemove.d.ts +7 -0
- package/reducers/arrayRemove.d.ts.map +1 -0
- package/reducers/arrayRemove.js +21 -0
- package/reducers/arrayRemove.js.map +1 -0
- package/reducers/arrayRemove.mjs +13 -0
- package/reducers/arrayRemove.mjs.map +1 -0
- package/reducers/arrayRemoveAll.d.ts +7 -0
- package/reducers/arrayRemoveAll.d.ts.map +1 -0
- package/reducers/arrayRemoveAll.js +14 -0
- package/reducers/arrayRemoveAll.js.map +1 -0
- package/reducers/arrayRemoveAll.mjs +8 -0
- package/reducers/arrayRemoveAll.mjs.map +1 -0
- package/reducers/arrayShift.d.ts +7 -0
- package/reducers/arrayShift.d.ts.map +1 -0
- package/reducers/arrayShift.js +14 -0
- package/reducers/arrayShift.js.map +1 -0
- package/reducers/arrayShift.mjs +8 -0
- package/reducers/arrayShift.mjs.map +1 -0
- package/reducers/arraySplice.d.ts +7 -0
- package/reducers/arraySplice.d.ts.map +1 -0
- package/reducers/arraySplice.js +20 -0
- package/reducers/arraySplice.js.map +1 -0
- package/reducers/arraySplice.mjs +14 -0
- package/reducers/arraySplice.mjs.map +1 -0
- package/reducers/arraySpliceRaw.d.ts +7 -0
- package/reducers/arraySpliceRaw.d.ts.map +1 -0
- package/reducers/arraySpliceRaw.js +26 -0
- package/reducers/arraySpliceRaw.js.map +1 -0
- package/reducers/arraySpliceRaw.mjs +18 -0
- package/reducers/arraySpliceRaw.mjs.map +1 -0
- package/reducers/arraySwap.d.ts +8 -0
- package/reducers/arraySwap.d.ts.map +1 -0
- package/reducers/arraySwap.js +27 -0
- package/reducers/arraySwap.js.map +1 -0
- package/reducers/arraySwap.mjs +18 -0
- package/reducers/arraySwap.mjs.map +1 -0
- package/reducers/arrayUnshift.d.ts +7 -0
- package/reducers/arrayUnshift.d.ts.map +1 -0
- package/reducers/arrayUnshift.js +15 -0
- package/reducers/arrayUnshift.js.map +1 -0
- package/reducers/arrayUnshift.mjs +11 -0
- package/reducers/arrayUnshift.mjs.map +1 -0
- package/reducers/arrayUnshiftRaw.d.ts +7 -0
- package/reducers/arrayUnshiftRaw.d.ts.map +1 -0
- package/reducers/arrayUnshiftRaw.js +17 -0
- package/reducers/arrayUnshiftRaw.js.map +1 -0
- package/reducers/arrayUnshiftRaw.mjs +9 -0
- package/reducers/arrayUnshiftRaw.mjs.map +1 -0
- package/reducers/initialize.d.ts +46 -0
- package/reducers/initialize.d.ts.map +1 -0
- package/reducers/initialize.js +46 -0
- package/reducers/initialize.js.map +1 -0
- package/reducers/initialize.mjs +38 -0
- package/reducers/initialize.mjs.map +1 -0
- package/reducers/removeHandlers.d.ts +24 -0
- package/reducers/removeHandlers.d.ts.map +1 -0
- package/reducers/removeHandlers.js +24 -0
- package/reducers/removeHandlers.js.map +1 -0
- package/reducers/removeHandlers.mjs +21 -0
- package/reducers/removeHandlers.mjs.map +1 -0
- package/reducers/setMeta.d.ts +6 -0
- package/reducers/setMeta.d.ts.map +1 -0
- package/reducers/setMeta.js +28 -0
- package/reducers/setMeta.js.map +1 -0
- package/reducers/setMeta.mjs +21 -0
- package/reducers/setMeta.mjs.map +1 -0
- package/reducers/setRawValue.d.ts +27 -0
- package/reducers/setRawValue.d.ts.map +1 -0
- package/reducers/setRawValue.js +35 -0
- package/reducers/setRawValue.js.map +1 -0
- package/reducers/setRawValue.mjs +26 -0
- package/reducers/setRawValue.mjs.map +1 -0
- package/reducers/setSubmitStatus.d.ts +24 -0
- package/reducers/setSubmitStatus.d.ts.map +1 -0
- package/reducers/setSubmitStatus.js +18 -0
- package/reducers/setSubmitStatus.js.map +1 -0
- package/reducers/setSubmitStatus.mjs +12 -0
- package/reducers/setSubmitStatus.mjs.map +1 -0
- package/reducers/setValue.d.ts +28 -0
- package/reducers/setValue.d.ts.map +1 -0
- package/reducers/setValue.js +41 -0
- package/reducers/setValue.js.map +1 -0
- package/reducers/setValue.mjs +32 -0
- package/reducers/setValue.mjs.map +1 -0
- package/reducers/submitSucceeded.d.ts +23 -0
- package/reducers/submitSucceeded.d.ts.map +1 -0
- package/reducers/submitSucceeded.js +22 -0
- package/reducers/submitSucceeded.js.map +1 -0
- package/reducers/submitSucceeded.mjs +13 -0
- package/reducers/submitSucceeded.mjs.map +1 -0
- package/reducers/util/getInverseArrayElementSchema.d.ts +3 -0
- package/reducers/util/getInverseArrayElementSchema.d.ts.map +1 -0
- package/reducers/util/getInverseArrayElementSchema.js +14 -0
- package/reducers/util/getInverseArrayElementSchema.js.map +1 -0
- package/reducers/util/getInverseArrayElementSchema.mjs +8 -0
- package/reducers/util/getInverseArrayElementSchema.mjs.map +1 -0
- package/reducers/util/updateRawArray.d.ts +7 -0
- package/reducers/util/updateRawArray.d.ts.map +1 -0
- package/reducers/util/updateRawArray.js +14 -0
- package/reducers/util/updateRawArray.js.map +1 -0
- package/reducers/util/updateRawArray.mjs +8 -0
- package/reducers/util/updateRawArray.mjs.map +1 -0
- package/src/FieldPath.ts +215 -0
- package/src/FormAction.ts +26 -0
- package/src/FormContext.ts +44 -0
- package/src/FormState.ts +36 -0
- package/src/FormStateContext.ts +13 -0
- package/src/actions/addHandlers.ts +23 -0
- package/src/actions/arrayActions.ts +183 -0
- package/src/actions/initialize.ts +16 -0
- package/src/actions/removeHandlers.ts +13 -0
- package/src/actions/setMeta.ts +17 -0
- package/src/actions/setMounted.ts +8 -0
- package/src/actions/setRawValue.ts +17 -0
- package/src/actions/setSubmitStatus.ts +26 -0
- package/src/actions/setValue.ts +21 -0
- package/src/actions/submit.ts +7 -0
- package/src/actions/submitSucceeded.ts +7 -0
- package/src/createFormMiddleware.ts +74 -0
- package/src/createFormProvider.tsx +101 -0
- package/src/createFormReducer.ts +91 -0
- package/src/createSelectFieldErrorMap.ts +47 -0
- package/src/createSelectFormStatus.ts +41 -0
- package/src/createSelectFormValues.ts +16 -0
- package/src/createZodForm.ts +57 -0
- package/src/index.ts +32 -0
- package/src/initFormState.ts +18 -0
- package/src/reducers/addHandlers.ts +21 -0
- package/src/reducers/arrayInsert.ts +19 -0
- package/src/reducers/arrayInsertRaw.ts +22 -0
- package/src/reducers/arrayMove.ts +36 -0
- package/src/reducers/arrayPop.ts +16 -0
- package/src/reducers/arrayPush.ts +20 -0
- package/src/reducers/arrayPushRaw.ts +18 -0
- package/src/reducers/arrayRemove.ts +22 -0
- package/src/reducers/arrayRemoveAll.ts +15 -0
- package/src/reducers/arrayShift.ts +15 -0
- package/src/reducers/arraySplice.ts +17 -0
- package/src/reducers/arraySpliceRaw.ts +30 -0
- package/src/reducers/arraySwap.ts +29 -0
- package/src/reducers/arrayUnshift.ts +16 -0
- package/src/reducers/arrayUnshiftRaw.ts +18 -0
- package/src/reducers/initialize.ts +47 -0
- package/src/reducers/removeHandlers.ts +21 -0
- package/src/reducers/setMeta.ts +26 -0
- package/src/reducers/setRawValue.ts +40 -0
- package/src/reducers/setSubmitStatus.ts +15 -0
- package/src/reducers/setValue.ts +49 -0
- package/src/reducers/submitSucceeded.ts +17 -0
- package/src/reducers/util/getInverseArrayElementSchema.ts +9 -0
- package/src/reducers/util/updateRawArray.ts +25 -0
- package/src/useArrayField.ts +161 -0
- package/src/useField.ts +152 -0
- package/src/useFormContext.ts +11 -0
- package/src/useFormDispatch.ts +13 -0
- package/src/useFormSelector.ts +9 -0
- package/src/useFormStatus.ts +14 -0
- package/src/useFormValues.ts +8 -0
- package/src/useHtmlField.ts +235 -0
- package/src/useInitialize.ts +14 -0
- package/src/useSubmit.ts +29 -0
- package/src/util/PathInSchema.ts +7 -0
- package/src/util/PathInType.ts +26 -0
- package/src/util/SchemaAt.ts +68 -0
- package/src/util/acceptsArray.ts +72 -0
- package/src/util/acceptsBigint.ts +71 -0
- package/src/util/acceptsNumber.ts +71 -0
- package/src/util/bindActionsToField.ts +22 -0
- package/src/util/get.ts +10 -0
- package/src/util/getArrayElementSchema.ts +74 -0
- package/src/util/maybeParse.ts +9 -0
- package/src/util/parsePathstring.ts +119 -0
- package/src/util/pathstring.ts +32 -0
- package/src/util/set.ts +25 -0
- package/src/util/setAdd.ts +6 -0
- package/src/util/setDelete.ts +6 -0
- package/useArrayField.d.ts +34 -0
- package/useArrayField.d.ts.map +1 -0
- package/useArrayField.js +99 -0
- package/useArrayField.js.map +1 -0
- package/useArrayField.mjs +75 -0
- package/useArrayField.mjs.map +1 -0
- package/useField.d.ts +34 -0
- package/useField.d.ts.map +1 -0
- package/useField.js +98 -0
- package/useField.js.map +1 -0
- package/useField.mjs +75 -0
- package/useField.mjs.map +1 -0
- package/useFormContext.d.ts +4 -0
- package/useFormContext.d.ts.map +1 -0
- package/useFormContext.js +17 -0
- package/useFormContext.js.map +1 -0
- package/useFormContext.mjs +10 -0
- package/useFormContext.mjs.map +1 -0
- package/useFormDispatch.d.ts +7 -0
- package/useFormDispatch.d.ts.map +1 -0
- package/useFormDispatch.js +10 -0
- package/useFormDispatch.js.map +1 -0
- package/useFormDispatch.mjs +4 -0
- package/useFormDispatch.mjs.map +1 -0
- package/useFormSelector.d.ts +6 -0
- package/useFormSelector.d.ts.map +1 -0
- package/useFormSelector.js +10 -0
- package/useFormSelector.js.map +1 -0
- package/useFormSelector.mjs +4 -0
- package/useFormSelector.mjs.map +1 -0
- package/useFormStatus.d.ts +13 -0
- package/useFormStatus.d.ts.map +1 -0
- package/useFormStatus.js +15 -0
- package/useFormStatus.js.map +1 -0
- package/useFormStatus.mjs +10 -0
- package/useFormStatus.mjs.map +1 -0
- package/useFormValues.d.ts +8 -0
- package/useFormValues.d.ts.map +1 -0
- package/useFormValues.js +14 -0
- package/useFormValues.js.map +1 -0
- package/useFormValues.mjs +9 -0
- package/useFormValues.mjs.map +1 -0
- package/useHtmlField.d.ts +41 -0
- package/useHtmlField.d.ts.map +1 -0
- package/useHtmlField.js +161 -0
- package/useHtmlField.js.map +1 -0
- package/useHtmlField.mjs +148 -0
- package/useHtmlField.mjs.map +1 -0
- package/useInitialize.d.ts +5 -0
- package/useInitialize.d.ts.map +1 -0
- package/useInitialize.js +18 -0
- package/useInitialize.js.map +1 -0
- package/useInitialize.mjs +11 -0
- package/useInitialize.mjs.map +1 -0
- package/useSubmit.d.ts +5 -0
- package/useSubmit.d.ts.map +1 -0
- package/useSubmit.js +53 -0
- package/useSubmit.js.map +1 -0
- package/useSubmit.mjs +39 -0
- package/useSubmit.mjs.map +1 -0
- package/util/PathInSchema.d.ts +5 -0
- package/util/PathInSchema.d.ts.map +1 -0
- package/util/PathInSchema.js +6 -0
- package/util/PathInSchema.js.map +1 -0
- package/util/PathInSchema.mjs +2 -0
- package/util/PathInSchema.mjs.map +1 -0
- package/util/PathInType.d.ts +11 -0
- package/util/PathInType.d.ts.map +1 -0
- package/util/PathInType.js +6 -0
- package/util/PathInType.js.map +1 -0
- package/util/PathInType.mjs +2 -0
- package/util/PathInType.mjs.map +1 -0
- package/util/SchemaAt.d.ts +5 -0
- package/util/SchemaAt.d.ts.map +1 -0
- package/util/SchemaAt.js +6 -0
- package/util/SchemaAt.js.map +1 -0
- package/util/SchemaAt.mjs +2 -0
- package/util/SchemaAt.mjs.map +1 -0
- package/util/acceptsArray.d.ts +3 -0
- package/util/acceptsArray.d.ts.map +1 -0
- package/util/acceptsArray.js +89 -0
- package/util/acceptsArray.js.map +1 -0
- package/util/acceptsArray.mjs +105 -0
- package/util/acceptsArray.mjs.map +1 -0
- package/util/acceptsBigint.d.ts +3 -0
- package/util/acceptsBigint.d.ts.map +1 -0
- package/util/acceptsBigint.js +88 -0
- package/util/acceptsBigint.js.map +1 -0
- package/util/acceptsBigint.mjs +104 -0
- package/util/acceptsBigint.mjs.map +1 -0
- package/util/acceptsNumber.d.ts +3 -0
- package/util/acceptsNumber.d.ts.map +1 -0
- package/util/acceptsNumber.js +88 -0
- package/util/acceptsNumber.js.map +1 -0
- package/util/acceptsNumber.mjs +104 -0
- package/util/acceptsNumber.mjs.map +1 -0
- package/util/bindActionsToField.d.ts +7 -0
- package/util/bindActionsToField.d.ts.map +1 -0
- package/util/bindActionsToField.js +22 -0
- package/util/bindActionsToField.js.map +1 -0
- package/util/bindActionsToField.mjs +4 -0
- package/util/bindActionsToField.mjs.map +1 -0
- package/util/get.d.ts +2 -0
- package/util/get.d.ts.map +1 -0
- package/util/get.js +12 -0
- package/util/get.js.map +1 -0
- package/util/get.mjs +4 -0
- package/util/get.mjs.map +1 -0
- package/util/getArrayElementSchema.d.ts +3 -0
- package/util/getArrayElementSchema.d.ts.map +1 -0
- package/util/getArrayElementSchema.js +87 -0
- package/util/getArrayElementSchema.js.map +1 -0
- package/util/getArrayElementSchema.mjs +102 -0
- package/util/getArrayElementSchema.mjs.map +1 -0
- package/util/maybeParse.d.ts +3 -0
- package/util/maybeParse.d.ts.map +1 -0
- package/util/maybeParse.js +11 -0
- package/util/maybeParse.js.map +1 -0
- package/util/maybeParse.mjs +5 -0
- package/util/maybeParse.mjs.map +1 -0
- package/util/parsePathstring.d.ts +40 -0
- package/util/parsePathstring.d.ts.map +1 -0
- package/util/parsePathstring.js +55 -0
- package/util/parsePathstring.js.map +1 -0
- package/util/parsePathstring.mjs +35 -0
- package/util/parsePathstring.mjs.map +1 -0
- package/util/pathstring.d.ts +13 -0
- package/util/pathstring.d.ts.map +1 -0
- package/util/pathstring.js +15 -0
- package/util/pathstring.js.map +1 -0
- package/util/pathstring.mjs +7 -0
- package/util/pathstring.mjs.map +1 -0
- package/util/set.d.ts +2 -0
- package/util/set.d.ts.map +1 -0
- package/util/set.js +33 -0
- package/util/set.js.map +1 -0
- package/util/set.mjs +23 -0
- package/util/set.mjs.map +1 -0
- package/util/setAdd.d.ts +2 -0
- package/util/setAdd.d.ts.map +1 -0
- package/util/setAdd.js +13 -0
- package/util/setAdd.js.map +1 -0
- package/util/setAdd.mjs +7 -0
- package/util/setAdd.mjs.map +1 -0
- package/util/setDelete.d.ts +2 -0
- package/util/setDelete.d.ts.map +1 -0
- package/util/setDelete.js +13 -0
- package/util/setDelete.js.map +1 -0
- package/util/setDelete.mjs +7 -0
- 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
|
+
}
|