@livelybone/react-form
Version:
A react from controller
10 lines (8 loc) • 9.93 kB
JavaScript
/**
* Bundle of @livelybone/react-form
* Generated: 2022-04-01
* Version: 2.1.1
* License: MIT
* Author: 2631541504@qq.com
*/
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],e):e((t=t||self).ReactForm={},t.React)}(this,function(t,o){"use strict";function e(e,t){var n,i=Object.keys(e);return Object.getOwnPropertySymbols&&(n=Object.getOwnPropertySymbols(e),t&&(n=n.filter(function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable})),i.push.apply(i,n)),i}function r(i){for(var t=1;t<arguments.length;t++){var o=null!=arguments[t]?arguments[t]:{};t%2?e(Object(o),!0).forEach(function(t){var e,n;e=i,n=o[t=t],t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n}):Object.getOwnPropertyDescriptors?Object.defineProperties(i,Object.getOwnPropertyDescriptors(o)):e(Object(o)).forEach(function(t){Object.defineProperty(i,t,Object.getOwnPropertyDescriptor(o,t))})}return i}function i(t,e){(null==e||e>t.length)&&(e=t.length);for(var n=0,i=new Array(e);n<e;n++)i[n]=t[n];return i}function a(t,e){return function(t){if(Array.isArray(t))return t}(t)||function(t,e){if("undefined"!=typeof Symbol&&Symbol.iterator in Object(t)){var n=[],i=!0,o=!1,r=void 0;try{for(var a,s=t[Symbol.iterator]();!(i=(a=s.next()).done)&&(n.push(a.value),!e||n.length!==e);i=!0);}catch(t){o=!0,r=t}finally{try{i||null==s.return||s.return()}finally{if(o)throw r}}return n}}(t,e)||function(t,e){if(t){if("string"==typeof t)return i(t,e);var n=Object.prototype.toString.call(t).slice(8,-1);return"Map"===(n="Object"===n&&t.constructor?t.constructor.name:n)||"Set"===n?Array.from(n):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?i(t,e):void 0}}(t,e)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function s(t,e){for(var n=0;n<e.length;n++){var i=e[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(t,i.key,i)}}function u(t,e,n){e in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n}function l(e,t){var n,i=Object.keys(e);return Object.getOwnPropertySymbols&&(n=Object.getOwnPropertySymbols(e),t&&(n=n.filter(function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable})),i.push.apply(i,n)),i}function p(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?l(n,!0).forEach(function(t){u(e,t,n[t])}):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):l(n).forEach(function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))})}return e}function c(t,e,n){e=p({},e,{},n.optionsForValidatorAndFormatter);return t.errorText=t.required&&!t.value&&0!==t.value?n.emptyErrorTemplate.replace("{label}",t.label||""):t.validator?t.validator(t.value,e):"",t.valid=!t.errorText,t.errorText}function d(t,e,n,i){t.value=t.formatter?t.formatter(e,p({},n,{},i.optionsForValidatorAndFormatter)):e,n[t.name]=t.value,"discount_recharge_min_amount"===t.name&&console.log(11113,n,t.name,t.value);e=t.validateOnChange;(void 0===e?i.validateOnChange:e)?c(t,n,i):t.errorText="",t.pristine=!1}function h(t,n){var i=p({},n.initialValues),o={},t=t.map(function(t){var e=void 0!==i[t.name]?i[t.name]:t.value,e=t.formatter?t.formatter(e,p({},i,{},n.optionsForValidatorAndFormatter)):e;return i[t.name]=e,o[t.name]=e,p({},t,{id:t.id||t.name,required:void 0===t.required||t.required,value:e,pristine:!0,valid:!0,errorText:"",validateOnChange:t.validateOnChange||!1})});return{data:o,items:t}}function m(t){t.valid=!0,t.errorText=""}var f=function(){function o(t){var e=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{},n=this,i=o;if(!(n instanceof i))throw new TypeError("Cannot call a class as a function");u(this,"items",void 0),u(this,"$errorText",""),u(this,"options",{}),u(this,"data",void 0),this.$updateOptions(e);n=h(t,this.options);this.items=n.items,this.data=n.data,this.$updateOptions({initialValues:this.data}),this.updateItemsRequired()}var t,e,n;return t=o,(e=[{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){var n,i=!(2<arguments.length&&void 0!==arguments[2])||arguments[2],o=this.getItemByName(t);o?(n=o.value,d(o,e,this.data,this.options),n!==o.value&&this.updateItemsRequired(),this.errorText="",i&&this.options.componentUpdateFn&&this.options.componentUpdateFn()):console.error(new Error("The name `".concat(t,"` isn't exist in this form")))}},{key:"itemsChange",value:function(i){var o=this,t=!(1<arguments.length&&void 0!==arguments[1])||arguments[1],r=!1;this.items.forEach(function(t){var e,n=t.name;console.log(11114,t,i),n in i&&(e=t.value,d(t,i[n],o.data,o.options),r=r||e!==t.value)}),r&&this.updateItemsRequired(),this.errorText="",t&&this.options.componentUpdateFn&&this.options.componentUpdateFn()}},{key:"itemValidate",value:function(t,e){var n=!(2<arguments.length&&void 0!==arguments[2])||arguments[2],t=this.getItemByName(t);return t?(e&&(t.pristine=!1),e=c(t,this.data,this.options),n&&this.options.componentUpdateFn&&this.options.componentUpdateFn(),e):"The name isn't exist in this form"}},{key:"updateValidateResult",value:function(n){var t=!(1<arguments.length&&void 0!==arguments[1])||arguments[1];this.items.forEach(function(t){var e;t.name in n&&((e=n[t.name])?(t.valid=!1,t.errorText=e):(t.valid=!0,t.errorText=""))}),t&&this.options.componentUpdateFn&&this.options.componentUpdateFn()}},{key:"formValidate",value:function(){for(var t=0<arguments.length&&void 0!==arguments[0]?arguments[0]:this.options.validateAll,e=!(1<arguments.length&&void 0!==arguments[1])||arguments[1],n="",i=0;i<this.items.length&&(t||!n);i+=1)var o=c(this.items[i],this.data,this.options),n=n||o;return e&&this.options.componentUpdateFn&&this.options.componentUpdateFn(),n}},{key:"submit",value:function(){var t=this,e=!(0<arguments.length&&void 0!==arguments[0])||arguments[0],n=(this.errorText="",this.formValidate(this.options.validateAll,!1));return(n?Promise.reject(new Error(n)):Promise.resolve(this.options.onSubmit(this.data))).finally(function(){e&&t.options.componentUpdateFn&&t.options.componentUpdateFn()})}},{key:"reset",value:function(){var t=0<arguments.length&&void 0!==arguments[0]?arguments[0]:this.options.initialValues,e=!(1<arguments.length&&void 0!==arguments[1])||arguments[1],t=(this.$updateOptions({initialValues:t}),h(this.items,this.options));this.items=t.items,this.data=t.data,this.updateItemsRequired(),e&&this.options.componentUpdateFn&&this.options.componentUpdateFn()}},{key:"resetItem",value:function(t){var e,n=1<arguments.length&&void 0!==arguments[1]?arguments[1]:this.options.initialValues[t],i=!(2<arguments.length&&void 0!==arguments[2])||arguments[2],o=this.getItemByName(t);o?(e=o.value,o.pristine=!0,o.value=o.formatter?o.formatter(n,p({},this.data,{},this.options.optionsForValidatorAndFormatter)):n,this.data[o.name]=o.value,this.options.initialValues[o.name]=n,e!==o.value&&this.updateItemsRequired(),m(o),i&&this.options.componentUpdateFn&&this.options.componentUpdateFn()):console.error(new Error("The name `".concat(t,"` isn't exist in this form")))}},{key:"clearValidateResult",value:function(t){var e,n=!(1<arguments.length&&void 0!==arguments[1])||arguments[1];t?(e=this.getItemByName(t))?m(e):console.error(new Error("The name `".concat(t,"` isn't exist in this form"))):this.items.forEach(m),n&&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=p({},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=p({},this.options.optionsForValidatorAndFormatter,{},t.optionsForValidatorAndFormatter),this.options.componentUpdateFn=t.componentUpdateFn||this.options.componentUpdateFn}},{key:"updateItemsRequired",value:function(){var e=p({},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 n=this,t=e.optionsForValidatorAndFormatter&&Object.keys(e.optionsForValidatorAndFormatter).some(function(t){return e.optionsForValidatorAndFormatter[t]!==n.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}}])&&s(t.prototype,e),n&&s(t,n),o}();t.inputItemChange=function(t,e,n){t.itemChange(e,n.target.value),n.target.value=t.data[e]||""},t.isAllItemFilled=function(t){return t.items.every(function(t){return!t.required||("string"==typeof t.value?t.value:void 0!==t.value)})},t.useForm=function(t,e){var n=a(o.useReducer(function(t){return t+1},0),2)[1],i=a(o.useState(function(){return new f(t,r(r({},e),{},{componentUpdateFn:n}))}),1)[0];return o.useLayoutEffect(function(){i.updateOptions(r(r({},e),{},{componentUpdateFn:n}))},[i,e,n]),i},Object.defineProperty(t,"__esModule",{value:!0})});