form-linker
Version:
Links form elements to each other.
1 lines • 8.54 kB
JavaScript
module.exports=function(t){var e={};function r(n){if(e[n])return e[n].exports;var i=e[n]={i:n,l:!1,exports:{}};return t[n].call(i.exports,i,i.exports,r),i.l=!0,i.exports}return r.m=t,r.c=e,r.d=function(t,e,n){r.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:n})},r.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},r.t=function(t,e){if(1&e&&(t=r(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var i in t)r.d(n,i,function(e){return t[e]}.bind(null,i));return n},r.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return r.d(e,"a",e),e},r.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},r.p="",r(r.s=1)}([function(t,e){t.exports=require("lodash")},function(t,e,r){"use strict";r.r(e),r.d(e,"default",(function(){return f}));var n=r(0);function i(t){if("undefined"==typeof Symbol||null==t[Symbol.iterator]){if(Array.isArray(t)||(t=a(t))){var e=0,r=function(){};return{s:r,n:function(){return e>=t.length?{done:!0}:{done:!1,value:t[e++]}},e:function(t){throw t},f:r}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var n,i,o=!0,s=!1;return{s:function(){n=t[Symbol.iterator]()},n:function(){var t=n.next();return o=t.done,t},e:function(t){s=!0,i=t},f:function(){try{o||null==n.return||n.return()}finally{if(s)throw i}}}}function o(t){return function(t){if(Array.isArray(t))return s(t)}(t)||function(t){if("undefined"!=typeof Symbol&&Symbol.iterator in Object(t))return Array.from(t)}(t)||a(t)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function a(t,e){if(t){if("string"==typeof t)return s(t,e);var r=Object.prototype.toString.call(t).slice(8,-1);return"Object"===r&&t.constructor&&(r=t.constructor.name),"Map"===r||"Set"===r?Array.from(t):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?s(t,e):void 0}}function s(t,e){(null==e||e>t.length)&&(e=t.length);for(var r=0,n=new Array(e);r<e;r++)n[r]=t[r];return n}function c(t){return(c="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}function l(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function u(t,e){for(var r=0;r<e.length;r++){var n=e[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}var f=function(){function t(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};l(this,t),this.schema=e.schema||{},this.fields=this.calcFields(),this.converters=e.converters||{},this.formatters=e.formatters||{},this.masks=e.masks||{},this.data={},this.parsedData=e.data||{},this.setValuesFromParsed(e.data||{}),this.originalData=Object.assign({},this.parsedData),this.errors={},this.refs={},this.changeCallback=e.onChange||function(){}}var e,r,a;return e=t,(r=[{key:"calcFields",value:function(){var t=this,e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:this.schema,r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"",n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:[];return Object.keys(e).forEach((function(i){"object"===c(e[i])?t.calcFields(e[i],r+i+".",n):n.push(r+i)})),n}},{key:"convert",value:function(t,e){var r=this,i=Object(n.get)(this.schema,t);return Object(n.isNil)(i)||i.split(".").forEach((function(t){Object(n.isNil)(r.converters[t])||(e=r.converters[t](e))})),e}},{key:"getError",value:function(t){return Object(n.get)(this.errors,t)||[]}},{key:"getErrors",value:function(){return this.errors}},{key:"setError",value:function(t,e){var r=!(arguments.length>2&&void 0!==arguments[2])||arguments[2],i=!(arguments.length>3&&void 0!==arguments[3])||arguments[3];if(Object(n.isEmpty)(e)){Object(n.unset)(this.errors,t);var o=t.indexOf(".")>-1;if(o)for(var a=t.slice(0,t.lastIndexOf("."));a&&Object(n.isEmpty)(Object(n.get)(this.errors,a));)Object(n.unset)(this.errors,a),a=a.slice(0,a.lastIndexOf("."))}else Object(n.set)(this.errors,t,e);if(i){var s=Object(n.get)(this.refs,t);"function"==typeof(null==s?void 0:s.forceUpdate)&&s.forceUpdate()}r&&this.changeCallback()}},{key:"setErrors",value:function(t){var e=this,r=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];this.errors=t,this.fields.forEach((function(r){var i=Object(n.get)(t,r)&&Object(n.get)(e.refs,r);"function"==typeof(null==i?void 0:i.forceUpdate)&&i.forceUpdate()})),r&&this.changeCallback()}},{key:"getValue",value:function(t){return Object(n.get)(this.data,t)}},{key:"getValues",value:function(){return this.data}},{key:"setValue",value:function(t,e){var r=!(arguments.length>2&&void 0!==arguments[2])||arguments[2],i=arguments.length>3&&void 0!==arguments[3]&&arguments[3];Object(n.set)(this.data,t,this.mask(t,e)),Object(n.set)(this.parsedData,t,this.format(t,e).parsed);var o=Object(n.get)(this.refs,t);"function"==typeof(null==o?void 0:o.forceUpdate)&&o.forceUpdate(i),r&&this.changeCallback()}},{key:"setValues",value:function(t){var e=this,r=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];this.fields.forEach((function(r){var i=Object(n.get)(t,r);void 0!==i&&e.setValue(r,i,!1,!0)})),r&&this.changeCallback()}},{key:"setValuesFromParsed",value:function(t){var e=this;this.fields.forEach((function(r){var i=Object(n.get)(t,r);void 0!==i&&e.setValue(r,e.convert(r,i),!1,!0)}))}},{key:"format",value:function(t,e){var r=this,i=Object(n.get)(this.schema,t),o={errors:[],formatted:e,parsed:e,valid:!0};return Object(n.isNil)(i)||i.split(".").forEach((function(t){Object(n.isNil)(r.formatters[t])||(o=r.formatters[t](o))})),o}},{key:"mask",value:function(t,e){var r=this,i=Object(n.get)(this.schema,t),o=e;return Object(n.isNil)(i)||i.split(".").forEach((function(t){Object(n.isNil)(r.masks[t])||(o=r.masks[t].mask(e))})),o}},{key:"isValid",value:function(){for(var t=!0,e=0;e<this.fields.length;e++)if(!1===this.format(this.fields[e],this.getValue(this.fields[e])).valid){t=!1;break}return t}},{key:"validate",value:function(t){var e=!(arguments.length>1&&void 0!==arguments[1])||arguments[1],r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:[],i=this.format(t,this.getValue(t)),a=i.errors,s=i.formatted,c=i.parsed;this.setError(t,[].concat(o(r),o(a)),!1,!1),this.setValue(t,s,!1,!0),Object(n.set)(this.parsedData,t,c),e&&this.changeCallback()}},{key:"validateAll",value:function(t){var e=this,r=!0,n={};"object"===c(t)&&(n=t,r=!1),this.fields.forEach((function(t){var r=n[t];e.validate(t,!1,r)})),r&&this.changeCallback()}},{key:"extractDifferences",value:function(t){var e={},r=this.parsedData;return this.fields.forEach((function(i){(!Object(n.isNil)(Object(n.get)(t,i))&&""!==Object(n.get)(t,i)||!Object(n.isNil)(Object(n.get)(r,i))&&""!==Object(n.get)(r,i))&&(Object(n.isEqual)(Object(n.get)(t,i),Object(n.get)(r,i))||Object(n.set)(e,i,Object(n.get)(r,i)))})),e}},{key:"setRef",value:function(t,e){Object(n.isNil)(e)?Object(n.unset)(this.refs,t):Object(n.set)(this.refs,t,e)}},{key:"getRef",value:function(t){return Object(n.get)(this.refs,t+".inputRef.current")}},{key:"focusOnField",value:function(t){var e,r;if(Object(n.isNil)(t)){var o,a=i(this.fields);try{for(a.s();!(o=a.n()).done;){var s=o.value;if(r=Object(n.get)(this.refs,s+".inputRef.current"),!Object(n.isNil)(r))break}}catch(t){a.e(t)}finally{a.f()}}else r=Object(n.get)(this.refs,t+".inputRef.current");"function"==typeof(null===(e=r)||void 0===e?void 0:e.focus)&&r.focus()}},{key:"scrollToError",value:function(){var t,e,r,o=this,a=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};this.validateAll(a);var s,c=i(this.fields);try{for(c.s();!(s=c.n()).done;){var l=s.value;if(e=this.getError(l),!Object(n.isEmpty)(e)&&(r=Object(n.get)(this.refs,l+".inputRef.current"),!Object(n.isNil)(r))){t=l;break}}}catch(t){c.e(t)}finally{c.f()}Object(n.isNil)(t)||"function"==typeof r.focus&&(r.focus(),"function"==typeof r.blur&&setTimeout((function(){r.blur(),o.setError(t,e)})))}},{key:"updateSchema",value:function(t){this.schema=t||{},this.fields=this.calcFields(),this.validateAll(),this.errors={}}}])&&u(e.prototype,r),a&&u(e,a),t}()}]);