UNPKG

effector-forms

Version:
3 lines (2 loc) 9.64 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("effector"),r=require("effector-react");const i={store:function({init:r,domain:i,existing:s},o){return s||(i?i.store(r,{and:o,sid:"-efg263"}):e.createStore(r,{and:o,sid:"-dyefrw"}))},event:function({domain:r,existing:i}){return i||(r?r.event({sid:"-y6riru"}):e.createEvent({sid:"-y6rhv4"}))}};function s(r,s,o){var n,t,a,d,u,l,m,c,v,f,h,g;const $="function"==typeof s.init?s.init():s.init,p=i.store({domain:o,existing:null===(n=s.units)||void 0===n?void 0:n.$value,init:$},{and:{sid:`${r}-$value`},name:"$value",sid:"-qmuyz8"}),x=i.store({domain:o,existing:null===(t=s.units)||void 0===t?void 0:t.$errors,init:[]},{and:{sid:`${r}-$errors`},name:"$errors",sid:"-vpp9qr"}),V=x.map((e=>e[0]?e[0]:null)),E=i.store({domain:o,existing:null===(a=s.units)||void 0===a?void 0:a.$initValue,init:$},{and:{sid:`${r}-$initValue`},name:"$initValue",sid:"yr2fi7"}),y=i.store({domain:o,existing:null===(d=s.units)||void 0===d?void 0:d.$isTouched,init:!1},{and:{sid:`${r}-$touched`},name:"$touched",sid:"-q2zc60"}),b=e.combine({and:[p,E,(e,r)=>e!==r],or:{name:"$isDirty",sid:"cgx1r6"}}),T=i.event({domain:o,existing:null===(u=s.units)||void 0===u?void 0:u.onChange},{name:"onChange",sid:"fko8ie"}),F=i.event({domain:o,existing:null===(l=s.units)||void 0===l?void 0:l.onBlur},{name:"onBlur",sid:"-kwhocu"}),k=i.event({domain:o,existing:null===(m=s.units)||void 0===m?void 0:m.changed},{name:"changed",sid:"svcdu2"}),w=i.event({domain:o,existing:null===(c=s.units)||void 0===c?void 0:c.addError},{name:"addError",sid:"utax3x"}),j=i.event({domain:o,existing:null===(v=s.units)||void 0===v?void 0:v.validate},{name:"validate",sid:"-al4q5y"}),q=i.event({domain:o,existing:null===(f=s.units)||void 0===f?void 0:f.resetErrors},{name:"resetErrors",sid:"-8bnwk6"}),D=i.event({domain:o,existing:null===(h=s.units)||void 0===h?void 0:h.resetValue},{name:"resetValue",sid:"-5k4ef5"}),S=i.event({domain:o,existing:null===(g=s.units)||void 0===g?void 0:g.reset},{name:"reset",sid:"-t5gx88"}),U=V.map((e=>null===e)),O=V.map((e=>(null==e?void 0:e.errorText)||"")),B=e.combine({and:[{value:p,errors:x,firstError:V,isValid:U,isDirty:b,isTouched:y}],or:{name:"$field",sid:"-3lcsvv"}}),C={value:p,initValue:E,isValid:U,isDirty:b,touched:y,errors:x,firstError:V,errorText:O,onChange:T,onBlur:F,addError:w,validate:j,reset:S,resetErrors:q,resetValue:D};return{changed:k,name:r,$initValue:E,$value:p,$errors:x,$firstError:V,$errorText:O,$isValid:U,$isDirty:b,$isTouched:y,$touched:y,$field:B,onChange:T,onBlur:F,addError:w,validate:j,set:T,reset:S,resetErrors:q,resetValue:D,filter:s.filter,"@@unitShape":()=>C}}function o(r,i){const{form:s,field:o,fieldConfig:n}=r,t=n.rules||[],a=s.validateOn||["submit"],d=n.validateOn||[],{$value:u,$errors:l,onBlur:m,changed:c,addError:v,validate:f,resetErrors:h,resetValue:g,reset:$}=o,p="function"==typeof t?e.createStore([],{and:{sid:`${o.name}-$rulesSources`},name:"rulesSources",sid:"8mhplv"}):e.combine({and:[t.map((({source:r},i)=>{const s=`${o.name}-$rulesSources-${i}`;return r||e.createStore(null,{and:{sid:s},sid:"kjwg5z"})}))],or:{name:"rulesSources",sid:"93jc02"}}),x=(V=t,(e,r,i)=>{const s=[],o="function"==typeof V?V(e,r):V;for(let n=0;n<o.length;n++){const t=o[n],a=i?i[n]:null,d=t.validator(e,r,a);"boolean"!=typeof d||d||s.push({rule:t.name,errorText:t.errorText,value:e}),"object"!=typeof d||d.isValid||s.push({rule:t.name,errorText:d.errorText,value:e})}return s});var V;const E=[...a,...d],y=[];if(E.includes("submit")){const r=e.sample({and:[{source:e.combine({and:[{fieldValue:u,form:s.$values,rulesSources:p}],or:{name:"source",sid:"a7in27"}}),clock:s.submit}],or:{name:"validationTrigger",sid:"ag2jep"}});y.push(r)}E.includes("blur")&&y.push(e.sample({and:[{source:e.combine({and:[{fieldValue:u,form:s.$values,rulesSources:p}],or:{name:"source",sid:"pcyjof"}}),clock:m}],or:{sid:"-mjjw0q"}})),E.includes("change")&&y.push(e.sample({and:[{source:e.combine({and:[{fieldValue:u,form:s.$values,rulesSources:p}],or:{name:"source",sid:"-uzr82o"}}),clock:e.merge([c,g,s.resetValues],{name:"clock",sid:"mklqro"})}],or:{sid:"-7v5lsp"}})),y.push(e.sample({and:[{source:e.combine({and:[{fieldValue:u,form:s.$values,rulesSources:p}],or:{name:"source",sid:"-gsekud"}}),clock:f}],or:{sid:"6c71fm"}})),y.push(e.sample({and:[{source:e.combine({and:[{fieldValue:u,form:s.$values,rulesSources:p}],or:{name:"source",sid:"-323jeq"}}),clock:s.validate}],or:{sid:"a4k0l6"}}));const b=e.sample({and:[{source:u,clock:v,fn:(e,{rule:r,errorText:i})=>({rule:r,value:e,errorText:i})}],or:{name:"addErrorWithValue",sid:"symbs8"}}),T=e.sample({and:[{source:u,clock:s.addErrors,fn:(e,r)=>({value:e,newErrors:r})}],or:{name:"addErrorsWithValue",sid:"-6mh24c"}});l.on(y,((e,{form:r,fieldValue:i,rulesSources:s})=>x(i,r,s))).on(b,((e,r)=>[r,...e])).on(T,((e,{value:r,newErrors:i})=>{const s=[];for(const e of i)e.field===o.name&&s.push({value:r,rule:e.rule,errorText:e.errorText});return[...s,...e]})).reset(h,s.reset,$,s.resetErrors),E.includes("change")||l.reset(c)}function n({field:r,form:i}){const{$value:s,$initValue:o,$touched:n,onChange:t,changed:a,name:d,reset:u,resetValue:l,filter:m}=r,{setForm:c,setInitialForm:v,resetForm:f,resetTouched:h,resetValues:g}=i,$=e.sample({and:[{source:o,clock:e.merge([u,l,g,f],{name:"clock",sid:"3kodws"})}],or:{name:"resetValueWithInit",sid:"oge6qb"}});n.on(a,(()=>!0)).reset(u,f,h),e.guard({and:[{source:t,filter:m||(()=>!0),target:a}],or:{sid:"-kx8mil"}}),o.on(v,((e,r)=>r.hasOwnProperty(d)?r[d]:e)),s.on(a,((e,r)=>r)).on([c,v],((e,r)=>r.hasOwnProperty(d)?r[d]:e)).on($,((e,r)=>r))}function t(e){const{value:i,errors:s,firstError:o,isValid:n,isDirty:t,isTouched:a}=r.useUnit(e.$field);return{name:e.name,value:i,errors:s,firstError:o,isValid:n,isDirty:t,touched:a,isTouched:a,onChange:r.useUnit(e.onChange),onBlur:r.useUnit(e.onBlur),addError:r.useUnit(e.addError),validate:r.useUnit(e.validate),reset:r.useUnit(e.reset),set:r.useUnit(e.onChange),resetErrors:r.useUnit(e.resetErrors),hasError:()=>null!==o,errorText:e=>o?e&&e[o.rule]?e[o.rule]:o.errorText||"":""}}exports.createForm=function(r){const{filter:t,domain:a,fields:d,validateOn:u,units:l}=r,m={},c=[],v=[];for(const e in d){if(!d.hasOwnProperty(e))continue;const r=s(e,d[e],a);m[e]=r,c.push(r.$isDirty),v.push(r.$touched)}const f=function(r){const i={};for(const e in r)r.hasOwnProperty(e)&&(i[e]=r[e].$value);return e.combine({and:[i],or:{sid:"39yu4w"}})}(m),h=function(r){const i=[];for(const e in r){if(!r.hasOwnProperty(e))continue;const{$firstError:s}=r[e];i.push(s)}return e.combine({and:[i],or:{name:"$firstErrors",sid:"-1vosn1"}}).map((e=>e.every((e=>null===e))))}(m),g=t?e.combine({and:[h,t,(e,r)=>e&&r],or:{name:"$isFormValid",sid:"j5pxai"}}):h,$=e.combine({and:[c],or:{name:"$isDirty",sid:"il8stq"}}).map((e=>e.some(Boolean))),p=e.combine({and:[v],or:{name:"$touched",sid:"2no1yv"}}).map((e=>e.some(Boolean))),x=e.combine({and:[{isValid:h,isDirty:$,touched:p}],or:{name:"$meta",sid:"yii6e7"}}),V=i.event({domain:a,existing:null==l?void 0:l.validate},{name:"validate",sid:"5z7ulc"}),E=i.event({domain:a,existing:null==l?void 0:l.submit},{name:"submitForm",sid:"-xys7v0"}),y=i.event({domain:a,existing:null==l?void 0:l.formValidated},{name:"formValidated",sid:"-2r221i"}),b=i.event({domain:a,existing:null==l?void 0:l.setInitialForm},{name:"setInitialForm",sid:"-q10ho7"}),T=i.event({domain:a,existing:null==l?void 0:l.setForm},{name:"setForm",sid:"l2bjv0"}),F=i.event({domain:a,existing:null==l?void 0:l.addErrors},{name:"addErrors",sid:"n5fjqj"}),k=i.event({domain:a,existing:null==l?void 0:l.reset},{name:"resetForm",sid:"pfltj4"}),w=i.event({domain:a,existing:null==l?void 0:l.resetValues},{name:"resetValues",sid:"-s67s76"}),j=i.event({domain:a,existing:null==l?void 0:l.resetErrors},{name:"resetErrors",sid:"hulqf7"}),q=i.event({domain:a,existing:null==l?void 0:l.resetTouched},{name:"resetTouched",sid:"2gwck3"}),D=e.sample({and:[{source:f,clock:E}],or:{name:"submitWithFormData",sid:"foyubg"}}),S=e.sample({and:[{source:f,clock:V}],or:{name:"validateWithFormData",sid:"-mn7k5o"}});for(const e in m){if(!m.hasOwnProperty(e))continue;const r=d[e],i=m[e];n({form:{setForm:T,setInitialForm:b,resetForm:k,resetTouched:q,resetValues:w},field:i}),o({form:{$values:f,submit:E,reset:k,addErrors:F,resetValues:w,resetErrors:j,validate:V,validateOn:u},fieldConfig:r,field:i})}e.guard({and:[{source:D,filter:g,target:y}],or:{sid:"4sls6r"}}),e.guard({and:[{source:S,filter:g,target:y}],or:{sid:"4vwjqx"}});const U={isValid:h,isDirty:$,touched:p,submit:E,reset:k,addErrors:F,validate:V,setForm:T,setInitialForm:b,resetTouched:q,resetValues:w,resetErrors:j,formValidated:y};return{fields:m,$values:f,$eachValid:h,$isValid:h,$isDirty:$,$touched:p,$meta:x,submit:E,validate:V,resetTouched:q,addErrors:F,reset:k,resetValues:w,resetErrors:j,setForm:T,setInitialForm:b,set:T,formValidated:y,"@@unitShape":()=>U}},exports.useField=t,exports.useForm=function(e){const i={},s={};for(const r in e.fields){if(!e.fields.hasOwnProperty(r))continue;const o=t(e.fields[r]);i[r]=o,s[r]=o.value}const{isValid:o,isDirty:n,touched:a}=r.useUnit(e.$meta);return{fields:i,values:s,hasError:e=>e?!!i[e]&&Boolean(i[e].firstError):!o,eachValid:o,isValid:o,isDirty:n,isTouched:a,touched:a,errors:e=>i[e]?i[e].errors:[],error:e=>i[e]?i[e].firstError:null,errorText:(e,r)=>{const s=i[e];return s&&s.firstError?r&&r[s.firstError.rule]?r[s.firstError.rule]:s.firstError.errorText||"":""},reset:r.useUnit(e.reset),submit:r.useUnit(e.submit),setForm:r.useUnit(e.setForm),set:r.useUnit(e.setForm),formValidated:r.useUnit(e.formValidated)}}; //# sourceMappingURL=effector-forms.cjs.js.map