@mobx-ecosystem/mobx-form 2.0.6 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -30,6 +30,18 @@ export declare class FormService<T extends Record<string, FieldService<unknown>>
30
30
  * @returns Object of field values
31
31
  */
32
32
  getValues: () => Record<string, unknown>;
33
+ /**
34
+ * Set fields by this
35
+ */
36
+ setFieldsByThis: (obj: any) => void;
37
+ /**
38
+ * Set object to values by form service keys
39
+ */
40
+ setValues: (obj: Record<string, unknown>) => void;
41
+ /**
42
+ * Set object to init values by form service keys
43
+ */
44
+ setInitValues: (obj: Record<string, unknown>) => void;
33
45
  /**
34
46
  * Set field values to init values
35
47
  */
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- import{isEqual as e}from"lodash";import{makeAutoObservable as t}from"mobx";var i=function(){return i=Object.assign||function(e){for(var t,i=1,r=arguments.length;i<r;i++)for(var n in t=arguments[i])Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n]);return e},i.apply(this,arguments)};function r(e,t,i,r){return new(i||(i=Promise))((function(n,o){function u(e){try{s(r.next(e))}catch(e){o(e)}}function a(e){try{s(r.throw(e))}catch(e){o(e)}}function s(e){var t;e.done?n(e.value):(t=e.value,t instanceof i?t:new i((function(e){e(t)}))).then(u,a)}s((r=r.apply(e,t||[])).next())}))}function n(e,t){var i,r,n,o,u={label:0,sent:function(){if(1&n[0])throw n[1];return n[1]},trys:[],ops:[]};return o={next:a(0),throw:a(1),return:a(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function a(a){return function(s){return function(a){if(i)throw new TypeError("Generator is already executing.");for(;o&&(o=0,a[0]&&(u=0)),u;)try{if(i=1,r&&(n=2&a[0]?r.return:a[0]?r.throw||((n=r.return)&&n.call(r),0):r.next)&&!(n=n.call(r,a[1])).done)return n;switch(r=0,n&&(a=[2&a[0],n.value]),a[0]){case 0:case 1:n=a;break;case 4:return u.label++,{value:a[1],done:!1};case 5:u.label++,r=a[1],a=[0];continue;case 7:a=u.ops.pop(),u.trys.pop();continue;default:if(!(n=u.trys,(n=n.length>0&&n[n.length-1])||6!==a[0]&&2!==a[0])){u=0;continue}if(3===a[0]&&(!n||a[1]>n[0]&&a[1]<n[3])){u.label=a[1];break}if(6===a[0]&&u.label<n[1]){u.label=n[1],n=a;break}if(n&&u.label<n[2]){u.label=n[2],u.ops.push(a);break}n[2]&&u.ops.pop(),u.trys.pop();continue}a=t.call(e,u)}catch(e){a=[6,e],r=0}finally{i=n=0}if(5&a[0])throw a[1];return{value:a[0]?a[1]:void 0,done:!0}}([a,s])}}}var o,u=function(){function r(e,i){var r=this;this._serviceType="field-service",this._initValue=void 0,this._value=void 0,this._error=void 0,this._disabled=!1,this.onChange=function(e,t){r.value=t,r.validate&&r.validate()},t(this),this.initValue=e,this.options=i}return Object.defineProperty(r.prototype,"initValue",{get:function(){return this._initValue},set:function(e){this._initValue=e,this._value=e,this.validate&&this.validate()},enumerable:!1,configurable:!0}),Object.defineProperty(r.prototype,"value",{get:function(){return this._value},set:function(e){this._value=e},enumerable:!1,configurable:!0}),Object.defineProperty(r.prototype,"error",{get:function(){return this._error},set:function(e){this._error=e},enumerable:!1,configurable:!0}),Object.defineProperty(r.prototype,"disabled",{get:function(){return this._disabled},set:function(e){this._disabled=e},enumerable:!1,configurable:!0}),Object.defineProperty(r.prototype,"isValid",{get:function(){return!this._error},enumerable:!1,configurable:!0}),Object.defineProperty(r.prototype,"isInit",{get:function(){return null!==(t=this.value)&&"object"==typeof t?e(this.value,this._initValue):this._value===this._initValue;var t},enumerable:!1,configurable:!0}),Object.defineProperty(r.prototype,"props",{get:function(){var e,t=this,r={value:this.value,error:this.error,disabled:this.disabled};return(null===(e=this.options)||void 0===e?void 0:e.onError)&&(r=i(i({},r),{onError:function(e){t.error=t.error||(null==e?void 0:e.toString())}})),i(i({},r),{onChange:this.onChange})},enumerable:!1,configurable:!0}),r}(),a=function(e){o=e.validation.validate},s=function(){if(!o)throw new Error("You must define configureForm to configure mobx-form")},l=function(){function e(e,i){var u=this;this.validate=function(){return r(u,void 0,void 0,(function(){var e,t;return n(this,(function(i){switch(i.label){case 0:return e=this.getValues(),[4,null==o?void 0:o(e,this.validationSchema)];case 1:return(t=i.sent())&&0!=Object.keys(t||[]).length?this.setErrors(t):this.resetErrors(),[2]}}))}))},this.getValues=function(){var e={};return u.keys.forEach((function(t){e[t]=u.fields[t].value})),e},this.setValuesAsInit=function(){u.keys.forEach((function(e){u.fields[e].initValue=u.fields[e].value}))},this.resetErrors=function(){u.keys.forEach((function(e){return u.fields[e].error=void 0}))},this.reset=function(){u.keys.forEach((function(e){return u.fields[e].value=u.fields[e].initValue})),u.validate()},this.disable=function(){u.keys.forEach((function(e){return u.fields[e].disabled=!0}))},this.enable=function(){u.keys.forEach((function(e){return u.fields[e].disabled=!1}))},s(),t(this),this.fields=e,this.validationSchema=i,this.keys.forEach((function(e){u.fields[e].validate=u.validate}))}return Object.defineProperty(e.prototype,"keys",{get:function(){return Object.keys(this.fields)},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"isValid",{get:function(){var e=this;return this.keys.every((function(t){return e.fields[t].isValid}))},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"isTouched",{get:function(){var e=this;return this.keys.some((function(t){return!e.fields[t].isInit}))},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"canBeSubmitted",{get:function(){return this.isTouched&&this.isValid},enumerable:!1,configurable:!0}),e.prototype.setErrors=function(e){var t=this;this.keys.forEach((function(i){t.fields[i].error=null==e?void 0:e[i]}))},e}();export{u as FieldService,l as FormService,s as _checkConfiguration,a as configureForm,o as validate};
1
+ import{isEqual as e}from"lodash";import{makeAutoObservable as t}from"mobx";var i=function(){return i=Object.assign||function(e){for(var t,i=1,n=arguments.length;i<n;i++)for(var r in t=arguments[i])Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r]);return e},i.apply(this,arguments)};function n(e,t,i,n){return new(i||(i=Promise))((function(r,o){function u(e){try{a(n.next(e))}catch(e){o(e)}}function s(e){try{a(n.throw(e))}catch(e){o(e)}}function a(e){var t;e.done?r(e.value):(t=e.value,t instanceof i?t:new i((function(e){e(t)}))).then(u,s)}a((n=n.apply(e,t||[])).next())}))}function r(e,t){var i,n,r,o,u={label:0,sent:function(){if(1&r[0])throw r[1];return r[1]},trys:[],ops:[]};return o={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function s(s){return function(a){return function(s){if(i)throw new TypeError("Generator is already executing.");for(;o&&(o=0,s[0]&&(u=0)),u;)try{if(i=1,n&&(r=2&s[0]?n.return:s[0]?n.throw||((r=n.return)&&r.call(n),0):n.next)&&!(r=r.call(n,s[1])).done)return r;switch(n=0,r&&(s=[2&s[0],r.value]),s[0]){case 0:case 1:r=s;break;case 4:return u.label++,{value:s[1],done:!1};case 5:u.label++,n=s[1],s=[0];continue;case 7:s=u.ops.pop(),u.trys.pop();continue;default:if(!(r=u.trys,(r=r.length>0&&r[r.length-1])||6!==s[0]&&2!==s[0])){u=0;continue}if(3===s[0]&&(!r||s[1]>r[0]&&s[1]<r[3])){u.label=s[1];break}if(6===s[0]&&u.label<r[1]){u.label=r[1],r=s;break}if(r&&u.label<r[2]){u.label=r[2],u.ops.push(s);break}r[2]&&u.ops.pop(),u.trys.pop();continue}s=t.call(e,u)}catch(e){s=[6,e],n=0}finally{i=r=0}if(5&s[0])throw s[1];return{value:s[0]?s[1]:void 0,done:!0}}([s,a])}}}var o,u=function(){function n(e,i){var n=this;this._serviceType="field-service",this._initValue=void 0,this._value=void 0,this._error=void 0,this._disabled=!1,this.onChange=function(e,t){n.value=t,n.validate&&n.validate()},t(this),this.initValue=e,this.options=i}return Object.defineProperty(n.prototype,"initValue",{get:function(){return this._initValue},set:function(e){this._initValue=e,this._value=e,this.validate&&this.validate()},enumerable:!1,configurable:!0}),Object.defineProperty(n.prototype,"value",{get:function(){return this._value},set:function(e){this._value=e},enumerable:!1,configurable:!0}),Object.defineProperty(n.prototype,"error",{get:function(){return this._error},set:function(e){this._error=e},enumerable:!1,configurable:!0}),Object.defineProperty(n.prototype,"disabled",{get:function(){return this._disabled},set:function(e){this._disabled=e},enumerable:!1,configurable:!0}),Object.defineProperty(n.prototype,"isValid",{get:function(){return!this._error},enumerable:!1,configurable:!0}),Object.defineProperty(n.prototype,"isInit",{get:function(){return null!==(t=this.value)&&"object"==typeof t?e(this.value,this._initValue):this._value===this._initValue;var t},enumerable:!1,configurable:!0}),Object.defineProperty(n.prototype,"props",{get:function(){var e,t=this,n={value:this.value,error:this.error,disabled:this.disabled};return(null===(e=this.options)||void 0===e?void 0:e.onError)&&(n=i(i({},n),{onError:function(e){t.error=t.error||(null==e?void 0:e.toString())}})),i(i({},n),{onChange:this.onChange})},enumerable:!1,configurable:!0}),n}(),s=function(e){o=e.validation.validate},a=function(){if(!o)throw new Error("You must define configureForm to configure mobx-form")},l=function(){function e(e,i){var s=this;this.validate=function(){return n(s,void 0,void 0,(function(){var e,t;return r(this,(function(i){switch(i.label){case 0:return e=this.getValues(),[4,null==o?void 0:o(e,this.validationSchema)];case 1:return(t=i.sent())&&0!=Object.keys(t||[]).length?this.setErrors(t):this.resetErrors(),[2]}}))}))},this.getValues=function(){var e={};return s.keys.forEach((function(t){e[t]=s.fields[t].value})),e},this.setFieldsByThis=function(e){var t={};Object.keys(e).forEach((function(i){e[i]&&e[i]instanceof u&&(t[i]=e[i])})),s.fields=t,s.keys.forEach((function(e){s.fields[e].validate=s.validate}))},this.setValues=function(e){s.keys.forEach((function(t){return s.fields[t].value=e[t]}))},this.setInitValues=function(e){s.keys.forEach((function(t){return s.fields[t].initValue=e[t]}))},this.setValuesAsInit=function(){s.keys.forEach((function(e){s.fields[e].initValue=s.fields[e].value}))},this.resetErrors=function(){s.keys.forEach((function(e){return s.fields[e].error=void 0}))},this.reset=function(){s.keys.forEach((function(e){return s.fields[e].value=s.fields[e].initValue})),s.validate()},this.disable=function(){s.keys.forEach((function(e){return s.fields[e].disabled=!0}))},this.enable=function(){s.keys.forEach((function(e){return s.fields[e].disabled=!1}))},a(),t(this),this.fields=e,this.validationSchema=i,this.keys.forEach((function(e){s.fields[e].validate=s.validate}))}return Object.defineProperty(e.prototype,"keys",{get:function(){return Object.keys(this.fields)},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"isValid",{get:function(){var e=this;return this.keys.every((function(t){return e.fields[t].isValid}))},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"isTouched",{get:function(){var e=this;return this.keys.some((function(t){return!e.fields[t].isInit}))},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"canBeSubmitted",{get:function(){return this.isTouched&&this.isValid},enumerable:!1,configurable:!0}),e.prototype.setErrors=function(e){var t=this;this.keys.forEach((function(i){t.fields[i].error=null==e?void 0:e[i]}))},e}();export{u as FieldService,l as FormService,a as _checkConfiguration,s as configureForm,o as validate};
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "type": "module",
3
3
  "name": "@mobx-ecosystem/mobx-form",
4
4
  "private": false,
5
- "version": "2.0.6",
5
+ "version": "2.1.0",
6
6
  "description": "provides the ability to use forms with validation in MobX stores",
7
7
  "main": "dist/index.js",
8
8
  "module": "dist/index.js",