{"version":3,"file":"useForm-BbBaDtS1.js","sources":["../../src/composables/useForm.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\nimport { configureYupLocale } from '@/utils/yupConfig';\nimport { Ref, ref } from 'vue';\nimport { useField, useForm as veeUseForm } from 'vee-validate';\n\nexport interface Form {\n form: Ref,\n reset: any,\n errors: Ref,\n setErrors: (messages: any) => void,\n resetErrors: () => void,\n validationMeta: any,\n validationValidate: any,\n validationValidateField: any\n handleSubmit: () => Promise\n}\n\n// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types\nexport default function useForm (fields: any, validationSchema?: any): Form {\n const defaultFormFields = ref(fields);\n const form = ref(fields);\n const errors = ref({} as any);\n\n let resetErrors = () => {\n errors.value = { ...defaultFormFields };\n };\n\n let setErrors: any = (messages: Array) => {\n errors.value = messages;\n };\n\n let validationMeta = null;\n let validationValidate: unknown = null;\n let validationValidateField: unknown = null;\n let reset: unknown = null;\n\n if (validationSchema) {\n const { meta, validate, setErrors: veeSetErrors, validateField, resetForm } = veeUseForm({ validationSchema: validationSchema.fields });\n\n configureYupLocale();\n\n setErrors = veeSetErrors;\n\n resetErrors = () => {\n const newErrors: any = {};\n\n Object.keys(errors.value).forEach((key) => {\n newErrors[key] = '';\n });\n\n setErrors(newErrors);\n };\n\n if (meta) {\n validationMeta = meta;\n }\n\n if (validate) {\n validationValidate = validate;\n }\n\n if (validateField) {\n validationValidateField = validateField;\n }\n\n if (resetForm) {\n reset = resetForm;\n }\n\n Object.keys(validationSchema.fields).forEach((key) => {\n const { value, errorMessage } = useField(key, validationSchema.fields[key], { initialValue: form.value[key] });\n\n form.value[key] = value;\n errors.value[key] = errorMessage;\n });\n }\n\n const handleSubmit = async () => {\n let valid = true;\n\n if (validationValidate) {\n valid = (await (validationValidate as any)()).valid;\n }\n\n if (valid) {\n await resetErrors();\n }\n\n return valid;\n };\n\n return {\n form,\n reset,\n errors,\n setErrors,\n resetErrors,\n validationMeta,\n validationValidate,\n handleSubmit,\n validationValidateField\n };\n}\n"],"names":["useForm","fields","validationSchema","defaultFormFields","ref","form","errors","resetErrors","setErrors","messages","validationMeta","validationValidate","validationValidateField","reset","meta","validate","veeSetErrors","validateField","resetForm","veeUseForm","configureYupLocale","newErrors","key","value","errorMessage","useField","valid"],"mappings":"iaAkBwB,SAAAA,EAAYC,EAAaC,EAAiC,CACxE,MAAAC,EAAoBC,EAAIH,CAAM,EAC9BI,EAAOD,EAAIH,CAAM,EACjBK,EAASF,EAAI,EAAS,EAE5B,IAAIG,EAAc,IAAM,CACbD,EAAA,MAAQ,CAAE,GAAGH,CAAkB,CAC1C,EAEIK,EAAkBC,GAA4B,CAC9CH,EAAO,MAAQG,CACnB,EAEIC,EAAiB,KACjBC,EAA8B,KAC9BC,EAAmC,KACnCC,EAAiB,KAErB,GAAIX,EAAkB,CAClB,KAAM,CAAE,KAAAY,EAAM,SAAAC,EAAU,UAAWC,EAAc,cAAAC,EAAe,UAAAC,CAAU,EAAIC,EAAW,CAAE,iBAAkBjB,EAAiB,OAAQ,EAEnHkB,EAAA,EAEPZ,EAAAQ,EAEZT,EAAc,IAAM,CAChB,MAAMc,EAAiB,CAAC,EAExB,OAAO,KAAKf,EAAO,KAAK,EAAE,QAASgB,GAAQ,CACvCD,EAAUC,CAAG,EAAI,EAAA,CACpB,EAEDd,EAAUa,CAAS,CACvB,EAEIP,IACiBJ,EAAAI,GAGjBC,IACqBJ,EAAAI,GAGrBE,IAC0BL,EAAAK,GAG1BC,IACQL,EAAAK,GAGZ,OAAO,KAAKhB,EAAiB,MAAM,EAAE,QAASoB,GAAQ,CAClD,KAAM,CAAE,MAAAC,EAAO,aAAAC,CAAA,EAAiBC,EAASH,EAAKpB,EAAiB,OAAOoB,CAAG,EAAG,CAAE,aAAcjB,EAAK,MAAMiB,CAAG,EAAG,EAExGjB,EAAA,MAAMiB,CAAG,EAAIC,EACXjB,EAAA,MAAMgB,CAAG,EAAIE,CAAA,CACvB,CAAA,CAiBE,MAAA,CACH,KAAAnB,EACA,MAAAQ,EACA,OAAAP,EACA,UAAAE,EACA,YAAAD,EACA,eAAAG,EACA,mBAAAC,EACA,aAtBiB,SAAY,CAC7B,IAAIe,EAAQ,GAEZ,OAAIf,IACSe,GAAA,MAAOf,KAA8B,OAG9Ce,GACA,MAAMnB,EAAY,EAGfmB,CACX,EAWI,wBAAAd,CACJ,CACJ"}