@livelybone/form
Version:
A cross-framework form management library, realized validate, format, reset, submit
10 lines (8 loc) • 8.55 kB
JavaScript
/**
* Bundle of @livelybone/form
* Generated: 2020-07-27
* Version: 2.10.6
* License: MIT
* Author: 2631541504@qq.com
*/
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t=t||self).Form={})}(this,function(t){"use strict";function o(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function n(t,e){for(var i=0;i<e.length;i++){var n=e[i];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}function i(t,e,i){return e&&n(t.prototype,e),i&&n(t,i),t}function r(t,e,i){return e in t?Object.defineProperty(t,e,{value:i,enumerable:!0,configurable:!0,writable:!0}):t[e]=i,t}function a(e,t){var i=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter(function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable})),i.push.apply(i,n)}return i}function s(e){for(var t=1;t<arguments.length;t++){var i=null!=arguments[t]?arguments[t]:{};t%2?a(i,!0).forEach(function(t){r(e,t,i[t])}):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(i)):a(i).forEach(function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(i,t))})}return e}function u(t,e){return function(t){if(Array.isArray(t))return t}(t)||function(t,e){if(!(Symbol.iterator in Object(t)||"[object Arguments]"===Object.prototype.toString.call(t)))return;var i=[],n=!0,o=!1,r=void 0;try{for(var a,s=t[Symbol.iterator]();!(n=(a=s.next()).done)&&(i.push(a.value),!e||i.length!==e);n=!0);}catch(t){o=!0,r=t}finally{try{n||null==s.return||s.return()}finally{if(o)throw r}}return i}(t,e)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance")}()}function l(t,e,i){var n=s({},e,{},i.optionsForValidatorAndFormatter);return t.errorText=t.required&&!t.value&&0!==t.value?i.emptyErrorTemplate.replace("{label}",t.label||""):t.validator?t.validator(t.value,n):"",t.valid=!t.errorText,t.errorText}function p(t,e,i,n){t.value=t.formatter?t.formatter(e,s({},i,{},n.optionsForValidatorAndFormatter)):e,i[t.name]=t.value;var o=t.validateOnChange;(void 0===o?n.validateOnChange:o)?l(t,i,n):t.errorText="",t.pristine=!1}function d(t,n){var o=s({},n.initialValues),r={},e=t.map(function(t){var e=void 0!==o[t.name]?o[t.name]:t.value,i=t.formatter?t.formatter(e,s({},o,{},n.optionsForValidatorAndFormatter)):e;return o[t.name]=i,r[t.name]=i,s({},t,{id:t.id||t.name,required:void 0===t.required||t.required,value:i,pristine:!0,valid:!0,errorText:"",validateOnChange:t.validateOnChange||!1})});return{data:r,items:e}}function h(t){t.valid=!0,t.errorText=""}var e=function(){function n(t){var e=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{};o(this,n),r(this,"items",void 0),r(this,"$errorText",""),r(this,"options",{}),r(this,"data",void 0),this.$updateOptions(e);var i=d(t,this.options);this.items=i.items,this.data=i.data,this.$updateOptions({initialValues:this.data}),this.updateItemsRequired()}return i(n,[{key:"getItemByName",value:function(e){return this.items.find(function(t){return t.name===e})}},{key:"getItemById",value:function(e){return this.items.find(function(t){return t.id===e})}},{key:"itemChange",value:function(t,e,i){var n=!(2<arguments.length&&void 0!==i)||i,o=this.getItemByName(t);if(o){var r=o.value;p(o,e,this.data,this.options),r!==o.value&&this.updateItemsRequired(),this.errorText="",n&&this.options.componentUpdateFn&&this.options.componentUpdateFn()}else console.error(new Error("The name `".concat(t,"` isn't exist in this form")))}},{key:"itemsChange",value:function(n,t){var o=this,e=!(1<arguments.length&&void 0!==t)||t,r=!1;this.items.forEach(function(t){var e=t.name;if(e in n){var i=t.value;p(t,n[e],o.data,o.options),r=r||i!==t.value}}),r&&this.updateItemsRequired(),this.errorText="",e&&this.options.componentUpdateFn&&this.options.componentUpdateFn()}},{key:"itemValidate",value:function(t,e,i){var n=!(2<arguments.length&&void 0!==i)||i,o=this.getItemByName(t);if(o){e&&(o.pristine=!1);var r=l(o,this.data,this.options);return n&&this.options.componentUpdateFn&&this.options.componentUpdateFn(),r}return"The name isn't exist in this form"}},{key:"updateValidateResult",value:function(i,t){var e=!(1<arguments.length&&void 0!==t)||t;this.items.forEach(function(t){if(t.name in i){var e=i[t.name];e?(t.valid=!1,t.errorText=e):(t.valid=!0,t.errorText="")}}),e&&this.options.componentUpdateFn&&this.options.componentUpdateFn()}},{key:"formValidate",value:function(t,e){for(var i=0<arguments.length&&void 0!==t?t:this.options.validateAll,n=!(1<arguments.length&&void 0!==e)||e,o="",r=0;r<this.items.length&&(i||!o);r+=1){var a=l(this.items[r],this.data,this.options);o=o||a}return n&&this.options.componentUpdateFn&&this.options.componentUpdateFn(),o}},{key:"submit",value:function(t){var e=this,i=!(0<arguments.length&&void 0!==t)||t;this.errorText="";var n=this.formValidate(this.options.validateAll,!1);return(n?Promise.reject(new Error(n)):Promise.resolve(this.options.onSubmit(this.data))).finally(function(){i&&e.options.componentUpdateFn&&e.options.componentUpdateFn()})}},{key:"reset",value:function(t,e){var i=0<arguments.length&&void 0!==t?t:this.options.initialValues,n=!(1<arguments.length&&void 0!==e)||e;this.$updateOptions({initialValues:i});var o=d(this.items,this.options);this.items=o.items,this.data=o.data,this.updateItemsRequired(),n&&this.options.componentUpdateFn&&this.options.componentUpdateFn()}},{key:"resetItem",value:function(t,e,i){var n=1<arguments.length&&void 0!==e?e:this.options.initialValues[t],o=!(2<arguments.length&&void 0!==i)||i,r=this.getItemByName(t);if(r){var a=r.value;r.pristine=!0,r.value=r.formatter?r.formatter(n,s({},this.data,{},this.options.optionsForValidatorAndFormatter)):n,this.data[r.name]=r.value,this.options.initialValues[r.name]=n,a!==r.value&&this.updateItemsRequired(),h(r),o&&this.options.componentUpdateFn&&this.options.componentUpdateFn()}else console.error(new Error("The name `".concat(t,"` isn't exist in this form")))}},{key:"clearValidateResult",value:function(t,e){var i=!(1<arguments.length&&void 0!==e)||e;if(t){var n=this.getItemByName(t);n?h(n):console.error(new Error("The name `".concat(t,"` isn't exist in this form")))}else this.items.forEach(h);i&&this.options.componentUpdateFn&&this.options.componentUpdateFn()}},{key:"$updateOptions",value:function(t){this.options.onSubmit=t.onSubmit||this.options.onSubmit||function(t){return Promise.resolve(t)},this.options.validateAll=t.validateAll||this.options.validateAll||!1,this.options.initialValues=s({},this.options.initialValues,{},t.initialValues),this.options.validateOnChange=t.validateOnChange||this.options.validateOnChange||!1,this.options.emptyErrorTemplate=t.emptyErrorTemplate||this.options.emptyErrorTemplate||"{label}不能为空",this.options.optionsForValidatorAndFormatter=s({},this.options.optionsForValidatorAndFormatter,{},t.optionsForValidatorAndFormatter),this.options.componentUpdateFn=t.componentUpdateFn||this.options.componentUpdateFn}},{key:"updateItemsRequired",value:function(){var e=s({},this.options.optionsForValidatorAndFormatter,{},this.data);this.items.forEach(function(t){t.required=t.calcRequired?!!t.calcRequired(e):void 0===t.required||t.required})}},{key:"updateOptions",value:function(e){var i=this,t=e.optionsForValidatorAndFormatter&&Object.keys(e.optionsForValidatorAndFormatter).some(function(t){return e.optionsForValidatorAndFormatter[t]!==i.options.optionsForValidatorAndFormatter[t]});this.$updateOptions(e),t&&this.updateItemsRequired()}},{key:"pristine",get:function(){return this.items.every(function(t){return t.pristine})}},{key:"valid",get:function(){return this.items.every(function(t){return t.valid})}},{key:"errorText",get:function(){if(this.$errorText)return this.$errorText;var t=this.items.find(function(t){return t.errorText});return t?t.errorText:""},set:function(t){this.$errorText=t}}]),n}(),m=function(){function e(t){o(this,e),r(this,"allItems",void 0),this.allItems=function(t){var o={};return Object.entries(t).forEach(function(t){var e=u(t,2),i=e[0],n=e[1];o[i]=s({},n,{id:i})}),o}(t)}return i(e,[{key:"getItem",value:function(t){var e=s({},this.allItems[t]);if(!e)throw new Error("The form item you search for by id `".concat(t,"` is not exist, please make sure param id correct"));return e}},{key:"getItems",value:function(t){return t.map(this.getItem.bind(this))}}]),e}();t.Form=e,t.FormItemsManager=m,Object.defineProperty(t,"__esModule",{value:!0})});