@duely/react-form
Advanced tools
Comparing version 0.4.4 to 0.4.5
@@ -1,2 +0,2 @@ | ||
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e(require("react")):"function"==typeof define&&define.amd?define(["react"],e):"object"==typeof exports?exports["@duely/react-form"]=e(require("react")):t["@duely/react-form"]=e(t.react)}(this,(function(t){return(()=>{"use strict";var e={297:e=>{e.exports=t}},s={};function i(t){var a=s[t];if(void 0!==a)return a.exports;var r=s[t]={exports:{}};return e[t](r,r.exports,i),r.exports}i.d=(t,e)=>{for(var s in e)i.o(e,s)&&!i.o(t,s)&&Object.defineProperty(t,s,{enumerable:!0,get:e[s]})},i.o=(t,e)=>Object.prototype.hasOwnProperty.call(t,e),i.r=t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})};var a={};return(()=>{i.r(a),i.d(a,{FormControl:()=>Y,FormFieldControl:()=>P,ValidationRules:()=>N,defaultGetElementValue:()=>T,defaultSetElementValue:()=>C,formFieldInfo:()=>D,useForm:()=>Z,useRerender:()=>e});var t=i(297);function e(){const[,e]=(0,t.useState)(0);return(0,t.useCallback)((()=>e((t=>t+1))),[])}var s,r,n,o,h,u,l,f,d,p,c,v,m,w,y,b,V,g,k,U,E,M,W,F,x=function(t,e,s,i,a){if("m"===i)throw new TypeError("Private method is not writable");if("a"===i&&!a)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof e?t!==e||!a:!e.has(t))throw new TypeError("Cannot write private member to an object whose class did not declare it");return"a"===i?a.call(t,s):a?a.value=s:e.set(t,s),s},S=function(t,e,s,i){if("a"===s&&!i)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof e?t!==e||!i:!e.has(t))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===s?i:"a"===s?i.call(t):i?i.value:e.get(t)};const O="Required";function T(t){return t.value}function C(t,e){return e=null!=e?e:"",t.value!==e&&(t.value=e,!0)}const D={checkbox:{props:{type:"checkbox"},getElementValue:t=>t.checked,setElementValue:(t,e)=>(e=null!=e&&e,t.checked!==e&&(t.checked=e,!0))},color:{props:{type:"color"}},date:{props:{type:"date"}},"datetime-local":{props:{type:"datetime-local"}},email:{props:{type:"email"}},file:{props:{type:"file"},shouldValidateOnChange:!0,getElementValue:t=>t.files},month:{props:{type:"month"}},number:{props:{type:"number"}},password:{props:{type:"password"}},radio:{props:{type:"radio"},shouldValidateOnChange:!0,getElementValue:t=>{var e;const s=null===(e=t.form)||void 0===e?void 0:e.elements[t.name];if(s)return s.value},setElementValue:(t,e)=>{var s;const i=null===(s=t.form)||void 0===s?void 0:s.elements[t.name];if(!i)return!1;const a=i;return e=null!=e?e:"",a.value!==e&&(""===e?Array.from(a).map((t=>t)).filter((t=>t.checked)).forEach((t=>t.checked=!1)):a.value=e,!0)}},range:{props:{type:"range"}},search:{props:{type:"search"}},tel:{props:{type:"tel"}},text:{props:{type:"text"}},time:{props:{type:"time"}},url:{props:{type:"url"}},week:{props:{type:"week"}},"select-one":{props:{},shouldValidateOnChange:!0},"select-multiple":{props:{},shouldValidateOnChange:!0},textarea:{props:{}}},j=Object.entries(D).filter((([,t])=>t.shouldValidateOnChange)).map((([t])=>t));class P{constructor(t,e){s.add(this),r.set(this,void 0),n.set(this,void 0),o.set(this,void 0),h.set(this,void 0),u.set(this,void 0),l.set(this,void 0),f.set(this,void 0),d.set(this,void 0),p.set(this,null),c.set(this,!1),v.set(this,!1),m.set(this,!1),w.set(this,[]),y.set(this,!1),b.set(this,[]),V.set(this,0),g.set(this,void 0),x(this,r,t,"f"),x(this,n,e,"f"),x(this,g,{},"f")}get props(){return S(this,h,"f")||x(this,h,{name:S(this,r,"f"),ref:t=>{t?S(this,s,"m",W).call(this,t):S(this,s,"m",F).call(this)},onChange:t=>{S(this,s,"m",E).call(this,t)},onBlur:t=>{S(this,s,"m",M).call(this,t)}},"f"),S(this,h,"f")}get type(){var t;return null===(t=S(this,o,"f"))||void 0===t?void 0:t.type}get value(){if(void 0!==S(this,o,"f"))return null!==S(this,o,"f")&&x(this,u,S(this,f,"f")(S(this,o,"f")),"f"),S(this,u,"f")}get defaultValue(){return S(this,l,"f")}get options(){return S(this,g,"f")}set options(t){S(this,g,"f")!==t&&(this.startUpdate(),void 0!==t.defaultValue&&this.setDefaultValue(t.defaultValue),x(this,g,t,"f"),this.endUpdate())}get isDirty(){return S(this,c,"f")}set isDirty(t){var e;S(this,c,"f")!==t&&(this.startUpdate(),x(this,c,t,"f"),x(this,y,!0,"f"),this.endUpdate(),(e=S(this,n,"f")).isDirty||(e.isDirty=t))}get isTouched(){return S(this,v,"f")}set isTouched(t){S(this,v,"f")!==t&&(this.startUpdate(),x(this,v,t,"f"),x(this,y,!0,"f"),this.endUpdate())}get error(){return S(this,p,"f")}set error(t){S(this,p,"f")!==t&&(this.startUpdate(),x(this,p,t,"f"),x(this,y,!0,"f"),this.endUpdate())}startUpdate(){x(this,V,+S(this,V,"f")+1,"f")}endUpdate(){0===x(this,V,+S(this,V,"f")-1,"f")&&(S(this,m,"f")&&(x(this,m,!1,"f"),S(this,w,"f").forEach((t=>t()))),S(this,y,"f")&&(x(this,y,!1,"f"),S(this,b,"f").forEach((t=>t()))))}setValue(t){S(this,o,"f")&&(this.startUpdate(),x(this,m,S(this,m,"f")||S(this,d,"f")(S(this,o,"f"),t),"f"),S(this,m,"f")&&S(this,s,"a",U)&&this.validate(),this.endUpdate())}setDefaultValue(t){x(this,l,t,"f"),S(this,o,"f")&&!S(this,s,"a",k)&&this.setValue(this.defaultValue)}subscribeToValueChanged(t){return S(this,w,"f").push(t),()=>{const e=S(this,w,"f").indexOf(t);-1!==e&&S(this,w,"f").splice(e,1)}}subscribeToStateChanged(t){return S(this,b,"f").push(t),()=>{const e=S(this,b,"f").indexOf(t);-1!==e&&S(this,b,"f").splice(e,1)}}validate(){var t;if(this.startUpdate(),this.options.required&&!S(this,s,"a",k))return this.error=O,this.endUpdate(),!1;if(S(this,o,"f")&&S(this,s,"a",k))for(const e of null!==(t=S(this,g,"f").rules)&&void 0!==t?t:[]){const t=e(this.value,S(this,o,"f"));if(t)return this.error=t,this.endUpdate(),!1}return this.error=null,this.endUpdate(),!0}reset(t){this.startUpdate(),x(this,p,null,"f"),x(this,c,!1,"f"),x(this,v,!1,"f"),void 0!==t&&this.setDefaultValue(t),S(this,o,"f")&&S(this,d,"f")(S(this,o,"f"),this.defaultValue),x(this,m,!0,"f"),x(this,y,!0,"f"),this.endUpdate()}focus(){var t;null===(t=S(this,o,"f"))||void 0===t||t.focus()}}r=new WeakMap,n=new WeakMap,o=new WeakMap,h=new WeakMap,u=new WeakMap,l=new WeakMap,f=new WeakMap,d=new WeakMap,p=new WeakMap,c=new WeakMap,v=new WeakMap,m=new WeakMap,w=new WeakMap,y=new WeakMap,b=new WeakMap,V=new WeakMap,g=new WeakMap,s=new WeakSet,k=function(){return![void 0,""].includes(this.value)},U=function(){return!!S(this,v,"f")||!(!S(this,o,"f")||!j.includes(S(this,o,"f").type))},E=function(t){this.startUpdate(),this.isDirty=!0,x(this,m,!0,"f"),S(this,s,"a",U)&&this.validate(),this.endUpdate()},M=function(t){this.startUpdate(),this.isTouched=!0,this.isDirty&&this.validate(),this.endUpdate()},W=function(t){var e,s;this.startUpdate(),x(this,o,t,"f"),x(this,f,null!==(e=D[t.type].getElementValue)&&void 0!==e?e:T,"f"),x(this,d,null!==(s=D[t.type].setElementValue)&&void 0!==s?s:C,"f"),void 0!==this.defaultValue&&this.setValue(this.defaultValue),void 0!==this.value&&x(this,m,!0,"f"),this.endUpdate()},F=function(){x(this,o,null,"f")};var B,$,q,z,R,G,_,A,I,K,H,J,L,N,Q=function(t,e,s,i,a){if("m"===i)throw new TypeError("Private method is not writable");if("a"===i&&!a)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof e?t!==e||!a:!e.has(t))throw new TypeError("Cannot write private member to an object whose class did not declare it");return"a"===i?a.call(t,s):a?a.value=s:e.set(t,s),s},X=function(t,e,s,i){if("a"===s&&!i)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof e?t!==e||!i:!e.has(t))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===s?i:"a"===s?i.call(t):i?i.value:e.get(t)};class Y{constructor(t){var e;B.add(this),$.set(this,!1),q.set(this,!1),z.set(this,!1),R.set(this,[]),G.set(this,!1),_.set(this,[]),A.set(this,0),I.set(this,void 0),K.set(this,{}),Q(this,I,null!==(e=null==t?void 0:t.defaultValues)&&void 0!==e?e:{},"f")}get value(){return Object.fromEntries(Object.entries(X(this,K,"f")).map((([t,e])=>[t,e.value])))}get isDirty(){return X(this,q,"f")}set isDirty(t){X(this,q,"f")!==t&&(this.startUpdate(),Q(this,q,t,"f"),Q(this,G,!0,"f"),this.endUpdate())}get isSubmitting(){return X(this,$,"f")}startUpdate(){Q(this,A,+X(this,A,"f")+1,"f")}endUpdate(){0===Q(this,A,+X(this,A,"f")-1,"f")&&(X(this,z,"f")&&(Q(this,z,!1,"f"),X(this,R,"f").forEach((t=>t()))),X(this,G,"f")&&(Q(this,G,!1,"f"),X(this,_,"f").forEach((t=>t()))))}register(t,e){return X(this,B,"m",H).call(this,t,e).props}useFormState(){const s=e();return(0,t.useEffect)((()=>X(this,B,"m",L).call(this,s)),[]),this}useFormFieldState(s){const i=e(),a=X(this,B,"m",H).call(this,s);return(0,t.useEffect)((()=>a.subscribeToStateChanged(i)),[]),a}useFormFieldValue(s){const i=e(),a=X(this,B,"m",H).call(this,s);return(0,t.useEffect)((()=>a.subscribeToValueChanged(i)),[]),a.value}useFormValue(){const s=e();return(0,t.useEffect)((()=>X(this,B,"m",J).call(this,s)),[]),this.value}reset(t){this.startUpdate(),t&&Q(this,I,t,"f"),Object.entries(X(this,K,"f")).forEach((([e,s])=>s.reset(null==t?void 0:t[e]))),this.isDirty=!1,this.endUpdate()}handleSubmit(t){const e=this;return s=>{return i=this,a=void 0,n=function*(){null==s||s.preventDefault();const i={};for(const[t,s]of Object.entries(X(e,K,"f"))){if(!s.validate())return void s.focus();i[t]=s.value}this.startUpdate(),Q(this,$,!0,"f"),Q(this,G,!0,"f"),this.endUpdate(),yield t(i,s),this.startUpdate(),Q(this,$,!1,"f"),Q(this,q,!1,"f"),Object.values(X(this,K,"f")).forEach((t=>t.isDirty=!1)),Q(this,G,!0,"f"),this.endUpdate()},new((r=void 0)||(r=Promise))((function(t,e){function s(t){try{h(n.next(t))}catch(t){e(t)}}function o(t){try{h(n.throw(t))}catch(t){e(t)}}function h(e){var i;e.done?t(e.value):(i=e.value,i instanceof r?i:new r((function(t){t(i)}))).then(s,o)}h((n=n.apply(i,a||[])).next())}));var i,a,r,n}}setValue(t,e){X(this,B,"m",H).call(this,t).setValue(e)}setDefaultValue(t,e){X(this,B,"m",H).call(this,t).setDefaultValue(e)}}function Z(e){const s=(0,t.useState)((()=>new Y(e)))[0];return(0,t.useMemo)((()=>({control:s,register:s.register.bind(s),useFormFieldState:s.useFormFieldState.bind(s),useFormFieldValue:s.useFormFieldValue.bind(s),useFormValue:s.useFormValue.bind(s),useFormState:s.useFormState.bind(s),reset:s.reset.bind(s),handleSubmit:s.handleSubmit.bind(s),setValue:s.setValue.bind(s),setDefaultValue:s.setDefaultValue.bind(s)})),[])}$=new WeakMap,q=new WeakMap,z=new WeakMap,R=new WeakMap,G=new WeakMap,_=new WeakMap,A=new WeakMap,I=new WeakMap,K=new WeakMap,B=new WeakSet,H=function(t,e){let s=X(this,K,"f")[t];const i=X(this,I,"f")[t];return e&&void 0===e.defaultValue&&void 0!==i&&(e.defaultValue=i),void 0===s&&(s=new P(t,this),X(this,K,"f")[t]=s,s.subscribeToValueChanged((()=>{this.startUpdate(),Q(this,z,!0,"f"),this.endUpdate()}))),e&&(s.options=e),s},J=function(t){return X(this,R,"f").push(t),()=>{const e=X(this,R,"f").indexOf(t);-1!==e&&X(this,R,"f").splice(e,1)}},L=function(t){return X(this,_,"f").push(t),()=>{const e=X(this,_,"f").indexOf(t);-1!==e&&X(this,_,"f").splice(e,1)}},function(t){t.maxFileSize=function(t){return e=>{var s,i;return(null===(s=null==e?void 0:e[0])||void 0===s?void 0:s.size)>t?`Maximum image size is ${i=t,i<1e3?`${i.toFixed(0)}B`:(i/=1e3)<1e3?`${i.toPrecision(3)}KB`:(i/=1e3)<1e3?`${i.toPrecision(3)}MB`:(i/=1e3)<1e3?`${i.toPrecision(3)}GB`:void 0}.`:void 0}}}(N||(N={}))})(),a})()})); | ||
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e(require("react")):"function"==typeof define&&define.amd?define(["react"],e):"object"==typeof exports?exports["@duely/react-form"]=e(require("react")):t["@duely/react-form"]=e(t.react)}(this,(function(t){return(()=>{"use strict";var e={297:e=>{e.exports=t}},i={};function s(t){var a=i[t];if(void 0!==a)return a.exports;var r=i[t]={exports:{}};return e[t](r,r.exports,s),r.exports}s.d=(t,e)=>{for(var i in e)s.o(e,i)&&!s.o(t,i)&&Object.defineProperty(t,i,{enumerable:!0,get:e[i]})},s.o=(t,e)=>Object.prototype.hasOwnProperty.call(t,e),s.r=t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})};var a={};return(()=>{s.r(a),s.d(a,{FormControl:()=>tt,FormFieldControl:()=>B,InputFilters:()=>X,ValidationRules:()=>Q,defaultGetElementValue:()=>D,defaultSetElementValue:()=>C,formFieldInfo:()=>j,useForm:()=>et,useRerender:()=>e});var t=s(297);function e(){const[,e]=(0,t.useState)(0);return(0,t.useCallback)((()=>e((t=>t+1))),[])}var i,r,n,o,h,u,l,f,d,p,c,v,m,w,y,b,V,g,k,U,E,M,F,W,x,S=function(t,e,i,s,a){if("m"===s)throw new TypeError("Private method is not writable");if("a"===s&&!a)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof e?t!==e||!a:!e.has(t))throw new TypeError("Cannot write private member to an object whose class did not declare it");return"a"===s?a.call(t,i):a?a.value=i:e.set(t,i),i},O=function(t,e,i,s){if("a"===i&&!s)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof e?t!==e||!s:!e.has(t))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===i?s:"a"===i?s.call(t):s?s.value:e.get(t)};const T="Required";function D(t){return t.value}function C(t,e){return e=null!=e?e:"",t.value!==e&&(t.value=e,!0)}const j={checkbox:{props:{type:"checkbox"},getElementValue:t=>t.checked,setElementValue:(t,e)=>(e=null!=e&&e,t.checked!==e&&(t.checked=e,!0))},color:{props:{type:"color"}},date:{props:{type:"date"}},"datetime-local":{props:{type:"datetime-local"}},email:{props:{type:"email"}},file:{props:{type:"file"},shouldValidateOnChange:!0,getElementValue:t=>t.files},month:{props:{type:"month"}},number:{props:{type:"number"}},password:{props:{type:"password"}},radio:{props:{type:"radio"},shouldValidateOnChange:!0,getElementValue:t=>{var e;const i=null===(e=t.form)||void 0===e?void 0:e.elements[t.name];if(i)return i.value},setElementValue:(t,e)=>{var i;const s=null===(i=t.form)||void 0===i?void 0:i.elements[t.name];if(!s)return!1;const a=s;return e=null!=e?e:"",a.value!==e&&(""===e?Array.from(a).map((t=>t)).filter((t=>t.checked)).forEach((t=>t.checked=!1)):a.value=e,!0)}},range:{props:{type:"range"}},search:{props:{type:"search"}},tel:{props:{type:"tel"}},text:{props:{type:"text"}},time:{props:{type:"time"}},url:{props:{type:"url"}},week:{props:{type:"week"}},"select-one":{props:{},shouldValidateOnChange:!0},"select-multiple":{props:{},shouldValidateOnChange:!0},textarea:{props:{}}},P=Object.entries(j).filter((([,t])=>t.shouldValidateOnChange)).map((([t])=>t));class B{constructor(t,e){i.add(this),r.set(this,void 0),n.set(this,void 0),o.set(this,void 0),h.set(this,void 0),u.set(this,void 0),l.set(this,void 0),f.set(this,void 0),d.set(this,void 0),p.set(this,null),c.set(this,!1),v.set(this,!1),m.set(this,!1),w.set(this,[]),y.set(this,!1),b.set(this,[]),V.set(this,0),g.set(this,void 0),S(this,r,t,"f"),S(this,n,e,"f"),S(this,g,{},"f")}get props(){return O(this,h,"f")||S(this,h,{name:O(this,r,"f"),ref:t=>{t?O(this,i,"m",W).call(this,t):O(this,i,"m",x).call(this)},onBeforeInput:t=>{O(this,i,"m",E).call(this,t)},onChange:t=>{O(this,i,"m",M).call(this,t)},onBlur:t=>{O(this,i,"m",F).call(this,t)}},"f"),O(this,h,"f")}get type(){var t;return null===(t=O(this,o,"f"))||void 0===t?void 0:t.type}get value(){if(void 0!==O(this,o,"f"))return null!==O(this,o,"f")&&S(this,u,O(this,f,"f")(O(this,o,"f")),"f"),O(this,u,"f")}get defaultValue(){return O(this,l,"f")}get options(){return O(this,g,"f")}set options(t){O(this,g,"f")!==t&&(this.startUpdate(),void 0!==t.defaultValue&&this.setDefaultValue(t.defaultValue),S(this,g,t,"f"),this.endUpdate())}get isDirty(){return O(this,c,"f")}set isDirty(t){var e;O(this,c,"f")!==t&&(this.startUpdate(),S(this,c,t,"f"),S(this,y,!0,"f"),this.endUpdate(),(e=O(this,n,"f")).isDirty||(e.isDirty=t))}get isTouched(){return O(this,v,"f")}set isTouched(t){O(this,v,"f")!==t&&(this.startUpdate(),S(this,v,t,"f"),S(this,y,!0,"f"),this.endUpdate())}get error(){return O(this,p,"f")}set error(t){O(this,p,"f")!==t&&(this.startUpdate(),S(this,p,t,"f"),S(this,y,!0,"f"),this.endUpdate())}startUpdate(){S(this,V,+O(this,V,"f")+1,"f")}endUpdate(){0===S(this,V,+O(this,V,"f")-1,"f")&&(O(this,m,"f")&&(S(this,m,!1,"f"),O(this,w,"f").forEach((t=>t()))),O(this,y,"f")&&(S(this,y,!1,"f"),O(this,b,"f").forEach((t=>t()))))}setValue(t){O(this,o,"f")&&(this.startUpdate(),S(this,m,O(this,m,"f")||O(this,d,"f")(O(this,o,"f"),t),"f"),O(this,m,"f")&&O(this,i,"a",U)&&this.validate(),this.endUpdate())}setDefaultValue(t){S(this,l,t,"f"),O(this,o,"f")&&!O(this,i,"a",k)&&this.setValue(this.defaultValue)}subscribeToValueChanged(t){return O(this,w,"f").push(t),()=>{const e=O(this,w,"f").indexOf(t);-1!==e&&O(this,w,"f").splice(e,1)}}subscribeToStateChanged(t){return O(this,b,"f").push(t),()=>{const e=O(this,b,"f").indexOf(t);-1!==e&&O(this,b,"f").splice(e,1)}}validate(){var t;if(this.startUpdate(),this.options.required&&!O(this,i,"a",k))return this.error=T,this.endUpdate(),!1;if(O(this,o,"f")&&O(this,i,"a",k))for(const e of null!==(t=O(this,g,"f").rules)&&void 0!==t?t:[]){const t=e(this.value,O(this,o,"f"));if(t)return this.error=t,this.endUpdate(),!1}return this.error=null,this.endUpdate(),!0}reset(t){this.startUpdate(),S(this,p,null,"f"),S(this,c,!1,"f"),S(this,v,!1,"f"),void 0!==t&&this.setDefaultValue(t),O(this,o,"f")&&O(this,d,"f")(O(this,o,"f"),this.defaultValue),S(this,m,!0,"f"),S(this,y,!0,"f"),this.endUpdate()}focus(){var t;null===(t=O(this,o,"f"))||void 0===t||t.focus()}}r=new WeakMap,n=new WeakMap,o=new WeakMap,h=new WeakMap,u=new WeakMap,l=new WeakMap,f=new WeakMap,d=new WeakMap,p=new WeakMap,c=new WeakMap,v=new WeakMap,m=new WeakMap,w=new WeakMap,y=new WeakMap,b=new WeakMap,V=new WeakMap,g=new WeakMap,i=new WeakSet,k=function(){return![void 0,""].includes(this.value)},U=function(){return!!O(this,v,"f")||!(!O(this,o,"f")||!P.includes(O(this,o,"f").type))},E=function(t){var e;const i=t.nativeEvent;O(this,g,"f").inputFilter&&i.data&&(null===(e=i.data.match(O(this,g,"f").inputFilter))||void 0===e?void 0:e[0].length)!==i.data.length&&t.preventDefault()},M=function(t){this.startUpdate(),this.isDirty=!0,S(this,m,!0,"f"),O(this,i,"a",U)&&this.validate(),this.endUpdate()},F=function(t){this.startUpdate(),this.isTouched=!0,this.isDirty&&this.validate(),this.endUpdate()},W=function(t){var e,i;this.startUpdate(),S(this,o,t,"f"),S(this,f,null!==(e=j[t.type].getElementValue)&&void 0!==e?e:D,"f"),S(this,d,null!==(i=j[t.type].setElementValue)&&void 0!==i?i:C,"f"),void 0!==this.defaultValue&&this.setValue(this.defaultValue),void 0!==this.value&&S(this,m,!0,"f"),this.endUpdate()},x=function(){S(this,o,null,"f")};var N,$,q,z,I,R,G,_,A,K,H,J,L,Q,X,Y=function(t,e,i,s,a){if("m"===s)throw new TypeError("Private method is not writable");if("a"===s&&!a)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof e?t!==e||!a:!e.has(t))throw new TypeError("Cannot write private member to an object whose class did not declare it");return"a"===s?a.call(t,i):a?a.value=i:e.set(t,i),i},Z=function(t,e,i,s){if("a"===i&&!s)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof e?t!==e||!s:!e.has(t))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===i?s:"a"===i?s.call(t):s?s.value:e.get(t)};class tt{constructor(t){var e;N.add(this),$.set(this,!1),q.set(this,!1),z.set(this,!1),I.set(this,[]),R.set(this,!1),G.set(this,[]),_.set(this,0),A.set(this,void 0),K.set(this,{}),Y(this,A,null!==(e=null==t?void 0:t.defaultValues)&&void 0!==e?e:{},"f")}get value(){return Object.fromEntries(Object.entries(Z(this,K,"f")).map((([t,e])=>[t,e.value])))}get isDirty(){return Z(this,q,"f")}set isDirty(t){Z(this,q,"f")!==t&&(this.startUpdate(),Y(this,q,t,"f"),Y(this,R,!0,"f"),this.endUpdate())}get isSubmitting(){return Z(this,$,"f")}startUpdate(){Y(this,_,+Z(this,_,"f")+1,"f")}endUpdate(){0===Y(this,_,+Z(this,_,"f")-1,"f")&&(Z(this,z,"f")&&(Y(this,z,!1,"f"),Z(this,I,"f").forEach((t=>t()))),Z(this,R,"f")&&(Y(this,R,!1,"f"),Z(this,G,"f").forEach((t=>t()))))}register(t,e){return Z(this,N,"m",H).call(this,t,e).props}useFormState(){const i=e();return(0,t.useEffect)((()=>Z(this,N,"m",L).call(this,i)),[]),this}useFormFieldState(i){const s=e(),a=Z(this,N,"m",H).call(this,i);return(0,t.useEffect)((()=>a.subscribeToStateChanged(s)),[]),a}useFormFieldValue(i){const s=e(),a=Z(this,N,"m",H).call(this,i);return(0,t.useEffect)((()=>a.subscribeToValueChanged(s)),[]),a.value}useFormValue(){const i=e();return(0,t.useEffect)((()=>Z(this,N,"m",J).call(this,i)),[]),this.value}reset(t){this.startUpdate(),t&&Y(this,A,t,"f"),Object.entries(Z(this,K,"f")).forEach((([e,i])=>i.reset(null==t?void 0:t[e]))),this.isDirty=!1,this.endUpdate()}handleSubmit(t){const e=this;return i=>{return s=this,a=void 0,n=function*(){null==i||i.preventDefault();const s={};for(const[t,i]of Object.entries(Z(e,K,"f"))){if(!i.validate())return void i.focus();s[t]=i.value}this.startUpdate(),Y(this,$,!0,"f"),Y(this,R,!0,"f"),this.endUpdate(),yield t(s,i),this.startUpdate(),Y(this,$,!1,"f"),Y(this,q,!1,"f"),Object.values(Z(this,K,"f")).forEach((t=>t.isDirty=!1)),Y(this,R,!0,"f"),this.endUpdate()},new((r=void 0)||(r=Promise))((function(t,e){function i(t){try{h(n.next(t))}catch(t){e(t)}}function o(t){try{h(n.throw(t))}catch(t){e(t)}}function h(e){var s;e.done?t(e.value):(s=e.value,s instanceof r?s:new r((function(t){t(s)}))).then(i,o)}h((n=n.apply(s,a||[])).next())}));var s,a,r,n}}setValue(t,e){Z(this,N,"m",H).call(this,t).setValue(e)}setDefaultValue(t,e){Z(this,N,"m",H).call(this,t).setDefaultValue(e)}}function et(e){const i=(0,t.useState)((()=>new tt(e)))[0];return(0,t.useMemo)((()=>({control:i,register:i.register.bind(i),useFormFieldState:i.useFormFieldState.bind(i),useFormFieldValue:i.useFormFieldValue.bind(i),useFormValue:i.useFormValue.bind(i),useFormState:i.useFormState.bind(i),reset:i.reset.bind(i),handleSubmit:i.handleSubmit.bind(i),setValue:i.setValue.bind(i),setDefaultValue:i.setDefaultValue.bind(i)})),[])}$=new WeakMap,q=new WeakMap,z=new WeakMap,I=new WeakMap,R=new WeakMap,G=new WeakMap,_=new WeakMap,A=new WeakMap,K=new WeakMap,N=new WeakSet,H=function(t,e){let i=Z(this,K,"f")[t];const s=Z(this,A,"f")[t];return e&&void 0===e.defaultValue&&void 0!==s&&(e.defaultValue=s),void 0===i&&(i=new B(t,this),Z(this,K,"f")[t]=i,i.subscribeToValueChanged((()=>{this.startUpdate(),Y(this,z,!0,"f"),this.endUpdate()}))),e&&(i.options=e),i},J=function(t){return Z(this,I,"f").push(t),()=>{const e=Z(this,I,"f").indexOf(t);-1!==e&&Z(this,I,"f").splice(e,1)}},L=function(t){return Z(this,G,"f").push(t),()=>{const e=Z(this,G,"f").indexOf(t);-1!==e&&Z(this,G,"f").splice(e,1)}},function(t){t.maxFileSize=function(t){return e=>{var i,s;return(null===(i=null==e?void 0:e[0])||void 0===i?void 0:i.size)>t?`Maximum image size is ${s=t,s<1e3?`${s.toFixed(0)}B`:(s/=1e3)<1e3?`${s.toPrecision(3)}KB`:(s/=1e3)<1e3?`${s.toPrecision(3)}MB`:(s/=1e3)<1e3?`${s.toPrecision(3)}GB`:void 0}.`:void 0}},t.isNumber=function(t){return isNaN(+t)?"Value is not a valid number":void 0},t.isPositiveNumber=function(t){return isNaN(+t)||+t<0?"Value is not a positive number":void 0}}(Q||(Q={})),function(t){t.integer=/\d*/,t.numeric=/[-0-9.,]*/}(X||(X={}))})(),a})()})); | ||
//# sourceMappingURL=index.js.map |
@@ -17,2 +17,3 @@ /// <reference types="react" /> | ||
ref(el: import("./FormFieldControl").FormFieldHTMLElement): void; | ||
onBeforeInput(event: import("react").FormEvent<import("./FormFieldControl").FormFieldHTMLElement>): void; | ||
onChange(event: import("react").ChangeEvent<import("./FormFieldControl").FormFieldHTMLElement>): void; | ||
@@ -19,0 +20,0 @@ onBlur(event: import("react").FocusEvent<import("./FormFieldControl").FormFieldHTMLElement>): void; |
@@ -1,2 +0,2 @@ | ||
import { ChangeEvent, FocusEvent } from 'react'; | ||
import { ChangeEvent, FocusEvent, FormEvent } from 'react'; | ||
import { FormControl } from './FormControl'; | ||
@@ -7,2 +7,3 @@ export declare type FormFieldRegisterOptions<T> = { | ||
rules?: ((value: T, element: FormFieldHTMLElement) => string | undefined)[]; | ||
inputFilter?: RegExp; | ||
}; | ||
@@ -24,2 +25,3 @@ export declare function defaultGetElementValue(element: FormFieldHTMLElement): any; | ||
ref(el: FormFieldHTMLElement): void; | ||
onBeforeInput(event: FormEvent<FormFieldHTMLElement>): void; | ||
onChange(event: ChangeEvent<FormFieldHTMLElement>): void; | ||
@@ -26,0 +28,0 @@ onBlur(event: FocusEvent<FormFieldHTMLElement>): void; |
@@ -0,1 +1,2 @@ | ||
import { FormEvent } from 'react'; | ||
import { ChangeEvent, FocusEvent } from 'react'; | ||
@@ -9,2 +10,3 @@ import { FormControl } from '../FormControl'; | ||
ref(el: FormFieldHTMLElement | null): void; | ||
onBeforeInput(event: FormEvent<FormFieldHTMLElement>): void; | ||
onChange(event: ChangeEvent<FormFieldHTMLElement>): void; | ||
@@ -11,0 +13,0 @@ onBlur(event: FocusEvent<FormFieldHTMLElement>): void; |
@@ -5,1 +5,2 @@ export * from './hooks'; | ||
export * from './validationRules'; | ||
export * from './inputFilters'; |
export declare namespace ValidationRules { | ||
function maxFileSize(bytes: number): (fileList: FileList) => string | undefined; | ||
function isNumber(value: string): "Value is not a valid number" | undefined; | ||
function isPositiveNumber(value: string): "Value is not a positive number" | undefined; | ||
} |
{ | ||
"name": "@duely/react-form", | ||
"version": "0.4.4", | ||
"version": "0.4.5", | ||
"description": "React useFrom hook for duely.app", | ||
@@ -5,0 +5,0 @@ "repository": "github:uoleevi/duely", |
@@ -5,1 +5,2 @@ export * from './hooks'; | ||
export * from './validationRules'; | ||
export * from './inputFilters'; |
@@ -19,2 +19,10 @@ function formatFileSize(size: number) { | ||
} | ||
export function isNumber(value: string) { | ||
return isNaN(+value) ? 'Value is not a valid number' : undefined; | ||
} | ||
export function isPositiveNumber(value: string) { | ||
return isNaN(+value) || +value < 0 ? 'Value is not a positive number' : undefined; | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
81207
22
923