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