@nimel/directorr-form
Version:
Form logic in directorr
2 lines (1 loc) • 7.03 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("mobx"),require("@nimel/directorr")):"function"==typeof define&&define.amd?define(["exports","mobx","@nimel/directorr"],e):e((t=t||self).index={},t.mobx,t.directorr)}(this,(function(t,e,o){"use strict";var r,n,i,a,c,s,u=function(){return(u=Object.assign||function(t){for(var e,o=1,r=arguments.length;o<r;o++)for(var n in e=arguments[o])Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n]);return t}).apply(this,arguments)};function f(t,e,o,r){var n,i=arguments.length,a=i<3?e:null===r?r=Object.getOwnPropertyDescriptor(e,o):r;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)a=Reflect.decorate(t,e,o,r);else for(var c=t.length-1;c>=0;c--)(n=t[c])&&(a=(i<3?n(a):i>3?n(e,o,a):n(e,o))||a);return i>3&&a&&Object.defineProperty(e,o,a),a}function l(t,e){if("object"==typeof Reflect&&"function"==typeof Reflect.metadata)return Reflect.metadata(t,e)}function d(t,e){var o="function"==typeof Symbol&&t[Symbol.iterator];if(!o)return t;var r,n,i=o.call(t),a=[];try{for(;(void 0===e||e-- >0)&&!(r=i.next()).done;)a.push(r.value)}catch(t){n={error:t}}finally{try{r&&!r.done&&(o=i.return)&&o.call(i)}finally{if(n)throw n.error}}return a}var p=(r=d(o.createActionAndEffect("FORM.CHANGE_VALUE"),2))[0],y=r[1],v=(n=d(o.createActionAndEffect("FORM.FOCUS"),2))[0],h=n[1],g=(i=d(o.createActionAndEffect("FORM.VISIT"),2))[0],b=i[1],m=(a=d(o.createActionAndEffect("FORM.CHANGE_STATUS"),2))[0],O=a[1],E=(c=d(o.createActionAndEffect("FORM.SUBMIT"),2))[0],S=c[1],j=(s=d(o.createActionAndEffect("FORM.RESET"),2))[0],F=s[1],T=function(){function t(t){var r=void 0===t?o.EMPTY_OBJECT:t,n=r.value,i=void 0===n?o.EMPTY_STRING:n,a=r.status,c=void 0===a?0:a,s=r.message,u=this;this.isChanged=!1,this.isVisited=!1,this.isFocused=!1,this.changeValue=function(t){return{value:t}},this.toChangeValue=function(t){var e=t.value;u.isChanged=!0,u.value=e},this.focus=function(){return u.visit(),{focus:!0}},this.blur=function(){return{focus:!1}},this.toFocus=function(t){var e=t.focus;u.isFocused=e},this.visit=function(){return u.isVisited?null:void 0},this.toVisit=function(){u.isVisited=!0},this.changeStatusToInvalid=function(t){return{status:2,message:t}},this.changeStatusToValid=function(){return{status:1}},this.toChangeStatus=function(t){var e=t.status,o=t.message;u.status=e,u.message=o},this.submit=function(){},this.reset=function(){},this.toReset=function(){u.value=u.defaultValue,u.message=u.defaultMessage,u.status=0},this.defaultValue=i,this.defaultMessage=s,this.value=i,this.status=c,this.message=s,e.makeObservable(this)}return Object.defineProperty(t.prototype,"isDefault",{get:function(){return 0===this.status},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"isValid",{get:function(){return 1===this.status},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"isInvalid",{get:function(){return 2===this.status},enumerable:!1,configurable:!0}),f([e.observable,l("design:type",String)],t.prototype,"value",void 0),f([e.observable,l("design:type",String)],t.prototype,"message",void 0),f([e.observable,l("design:type",Number)],t.prototype,"status",void 0),f([e.observable,l("design:type",Object)],t.prototype,"isChanged",void 0),f([e.observable,l("design:type",Object)],t.prototype,"isVisited",void 0),f([e.observable,l("design:type",Object)],t.prototype,"isFocused",void 0),f([e.computed,l("design:type",Object),l("design:paramtypes",[])],t.prototype,"isDefault",null),f([e.computed,l("design:type",Object),l("design:paramtypes",[])],t.prototype,"isValid",null),f([e.computed,l("design:type",Object),l("design:paramtypes",[])],t.prototype,"isInvalid",null),f([p,l("design:type",Object)],t.prototype,"changeValue",void 0),f([y,l("design:type",Object)],t.prototype,"toChangeValue",void 0),f([v,l("design:type",Object)],t.prototype,"focus",void 0),f([v,l("design:type",Object)],t.prototype,"blur",void 0),f([h,l("design:type",Object)],t.prototype,"toFocus",void 0),f([g,l("design:type",Object)],t.prototype,"visit",void 0),f([b,l("design:type",Object)],t.prototype,"toVisit",void 0),f([m,l("design:type",Object)],t.prototype,"changeStatusToInvalid",void 0),f([m,l("design:type",Object)],t.prototype,"changeStatusToValid",void 0),f([O,l("design:type",Object)],t.prototype,"toChangeStatus",void 0),f([E,l("design:type",Object)],t.prototype,"submit",void 0),f([j,l("design:type",Object)],t.prototype,"reset",void 0),f([F,l("design:type",Object)],t.prototype,"toReset",void 0),t}(),V=function(t){return"".concat(t,": use only with effect decorator")},A=function(t,e,o){return"".concat(t,": formStore in prop=").concat(e," not exist in class=").concat(o)},P=function(t,e,o){return"".concat(t,": formStore in prop=").concat(e," not like FormStore=").concat(o)},R={abortEarly:!1,strict:!0,payloadProp:"connectStoreProperty"};function w(t,e,o){if(void 0===o&&(o={}),!function(t){return!!t.fields}(e))throw new TypeError(function(t,e){return"".concat(t,": call with arg=").concat(e," not like yup - ObjectSchema")}(t,e));return[e,u(u({},R),o),Object.keys(e.fields)]}function C(t){return!!("value"in t&&t.changeStatusToInvalid&&t.changeStatusToValid)}function M(t,e,o){for(var r={},n=0,i=e.length,a=void 0,c=void 0;n<i;++n){if(!(c=o[a=e[n]]))throw new Error(A(t,a,o));if(!C(c))throw new TypeError(P(t,a,c));r[a]=c.value||void 0}return r}function I(t,e,r,n){var i=d(n,3),a=i[0],c=i[1],s=i[2];if(o.isLikeAction(t))throw new Error(V("validate"));var f=t||o.EMPTY_OBJECT,l=f[c.payloadProp];if(l){var p=M("validate",s,r);try{a.validateSyncAt(l,p,c),r[l].changeStatusToValid()}catch(t){r[l].changeStatusToInvalid(t.errors[0]),f=u(u({},f),{validationError:t})}return e(f)}}var _=o.createPropertyDecoratorFactory("validate",(function(t,e,r,n,i){if(void 0===i&&(i=I),!o.isFunction(e))throw new TypeError(o.callWithPropNotEquallFunc("validate",r));return function(o){return i(o,e,t,n)}}),w);function x(t,e,r,n){var i=d(n,3),a=i[0],c=i[1],s=i[2];if(o.isLikeAction(t))throw new Error(V("validateAll"));var f=t||o.EMPTY_OBJECT,l=M("validateAll",s,r);try{a.validateSync(l,c),s.forEach((function(t){return r[t].changeStatusToValid()}))}catch(t){t.inner.forEach((function(t){return r[t.path].changeStatusToInvalid(t.message)})),f=u(u({},f),{validationError:t})}return e(f)}var N=o.createPropertyDecoratorFactory("validateAll",(function(t,e,r,n,i){if(void 0===i&&(i=x),!o.isFunction(e))throw new Error(o.callWithPropNotEquallFunc("validateAll",r));return function(o){return i(o,e,t,n)}}),w);Object.defineProperty(t,"EMPTY_OBJECT",{enumerable:!0,get:function(){return o.EMPTY_OBJECT}}),Object.defineProperty(t,"EMPTY_STRING",{enumerable:!0,get:function(){return o.EMPTY_STRING}}),t.FormStore=T,t.actionFormChangeStatus=m,t.actionFormChangeValue=p,t.actionFormFocus=v,t.actionFormReset=j,t.actionFormSubmit=E,t.actionFormVisit=g,t.effectFormChangeStatus=O,t.effectFormChangeValue=y,t.effectFormFocus=h,t.effectFormReset=F,t.effectFormSubmit=S,t.effectFormVisit=b,t.validate=_,t.validateAll=N,Object.defineProperty(t,"__esModule",{value:!0})}));