@verifiedinc-public/shared-ui-elements 9.15.0 → 9.15.1
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.
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";import{useState as
|
|
1
|
+
"use strict";import{useState as A,useCallback as i,useContext as T,createContext as K}from"react";import{jsx as N}from"react/jsx-runtime";const x=K(null),W=()=>{const m=T(x);if(!m)throw new Error("useForm must be used within a FormProvider");return m},L=({children:m,form:O,onSubmit:h})=>{const[n,c]=A({form:O,isSubmitting:!1,isSubmitSuccess:!1}),s=i(e=>{var t;if(!n.form)return;const r=e.split(".");if(r.length===1){const a=r[0],w=n.form.fields[a];if(w)return w;for(const g of Object.values(n.form.fields))if(g.children){for(const j of Object.values(g.children))if(j.id===a)return j}return}const[o,...l]=r,f=n.form.fields[o];if(!(f!=null&&f.children))return;let u=f;for(const a of l){if(!((t=u?.children)!=null&&t[a]))return;u=u.children[a]}return u},[n.form]),b=i(()=>n.form?n.form.isValid:!1,[n.form]),y=i(e=>{c(t=>({...t,form:e}))},[]),F=i(e=>{if(!n.form)return null;const t=e.split(".");if(t.length<2)return null;const r=t.slice(0,-1).join("."),o=t[t.length-1],l=s(r);if(!l||l.schema.characteristics.inputType!=="composite")throw new Error(`Parent field not found: ${r}`);return{parentField:l,childKey:o}},[n.form,s]),S=i(e=>{if(!e.children)return{};const t={};return Object.entries(e.children).forEach(([r,o])=>{t[r]=o.value}),t},[]),p=i(e=>{const t=F(e);if(t){const{parentField:r}=t,o=S(r);r.value=Object.keys(o).length>0?o:{};const l=e.split(".");if(l.length>2){const f=l.slice(0,-1).join(".");p(f)}}},[F,S]),E=i((e,t)=>{const r=s(e);if(!r){console.warn(`Attempted to update non-existent field: ${e}`);return}r.value!==t&&(r.value=t,p(e),c(o=>o.form?{...o,form:o.form}:o))},[s,p]),V=i((e,t)=>{const r=s(e);if(!r){console.warn(`Attempted to set touched state for non-existent field: ${e}`);return}r.touched!==t&&(r.touched=t,c(o=>o.form?{...o,form:o.form}:o))},[s]),P=i(()=>{c(e=>{if(!e.form)return e;const t=r=>{r.value=r.defaultValue,r.touched=!1,r.children&&Object.values(r.children).forEach(t)};return Object.values(e.form.fields).forEach(t),{...e,isSubmitting:!1,isSubmitSuccess:!1}})},[]),v=i(e=>{c(t=>({...t,isSubmitting:e}))},[]),d=i(e=>{c(t=>({...t,isSubmitSuccess:e}))},[]),$=i(async()=>{if(!(!b()||!n.form)){v(!0),d(!1);try{h&&(await h(n.form),d(!0))}catch(e){console.error("Form submission failed:",e),d(!1)}finally{v(!1)}}},[n.form,h,b,v,d]),C=i((e,t)=>{c(r=>{if(!r.form)return console.warn("No form instance available"),r;const o=s(e);return o?(o.replaceWithVariant(t),{...r,form:r.form}):(console.warn(`Field ${e} not found`),r)})},[s]),k={state:n,setForm:y,updateFieldValue:E,setFieldTouched:V,getField:s,validateForm:b,resetForm:P,submitForm:$,replaceFieldWithVariant:C};return N(x.Provider,{value:k,children:m})};export{L as FormProvider,W as useForm};
|