UNPKG

form-test-victor

Version:

# Performant forms and wizard library for React

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