form-test-victor
Version:
# Performant forms and wizard library for React
3 lines (2 loc) • 13.5 kB
JavaScript
import e,{useContext as n,createContext as t,useState as r,useRef as u,useCallback as o,useEffect as i,useMemo as c}from"react";var a,s={formInternal:{registerField:function(){},unregisterField:function(){},addValueSubscriber:function(){},removeValueSubscriber:function(){},addValidationStatusSubscriber:function(){},removeValidationStatusSubscriber:function(){},handleOnChange:function(){},handleOnBlur:function(){},getFormValuesForNames:function(){return{}},getFormErrorsForNames:function(){return{}},updateValidationStatus:function(){}},getFormValues:function(){return{}},resetForm:function(){},setFormValues:function(){}},l=/*#__PURE__*/t(s);function f(){return n(l)}function d(e){var n=!1,t=!1;return Object.values(e).forEach(function(e){e.status===a.UNDETERMINED?t=!0:e.status===a.INVALID&&(n=!0)}),n?a.INVALID:t?a.UNDETERMINED:a.VALID}!function(e){e.VALID="VALID",e.INVALID="INVALID",e.UNDETERMINED="UNDETERMINED"}(a||(a={}));var v,h={status:a.VALID,message:void 0},g={status:a.UNDETERMINED,message:void 0},m=/*#__PURE__*/t({registerRule:function(){},unregisterRule:function(){}});function p(n){var t=n.name,s=n.children,l=n.render,d=n.data,v=f().formInternal,p=v.registerField,S=v.unregisterField,E=v.handleOnChange,N=v.handleOnBlur,V=v.updateValidationStatus,O=function(){var e=r([]),n=e[0],t=e[1],u=r([]),i=u[0],a=u[1],s=o(function(e,n,r){r?a(function(t){return[].concat(t,[{validatorFn:e,errorMessage:n}])}):t(function(t){return[].concat(t,[{validatorFn:e,errorMessage:n}])})},[]),l=o(function(e,n){n?a(function(n){return n.filter(function(n){return n.validatorFn!==e})}):t(function(n){return n.filter(function(n){return n.validatorFn!==e})})},[]),f=c(function(){return{registerRule:s,unregisterRule:l}},[s,l]);return c(function(){return{ruleContext:f,rules:n,debouncedRules:i}},[i,f,n])}(),b=O.ruleContext,R=O.rules,F=O.debouncedRules,I=r(void 0),P=I[0],C=I[1],D=r(!0),A=D[0],w=D[1],L=function(e,n,t){var c=e.length+n.length,s=r(c?g:h),l=s[0],f=s[1],d=u({countRulesResolved:0,phaseId:0,hasError:!1}),v=o(function(e,n,t){e.forEach(function(e){var r=e.errorMessage;d.current.hasError||Promise.resolve((0,e.validatorFn)(n)).then(function(e){if(t===d.current.phaseId){if(d.current.countRulesResolved+=1,!e&&!d.current.hasError)throw new Error(r);e&&d.current.countRulesResolved===c&&f(h)}}).catch(function(e){var n={status:a.INVALID,message:e.message};d.current.hasError=!0,f(n)})})},[c]);return i(function(){if(!c)return f(h),function(){};f(g);var r=d.current;r.phaseId+=1,r.countRulesResolved=0,r.hasError=!1;var u=r.phaseId;v(e,t,u);var o=setTimeout(function(){v(n,t,u)},500);return function(){return clearTimeout(o)}},[n,c,e,v,t]),l}(R,F,P),_=u(!1);i(function(){return p(t,C),function(){return S(t)}},[t,p,S]),i(function(){A&&_.current&&w(!1)},[A,P]),i(function(){V(t,L)},[t,V,L]);var y=o(function(e){E(t,e,_.current)},[E,t]),T=o(function(){_.current=!1,N(t,d)},[N,t,d]),U=o(function(){_.current=!0},[]);/*#__PURE__*/return e.createElement(m.Provider,{value:b},l({name:t,value:P,onFocus:U,onBlur:T,onChange:y},{isPristine:A,validationStatus:L}),s)}function S(e,n){return function(){var t=[].slice.call(arguments);e&&e.apply(void 0,t),n.apply(void 0,t)}}function E(){return E=Object.assign||function(e){for(var n=1;n<arguments.length;n++){var t=arguments[n];for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r])}return e},E.apply(this,arguments)}function N(n){/*#__PURE__*/return e.createElement(p,E({},n,{render:function(){return null}}))}function V(e){var t=e.message,r=e.children,u=e.validationFn,o=e.isDebounced,c=void 0!==o&&o,a=n(m),s=a.registerRule,l=a.unregisterRule;return i(function(){return s(u,t,c),function(){return l(u,c)}},[c,t,s,l,u]),r}function O(e){var n,t,r=(void 0===e?{}:e).onUpdateAfterBlur,i=u({errors:{},values:{}}).current,s=u({}).current,l=u(),f=o(function(e){Object.keys(s).forEach(function(n){e(s[n])})},[s]),d=u(((n={})[v.ON_CHANGE]={global:new Set,scoped:{}},n[v.ON_BLUR]={global:new Set,scoped:{}},n)).current,h=u(((t={})[v.ON_CHANGE]={global:new Set,scoped:{}},t[v.ON_BLUR]={global:new Set,scoped:{}},t)).current,m=o(function(e){var n=e||Object.keys(s),t={};return n.forEach(function(e){var n;null!=(n=s[e])&&n.isRegistered&&(t[e]=s[e].value)}),t},[s]),p=o(function(e){var n=e||Object.keys(s),t={};return n.forEach(function(e){var n;null!=(n=s[e])&&n.isRegistered&&(t[e]=s[e].validation)}),t},[s]),S=o(function(){return m()},[m]),N=o(function(e,n){d[n].scoped[e]&&d[n].scoped[e].forEach(function(n){n(function(n){var t;return E({},n,((t={})[e]=s[e].isRegistered?s[e].value:void 0,t))})}),d[n].global.size&&(clearTimeout(i.values[n]),i.values[n]=setTimeout(function(){var e=S();d[n].global.forEach(function(n){n(e)})},0))},[d,s,S,i]),V=o(function(e,n){h[n].scoped[e]&&h[n].scoped[e].forEach(function(n){n(function(n){var t=n;return s[e].isRegistered?t[e]=s[e].validation:delete t[e],t})}),h[n].global.size&&(clearTimeout(i.errors[n]),i.errors[n]=setTimeout(function(){var e=p();h[n].global.forEach(function(n){n(e)})},0))},[h,s,p,i]),O=o(function(e){N(e,v.ON_CHANGE),N(e,v.ON_BLUR)},[N]),b=o(function(e){V(e,v.ON_CHANGE),V(e,v.ON_BLUR)},[V]),R=o(function(e){s[e].isRegistered=!1,O(e),b(e)},[s,O,b]),F=o(function(e,n,t,r){var u=t;r?r.forEach(function(t){u[n].scoped[t]||(u[n].scoped[t]=new Set),u[n].scoped[t].add(e)}):u[n].global.add(e)},[]),I=o(function(e,n,t){F(e,n,d,t),e(m(t))},[F,d,m]),P=o(function(e,n){F(e,v.ON_CHANGE,h,n),e(p(n))},[F,h,p]),C=o(function(e,n){var t,r,u=null==(t=s[e])?void 0:t.value;if(null!=(r=s[e])&&r.isRegistered)throw new Error('Attempting to register field "'+e+'" a second time');s[e]={name:e,isRegistered:!0,value:u,validation:g},I(function(t){var r="function"==typeof t?t({}):t;n(null==r?void 0:r[e])},v.ON_CHANGE,[e]),O(e)},[s,I,O]),D=o(function(e,n,t,r){r?r.forEach(function(r){t[n].scoped[r].delete(e)}):t[n].global.delete(e)},[]),A=o(function(e,n,t){D(e,n,d,t)},[d,D]),w=o(function(e,n){D(e,v.ON_CHANGE,h,n)},[h,D]),L=o(function(e,n){void 0===n&&(n=!1),n&&f(function(e){var n=e.name;s[n].value=void 0,O(n)}),Object.keys(e).forEach(function(n){s[n]?s[n].value=e[n]:s[n]={name:n,isRegistered:!1,value:e[n],validation:g},O(n)})},[f,s,O]),_=o(function(e,n,t){s[e].value!==n&&(t&&(l.current=e),s[e].value=n,N(e,v.ON_CHANGE))},[s,N]),y=o(function(e,n){void 0===n&&(n={});try{var t=function(){l.current=void 0};N(e,v.ON_BLUR);var u=function(){if(r&&l.current===e&&s[e].validation.status===a.VALID)return Promise.resolve(r(e,s[e].value,n,{getFormValues:S,setFormValues:L})).then(function(){})}();return Promise.resolve(u&&u.then?u.then(t):t())}catch(e){return Promise.reject(e)}},[N,r,s,S,L]),T=o(function(e,n){s[e].validation=n,V(e,v.ON_CHANGE)},[s,V]),U=o(function(){return L({},!0)},[L]);return c(function(){return{formInternal:{registerField:C,unregisterField:R,addValueSubscriber:I,removeValueSubscriber:A,addValidationStatusSubscriber:P,removeValidationStatusSubscriber:w,handleOnChange:_,handleOnBlur:y,getFormValuesForNames:m,getFormErrorsForNames:p,updateValidationStatus:T},getFormValues:S,resetForm:U,setFormValues:L}},[C,R,I,A,P,w,_,y,m,p,T,S,U,L])}function b(e,n,t){var u=f(),o=(t||u).formInternal,c=o.addValueSubscriber,a=o.removeValueSubscriber,l=r({}),d=l[0],v=l[1];if(!t&&u===s)throw new Error('No form context could be found while calling "useValues".');return i(function(){return c(v,e,n),function(){return a(v,e,n)}},[c,a,e,null==n?void 0:n.join()]),d}function R(e,n){return b(v.ON_CHANGE,e,n)}function F(e,n){return b(v.ON_BLUR,e,n)}function I(e,n){var t=f(),u=(n||t).formInternal,o=u.addValidationStatusSubscriber,c=u.removeValidationStatusSubscriber,a=r({}),l=a[0],d=a[1];if(!n&&t===s)throw new Error('No form context could be found while calling "useValidations".');return i(function(){return o(d,e),function(){return c(d,e)}},[o,null==e?void 0:e.join(),c]),l}function P(e){var n=I(void 0,e),t=r(a.UNDETERMINED),u=t[0],o=t[1];return i(function(){o(d(n))},[n]),c(function(){return{formStatus:u,isFormValid:u===a.VALID}},[u])}function C(e,n){var t=f().setFormValues,r=R([e],n)[e];return c(function(){return{name:e,value:r,setValue:function(r){var u,o;return n?n.setFormValues(((u={})[e]=r,u)):t(((o={})[e]=r,o))}}},[e,r,n,t])}function D(n){/*#__PURE__*/return e.createElement(l.Provider,{value:n.form},n.children)}p.defaultProps={children:null},N.defaultProps={children:null},V.defaultProps={children:null,isDebounced:!1},function(e){e.ON_CHANGE="ON_CHANGE",e.ON_BLUR="ON_BLUR"}(v||(v={}));var A={steps:[],currentStep:void 0,registerStep:function(){},unregisterStep:function(){},handleOnNext:function(){return Promise.resolve()},handleOnPrevious:function(){},updatePlaceholderContent:function(){},resetPlaceholderContent:function(){},registerPlaceholder:function(){},unregisterPlaceholder:function(){},isLastStep:!1,isFirstStep:!1,hasNoFooter:!0,stepStatusSetter:function(){},isStepReady:!1,stepsTitles:[],setIsStepReady:function(){},setValuesGetterForCurrentStep:function(){},getValuesOfCurrentStep:function(){},getValuesOfStep:function(){},getValuesOfSteps:function(){return{}}},w=/*#__PURE__*/t(A);function L(){return n(w)}function _(e,n){var t=u({}),i=u(function(){}),a=r(),s=a[0],l=a[1],f=r([]),d=f[0],v=f[1],h=u({}),g=u(new Set),m=r(!1),p=m[0],S=m[1],N=u(new Set),V=u(new Set),O=u([]),b=function(e){return e-1>=0},R=function(e,n){return e+1<=n.length-1},F=o(function(){if(s){var e=i.current();e&&(t.current[s]=e)}},[s]),I=o(function(e){return t.current[e]},[]),P=o(function(){return s?I(s):void 0},[s,I]),C=o(function(){return t.current},[]),D=o(function(e,n,t,r){v(function(n){if(-1!==n.indexOf(e))throw new Error('Attempting to register step "'+e+'" a second time');return 0===n.length&&l(e),[].concat(n,[e])}),n&&(h.current[e]=n),t&&g.current.add(e),O.current=[].concat(O.current,[{name:e,title:r}])},[l]),A=o(function(e){v(function(n){var t,r=n.indexOf(e);if(-1===r)throw new Error("No step "+e+" to be unregistered was found.");return b(r)?t=n[r-1]:R(r,n)&&(t=n[r+1]),l(t),[].concat(n.slice(0,r),n.slice(r+1))}),delete h.current[e],g.current.delete(e),O.current=O.current.filter(function(n){return n.name!==e})},[l]),w=o(function(){try{var n,r=function(r){if(n)return r;F();var u=d.indexOf(s);return R(u,d)?(S(!1),void setTimeout(function(){return l(d[u+1])},0)):Promise.resolve(e(t.current)).then(function(){})};if(!s)return Promise.resolve();var u=function(){if(h.current[s])return Promise.resolve(h.current[s](P())).then(function(e){e||(n=1)})}();return Promise.resolve(u&&u.then?u.then(r):r(u))}catch(e){return Promise.reject(e)}},[s,P,e,F,d]),L=o(function(){if(s&&s){var e=d.indexOf(s);if(b(e))return S(!1),F(),void setTimeout(function(){return l(d[e-1])},0);n()}},[s,n,F,d]),_=o(function(e,n){N.current.add(e),V.current.add(n)},[N,V]),y=o(function(e,n){N.current.delete(e),V.current.delete(n)},[N,V]),T=o(function(e,n){N.current.forEach(function(t){t(function(t){var r;return E({},t,((r={})[e]=n,r))})})},[N]),U=o(function(e){e?T(e,void 0):N.current.forEach(function(e){e({})})},[T,N]),G=o(function(e){i.current=e},[]),x=o(function(e){V.current.forEach(function(n){n(e)})},[]);return c(function(){return Object.defineProperties({steps:d,currentStep:s,registerStep:D,unregisterStep:A,handleOnNext:w,handleOnPrevious:L,updatePlaceholderContent:T,resetPlaceholderContent:U,registerPlaceholder:_,unregisterPlaceholder:y,stepStatusSetter:x,isStepReady:p,setIsStepReady:S,setValuesGetterForCurrentStep:G,getValuesOfCurrentStep:P,getValuesOfStep:I,getValuesOfSteps:C},{isLastStep:{get:function(){return!d.length||s===d[d.length-1]}},isFirstStep:{get:function(){return!d.length||s===d[0]}},hasNoFooter:{get:function(){return s&&g.current.has(s)}},stepsTitles:{get:function(){return O.current}}})},[d,s,D,A,w,L,T,U,_,y,x,p,G,P,I,C])}function y(e){var n=O(e),t=L(),r=t.stepStatusSetter,c=t.setValuesGetterForCurrentStep,s=t.getValuesOfCurrentStep,l=u(!1),f=n.formInternal.addValidationStatusSubscriber,v=n.getFormValues,h=n.setFormValues;return i(function(){r(a.UNDETERMINED);var e=function(e){r(d(e))};return setTimeout(function(){return f(e)},0),function(){c(function(){}),r(a.VALID)}},[r,c,f]),i(function(){c(v)},[v,c]),i(function(){var e=s();e&&(l.current=!0,h(e))},[s,h]),{initFormValues:o(function(e){l.current||h(e)},[h]),form:n}}function T(){var e=L(),n=e.handleOnNext,t=e.handleOnPrevious,u=e.registerPlaceholder,c=e.unregisterPlaceholder,s=r({}),l=s[0],f=s[1],d=r(!1),v=d[0],h=d[1],g=r(a.VALID),m=g[0],p=g[1];return i(function(){return u(f,p),function(){return c(f,p)}},[u,c,f,p]),{handleOnNext:o(function(){try{return h(!0),Promise.resolve(n()).then(function(){h(!1)})}catch(e){return Promise.reject(e)}},[n]),handleOnPrevious:t,placeholderContent:l,isLoading:v,stepStatus:m}}function U(e){var n=e.placement,t=e.children,r=L(),u=r.updatePlaceholderContent,o=r.resetPlaceholderContent;return i(function(){return u(n,t),function(){return o(n)}},[u,n,t,o]),null}function G(e){var n=e.children,t=e.name,r=e.onNext,u=e.noFooter,o=e.title,c=L(),a=c.registerStep,s=c.unregisterStep,l=c.currentStep,f=c.setIsStepReady;if(c===A)throw new Error("Step not in wizard");return i(function(){return a(t,r,u,o),function(){return s(t)}},[a,s,t,r,u,o]),i(function(){l===t&&f(!0)},[l,t,f]),l!==t?null:n}export{p as Field,D as Form,N as HiddenField,U as Placeholder,V as Rule,G as Step,a as VALIDATION_OUTCOME,w as WizardContext,S as composeEventHandlers,O as useForm,f as useFormContext,P as useFormStatus,C as useHiddenField,F as useOnBlurValues,R as useOnChangeValues,T as usePlaceholder,y as useStepForm,I as useValidations,_ as useWizard,L as useWizardContext};
//# sourceMappingURL=index.module.js.map