@volverjs/form-vue 1.0.0-beta.1 → 1.0.0-beta.2
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/dist/index.es.js +390 -349
- package/dist/index.umd.js +1 -1
- package/dist/src/VvForm.d.ts +40 -27
- package/dist/src/enums.d.ts +4 -1
- package/dist/src/index.d.ts +116 -78
- package/dist/src/types.d.ts +4 -1
- package/package.json +14 -14
- package/src/VvForm.ts +99 -60
- package/src/VvFormField.ts +1 -1
- package/src/VvFormTemplate.ts +1 -0
- package/src/VvFormWrapper.ts +1 -1
- package/src/enums.ts +3 -0
- package/src/index.ts +14 -6
- package/src/types.ts +4 -1
package/dist/index.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(b,r){typeof exports=="object"&&typeof module<"u"?r(exports,require("vue"),require("@vueuse/core"),require("zod")):typeof define=="function"&&define.amd?define(["exports","vue","@vueuse/core","zod"],r):(b=typeof globalThis<"u"?globalThis:b||self,r(b["@volverjs/form-vue"]={},b.Vue,b.VueUseCore,b.zod))})(this,function(b,r,q,g){"use strict";function $(e){return Array.isArray(e)}function M(e){return typeof e<"u"}function L(e){return e===null}function W(e){return typeof e=="object"}function Z(e){return typeof e=="string"}function S(e){return typeof e>"u"}const J=/^[0-9]+$/,P=["__proto__","prototype","constructor"];function I(e,i,a){const f=M(a)?a:void 0;if(!W(e)||!Z(i))return f;const s=K(i);if(s.length!==0){for(const t of s){if(t==="*")continue;const l=function(o){return o.map(u=>S(u)||L(u)?u:$(u)?l(u):u[t])};if($(e)&&!J.test(t)?e=l(e):e=e[t],S(e)||L(e))break}return S(e)?f:e}}function R(e,i,a){if(!W(e)||!Z(i))return;const f=K(i);if(f.length===0)return;const s=f.length;for(let t=0;t<s;t++){const l=f[t];if(t===s-1){e[l]=a;return}if(l==="*"&&$(e)){const o=f.slice(t+1).join(".");for(const u of e)R(u,o,a);return}S(e[l])&&(e[l]={}),e=e[l]}}function K(e){const i=e.split(/[.]|(?:\[(\d|\*)\])/).filter(a=>!!a);return i.some(a=>P.indexOf(a)!==-1)?[]:i}var d=(e=>(e.text="text",e.number="number",e.email="email",e.password="password",e.tel="tel",e.url="url",e.search="search",e.date="date",e.time="time",e.datetimeLocal="datetime-local",e.month="month",e.week="week",e.color="color",e.select="select",e.checkbox="checkbox",e.radio="radio",e.textarea="textarea",e.radioGroup="radioGroup",e.checkboxGroup="checkboxGroup",e.combobox="combobox",e.custom="custom",e))(d||{}),A=(e=>(e.invalid="invalid",e.valid="valid",e))(A||{});const T=(e,i,a,f)=>r.defineComponent({name:"FieldComponent",props:{type:{type:String,validator:s=>Object.values(d).includes(s),default:d.custom},is:{type:[Object,String],default:void 0},name:{type:[String,Number,Boolean,Symbol],required:!0},props:{type:[Object,Function],default:()=>({})},showValid:{type:Boolean,default:!1},defaultValue:{type:[String,Number,Boolean,Array,Object],default:void 0},lazyLoad:{type:Boolean,default:!1}},emits:["invalid","valid","update:formData","update:modelValue"],expose:["invalid","invalidLabel","errors"],setup(s,{slots:t,emit:l}){const o=r.computed({get(){if(n!=null&&n.formData)return I(Object(n.formData.value),String(s.name))},set(m){n!=null&&n.formData&&(R(Object(n.formData.value),String(s.name),m),l("update:modelValue",{newValue:o.value,formData:n==null?void 0:n.formData}))}});r.onMounted(()=>{o.value===void 0&&s.defaultValue!==void 0&&(o.value=s.defaultValue)}),r.onBeforeUnmount(()=>{O(),x()});const u=r.inject(i,void 0);u&&u.fields.value.add(s.name);const n=r.inject(e),{props:c,name:y}=r.toRefs(s),V=r.computed(()=>{if(n!=null&&n.errors.value)return I(n.errors.value,String(s.name))}),v=r.computed(()=>{var m;return(m=V.value)==null?void 0:m._errors}),h=r.computed(()=>V.value!==void 0),O=r.watch(h,()=>{h.value?(l("invalid",v.value),u&&u.errors.value.set(s.name,{_errors:v.value})):(l("valid",o.value),u&&u.errors.value.delete(s.name))}),x=r.watch(()=>n==null?void 0:n.formData,()=>{l("update:formData",n==null?void 0:n.formData)},{deep:!0}),p=m=>{o.value=m},w=r.computed(()=>{let m=c.value;return typeof m=="function"&&(m=m(n==null?void 0:n.formData)),Object.keys(m).reduce((C,k)=>(C[k]=r.unref(m[k]),C),{})}),N=r.computed(()=>({...w.value,name:w.value.name??s.name,invalid:h.value,valid:s.showValid?!!(!h.value&&o.value):void 0,type:(m=>{if([d.text,d.number,d.email,d.password,d.tel,d.url,d.search,d.date,d.time,d.datetimeLocal,d.month,d.week,d.color].includes(m))return m})(s.type),invalidLabel:v.value,modelValue:o.value,"onUpdate:modelValue":p}));return r.provide(a,{name:r.readonly(y),errors:r.readonly(V)}),{component:r.computed(()=>{if(s.type===d.custom)return{render(){var m;return((m=t.default)==null?void 0:m.call(t,{modelValue:o.value,onUpdate:p,submit:n==null?void 0:n.submit,validate:n==null?void 0:n.validate,invalid:h.value,invalidLabel:v.value,formData:n==null?void 0:n.formData.value,formErrors:n==null?void 0:n.errors.value,errors:V.value}))??t.defalut}};if(!((f==null?void 0:f.lazyLoad)??s.lazyLoad)){let m;switch(s.type){case d.select:m=r.resolveComponent("VvSelect");break;case d.checkbox:m=r.resolveComponent("VvCheckbox");break;case d.radio:m=r.resolveComponent("VvRadio");break;case d.textarea:m=r.resolveComponent("VvTextarea");break;case d.radioGroup:m=r.resolveComponent("VvRadioGroup");break;case d.checkboxGroup:m=r.resolveComponent("VvCheckboxGroup");break;case d.combobox:m=r.resolveComponent("VvCombobox");break;default:m=r.resolveComponent("VvInputText")}if(typeof m!="string")return m;console.warn(`[form-vue warn]: ${m} not found, the component will be loaded asynchronously. To avoid this warning, please set "lazyLoad" option.`)}return r.defineAsyncComponent(async()=>{switch(f!=null&&f.sideEffects&&await Promise.resolve(f.sideEffects(s.type)),s.type){case d.textarea:return import("@volverjs/ui-vue/vv-textarea");case d.radio:return import("@volverjs/ui-vue/vv-radio");case d.radioGroup:return import("@volverjs/ui-vue/vv-radio-group");case d.checkbox:return import("@volverjs/ui-vue/vv-checkbox");case d.checkboxGroup:return import("@volverjs/ui-vue/vv-checkbox-group");case d.select:return import("@volverjs/ui-vue/vv-select");case d.combobox:return import("@volverjs/ui-vue/vv-combobox")}return import("@volverjs/ui-vue/vv-input-text")})}),hasProps:N,invalid:h}},render(){return this.is?r.h(this.is,this.hasProps,this.$slots):this.type===d.custom?r.h(this.component,null,this.$slots):r.h(this.component,this.hasProps,this.$slots)}}),E=(e,i={})=>{const a=t=>{let l=t;for(;l instanceof g.ZodEffects;)l=l.innerType();return l instanceof g.ZodOptional&&(l=l._def.innerType),l},f=a(e);return{...(f instanceof g.ZodObject?f._def.unknownKeys==="passthrough":!1)?i:{},...Object.fromEntries(Object.entries(f.shape).map(([t,l])=>{const o=i[t];let u=a(l),n;if(u instanceof g.ZodDefault&&(n=u._def.defaultValue(),u=u._def.innerType),o===null&&u instanceof g.ZodNullable)return[t,o];if(u instanceof g.ZodSchema){const c=l.safeParse(o);if(c.success)return[t,c.data??n]}if(u instanceof g.ZodArray&&Array.isArray(o)&&o.length){const c=a(u._def.type);if(c instanceof g.ZodObject)return[t,o.map(y=>E(c,y&&typeof y=="object"?y:void 0))??n]}return u instanceof g.ZodObject?[t,E(u,o&&typeof o=="object"?o:n)]:[t,n]}))}},z=(e,i,a,f)=>{const s=r.ref(),t=r.ref(),l=r.ref(),o=r.defineComponent({name:"FormComponent",props:{modelValue:{type:Object,default:()=>({})},updateThrottle:{type:Number,default:500},continuosValidation:{type:Boolean,default:!1},template:{type:[Array,Function],default:void 0}},emits:["invalid","valid","submit","update:modelValue"],expose:["submit","validate","errors","status","valid","invalid"],setup(u,{emit:n}){l.value=E(e,r.toRaw(u.modelValue)),r.watch(()=>u.modelValue,v=>{if(v){const h=r.isProxy(v)?r.toRaw(v):v;l.value=typeof(h==null?void 0:h.clone)=="function"?h.clone():JSON.parse(JSON.stringify(h))}},{deep:!0}),q.watchThrottled(l,async v=>{var h;((s.value||(a==null?void 0:a.continuosValidation))??u.continuosValidation)&&await c(),(!v||!u.modelValue||JSON.stringify(v)!==JSON.stringify(u.modelValue))&&(n("update:modelValue",v),(h=a==null?void 0:a.onUpdate)==null||h.call(a,r.toRaw(v)))},{deep:!0,throttle:(a==null?void 0:a.updateThrottle)??u.updateThrottle});const c=async(v=l.value)=>{var O,x,p;const h=await e.safeParseAsync(v);return h.success?(s.value=void 0,t.value=A.valid,l.value=h.data,n("update:modelValue",l.value),(x=a==null?void 0:a.onUpdate)==null||x.call(a,r.toRaw(l.value)),n("valid",h.data),(p=a==null?void 0:a.onValid)==null||p.call(a,r.toRaw(l.value)),!0):(s.value=h.error.format(),t.value=A.invalid,n("invalid",s.value),(O=a==null?void 0:a.onInvalid)==null||O.call(a,r.toRaw(s.value)),!1)},y=async()=>{var v;return await c()?(n("submit",l.value),(v=a==null?void 0:a.onSubmit)==null||v.call(a,r.toRaw(l.value)),!0):!1},V=r.computed(()=>t.value===A.invalid);return r.provide(i,{formData:l,submit:y,validate:c,errors:r.readonly(s),status:r.readonly(t),invalid:V}),{formData:l,submit:y,validate:c,errors:r.readonly(s),status:r.readonly(t),invalid:V}},render(){const u=()=>{var n,c;return((c=(n=this.$slots)==null?void 0:n.default)==null?void 0:c.call(n,{formData:this.formData,submit:this.submit,validate:this.validate,errors:this.errors,status:this.status,invalid:this.invalid}))??this.$slots.default};return r.h("form",{onSubmit:r.withModifiers(this.submit,["prevent"])},(this.template??(a==null?void 0:a.template))&&f?[r.h(f,{schema:this.template??(a==null?void 0:a.template)},{default:u})]:{default:u})}});return{errors:s,status:t,formData:l,VvForm:o}},H=(e,i)=>r.defineComponent({name:"WrapperComponent",props:{name:{type:String,required:!0},tag:{type:String,default:void 0}},emits:["invalid","valid"],expose:["fields","invalid"],setup(f,{emit:s}){const t=r.inject(e),l=r.inject(i,void 0),o=r.ref(new Set),u=r.ref(new Map),{name:n}=r.toRefs(f);r.provide(i,{name:r.readonly(n),errors:u,fields:o}),r.watch(o,y=>{l!=null&&l.fields&&y.forEach(V=>{l==null||l.fields.value.add(V)})},{deep:!0}),r.watch(()=>new Map(u.value),(y,V)=>{l!=null&&l.errors&&(Array.from(V.keys()).forEach(v=>{l.errors.value.delete(v)}),Array.from(y.keys()).forEach(v=>{const h=y.get(v);h&&l.errors.value.set(v,h)}))},{deep:!0});const c=r.computed(()=>t!=null&&t.invalid.value?u.value.size>0:!1);return r.watch(c,()=>{c.value?s("invalid"):s("valid")}),{formData:t==null?void 0:t.formData,errors:t==null?void 0:t.errors,submit:t==null?void 0:t.submit,validate:t==null?void 0:t.validate,invalid:c,fields:o,fieldsErrors:u}},render(){var f,s;return this.tag?r.h(this.tag,null,{default:()=>{var t,l;return((l=(t=this.$slots).default)==null?void 0:l.call(t,{invalid:this.invalid,formData:this.formData,submit:this.submit,validate:this.validate,errors:this.errors,fieldsErrors:this.fieldsErrors}))??this.$slots.defalut}}):((s=(f=this.$slots).default)==null?void 0:s.call(f,{invalid:this.invalid,formData:this.formData,submit:this.submit,validate:this.validate,errors:this.errors,fieldsErrors:this.fieldsErrors}))??this.$slots.defalut}}),Q=(e,i)=>{const a=r.defineComponent({props:{schema:{type:[Array,Function],required:!0}},setup(f,{slots:s}){const t=r.inject(e);if(t!=null&&t.formData)return()=>{var n;const l=typeof f.schema=="function"?f.schema(t):f.schema;let o;const u=l.reduce((c,y)=>{const V=typeof y=="function"?y(t):y,{vvIs:v,vvName:h,vvSlots:O,vvChildren:x,vvIf:p,vvElseIf:w,vvType:N,vvDefaultValue:U,vvShowValid:m,vvContent:C,...k}=V;if(p!==void 0){if(typeof p=="string"?o=!!I(Object(t.formData.value),p):typeof p=="function"?o=r.unref(p(t)):o=r.unref(p),!o)return c}else if(w!==void 0&&o!==void 0){if(o||(typeof w=="string"?o=!!I(Object(t.formData.value),w):typeof w=="function"?o=r.unref(w(t)):o=r.unref(w),!o))return c}else o=void 0;const G=x?r.h(a,{schema:x}):void 0;return h?(c.push(r.h(i,{name:h,is:v,type:N,defaultValue:U,showValid:m,props:k},O??G??C)),c):v?(c.push(r.h(v,k,O??G??C)),c):(x&&c.push(G),c)},[]);return u.push((n=s==null?void 0:s.default)==null?void 0:n.call(s,{formData:t==null?void 0:t.formData.value,submit:t==null?void 0:t.submit,validate:t==null?void 0:t.validate,errors:t==null?void 0:t.errors.value,status:t==null?void 0:t.status.value,invalid:t==null?void 0:t.invalid.value})),u}}});return a},_=(e,i={})=>{const a=Symbol(),f=Symbol(),s=Symbol(),t=H(a,f),l=T(a,f,s,i),o=Q(a,l),{VvForm:u,errors:n,status:c,formData:y}=z(e,a,i,o);return{VvForm:u,VvFormWrapper:t,VvFormField:l,VvFormTemplate:o,formInjectionKey:a,formWrapperInjectionKey:f,formFieldInjectionKey:s,errors:n,status:c,formData:y}},B=Symbol(),X=e=>{let i={};return e.schema&&(i=_(e.schema,e)),{...i,install(a,{global:f=!1}={}){a.provide(B,e),f&&(a.config.globalProperties.$vvForm=e,i!=null&&i.VvForm&&a.component("VvForm",i.VvForm),i!=null&&i.VvFormWrapper&&a.component("VvFormWrapper",i.VvFormWrapper),i!=null&&i.VvFormField&&a.component("VvFormField",i.VvFormField),i!=null&&i.VvFormTemplate&&a.component("VvFormTemplate",i.VvFormTemplate))}}},Y=(e,i={})=>r.getCurrentInstance()?_(e,{...r.inject(B,{}),...i}):_(e,i),D=(e,i={})=>_(e,i);b.FormFieldType=d,b.createForm=X,b.defaultObjectBySchema=E,b.formFactory=D,b.pluginInjectionKey=B,b.useForm=Y,Object.defineProperty(b,Symbol.toStringTag,{value:"Module"})});
|
|
1
|
+
(function(p,r){typeof exports=="object"&&typeof module<"u"?r(exports,require("vue"),require("@vueuse/core"),require("zod")):typeof define=="function"&&define.amd?define(["exports","vue","@vueuse/core","zod"],r):(p=typeof globalThis<"u"?globalThis:p||self,r(p["@volverjs/form-vue"]={},p.Vue,p.VueUseCore,p.zod))})(this,function(p,r,G,O){"use strict";function $(e){return Array.isArray(e)}function M(e){return typeof e<"u"}function L(e){return e===null}function N(e){return typeof e=="object"}function Z(e){return typeof e=="string"}function A(e){return typeof e>"u"}const J=/^[0-9]+$/,P=["__proto__","prototype","constructor"];function E(e,i,a){const d=M(a)?a:void 0;if(!N(e)||!Z(i))return d;const s=q(i);if(s.length!==0){for(const t of s){if(t==="*")continue;const n=function(u){return u.map(f=>A(f)||L(f)?f:$(f)?n(f):f[t])};if($(e)&&!J.test(t)?e=n(e):e=e[t],A(e)||L(e))break}return A(e)?d:e}}function K(e,i,a){if(!N(e)||!Z(i))return;const d=q(i);if(d.length===0)return;const s=d.length;for(let t=0;t<s;t++){const n=d[t];if(t===s-1){e[n]=a;return}if(n==="*"&&$(e)){const u=d.slice(t+1).join(".");for(const f of e)K(f,u,a);return}A(e[n])&&(e[n]={}),e=e[n]}}function q(e){const i=e.split(/[.]|(?:\[(\d|\*)\])/).filter(a=>!!a);return i.some(a=>P.indexOf(a)!==-1)?[]:i}var c=(e=>(e.text="text",e.number="number",e.email="email",e.password="password",e.tel="tel",e.url="url",e.search="search",e.date="date",e.time="time",e.datetimeLocal="datetime-local",e.month="month",e.week="week",e.color="color",e.select="select",e.checkbox="checkbox",e.radio="radio",e.textarea="textarea",e.radioGroup="radioGroup",e.checkboxGroup="checkboxGroup",e.combobox="combobox",e.custom="custom",e))(c||{}),w=(e=>(e.invalid="invalid",e.valid="valid",e.submitting="submitting",e.updated="updated",e.unknown="unknown",e))(w||{});const z=(e,i,a,d)=>r.defineComponent({name:"VvFormField",props:{type:{type:String,validator:s=>Object.values(c).includes(s),default:c.custom},is:{type:[Object,String],default:void 0},name:{type:[String,Number,Boolean,Symbol],required:!0},props:{type:[Object,Function],default:()=>({})},showValid:{type:Boolean,default:!1},defaultValue:{type:[String,Number,Boolean,Array,Object],default:void 0},lazyLoad:{type:Boolean,default:!1}},emits:["invalid","valid","update:formData","update:modelValue"],expose:["invalid","invalidLabel","errors"],setup(s,{slots:t,emit:n}){const u=r.computed({get(){if(l!=null&&l.formData)return E(Object(l.formData.value),String(s.name))},set(o){l!=null&&l.formData&&(K(Object(l.formData.value),String(s.name),o),n("update:modelValue",{newValue:u.value,formData:l==null?void 0:l.formData}))}});r.onMounted(()=>{u.value===void 0&&s.defaultValue!==void 0&&(u.value=s.defaultValue)}),r.onBeforeUnmount(()=>{V(),g()});const f=r.inject(i,void 0);f&&f.fields.value.add(s.name);const l=r.inject(e),{props:v,name:b}=r.toRefs(s),h=r.computed(()=>{if(l!=null&&l.errors.value)return E(l.errors.value,String(s.name))}),m=r.computed(()=>{var o;return(o=h.value)==null?void 0:o._errors}),y=r.computed(()=>h.value!==void 0),V=r.watch(y,()=>{y.value?(n("invalid",m.value),f&&f.errors.value.set(s.name,{_errors:m.value})):(n("valid",u.value),f&&f.errors.value.delete(s.name))}),g=r.watch(()=>l==null?void 0:l.formData,()=>{n("update:formData",l==null?void 0:l.formData)},{deep:!0}),x=o=>{u.value=o},k=r.computed(()=>{let o=v.value;return typeof o=="function"&&(o=o(l==null?void 0:l.formData)),Object.keys(o).reduce((I,U)=>(I[U]=r.unref(o[U]),I),{})}),C=r.computed(()=>({...k.value,name:k.value.name??s.name,invalid:y.value,valid:s.showValid?!!(!y.value&&u.value):void 0,type:(o=>{if([c.text,c.number,c.email,c.password,c.tel,c.url,c.search,c.date,c.time,c.datetimeLocal,c.month,c.week,c.color].includes(o))return o})(s.type),invalidLabel:m.value,modelValue:u.value,"onUpdate:modelValue":x}));return r.provide(a,{name:r.readonly(b),errors:r.readonly(h)}),{component:r.computed(()=>{if(s.type===c.custom)return{render(){var o;return((o=t.default)==null?void 0:o.call(t,{modelValue:u.value,onUpdate:x,submit:l==null?void 0:l.submit,validate:l==null?void 0:l.validate,invalid:y.value,invalidLabel:m.value,formData:l==null?void 0:l.formData.value,formErrors:l==null?void 0:l.errors.value,errors:h.value}))??t.defalut}};if(!((d==null?void 0:d.lazyLoad)??s.lazyLoad)){let o;switch(s.type){case c.select:o=r.resolveComponent("VvSelect");break;case c.checkbox:o=r.resolveComponent("VvCheckbox");break;case c.radio:o=r.resolveComponent("VvRadio");break;case c.textarea:o=r.resolveComponent("VvTextarea");break;case c.radioGroup:o=r.resolveComponent("VvRadioGroup");break;case c.checkboxGroup:o=r.resolveComponent("VvCheckboxGroup");break;case c.combobox:o=r.resolveComponent("VvCombobox");break;default:o=r.resolveComponent("VvInputText")}if(typeof o!="string")return o;console.warn(`[form-vue warn]: ${o} not found, the component will be loaded asynchronously. To avoid this warning, please set "lazyLoad" option.`)}return r.defineAsyncComponent(async()=>{switch(d!=null&&d.sideEffects&&await Promise.resolve(d.sideEffects(s.type)),s.type){case c.textarea:return import("@volverjs/ui-vue/vv-textarea");case c.radio:return import("@volverjs/ui-vue/vv-radio");case c.radioGroup:return import("@volverjs/ui-vue/vv-radio-group");case c.checkbox:return import("@volverjs/ui-vue/vv-checkbox");case c.checkboxGroup:return import("@volverjs/ui-vue/vv-checkbox-group");case c.select:return import("@volverjs/ui-vue/vv-select");case c.combobox:return import("@volverjs/ui-vue/vv-combobox")}return import("@volverjs/ui-vue/vv-input-text")})}),hasProps:C,invalid:y}},render(){return this.is?r.h(this.is,this.hasProps,this.$slots):this.type===c.custom?r.h(this.component,null,this.$slots):r.h(this.component,this.hasProps,this.$slots)}}),W=(e,i={})=>{const a=t=>{let n=t;for(;n instanceof O.ZodEffects;)n=n.innerType();return n instanceof O.ZodOptional&&(n=n._def.innerType),n},d=a(e);return{...(d instanceof O.ZodObject?d._def.unknownKeys==="passthrough":!1)?i:{},...Object.fromEntries(Object.entries(d.shape).map(([t,n])=>{const u=i[t];let f=a(n),l;if(f instanceof O.ZodDefault&&(l=f._def.defaultValue(),f=f._def.innerType),u===null&&f instanceof O.ZodNullable)return[t,u];if(f instanceof O.ZodSchema){const v=n.safeParse(u);if(v.success)return[t,v.data??l]}if(f instanceof O.ZodArray&&Array.isArray(u)&&u.length){const v=a(f._def.type);if(v instanceof O.ZodObject)return[t,u.map(b=>W(v,b&&typeof b=="object"?b:void 0))??l]}return f instanceof O.ZodObject?[t,W(f,u&&typeof u=="object"?u:l)]:[t,l]}))}},T=(e,i,a,d)=>{const s=r.ref(),t=r.ref(),n=r.ref(),u=async(h=n.value)=>{const m=await e.safeParseAsync(h);return m.success?(s.value=void 0,t.value=w.valid,n.value=m.data,!0):(s.value=m.error.format(),t.value=w.invalid,!1)},f=async()=>await u()?(t.value=w.submitting,!0):!1,{ignoreUpdates:l,stop:v}=G.watchIgnorable(n,()=>{t.value=w.updated},{deep:!0,eventFilter:G.throttleFilter((a==null?void 0:a.updateThrottle)??500)}),b=r.defineComponent({name:"VvForm",props:{continuosValidation:{type:Boolean,default:!1},modelValue:{type:Object,default:()=>({})},tag:{type:String,default:"form"},template:{type:[Array,Function],default:void 0}},emits:["invalid","valid","submit","update:modelValue"],expose:["submit","validate","errors","status","valid","invalid"],setup(h,{emit:m}){n.value=W(e,r.toRaw(h.modelValue)),r.watch(()=>h.modelValue,V=>{if(V){const g=r.isProxy(V)?r.toRaw(V):V;n.value=typeof(g==null?void 0:g.clone)=="function"?g.clone():JSON.parse(JSON.stringify(g))}},{deep:!0}),r.watch(t,async V=>{var g,x,k,C,S;if(V===w.invalid){const o=r.toRaw(s.value);m("invalid",o),(g=a==null?void 0:a.onInvalid)==null||g.call(a,o);return}if(V===w.valid){const o=r.toRaw(n.value);m("valid",o),(x=a==null?void 0:a.onValid)==null||x.call(a,o),m("update:modelValue",o),(k=a==null?void 0:a.onUpdate)==null||k.call(a,o);return}if(V===w.submitting){const o=r.toRaw(n.value);m("submit",o),(C=a==null?void 0:a.onSubmit)==null||C.call(a,o)}if(V===w.updated){if((s.value||a!=null&&a.continuosValidation||h.continuosValidation)&&await u(),!n.value||!h.modelValue||JSON.stringify(n.value)!==JSON.stringify(h.modelValue)){const o=r.toRaw(n.value);m("update:modelValue",o),(S=a==null?void 0:a.onUpdate)==null||S.call(a,o)}t.value===w.updated&&(t.value=w.unknown)}});const y=r.computed(()=>t.value===w.invalid);return r.provide(i,{formData:n,submit:f,validate:u,ignoreUpdates:l,stopUpdatesWatch:v,errors:r.readonly(s),status:r.readonly(t),invalid:y}),{formData:n,submit:f,validate:u,ignoreUpdates:l,stopUpdatesWatch:v,errors:r.readonly(s),status:r.readonly(t),invalid:y}},render(){const h=()=>{var m,y;return((y=(m=this.$slots)==null?void 0:m.default)==null?void 0:y.call(m,{formData:this.formData,submit:this.submit,validate:this.validate,ignoreUpdates:this.ignoreUpdates,stopUpdatesWatch:this.stopUpdatesWatch,errors:this.errors,status:this.status,invalid:this.invalid}))??this.$slots.default};return r.h(this.tag,{onSubmit:r.withModifiers(this.submit,["prevent"])},(this.template??(a==null?void 0:a.template))&&d?[r.h(d,{schema:this.template??(a==null?void 0:a.template)},{default:h})]:{default:h})}});return{errors:s,status:t,formData:n,validate:u,submit:f,ignoreUpdates:l,stopUpdatesWatch:v,VvForm:b}},H=(e,i)=>r.defineComponent({name:"VvFormWrapper",props:{name:{type:String,required:!0},tag:{type:String,default:void 0}},emits:["invalid","valid"],expose:["fields","invalid"],setup(d,{emit:s}){const t=r.inject(e),n=r.inject(i,void 0),u=r.ref(new Set),f=r.ref(new Map),{name:l}=r.toRefs(d);r.provide(i,{name:r.readonly(l),errors:f,fields:u}),r.watch(u,b=>{n!=null&&n.fields&&b.forEach(h=>{n==null||n.fields.value.add(h)})},{deep:!0}),r.watch(()=>new Map(f.value),(b,h)=>{n!=null&&n.errors&&(Array.from(h.keys()).forEach(m=>{n.errors.value.delete(m)}),Array.from(b.keys()).forEach(m=>{const y=b.get(m);y&&n.errors.value.set(m,y)}))},{deep:!0});const v=r.computed(()=>t!=null&&t.invalid.value?f.value.size>0:!1);return r.watch(v,()=>{v.value?s("invalid"):s("valid")}),{formData:t==null?void 0:t.formData,errors:t==null?void 0:t.errors,submit:t==null?void 0:t.submit,validate:t==null?void 0:t.validate,invalid:v,fields:u,fieldsErrors:f}},render(){var d,s;return this.tag?r.h(this.tag,null,{default:()=>{var t,n;return((n=(t=this.$slots).default)==null?void 0:n.call(t,{invalid:this.invalid,formData:this.formData,submit:this.submit,validate:this.validate,errors:this.errors,fieldsErrors:this.fieldsErrors}))??this.$slots.defalut}}):((s=(d=this.$slots).default)==null?void 0:s.call(d,{invalid:this.invalid,formData:this.formData,submit:this.submit,validate:this.validate,errors:this.errors,fieldsErrors:this.fieldsErrors}))??this.$slots.defalut}}),Q=(e,i)=>{const a=r.defineComponent({name:"VvFormTemplate",props:{schema:{type:[Array,Function],required:!0}},setup(d,{slots:s}){const t=r.inject(e);if(t!=null&&t.formData)return()=>{var l;const n=typeof d.schema=="function"?d.schema(t):d.schema;let u;const f=n.reduce((v,b)=>{const h=typeof b=="function"?b(t):b,{vvIs:m,vvName:y,vvSlots:V,vvChildren:g,vvIf:x,vvElseIf:k,vvType:C,vvDefaultValue:S,vvShowValid:o,vvContent:I,...U}=h;if(x!==void 0){if(typeof x=="string"?u=!!E(Object(t.formData.value),x):typeof x=="function"?u=r.unref(x(t)):u=r.unref(x),!u)return v}else if(k!==void 0&&u!==void 0){if(u||(typeof k=="string"?u=!!E(Object(t.formData.value),k):typeof k=="function"?u=r.unref(k(t)):u=r.unref(k),!u))return v}else u=void 0;const R=g?r.h(a,{schema:g}):void 0;return y?(v.push(r.h(i,{name:y,is:m,type:C,defaultValue:S,showValid:o,props:U},V??R??I)),v):m?(v.push(r.h(m,U,V??R??I)),v):(g&&v.push(R),v)},[]);return f.push((l=s==null?void 0:s.default)==null?void 0:l.call(s,{formData:t==null?void 0:t.formData.value,submit:t==null?void 0:t.submit,validate:t==null?void 0:t.validate,errors:t==null?void 0:t.errors.value,status:t==null?void 0:t.status.value,invalid:t==null?void 0:t.invalid.value})),f}}});return a},_=(e,i={})=>{const a=Symbol(),d=Symbol(),s=Symbol(),t=H(a,d),n=z(a,d,s,i),u=Q(a,n),{VvForm:f,errors:l,status:v,formData:b,validate:h,submit:m,ignoreUpdates:y,stopUpdatesWatch:V}=T(e,a,i,u);return{VvForm:f,VvFormWrapper:t,VvFormField:n,VvFormTemplate:u,formInjectionKey:a,formWrapperInjectionKey:d,formFieldInjectionKey:s,errors:l,status:v,formData:b,validate:h,submit:m,ignoreUpdates:y,stopUpdatesWatch:V}},B=Symbol(),X=e=>{let i={};return e.schema&&(i=_(e.schema,e)),{...i,install(a,{global:d=!1}={}){a.provide(B,e),d&&(a.config.globalProperties.$vvForm=e,i!=null&&i.VvForm&&a.component("VvForm",i.VvForm),i!=null&&i.VvFormWrapper&&a.component("VvFormWrapper",i.VvFormWrapper),i!=null&&i.VvFormField&&a.component("VvFormField",i.VvFormField),i!=null&&i.VvFormTemplate&&a.component("VvFormTemplate",i.VvFormTemplate))}}},Y=(e,i={})=>r.getCurrentInstance()?_(e,{...r.inject(B,{}),...i}):_(e,i),D=(e,i={})=>_(e,i);p.FormFieldType=c,p.createForm=X,p.defaultObjectBySchema=W,p.formFactory=D,p.pluginInjectionKey=B,p.useForm=Y,Object.defineProperty(p,Symbol.toStringTag,{value:"Module"})});
|
package/dist/src/VvForm.d.ts
CHANGED
|
@@ -1,11 +1,16 @@
|
|
|
1
|
-
import { type Component, type InjectionKey, type DeepReadonly, type Ref, type PropType } from 'vue';
|
|
2
|
-
import
|
|
1
|
+
import { type Component, type InjectionKey, type DeepReadonly, type Ref, type PropType, type WatchStopHandle } from 'vue';
|
|
2
|
+
import { type IgnoredUpdater } from '@vueuse/core';
|
|
3
|
+
import { type z, type TypeOf } from 'zod';
|
|
3
4
|
import type { FormComponentOptions, FormSchema, FormTemplate, InjectedFormData } from './types';
|
|
4
5
|
import { FormStatus } from './enums';
|
|
5
6
|
export declare const defineForm: <Schema extends FormSchema>(schema: Schema, provideKey: InjectionKey<InjectedFormData<Schema>>, options?: FormComponentOptions<Schema> | undefined, VvFormTemplate?: Component) => {
|
|
6
7
|
errors: Ref<z.inferFormattedError<Schema, string> | undefined>;
|
|
7
8
|
status: Ref<FormStatus | undefined>;
|
|
8
9
|
formData: Ref<Partial<z.TypeOf<Schema> | undefined>>;
|
|
10
|
+
validate: (value?: Partial<z.TypeOf<Schema> | undefined>) => Promise<boolean>;
|
|
11
|
+
submit: () => Promise<boolean>;
|
|
12
|
+
ignoreUpdates: IgnoredUpdater;
|
|
13
|
+
stopUpdatesWatch: WatchStopHandle;
|
|
9
14
|
/**
|
|
10
15
|
* An hack to add types to the default slot
|
|
11
16
|
*/
|
|
@@ -16,8 +21,8 @@ export declare const defineForm: <Schema extends FormSchema>(schema: Schema, pro
|
|
|
16
21
|
$props: {
|
|
17
22
|
template?: FormTemplate<Schema> | undefined;
|
|
18
23
|
modelValue?: Record<string, any> | undefined;
|
|
19
|
-
updateThrottle?: number | undefined;
|
|
20
24
|
continuosValidation?: boolean | undefined;
|
|
25
|
+
tag?: string | undefined;
|
|
21
26
|
onInvalid?: ((...args: any[]) => any) | undefined;
|
|
22
27
|
onValid?: ((...args: any[]) => any) | undefined;
|
|
23
28
|
"onUpdate:modelValue"?: ((...args: any[]) => any) | undefined;
|
|
@@ -81,17 +86,17 @@ export declare const defineForm: <Schema extends FormSchema>(schema: Schema, pro
|
|
|
81
86
|
$emit: (event: "invalid" | "valid" | "update:modelValue" | "submit", ...args: any[]) => void;
|
|
82
87
|
$el: any;
|
|
83
88
|
$options: import("vue").ComponentOptionsBase<Readonly<import("vue").ExtractPropTypes<{
|
|
89
|
+
continuosValidation: {
|
|
90
|
+
type: BooleanConstructor;
|
|
91
|
+
default: boolean;
|
|
92
|
+
};
|
|
84
93
|
modelValue: {
|
|
85
94
|
type: ObjectConstructor;
|
|
86
95
|
default: () => {};
|
|
87
96
|
};
|
|
88
|
-
|
|
89
|
-
type:
|
|
90
|
-
default:
|
|
91
|
-
};
|
|
92
|
-
continuosValidation: {
|
|
93
|
-
type: BooleanConstructor;
|
|
94
|
-
default: boolean;
|
|
97
|
+
tag: {
|
|
98
|
+
type: StringConstructor;
|
|
99
|
+
default: string;
|
|
95
100
|
};
|
|
96
101
|
template: {
|
|
97
102
|
type: PropType<FormTemplate<Schema>>;
|
|
@@ -106,14 +111,16 @@ export declare const defineForm: <Schema extends FormSchema>(schema: Schema, pro
|
|
|
106
111
|
formData: Ref<Partial<z.TypeOf<Schema> | undefined>>;
|
|
107
112
|
submit: () => Promise<boolean>;
|
|
108
113
|
validate: (value?: Partial<z.TypeOf<Schema> | undefined>) => Promise<boolean>;
|
|
114
|
+
ignoreUpdates: IgnoredUpdater;
|
|
115
|
+
stopUpdatesWatch: WatchStopHandle;
|
|
109
116
|
errors: Readonly<Ref<DeepReadonly<z.inferFormattedError<Schema, string>> | undefined>>;
|
|
110
117
|
status: Readonly<Ref<FormStatus | undefined>>;
|
|
111
118
|
invalid: import("vue").ComputedRef<boolean>;
|
|
112
119
|
}, unknown, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, ("invalid" | "valid" | "update:modelValue" | "submit")[], string, {
|
|
113
120
|
template: FormTemplate<Schema>;
|
|
114
121
|
modelValue: Record<string, any>;
|
|
115
|
-
updateThrottle: number;
|
|
116
122
|
continuosValidation: boolean;
|
|
123
|
+
tag: string;
|
|
117
124
|
}, {}, string, {}> & {
|
|
118
125
|
beforeCreate?: ((() => void) | (() => void)[]) | undefined;
|
|
119
126
|
created?: ((() => void) | (() => void)[]) | undefined;
|
|
@@ -133,19 +140,19 @@ export declare const defineForm: <Schema extends FormSchema>(schema: Schema, pro
|
|
|
133
140
|
};
|
|
134
141
|
$forceUpdate: () => void;
|
|
135
142
|
$nextTick: typeof import("vue").nextTick;
|
|
136
|
-
$watch<T extends string | ((...args: any) => any)>(source: T, cb: T extends (...args: any) => infer R ? (args_0: R, args_1: R) => any : (...args: any) => any, options?: import("vue").WatchOptions<boolean> | undefined):
|
|
143
|
+
$watch<T extends string | ((...args: any) => any)>(source: T, cb: T extends (...args: any) => infer R ? (args_0: R, args_1: R) => any : (...args: any) => any, options?: import("vue").WatchOptions<boolean> | undefined): WatchStopHandle;
|
|
137
144
|
} & Readonly<import("vue").ExtractPropTypes<{
|
|
145
|
+
continuosValidation: {
|
|
146
|
+
type: BooleanConstructor;
|
|
147
|
+
default: boolean;
|
|
148
|
+
};
|
|
138
149
|
modelValue: {
|
|
139
150
|
type: ObjectConstructor;
|
|
140
151
|
default: () => {};
|
|
141
152
|
};
|
|
142
|
-
|
|
143
|
-
type:
|
|
144
|
-
default:
|
|
145
|
-
};
|
|
146
|
-
continuosValidation: {
|
|
147
|
-
type: BooleanConstructor;
|
|
148
|
-
default: boolean;
|
|
153
|
+
tag: {
|
|
154
|
+
type: StringConstructor;
|
|
155
|
+
default: string;
|
|
149
156
|
};
|
|
150
157
|
template: {
|
|
151
158
|
type: PropType<FormTemplate<Schema>>;
|
|
@@ -160,6 +167,8 @@ export declare const defineForm: <Schema extends FormSchema>(schema: Schema, pro
|
|
|
160
167
|
formData: Ref<Partial<z.TypeOf<Schema> | undefined>>;
|
|
161
168
|
submit: () => Promise<boolean>;
|
|
162
169
|
validate: (value?: Partial<z.TypeOf<Schema> | undefined>) => Promise<boolean>;
|
|
170
|
+
ignoreUpdates: IgnoredUpdater;
|
|
171
|
+
stopUpdatesWatch: WatchStopHandle;
|
|
163
172
|
errors: Readonly<Ref<DeepReadonly<z.inferFormattedError<Schema, string>> | undefined>>;
|
|
164
173
|
status: Readonly<Ref<FormStatus | undefined>>;
|
|
165
174
|
invalid: import("vue").ComputedRef<boolean>;
|
|
@@ -168,17 +177,17 @@ export declare const defineForm: <Schema extends FormSchema>(schema: Schema, pro
|
|
|
168
177
|
__isTeleport?: undefined;
|
|
169
178
|
__isSuspense?: undefined;
|
|
170
179
|
} & import("vue").ComponentOptionsBase<Readonly<import("vue").ExtractPropTypes<{
|
|
180
|
+
continuosValidation: {
|
|
181
|
+
type: BooleanConstructor;
|
|
182
|
+
default: boolean;
|
|
183
|
+
};
|
|
171
184
|
modelValue: {
|
|
172
185
|
type: ObjectConstructor;
|
|
173
186
|
default: () => {};
|
|
174
187
|
};
|
|
175
|
-
|
|
176
|
-
type:
|
|
177
|
-
default:
|
|
178
|
-
};
|
|
179
|
-
continuosValidation: {
|
|
180
|
-
type: BooleanConstructor;
|
|
181
|
-
default: boolean;
|
|
188
|
+
tag: {
|
|
189
|
+
type: StringConstructor;
|
|
190
|
+
default: string;
|
|
182
191
|
};
|
|
183
192
|
template: {
|
|
184
193
|
type: PropType<FormTemplate<Schema>>;
|
|
@@ -193,20 +202,24 @@ export declare const defineForm: <Schema extends FormSchema>(schema: Schema, pro
|
|
|
193
202
|
formData: Ref<Partial<z.TypeOf<Schema> | undefined>>;
|
|
194
203
|
submit: () => Promise<boolean>;
|
|
195
204
|
validate: (value?: Partial<z.TypeOf<Schema> | undefined>) => Promise<boolean>;
|
|
205
|
+
ignoreUpdates: IgnoredUpdater;
|
|
206
|
+
stopUpdatesWatch: WatchStopHandle;
|
|
196
207
|
errors: Readonly<Ref<DeepReadonly<z.inferFormattedError<Schema, string>> | undefined>>;
|
|
197
208
|
status: Readonly<Ref<FormStatus | undefined>>;
|
|
198
209
|
invalid: import("vue").ComputedRef<boolean>;
|
|
199
210
|
}, unknown, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, ("invalid" | "valid" | "update:modelValue" | "submit")[], "invalid" | "valid" | "update:modelValue" | "submit", {
|
|
200
211
|
template: FormTemplate<Schema>;
|
|
201
212
|
modelValue: Record<string, any>;
|
|
202
|
-
updateThrottle: number;
|
|
203
213
|
continuosValidation: boolean;
|
|
214
|
+
tag: string;
|
|
204
215
|
}, {}, string, {}> & import("vue").VNodeProps & import("vue").AllowedComponentProps & import("vue").ComponentCustomProps & (new () => {
|
|
205
216
|
$slots: {
|
|
206
217
|
default: (_: {
|
|
207
218
|
formData: unknown extends Partial<z.TypeOf<Schema>> | undefined ? undefined : Partial<z.TypeOf<Schema>> | undefined;
|
|
208
219
|
submit: () => Promise<boolean>;
|
|
209
220
|
validate: () => Promise<boolean>;
|
|
221
|
+
ignoreUpdates: IgnoredUpdater;
|
|
222
|
+
stopUpdatesWatch: WatchStopHandle;
|
|
210
223
|
errors: Readonly<Ref<DeepReadonly<z.inferFormattedError<Schema, string>>>>;
|
|
211
224
|
status: Ref<DeepReadonly<`${FormStatus}` | undefined>>;
|
|
212
225
|
invalid: Ref<DeepReadonly<boolean>>;
|