@volverjs/form-vue 1.0.0-beta.1 → 1.0.0-beta.3
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 +400 -358
- package/dist/index.umd.js +1 -1
- package/dist/src/VvForm.d.ts +41 -27
- package/dist/src/enums.d.ts +4 -1
- package/dist/src/index.d.ts +119 -78
- package/dist/src/types.d.ts +4 -1
- package/package.json +14 -14
- package/src/VvForm.ts +101 -62
- 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 +16 -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,u,a){const d=M(a)?a:void 0;if(!N(e)||!Z(u))return d;const i=q(u);if(i.length!==0){for(const t of i){if(t==="*")continue;const s=function(n){return n.map(f=>A(f)||L(f)?f:$(f)?s(f):f[t])};if($(e)&&!J.test(t)?e=s(e):e=e[t],A(e)||L(e))break}return A(e)?d:e}}function K(e,u,a){if(!N(e)||!Z(u))return;const d=q(u);if(d.length===0)return;const i=d.length;for(let t=0;t<i;t++){const s=d[t];if(t===i-1){e[s]=a;return}if(s==="*"&&$(e)){const n=d.slice(t+1).join(".");for(const f of e)K(f,n,a);return}A(e[s])&&(e[s]={}),e=e[s]}}function q(e){const u=e.split(/[.]|(?:\[(\d|\*)\])/).filter(a=>!!a);return u.some(a=>P.indexOf(a)!==-1)?[]:u}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,u,a,d)=>r.defineComponent({name:"VvFormField",props:{type:{type:String,validator:i=>Object.values(c).includes(i),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(i,{slots:t,emit:s}){const n=r.computed({get(){if(l!=null&&l.formData)return E(Object(l.formData.value),String(i.name))},set(o){l!=null&&l.formData&&(K(Object(l.formData.value),String(i.name),o),s("update:modelValue",{newValue:n.value,formData:l==null?void 0:l.formData}))}});r.onMounted(()=>{n.value===void 0&&i.defaultValue!==void 0&&(n.value=i.defaultValue)}),r.onBeforeUnmount(()=>{b(),V()});const f=r.inject(u,void 0);f&&f.fields.value.add(i.name);const l=r.inject(e),{props:v,name:y}=r.toRefs(i),g=r.computed(()=>{if(l!=null&&l.errors.value)return E(l.errors.value,String(i.name))}),h=r.computed(()=>{var o;return(o=g.value)==null?void 0:o._errors}),m=r.computed(()=>g.value!==void 0),b=r.watch(m,()=>{m.value?(s("invalid",h.value),f&&f.errors.value.set(i.name,{_errors:h.value})):(s("valid",n.value),f&&f.errors.value.delete(i.name))}),V=r.watch(()=>l==null?void 0:l.formData,()=>{s("update:formData",l==null?void 0:l.formData)},{deep:!0}),x=o=>{n.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??i.name,invalid:m.value,valid:i.showValid?!!(!m.value&&n.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})(i.type),invalidLabel:h.value,modelValue:n.value,"onUpdate:modelValue":x}));return r.provide(a,{name:r.readonly(y),errors:r.readonly(g)}),{component:r.computed(()=>{if(i.type===c.custom)return{render(){var o;return((o=t.default)==null?void 0:o.call(t,{modelValue:n.value,onUpdate:x,submit:l==null?void 0:l.submit,validate:l==null?void 0:l.validate,invalid:m.value,invalidLabel:h.value,formData:l==null?void 0:l.formData.value,formErrors:l==null?void 0:l.errors.value,errors:g.value}))??t.defalut}};if(!((d==null?void 0:d.lazyLoad)??i.lazyLoad)){let o;switch(i.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(i.type)),i.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:m}},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,u={})=>{const a=t=>{let s=t;for(;s instanceof O.ZodEffects;)s=s.innerType();return s instanceof O.ZodOptional&&(s=s._def.innerType),s},d=a(e);return{...(d instanceof O.ZodObject?d._def.unknownKeys==="passthrough":!1)?u:{},...Object.fromEntries(Object.entries(d.shape).map(([t,s])=>{const n=u[t];let f=a(s),l;if(f instanceof O.ZodDefault&&(l=f._def.defaultValue(),f=f._def.innerType),n===null&&f instanceof O.ZodNullable)return[t,n];if(f instanceof O.ZodSchema){const v=s.safeParse(n);if(v.success)return[t,v.data??l]}if(f instanceof O.ZodArray&&Array.isArray(n)&&n.length){const v=a(f._def.type);if(v instanceof O.ZodObject)return[t,n.map(y=>W(v,y&&typeof y=="object"?y:void 0))??l]}return f instanceof O.ZodObject?[t,W(f,n&&typeof n=="object"?n:l)]:[t,l]}))}},T=(e,u,a,d)=>{const i=r.ref(),t=r.ref(),s=r.computed(()=>t.value===w.invalid),n=r.ref(),f=async(h=n.value)=>{const m=await e.safeParseAsync(h);return m.success?(i.value=void 0,t.value=w.valid,n.value=m.data,!0):(i.value=m.error.format(),t.value=w.invalid,!1)},l=async()=>await f()?(t.value=w.submitting,!0):!1,{ignoreUpdates:v,stop:y}=G.watchIgnorable(n,()=>{t.value=w.updated},{deep:!0,eventFilter:G.throttleFilter((a==null?void 0:a.updateThrottle)??500)}),g=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}){return n.value=W(e,r.toRaw(h.modelValue)),r.watch(()=>h.modelValue,b=>{if(b){const V=r.isProxy(b)?r.toRaw(b):b;n.value=typeof(V==null?void 0:V.clone)=="function"?V.clone():JSON.parse(JSON.stringify(V))}},{deep:!0}),r.watch(t,async b=>{var V,x,k,C,S;if(b===w.invalid){const o=r.toRaw(i.value);m("invalid",o),(V=a==null?void 0:a.onInvalid)==null||V.call(a,o);return}if(b===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(b===w.submitting){const o=r.toRaw(n.value);m("submit",o),(C=a==null?void 0:a.onSubmit)==null||C.call(a,o)}if(b===w.updated){if((i.value||a!=null&&a.continuosValidation||h.continuosValidation)&&await f(),!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)}}),r.provide(u,{formData:n,submit:l,validate:f,ignoreUpdates:v,stopUpdatesWatch:y,errors:r.readonly(i),status:r.readonly(t),invalid:s}),{formData:n,submit:l,validate:f,ignoreUpdates:v,stopUpdatesWatch:y,errors:r.readonly(i),status:r.readonly(t),invalid:s}},render(){const h=()=>{var m,b;return((b=(m=this.$slots)==null?void 0:m.default)==null?void 0:b.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:i,status:t,invalid:s,formData:n,validate:f,submit:l,ignoreUpdates:v,stopUpdatesWatch:y,VvForm:g}},H=(e,u)=>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:i}){const t=r.inject(e),s=r.inject(u,void 0),n=r.ref(new Set),f=r.ref(new Map),{name:l}=r.toRefs(d);r.provide(u,{name:r.readonly(l),errors:f,fields:n}),r.watch(n,y=>{s!=null&&s.fields&&y.forEach(g=>{s==null||s.fields.value.add(g)})},{deep:!0}),r.watch(()=>new Map(f.value),(y,g)=>{s!=null&&s.errors&&(Array.from(g.keys()).forEach(h=>{s.errors.value.delete(h)}),Array.from(y.keys()).forEach(h=>{const m=y.get(h);m&&s.errors.value.set(h,m)}))},{deep:!0});const v=r.computed(()=>t!=null&&t.invalid.value?f.value.size>0:!1);return r.watch(v,()=>{v.value?i("invalid"):i("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:n,fieldsErrors:f}},render(){var d,i;return this.tag?r.h(this.tag,null,{default:()=>{var t,s;return((s=(t=this.$slots).default)==null?void 0:s.call(t,{invalid:this.invalid,formData:this.formData,submit:this.submit,validate:this.validate,errors:this.errors,fieldsErrors:this.fieldsErrors}))??this.$slots.defalut}}):((i=(d=this.$slots).default)==null?void 0:i.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,u)=>{const a=r.defineComponent({name:"VvFormTemplate",props:{schema:{type:[Array,Function],required:!0}},setup(d,{slots:i}){const t=r.inject(e);if(t!=null&&t.formData)return()=>{var l;const s=typeof d.schema=="function"?d.schema(t):d.schema;let n;const f=s.reduce((v,y)=>{const g=typeof y=="function"?y(t):y,{vvIs:h,vvName:m,vvSlots:b,vvChildren:V,vvIf:x,vvElseIf:k,vvType:C,vvDefaultValue:S,vvShowValid:o,vvContent:I,...U}=g;if(x!==void 0){if(typeof x=="string"?n=!!E(Object(t.formData.value),x):typeof x=="function"?n=r.unref(x(t)):n=r.unref(x),!n)return v}else if(k!==void 0&&n!==void 0){if(n||(typeof k=="string"?n=!!E(Object(t.formData.value),k):typeof k=="function"?n=r.unref(k(t)):n=r.unref(k),!n))return v}else n=void 0;const R=V?r.h(a,{schema:V}):void 0;return m?(v.push(r.h(u,{name:m,is:h,type:C,defaultValue:S,showValid:o,props:U},b??R??I)),v):h?(v.push(r.h(h,U,b??R??I)),v):(V&&v.push(R),v)},[]);return f.push((l=i==null?void 0:i.default)==null?void 0:l.call(i,{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,u={})=>{const a=Symbol(),d=Symbol(),i=Symbol(),t=H(a,d),s=z(a,d,i,u),n=Q(a,s),{VvForm:f,errors:l,status:v,invalid:y,formData:g,validate:h,submit:m,ignoreUpdates:b,stopUpdatesWatch:V}=T(e,a,u,n);return{VvForm:f,VvFormWrapper:t,VvFormField:s,VvFormTemplate:n,formInjectionKey:a,formWrapperInjectionKey:d,formFieldInjectionKey:i,errors:l,status:v,invalid:y,formData:g,validate:h,submit:m,ignoreUpdates:b,stopUpdatesWatch:V}},B=Symbol(),X=e=>{let u={};return e.schema&&(u=_(e.schema,e)),{...u,install(a,{global:d=!1}={}){a.provide(B,e),d&&(a.config.globalProperties.$vvForm=e,u!=null&&u.VvForm&&a.component("VvForm",u.VvForm),u!=null&&u.VvFormWrapper&&a.component("VvFormWrapper",u.VvFormWrapper),u!=null&&u.VvFormField&&a.component("VvFormField",u.VvFormField),u!=null&&u.VvFormTemplate&&a.component("VvFormTemplate",u.VvFormTemplate))}}},Y=(e,u={})=>r.getCurrentInstance()?_(e,{...r.inject(B,{}),...u}):_(e,u),D=(e,u={})=>_(e,u);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,17 @@
|
|
|
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>;
|
|
9
|
+
invalid: import("vue").ComputedRef<boolean>;
|
|
8
10
|
formData: Ref<Partial<z.TypeOf<Schema> | undefined>>;
|
|
11
|
+
validate: (value?: Partial<z.TypeOf<Schema> | undefined>) => Promise<boolean>;
|
|
12
|
+
submit: () => Promise<boolean>;
|
|
13
|
+
ignoreUpdates: IgnoredUpdater;
|
|
14
|
+
stopUpdatesWatch: WatchStopHandle;
|
|
9
15
|
/**
|
|
10
16
|
* An hack to add types to the default slot
|
|
11
17
|
*/
|
|
@@ -16,8 +22,8 @@ export declare const defineForm: <Schema extends FormSchema>(schema: Schema, pro
|
|
|
16
22
|
$props: {
|
|
17
23
|
template?: FormTemplate<Schema> | undefined;
|
|
18
24
|
modelValue?: Record<string, any> | undefined;
|
|
19
|
-
updateThrottle?: number | undefined;
|
|
20
25
|
continuosValidation?: boolean | undefined;
|
|
26
|
+
tag?: string | undefined;
|
|
21
27
|
onInvalid?: ((...args: any[]) => any) | undefined;
|
|
22
28
|
onValid?: ((...args: any[]) => any) | undefined;
|
|
23
29
|
"onUpdate:modelValue"?: ((...args: any[]) => any) | undefined;
|
|
@@ -81,17 +87,17 @@ export declare const defineForm: <Schema extends FormSchema>(schema: Schema, pro
|
|
|
81
87
|
$emit: (event: "invalid" | "valid" | "update:modelValue" | "submit", ...args: any[]) => void;
|
|
82
88
|
$el: any;
|
|
83
89
|
$options: import("vue").ComponentOptionsBase<Readonly<import("vue").ExtractPropTypes<{
|
|
90
|
+
continuosValidation: {
|
|
91
|
+
type: BooleanConstructor;
|
|
92
|
+
default: boolean;
|
|
93
|
+
};
|
|
84
94
|
modelValue: {
|
|
85
95
|
type: ObjectConstructor;
|
|
86
96
|
default: () => {};
|
|
87
97
|
};
|
|
88
|
-
|
|
89
|
-
type:
|
|
90
|
-
default:
|
|
91
|
-
};
|
|
92
|
-
continuosValidation: {
|
|
93
|
-
type: BooleanConstructor;
|
|
94
|
-
default: boolean;
|
|
98
|
+
tag: {
|
|
99
|
+
type: StringConstructor;
|
|
100
|
+
default: string;
|
|
95
101
|
};
|
|
96
102
|
template: {
|
|
97
103
|
type: PropType<FormTemplate<Schema>>;
|
|
@@ -106,14 +112,16 @@ export declare const defineForm: <Schema extends FormSchema>(schema: Schema, pro
|
|
|
106
112
|
formData: Ref<Partial<z.TypeOf<Schema> | undefined>>;
|
|
107
113
|
submit: () => Promise<boolean>;
|
|
108
114
|
validate: (value?: Partial<z.TypeOf<Schema> | undefined>) => Promise<boolean>;
|
|
115
|
+
ignoreUpdates: IgnoredUpdater;
|
|
116
|
+
stopUpdatesWatch: WatchStopHandle;
|
|
109
117
|
errors: Readonly<Ref<DeepReadonly<z.inferFormattedError<Schema, string>> | undefined>>;
|
|
110
118
|
status: Readonly<Ref<FormStatus | undefined>>;
|
|
111
119
|
invalid: import("vue").ComputedRef<boolean>;
|
|
112
120
|
}, unknown, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, ("invalid" | "valid" | "update:modelValue" | "submit")[], string, {
|
|
113
121
|
template: FormTemplate<Schema>;
|
|
114
122
|
modelValue: Record<string, any>;
|
|
115
|
-
updateThrottle: number;
|
|
116
123
|
continuosValidation: boolean;
|
|
124
|
+
tag: string;
|
|
117
125
|
}, {}, string, {}> & {
|
|
118
126
|
beforeCreate?: ((() => void) | (() => void)[]) | undefined;
|
|
119
127
|
created?: ((() => void) | (() => void)[]) | undefined;
|
|
@@ -133,19 +141,19 @@ export declare const defineForm: <Schema extends FormSchema>(schema: Schema, pro
|
|
|
133
141
|
};
|
|
134
142
|
$forceUpdate: () => void;
|
|
135
143
|
$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):
|
|
144
|
+
$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
145
|
} & Readonly<import("vue").ExtractPropTypes<{
|
|
146
|
+
continuosValidation: {
|
|
147
|
+
type: BooleanConstructor;
|
|
148
|
+
default: boolean;
|
|
149
|
+
};
|
|
138
150
|
modelValue: {
|
|
139
151
|
type: ObjectConstructor;
|
|
140
152
|
default: () => {};
|
|
141
153
|
};
|
|
142
|
-
|
|
143
|
-
type:
|
|
144
|
-
default:
|
|
145
|
-
};
|
|
146
|
-
continuosValidation: {
|
|
147
|
-
type: BooleanConstructor;
|
|
148
|
-
default: boolean;
|
|
154
|
+
tag: {
|
|
155
|
+
type: StringConstructor;
|
|
156
|
+
default: string;
|
|
149
157
|
};
|
|
150
158
|
template: {
|
|
151
159
|
type: PropType<FormTemplate<Schema>>;
|
|
@@ -160,6 +168,8 @@ export declare const defineForm: <Schema extends FormSchema>(schema: Schema, pro
|
|
|
160
168
|
formData: Ref<Partial<z.TypeOf<Schema> | undefined>>;
|
|
161
169
|
submit: () => Promise<boolean>;
|
|
162
170
|
validate: (value?: Partial<z.TypeOf<Schema> | undefined>) => Promise<boolean>;
|
|
171
|
+
ignoreUpdates: IgnoredUpdater;
|
|
172
|
+
stopUpdatesWatch: WatchStopHandle;
|
|
163
173
|
errors: Readonly<Ref<DeepReadonly<z.inferFormattedError<Schema, string>> | undefined>>;
|
|
164
174
|
status: Readonly<Ref<FormStatus | undefined>>;
|
|
165
175
|
invalid: import("vue").ComputedRef<boolean>;
|
|
@@ -168,17 +178,17 @@ export declare const defineForm: <Schema extends FormSchema>(schema: Schema, pro
|
|
|
168
178
|
__isTeleport?: undefined;
|
|
169
179
|
__isSuspense?: undefined;
|
|
170
180
|
} & import("vue").ComponentOptionsBase<Readonly<import("vue").ExtractPropTypes<{
|
|
181
|
+
continuosValidation: {
|
|
182
|
+
type: BooleanConstructor;
|
|
183
|
+
default: boolean;
|
|
184
|
+
};
|
|
171
185
|
modelValue: {
|
|
172
186
|
type: ObjectConstructor;
|
|
173
187
|
default: () => {};
|
|
174
188
|
};
|
|
175
|
-
|
|
176
|
-
type:
|
|
177
|
-
default:
|
|
178
|
-
};
|
|
179
|
-
continuosValidation: {
|
|
180
|
-
type: BooleanConstructor;
|
|
181
|
-
default: boolean;
|
|
189
|
+
tag: {
|
|
190
|
+
type: StringConstructor;
|
|
191
|
+
default: string;
|
|
182
192
|
};
|
|
183
193
|
template: {
|
|
184
194
|
type: PropType<FormTemplate<Schema>>;
|
|
@@ -193,20 +203,24 @@ export declare const defineForm: <Schema extends FormSchema>(schema: Schema, pro
|
|
|
193
203
|
formData: Ref<Partial<z.TypeOf<Schema> | undefined>>;
|
|
194
204
|
submit: () => Promise<boolean>;
|
|
195
205
|
validate: (value?: Partial<z.TypeOf<Schema> | undefined>) => Promise<boolean>;
|
|
206
|
+
ignoreUpdates: IgnoredUpdater;
|
|
207
|
+
stopUpdatesWatch: WatchStopHandle;
|
|
196
208
|
errors: Readonly<Ref<DeepReadonly<z.inferFormattedError<Schema, string>> | undefined>>;
|
|
197
209
|
status: Readonly<Ref<FormStatus | undefined>>;
|
|
198
210
|
invalid: import("vue").ComputedRef<boolean>;
|
|
199
211
|
}, unknown, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, ("invalid" | "valid" | "update:modelValue" | "submit")[], "invalid" | "valid" | "update:modelValue" | "submit", {
|
|
200
212
|
template: FormTemplate<Schema>;
|
|
201
213
|
modelValue: Record<string, any>;
|
|
202
|
-
updateThrottle: number;
|
|
203
214
|
continuosValidation: boolean;
|
|
215
|
+
tag: string;
|
|
204
216
|
}, {}, string, {}> & import("vue").VNodeProps & import("vue").AllowedComponentProps & import("vue").ComponentCustomProps & (new () => {
|
|
205
217
|
$slots: {
|
|
206
218
|
default: (_: {
|
|
207
219
|
formData: unknown extends Partial<z.TypeOf<Schema>> | undefined ? undefined : Partial<z.TypeOf<Schema>> | undefined;
|
|
208
220
|
submit: () => Promise<boolean>;
|
|
209
221
|
validate: () => Promise<boolean>;
|
|
222
|
+
ignoreUpdates: IgnoredUpdater;
|
|
223
|
+
stopUpdatesWatch: WatchStopHandle;
|
|
210
224
|
errors: Readonly<Ref<DeepReadonly<z.inferFormattedError<Schema, string>>>>;
|
|
211
225
|
status: Ref<DeepReadonly<`${FormStatus}` | undefined>>;
|
|
212
226
|
invalid: Ref<DeepReadonly<boolean>>;
|