@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,183 @@
|
|
|
1
|
+
import { FieldPathForValue } from '../FieldPath'
|
|
2
|
+
import z from 'zod'
|
|
3
|
+
|
|
4
|
+
export type ArrayFieldPath<V = any, R = any> = FieldPathForValue<
|
|
5
|
+
V[] | null | undefined,
|
|
6
|
+
R[] | null | undefined
|
|
7
|
+
>
|
|
8
|
+
|
|
9
|
+
type ValueFor<Field extends ArrayFieldPath> = NonNullable<
|
|
10
|
+
z.output<Field['schema']>
|
|
11
|
+
>[number]
|
|
12
|
+
|
|
13
|
+
type RawValueFor<Field extends ArrayFieldPath> = NonNullable<
|
|
14
|
+
z.input<Field['schema']>
|
|
15
|
+
>[number]
|
|
16
|
+
|
|
17
|
+
export type ArrayInsertAction<Field extends ArrayFieldPath = ArrayFieldPath> =
|
|
18
|
+
ReturnType<typeof arrayInsert<Field>>
|
|
19
|
+
|
|
20
|
+
export function arrayInsert<Field extends ArrayFieldPath>(
|
|
21
|
+
field: Field,
|
|
22
|
+
index: number,
|
|
23
|
+
value: ValueFor<Field>
|
|
24
|
+
) {
|
|
25
|
+
return { type: 'arrayInsert', field, index, value } as const
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export type ArrayInsertRawAction<
|
|
29
|
+
Field extends ArrayFieldPath = ArrayFieldPath
|
|
30
|
+
> = ReturnType<typeof arrayInsertRaw<Field>>
|
|
31
|
+
|
|
32
|
+
export function arrayInsertRaw<Field extends ArrayFieldPath>(
|
|
33
|
+
field: Field,
|
|
34
|
+
index: number,
|
|
35
|
+
rawValue: RawValueFor<Field>
|
|
36
|
+
) {
|
|
37
|
+
return { type: 'arrayInsertRaw', field, index, rawValue } as const
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export type ArrayMoveAction = ReturnType<typeof arrayMove>
|
|
41
|
+
|
|
42
|
+
export function arrayMove(field: ArrayFieldPath, from: number, to: number) {
|
|
43
|
+
return { type: 'arrayMove', field, from, to } as const
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export type ArrayPopAction = ReturnType<typeof arrayPop>
|
|
47
|
+
|
|
48
|
+
export function arrayPop(field: ArrayFieldPath) {
|
|
49
|
+
return { type: 'arrayPop', field } as const
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export type ArrayPushAction<Field extends ArrayFieldPath = ArrayFieldPath> =
|
|
53
|
+
ReturnType<typeof arrayPush<Field>>
|
|
54
|
+
|
|
55
|
+
export function arrayPush<Field extends ArrayFieldPath>(
|
|
56
|
+
field: Field,
|
|
57
|
+
value: ValueFor<Field>
|
|
58
|
+
) {
|
|
59
|
+
return { type: 'arrayPush', field, value } as const
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export type ArrayPushRawAction<Field extends ArrayFieldPath = ArrayFieldPath> =
|
|
63
|
+
ReturnType<typeof arrayPushRaw<Field>>
|
|
64
|
+
|
|
65
|
+
export function arrayPushRaw<Field extends ArrayFieldPath>(
|
|
66
|
+
field: Field,
|
|
67
|
+
rawValue: RawValueFor<Field>
|
|
68
|
+
) {
|
|
69
|
+
return { type: 'arrayPushRaw', field, rawValue } as const
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export type ArrayRemoveAction = ReturnType<typeof arrayRemove>
|
|
73
|
+
|
|
74
|
+
export function arrayRemove(field: ArrayFieldPath, index: number) {
|
|
75
|
+
return { type: 'arrayRemove', field, index } as const
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export type ArrayRemoveAllAction = ReturnType<typeof arrayRemoveAll>
|
|
79
|
+
|
|
80
|
+
export function arrayRemoveAll(field: ArrayFieldPath) {
|
|
81
|
+
return { type: 'arrayRemoveAll', field } as const
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export type ArrayShiftAction = ReturnType<typeof arrayShift>
|
|
85
|
+
|
|
86
|
+
export function arrayShift(field: ArrayFieldPath) {
|
|
87
|
+
return { type: 'arrayShift', field } as const
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export type ArraySpliceAction<Field extends ArrayFieldPath = ArrayFieldPath> =
|
|
91
|
+
ReturnType<typeof arraySplice<Field>>
|
|
92
|
+
|
|
93
|
+
export function arraySplice<Field extends ArrayFieldPath>(
|
|
94
|
+
field: Field,
|
|
95
|
+
index: number,
|
|
96
|
+
deleteCount: number,
|
|
97
|
+
...values: ValueFor<Field>[]
|
|
98
|
+
) {
|
|
99
|
+
return { type: 'arraySplice', field, index, deleteCount, values } as const
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export type ArraySpliceRawAction<
|
|
103
|
+
Field extends ArrayFieldPath = ArrayFieldPath
|
|
104
|
+
> = ReturnType<typeof arraySpliceRaw<Field>>
|
|
105
|
+
|
|
106
|
+
export function arraySpliceRaw<Field extends ArrayFieldPath>(
|
|
107
|
+
field: Field,
|
|
108
|
+
index: number,
|
|
109
|
+
deleteCount: number,
|
|
110
|
+
...rawValues: RawValueFor<Field>[]
|
|
111
|
+
) {
|
|
112
|
+
return {
|
|
113
|
+
type: 'arraySpliceRaw',
|
|
114
|
+
field,
|
|
115
|
+
index,
|
|
116
|
+
deleteCount,
|
|
117
|
+
rawValues,
|
|
118
|
+
} as const
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export type ArraySwapAction = ReturnType<typeof arraySwap>
|
|
122
|
+
|
|
123
|
+
export function arraySwap(
|
|
124
|
+
field: ArrayFieldPath,
|
|
125
|
+
indexA: number,
|
|
126
|
+
indexB: number
|
|
127
|
+
) {
|
|
128
|
+
return { type: 'arraySwap', field, indexA, indexB } as const
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export type ArrayUnshiftAction<Field extends ArrayFieldPath = ArrayFieldPath> =
|
|
132
|
+
ReturnType<typeof arrayUnshift<Field>>
|
|
133
|
+
|
|
134
|
+
export function arrayUnshift<Field extends ArrayFieldPath>(
|
|
135
|
+
field: Field,
|
|
136
|
+
value: ValueFor<Field>
|
|
137
|
+
) {
|
|
138
|
+
return { type: 'arrayUnshift', field, value } as const
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export type ArrayUnshiftRawAction<
|
|
142
|
+
Field extends ArrayFieldPath = ArrayFieldPath
|
|
143
|
+
> = ReturnType<typeof arrayUnshiftRaw<Field>>
|
|
144
|
+
|
|
145
|
+
export function arrayUnshiftRaw<Field extends ArrayFieldPath>(
|
|
146
|
+
field: Field,
|
|
147
|
+
rawValue: RawValueFor<Field>
|
|
148
|
+
) {
|
|
149
|
+
return { type: 'arrayUnshiftRaw', field, rawValue } as const
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export type ArrayAction<Field extends ArrayFieldPath = ArrayFieldPath> =
|
|
153
|
+
| ArrayInsertAction<Field>
|
|
154
|
+
| ArrayInsertRawAction<Field>
|
|
155
|
+
| ArrayMoveAction
|
|
156
|
+
| ArrayPopAction
|
|
157
|
+
| ArrayPushAction<Field>
|
|
158
|
+
| ArrayPushRawAction<Field>
|
|
159
|
+
| ArrayRemoveAction
|
|
160
|
+
| ArrayRemoveAllAction
|
|
161
|
+
| ArrayShiftAction
|
|
162
|
+
| ArraySpliceAction<Field>
|
|
163
|
+
| ArraySpliceRawAction<Field>
|
|
164
|
+
| ArraySwapAction
|
|
165
|
+
| ArrayUnshiftAction<Field>
|
|
166
|
+
| ArrayUnshiftRawAction<Field>
|
|
167
|
+
|
|
168
|
+
export const arrayActions = {
|
|
169
|
+
insert: arrayInsert,
|
|
170
|
+
insertRaw: arrayInsertRaw,
|
|
171
|
+
move: arrayMove,
|
|
172
|
+
pop: arrayPop,
|
|
173
|
+
push: arrayPush,
|
|
174
|
+
pushRaw: arrayPushRaw,
|
|
175
|
+
remove: arrayRemove,
|
|
176
|
+
removeAll: arrayRemoveAll,
|
|
177
|
+
shift: arrayShift,
|
|
178
|
+
splice: arraySplice,
|
|
179
|
+
spliceRaw: arraySpliceRaw,
|
|
180
|
+
swap: arraySwap,
|
|
181
|
+
unshift: arrayUnshift,
|
|
182
|
+
unshiftRaw: arrayUnshiftRaw,
|
|
183
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import z from 'zod'
|
|
2
|
+
|
|
3
|
+
export type InitializeAction<T extends z.ZodTypeAny> = ReturnType<
|
|
4
|
+
typeof initialize<T>
|
|
5
|
+
>
|
|
6
|
+
|
|
7
|
+
export function initialize<T extends z.ZodTypeAny>(props: {
|
|
8
|
+
rawValues?: z.input<T>
|
|
9
|
+
values?: z.output<T>
|
|
10
|
+
keepSubmitSucceeded?: boolean
|
|
11
|
+
}) {
|
|
12
|
+
return {
|
|
13
|
+
type: 'initialize',
|
|
14
|
+
...props,
|
|
15
|
+
} as const
|
|
16
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import z from 'zod'
|
|
2
|
+
import { Handlers } from './addHandlers'
|
|
3
|
+
|
|
4
|
+
export type RemoveHandlersAction<T extends z.ZodTypeAny> = ReturnType<
|
|
5
|
+
typeof removeHandlers<T>
|
|
6
|
+
>
|
|
7
|
+
|
|
8
|
+
export function removeHandlers<T extends z.ZodTypeAny>(handlers: Handlers<T>) {
|
|
9
|
+
return {
|
|
10
|
+
type: 'removeHandlers',
|
|
11
|
+
...handlers,
|
|
12
|
+
} as const
|
|
13
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { FieldPath } from '../FieldPath'
|
|
2
|
+
import { FieldMeta } from '../FormState'
|
|
3
|
+
|
|
4
|
+
export type SetMetaAction<Field extends FieldPath> = ReturnType<
|
|
5
|
+
typeof setMeta<Field>
|
|
6
|
+
>
|
|
7
|
+
|
|
8
|
+
export function setMeta<Field extends FieldPath>(
|
|
9
|
+
field: Field,
|
|
10
|
+
meta: Partial<FieldMeta>
|
|
11
|
+
) {
|
|
12
|
+
return {
|
|
13
|
+
type: 'setMeta',
|
|
14
|
+
field,
|
|
15
|
+
meta,
|
|
16
|
+
} as const
|
|
17
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import z from 'zod'
|
|
2
|
+
import { FieldPath } from '../FieldPath'
|
|
3
|
+
|
|
4
|
+
export type SetRawValueAction<Field extends FieldPath> = ReturnType<
|
|
5
|
+
typeof setRawValue<Field>
|
|
6
|
+
>
|
|
7
|
+
|
|
8
|
+
export function setRawValue<Field extends FieldPath>(
|
|
9
|
+
field: Field,
|
|
10
|
+
rawValue: z.input<Field['schema']>
|
|
11
|
+
) {
|
|
12
|
+
return {
|
|
13
|
+
type: 'setRawValue',
|
|
14
|
+
field,
|
|
15
|
+
rawValue,
|
|
16
|
+
} as const
|
|
17
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import z from 'zod'
|
|
2
|
+
import { FormState } from '../FormState'
|
|
3
|
+
|
|
4
|
+
export type SetSubmitStatusAction<T extends z.ZodTypeAny> = ReturnType<
|
|
5
|
+
typeof setSubmitStatus<T>
|
|
6
|
+
>
|
|
7
|
+
|
|
8
|
+
export function setSubmitStatus<T extends z.ZodTypeAny>(
|
|
9
|
+
options: Partial<
|
|
10
|
+
Pick<
|
|
11
|
+
FormState<T>,
|
|
12
|
+
| 'submitting'
|
|
13
|
+
| 'submitError'
|
|
14
|
+
| 'submitPromise'
|
|
15
|
+
| 'submitSucceeded'
|
|
16
|
+
| 'submitFailed'
|
|
17
|
+
| 'submittedValues'
|
|
18
|
+
| 'rawSubmittedValues'
|
|
19
|
+
>
|
|
20
|
+
>
|
|
21
|
+
) {
|
|
22
|
+
return {
|
|
23
|
+
type: 'setSubmitStatus',
|
|
24
|
+
...options,
|
|
25
|
+
} as const
|
|
26
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import z from 'zod'
|
|
2
|
+
import { FieldPath } from '../FieldPath'
|
|
3
|
+
|
|
4
|
+
export type SetValueAction<Field extends FieldPath> = ReturnType<
|
|
5
|
+
typeof setValue<Field>
|
|
6
|
+
>
|
|
7
|
+
|
|
8
|
+
export function setValue<Field extends FieldPath>(
|
|
9
|
+
field: Field,
|
|
10
|
+
value: z.output<Field['schema']>,
|
|
11
|
+
options?: {
|
|
12
|
+
normalize?: boolean
|
|
13
|
+
}
|
|
14
|
+
) {
|
|
15
|
+
return {
|
|
16
|
+
type: 'setValue',
|
|
17
|
+
field,
|
|
18
|
+
value,
|
|
19
|
+
...options,
|
|
20
|
+
} as const
|
|
21
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { Dispatch, Middleware } from 'redux'
|
|
2
|
+
import z from 'zod'
|
|
3
|
+
import { FormAction } from './FormAction'
|
|
4
|
+
import { FormState } from './FormState'
|
|
5
|
+
import { SubmitAction } from './actions/submit'
|
|
6
|
+
import { setSubmitStatus } from './actions/setSubmitStatus'
|
|
7
|
+
import { submitSucceeded } from './actions/submitSucceeded'
|
|
8
|
+
|
|
9
|
+
export function createFormMiddleware<T extends z.ZodTypeAny>(): Middleware<
|
|
10
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
11
|
+
{},
|
|
12
|
+
FormState<T>,
|
|
13
|
+
Dispatch<FormAction<T>>
|
|
14
|
+
> {
|
|
15
|
+
return (store) => (next) => (action) => {
|
|
16
|
+
const result: FormState<T> = next(action) as any
|
|
17
|
+
|
|
18
|
+
const nextState = store.getState()
|
|
19
|
+
|
|
20
|
+
if (isSubmitAction(action) && !nextState.submitting) {
|
|
21
|
+
const {
|
|
22
|
+
onSubmit,
|
|
23
|
+
onSubmitSucceeded,
|
|
24
|
+
onSubmitFailed,
|
|
25
|
+
values,
|
|
26
|
+
rawValues,
|
|
27
|
+
initialValues,
|
|
28
|
+
} = nextState
|
|
29
|
+
const submitPromise = (async () => {
|
|
30
|
+
if (nextState.validationError) throw nextState.validationError
|
|
31
|
+
for (const fn of onSubmit) await fn(values, { initialValues })
|
|
32
|
+
})()
|
|
33
|
+
store.dispatch(
|
|
34
|
+
setSubmitStatus({
|
|
35
|
+
submitting: true,
|
|
36
|
+
submittedValues: values,
|
|
37
|
+
rawSubmittedValues: rawValues,
|
|
38
|
+
submitError: undefined,
|
|
39
|
+
submitSucceeded: false,
|
|
40
|
+
submitFailed: false,
|
|
41
|
+
submitPromise,
|
|
42
|
+
})
|
|
43
|
+
)
|
|
44
|
+
submitPromise.then(
|
|
45
|
+
async () => {
|
|
46
|
+
if (store.getState().submitPromise !== submitPromise) return
|
|
47
|
+
store.dispatch(submitSucceeded())
|
|
48
|
+
for (const fn of onSubmitSucceeded) await fn()
|
|
49
|
+
},
|
|
50
|
+
async (error) => {
|
|
51
|
+
if (store.getState().submitPromise !== submitPromise) return
|
|
52
|
+
store.dispatch(
|
|
53
|
+
setSubmitStatus({
|
|
54
|
+
submitting: false,
|
|
55
|
+
submitError: error,
|
|
56
|
+
submitSucceeded: false,
|
|
57
|
+
submitFailed: true,
|
|
58
|
+
submitPromise: undefined,
|
|
59
|
+
})
|
|
60
|
+
)
|
|
61
|
+
for (const fn of onSubmitFailed) await fn(error)
|
|
62
|
+
}
|
|
63
|
+
)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return result
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function isSubmitAction(action: any): action is SubmitAction {
|
|
71
|
+
return (
|
|
72
|
+
action instanceof Object && 'type' in action && action.type === 'submit'
|
|
73
|
+
)
|
|
74
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import React from 'react'
|
|
2
|
+
import { Store, applyMiddleware, bindActionCreators, createStore } from 'redux'
|
|
3
|
+
import z from 'zod'
|
|
4
|
+
import { setMounted } from './actions/setMounted'
|
|
5
|
+
import { createFormReducer } from './createFormReducer'
|
|
6
|
+
import { FormAction } from './FormAction'
|
|
7
|
+
import { FormState } from './FormState'
|
|
8
|
+
import { Provider } from 'react-redux'
|
|
9
|
+
import { initialize } from './actions/initialize'
|
|
10
|
+
import { setRawValue } from './actions/setRawValue'
|
|
11
|
+
import { setValue } from './actions/setValue'
|
|
12
|
+
import { FormContext, FormContextProps } from './FormContext'
|
|
13
|
+
import { FormStateContext } from './FormStateContext'
|
|
14
|
+
import { createFormMiddleware } from './createFormMiddleware'
|
|
15
|
+
import { submit } from './actions/submit'
|
|
16
|
+
import { setSubmitStatus } from './actions/setSubmitStatus'
|
|
17
|
+
import { setMeta } from './actions/setMeta'
|
|
18
|
+
import { addHandlers } from './actions/addHandlers'
|
|
19
|
+
import { removeHandlers } from './actions/removeHandlers'
|
|
20
|
+
import { arrayActions } from './actions/arrayActions'
|
|
21
|
+
|
|
22
|
+
export const createFormProvider = <T extends z.ZodTypeAny>(
|
|
23
|
+
props: Pick<
|
|
24
|
+
FormContextProps<T>,
|
|
25
|
+
| 'root'
|
|
26
|
+
| 'schema'
|
|
27
|
+
| 'inverseSchema'
|
|
28
|
+
| 'selectFormStatus'
|
|
29
|
+
| 'selectFieldErrorMap'
|
|
30
|
+
| 'selectFormValues'
|
|
31
|
+
>
|
|
32
|
+
) =>
|
|
33
|
+
function FormProvider({ children }: { children: React.ReactElement }) {
|
|
34
|
+
const storeRef = React.useRef<Store<FormState<T>, FormAction<T>>>()
|
|
35
|
+
if (!storeRef.current) {
|
|
36
|
+
storeRef.current = createStore(
|
|
37
|
+
createFormReducer(props),
|
|
38
|
+
applyMiddleware(createFormMiddleware())
|
|
39
|
+
)
|
|
40
|
+
}
|
|
41
|
+
const store = storeRef.current
|
|
42
|
+
const { dispatch } = store
|
|
43
|
+
|
|
44
|
+
const getValues = React.useCallback(() => store.getState().values, [])
|
|
45
|
+
const getRawValues = React.useCallback(() => store.getState().rawValues, [])
|
|
46
|
+
const getInitialValues = React.useCallback(
|
|
47
|
+
() => store.getState().initialValues,
|
|
48
|
+
[]
|
|
49
|
+
)
|
|
50
|
+
const getRawInitialValues = React.useCallback(
|
|
51
|
+
() => store.getState().rawInitialValues,
|
|
52
|
+
[]
|
|
53
|
+
)
|
|
54
|
+
const getStatus = React.useCallback(
|
|
55
|
+
() => props.selectFormStatus(store.getState()),
|
|
56
|
+
[]
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
React.useEffect(
|
|
60
|
+
() => () => {
|
|
61
|
+
store.dispatch(setMounted(false))
|
|
62
|
+
},
|
|
63
|
+
[]
|
|
64
|
+
)
|
|
65
|
+
const formContext = React.useMemo(
|
|
66
|
+
(): FormContextProps<T> => ({
|
|
67
|
+
...props,
|
|
68
|
+
getValues,
|
|
69
|
+
getRawValues,
|
|
70
|
+
getInitialValues,
|
|
71
|
+
getRawInitialValues,
|
|
72
|
+
getStatus,
|
|
73
|
+
...bindActionCreators(
|
|
74
|
+
{
|
|
75
|
+
initialize: initialize<T>,
|
|
76
|
+
addHandlers: addHandlers<T>,
|
|
77
|
+
removeHandlers: removeHandlers<T>,
|
|
78
|
+
submit,
|
|
79
|
+
setSubmitStatus: setSubmitStatus<T>,
|
|
80
|
+
setMeta: setMeta as any,
|
|
81
|
+
setRawValue: setRawValue as any,
|
|
82
|
+
setValue: setValue as any,
|
|
83
|
+
},
|
|
84
|
+
dispatch
|
|
85
|
+
),
|
|
86
|
+
arrayActions: bindActionCreators(arrayActions, dispatch),
|
|
87
|
+
}),
|
|
88
|
+
[]
|
|
89
|
+
)
|
|
90
|
+
|
|
91
|
+
return (
|
|
92
|
+
<FormContext.Provider value={formContext as any}>
|
|
93
|
+
<Provider
|
|
94
|
+
store={store}
|
|
95
|
+
context={FormStateContext as FormStateContext<T>}
|
|
96
|
+
>
|
|
97
|
+
{children}
|
|
98
|
+
</Provider>
|
|
99
|
+
</FormContext.Provider>
|
|
100
|
+
)
|
|
101
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { Reducer } from 'redux'
|
|
2
|
+
import z from 'zod'
|
|
3
|
+
import { FormAction } from './FormAction'
|
|
4
|
+
import { FormState } from './FormState'
|
|
5
|
+
import { initFormState } from './initFormState'
|
|
6
|
+
import { addHandlersReducer } from './reducers/addHandlers'
|
|
7
|
+
import { removeHandlersReducer } from './reducers/removeHandlers'
|
|
8
|
+
import { createSetValueReducer } from './reducers/setValue'
|
|
9
|
+
import { createSetRawValueReducer } from './reducers/setRawValue'
|
|
10
|
+
import { setMetaReducer } from './reducers/setMeta'
|
|
11
|
+
import { submitSucceededReducer } from './reducers/submitSucceeded'
|
|
12
|
+
import { setSubmitStatusReducer } from './reducers/setSubmitStatus'
|
|
13
|
+
import { createInitializeReducer } from './reducers/initialize'
|
|
14
|
+
import { arrayPopReducer } from './reducers/arrayPop'
|
|
15
|
+
import { arrayMoveReducer } from './reducers/arrayMove'
|
|
16
|
+
import { arrayRemoveReducer } from './reducers/arrayRemove'
|
|
17
|
+
import { arrayRemoveAllReducer } from './reducers/arrayRemoveAll'
|
|
18
|
+
import { arrayShiftReducer } from './reducers/arrayShift'
|
|
19
|
+
import { arraySwapReducer } from './reducers/arraySwap'
|
|
20
|
+
import { arrayPushRawReducer } from './reducers/arrayPushRaw'
|
|
21
|
+
import { arrayInsertRawReducer } from './reducers/arrayInsertRaw'
|
|
22
|
+
import { arraySpliceRawReducer } from './reducers/arraySpliceRaw'
|
|
23
|
+
import { arrayUnshiftRawReducer } from './reducers/arrayUnshiftRaw'
|
|
24
|
+
import { arrayPushReducer } from './reducers/arrayPush'
|
|
25
|
+
import { arrayUnshiftReducer } from './reducers/arrayUnshift'
|
|
26
|
+
import { arrayInsertReducer } from './reducers/arrayInsert'
|
|
27
|
+
import { arraySpliceReducer } from './reducers/arraySplice'
|
|
28
|
+
|
|
29
|
+
export function createFormReducer<T extends z.ZodTypeAny>(options: {
|
|
30
|
+
schema: T
|
|
31
|
+
inverseSchema: z.ZodType<z.input<T>, any, z.output<T>>
|
|
32
|
+
}): Reducer<FormState<T>, FormAction<T>> {
|
|
33
|
+
const initializeReducer = createInitializeReducer(options)
|
|
34
|
+
const setValueReducer = createSetValueReducer(options)
|
|
35
|
+
const setRawValueReducer = createSetRawValueReducer(options)
|
|
36
|
+
const formReducer = (
|
|
37
|
+
state: FormState<T> = initFormState(),
|
|
38
|
+
action: FormAction<T>
|
|
39
|
+
): FormState<T> => {
|
|
40
|
+
switch (action.type) {
|
|
41
|
+
case 'setMounted':
|
|
42
|
+
return { ...state, mounted: action.mounted }
|
|
43
|
+
case 'addHandlers':
|
|
44
|
+
return addHandlersReducer(state, action)
|
|
45
|
+
case 'removeHandlers':
|
|
46
|
+
return removeHandlersReducer(state, action)
|
|
47
|
+
case 'initialize':
|
|
48
|
+
return initializeReducer(state, action)
|
|
49
|
+
case 'setSubmitStatus':
|
|
50
|
+
return setSubmitStatusReducer(state, action)
|
|
51
|
+
case 'submitSucceeded':
|
|
52
|
+
return submitSucceededReducer(state)
|
|
53
|
+
case 'setValue':
|
|
54
|
+
return setValueReducer(state, action)
|
|
55
|
+
case 'setRawValue':
|
|
56
|
+
return setRawValueReducer(state, action)
|
|
57
|
+
case 'setMeta':
|
|
58
|
+
return setMetaReducer(state, action)
|
|
59
|
+
case 'arrayInsert':
|
|
60
|
+
return arrayInsertReducer(formReducer, state, action)
|
|
61
|
+
case 'arrayInsertRaw':
|
|
62
|
+
return arrayInsertRawReducer(formReducer, state, action)
|
|
63
|
+
case 'arrayMove':
|
|
64
|
+
return arrayMoveReducer(formReducer, state, action)
|
|
65
|
+
case 'arrayPop':
|
|
66
|
+
return arrayPopReducer(formReducer, state, action)
|
|
67
|
+
case 'arrayPush':
|
|
68
|
+
return arrayPushReducer(formReducer, state, action)
|
|
69
|
+
case 'arrayPushRaw':
|
|
70
|
+
return arrayPushRawReducer(formReducer, state, action)
|
|
71
|
+
case 'arrayRemove':
|
|
72
|
+
return arrayRemoveReducer(formReducer, state, action)
|
|
73
|
+
case 'arrayRemoveAll':
|
|
74
|
+
return arrayRemoveAllReducer(formReducer, state, action)
|
|
75
|
+
case 'arrayShift':
|
|
76
|
+
return arrayShiftReducer(formReducer, state, action)
|
|
77
|
+
case 'arraySplice':
|
|
78
|
+
return arraySpliceReducer(formReducer, state, action)
|
|
79
|
+
case 'arraySpliceRaw':
|
|
80
|
+
return arraySpliceRawReducer(formReducer, state, action)
|
|
81
|
+
case 'arraySwap':
|
|
82
|
+
return arraySwapReducer(formReducer, state, action)
|
|
83
|
+
case 'arrayUnshift':
|
|
84
|
+
return arrayUnshiftReducer(formReducer, state, action)
|
|
85
|
+
case 'arrayUnshiftRaw':
|
|
86
|
+
return arrayUnshiftRawReducer(formReducer, state, action)
|
|
87
|
+
}
|
|
88
|
+
return state
|
|
89
|
+
}
|
|
90
|
+
return formReducer
|
|
91
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import z from 'zod'
|
|
2
|
+
import { pathstring } from './util/pathstring'
|
|
3
|
+
import { createSelector } from 'reselect'
|
|
4
|
+
import { FormState } from './FormState'
|
|
5
|
+
|
|
6
|
+
export type SelectFieldErrorMap = ReturnType<typeof createSelectFieldErrorMap>
|
|
7
|
+
|
|
8
|
+
export function createSelectFieldErrorMap() {
|
|
9
|
+
return createSelector(
|
|
10
|
+
[
|
|
11
|
+
(state: FormState<any>) => state.validationError,
|
|
12
|
+
(state: FormState<any>) => state.submitError,
|
|
13
|
+
],
|
|
14
|
+
(...errors: any[]): { [K in string]?: string } =>
|
|
15
|
+
Object.fromEntries(
|
|
16
|
+
errors.flatMap((e) =>
|
|
17
|
+
isZodError(e)
|
|
18
|
+
? e.issues.map((issue) => [
|
|
19
|
+
pathstring(issue.path),
|
|
20
|
+
messageForIssue(issue),
|
|
21
|
+
])
|
|
22
|
+
: []
|
|
23
|
+
)
|
|
24
|
+
)
|
|
25
|
+
)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function isZodError(error: any): error is z.ZodError {
|
|
29
|
+
return error && error.name === 'ZodError'
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Gets less confusing error messages for ordinary users for certain ZodIssues
|
|
34
|
+
*/
|
|
35
|
+
function messageForIssue(issue: z.ZodIssue): string {
|
|
36
|
+
if (issue.code === 'invalid_type') {
|
|
37
|
+
return issue.received === 'null' || issue.received === 'undefined'
|
|
38
|
+
? // Without this, the error would say "Expected <type>, received null"
|
|
39
|
+
// or "Invalid number" as below
|
|
40
|
+
'Required'
|
|
41
|
+
: issue.expected === 'number' || issue.expected === 'bigint'
|
|
42
|
+
? // Without this, invalid text input for z.number() would say "Expected number, received string"
|
|
43
|
+
'Invalid number'
|
|
44
|
+
: issue.message
|
|
45
|
+
}
|
|
46
|
+
return issue.message
|
|
47
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { createSelector, createStructuredSelector } from 'reselect'
|
|
2
|
+
import { FormState } from './FormState'
|
|
3
|
+
import isEqual from 'fast-deep-equal'
|
|
4
|
+
|
|
5
|
+
export type SelectFormStatus = ReturnType<typeof createSelectFormStatus>
|
|
6
|
+
export type FormStatus = ReturnType<SelectFormStatus>
|
|
7
|
+
|
|
8
|
+
export function createSelectFormStatus() {
|
|
9
|
+
let lastValidationError: FormState<any>['validationError'] = undefined
|
|
10
|
+
|
|
11
|
+
function selectValidationError({ validationError }: FormState<any>) {
|
|
12
|
+
if (
|
|
13
|
+
validationError === lastValidationError ||
|
|
14
|
+
isEqual(validationError?.issues, lastValidationError?.issues)
|
|
15
|
+
) {
|
|
16
|
+
return lastValidationError
|
|
17
|
+
}
|
|
18
|
+
return (lastValidationError = validationError)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const selectPristine = createSelector(
|
|
22
|
+
[
|
|
23
|
+
(state: FormState<any>) => state.values,
|
|
24
|
+
(state: FormState<any>) => state.initialValues,
|
|
25
|
+
],
|
|
26
|
+
isEqual
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
return createStructuredSelector({
|
|
30
|
+
initialized: (state: FormState<any>) => state.initialized,
|
|
31
|
+
submitting: (state: FormState<any>) => state.submitting,
|
|
32
|
+
submitSucceeded: (state: FormState<any>) => state.submitSucceeded,
|
|
33
|
+
submitFailed: (state: FormState<any>) => state.submitFailed,
|
|
34
|
+
submitError: (state: FormState<any>) => state.submitError,
|
|
35
|
+
validationError: selectValidationError,
|
|
36
|
+
valid: (state: FormState<any>) => selectValidationError(state) == null,
|
|
37
|
+
invalid: (state: FormState<any>) => selectValidationError(state) != null,
|
|
38
|
+
pristine: selectPristine,
|
|
39
|
+
dirty: (state: FormState<any>) => !selectPristine(state),
|
|
40
|
+
})
|
|
41
|
+
}
|