react-nested-validation
Advanced tools
Comparing version 0.1.2 to 0.2.0
@@ -1,1 +0,1 @@ | ||
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t(require("react")):"function"==typeof define&&define.amd?define(["react"],t):"object"==typeof exports?exports["react-nested-validation"]=t(require("react")):e["react-nested-validation"]=t(e.react)}(window,function(e){return function(e){var t={};function r(n){if(t[n])return t[n].exports;var a=t[n]={i:n,l:!1,exports:{}};return e[n].call(a.exports,a,a.exports,r),a.l=!0,a.exports}return r.m=e,r.c=t,r.d=function(e,t,n){r.o(e,t)||Object.defineProperty(e,t,{configurable:!1,enumerable:!0,get:n})},r.r=function(e){Object.defineProperty(e,"__esModule",{value:!0})},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,"a",t),t},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.p="",r(r.s=0)}({0:function(e,t,r){e.exports=r("tjUo")},Al62:function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0});t.isNil=function(e){return void 0===e||null===e||""===e},t.isFunc=function(e){return"function"==typeof e},t.capitalize=function(e){return""+e[0].toUpperCase()+e.slice(1)}},"D+7e":function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.default=void 0;var n,a=function(){return function(e,t){if(Array.isArray(e))return e;if(Symbol.iterator in Object(e))return function(e,t){var r=[],n=!0,a=!1,o=void 0;try{for(var i,u=e[Symbol.iterator]();!(n=(i=u.next()).done)&&(r.push(i.value),!t||r.length!==t);n=!0);}catch(e){a=!0,o=e}finally{try{!n&&u.return&&u.return()}finally{if(a)throw o}}return r}(e,t);throw new TypeError("Invalid attempt to destructure non-iterable instance")}}(),o=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e},i=function(){function e(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}return function(t,r,n){return r&&e(t.prototype,r),n&&e(t,n),t}}(),u=r("pR90"),s=(n=u)&&n.__esModule?n:{default:n};function l(e){if(Array.isArray(e)){for(var t=0,r=Array(e.length);t<e.length;t++)r[t]=e[t];return r}return Array.from(e)}var f=function(){function e(){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this.errors=s.default.normalizeErrors()}return i(e,[{key:"addFieldError",value:function(e,t){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:s.default.ERROR,n=this.errors.fields[e]||[];n.push({type:r,message:t}),this.errors.fields[e]=n,this.updateCounts(r)}},{key:"addFormError",value:function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:s.default.ERROR,r=this.errors.form||[];r.push({type:t,message:e}),this.errors.form=r,this.updateCounts(t)}},{key:"updateCounts",value:function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:1;this.errors.counts[e]=(this.errors.counts[e]||0)+t}}],[{key:"merge",value:function(e,t){return{fields:o({},e.fields,t.fields),form:[].concat(l(e.form),l(t.form)),counts:this._mergeCounts(e.counts,t.counts)}}},{key:"_mergeCounts",value:function(e,t){var r=o({},e),n=!0,i=!1,u=void 0;try{for(var s,l=Object.entries(t)[Symbol.iterator]();!(n=(s=l.next()).done);n=!0){var f=s.value,c=a(f,2),v=c[0],d=c[1];r[v]=(r[v]||0)+d}}catch(e){i=!0,u=e}finally{try{!n&&l.return&&l.return()}finally{if(i)throw u}}return r}}]),e}();t.default=f},JE77:function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var n=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e},a=function(){function e(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}return function(t,r,n){return r&&e(t.prototype,r),n&&e(t,n),t}}(),o=s(r("cDcd")),i=r("Al62"),u=s(r("yK9j"));function s(e){return e&&e.__esModule?e:{default:e}}function l(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function f(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t}t.default=function(e){return function(t){var r,s,c,v=e.initialPage,d=function(e,t){var r={};for(var n in e)t.indexOf(n)>=0||Object.prototype.hasOwnProperty.call(e,n)&&(r[n]=e[n]);return r}(e,["initialPage"]);return(0,u.default)(d)((c=s=function(e){function r(){var e,t,a;!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,r);for(var o=arguments.length,u=Array(o),s=0;s<o;s++)u[s]=arguments[s];return t=a=f(this,(e=r.__proto__||Object.getPrototypeOf(r)).call.apply(e,[this].concat(u))),a.state={page:(0,i.isFunc)(v)?v(a.props):v,history:[],visited:{}},a.nextPage=function(e,t,r){var o=a.state,u=o.page,s=o.visited;e&&e.validate(!0);var f=!s[u];e&&(e.state.errors.counts.error>0||e.state.errors.counts.warning>0&&f)?(r&&r(u,e),a.setState({visited:n({},s,l({},u,!0))})):(0,i.isFunc)(t)?t():a.setState({page:t,visited:n({},s,l({},u,!0)),history:[].concat(function(e){if(Array.isArray(e)){for(var t=0,r=Array(e.length);t<e.length;t++)r[t]=e[t];return r}return Array.from(e)}(a.state.history),[u])})},a.previousPage=function(){var e=a.state.history;e.length>0&&a.setState({page:e[e.length-1],history:e.slice(0,e.length-1)})},a.renderPage=function(e){var t=a.state.page;return e["render"+(0,i.capitalize)(t)]()},f(a,t)}return function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)}(r,o.default.Component),a(r,[{key:"render",value:function(){return o.default.createElement(t,n({},this.props,{nextPage:this.nextPage,previousPage:this.previousPage,finishWizard:this.finishWizard,renderPage:this.renderPage}))}}]),r}(),s.Form=e.form,r=c))||r}}},cDcd:function(t,r){t.exports=e},pR90:function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.default=void 0;var n,a,o,i=function(){return function(e,t){if(Array.isArray(e))return e;if(Symbol.iterator in Object(e))return function(e,t){var r=[],n=!0,a=!1,o=void 0;try{for(var i,u=e[Symbol.iterator]();!(n=(i=u.next()).done)&&(r.push(i.value),!t||r.length!==t);n=!0);}catch(e){a=!0,o=e}finally{try{!n&&u.return&&u.return()}finally{if(a)throw o}}return r}(e,t);throw new TypeError("Invalid attempt to destructure non-iterable instance")}}(),u=function(){function e(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}return function(t,r,n){return r&&e(t.prototype,r),n&&e(t,n),t}}(),s=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e},l=r("D+7e"),f=(o=l)&&o.__esModule?o:{default:o};function c(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function v(e){if(Array.isArray(e)){for(var t=0,r=Array(e.length);t<e.length;t++)r[t]=e[t];return r}return Array.from(e)}function d(e,t){return s({},e,{values:s({},e.values||{},t)})}var h=(a=n=function(){function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this.state=e.normalize(t),this.nested={},this.fieldValidators={},this.defaultValues={}}return u(e,[{key:"parse",value:function(e,t){var r=this,n=e;if(Array.isArray(this.nested))e=(e||[]).map(function(e){return(new r.nested[0]).parse(e,t)}),e=[].concat(v(this.state.values||[]),v(e));else{if(this.nested){var a=s({},e=e||{}),o=!0,u=!1,l=void 0;try{for(var f,c=Object.entries(this.nested)[Symbol.iterator]();!(o=(f=c.next()).done);o=!0){var d=f.value,h=i(d,2),y=h[0],p=new(0,h[1]);a[y]=p.parse(e[y],t)}}catch(e){u=!0,l=e}finally{try{!o&&c.return&&c.return()}finally{if(u)throw l}}e=a}else e=e||{};e=s({},this.state.values||{},e)}this.state=s({},this.state,{values:e,touched:{},initial:n});var m=!0,b=!1,g=void 0;try{for(var w,O=(this.multi||[])[Symbol.iterator]();!(m=(w=O.next()).done);m=!0){var j=w.value;this.state=new j(this.state).parse(e,t)}}catch(e){b=!0,g=e}finally{try{!m&&O.return&&O.return()}finally{if(b)throw g}}return this.validate(t),this.state}},{key:"renderValues",value:function(e){if(void 0===e&&(e=this.state.values),Array.isArray(this.nested)){var t=this.nested[0];e=(e||[]).map(function(e){return new t(e).renderValues()})}else{if(e=e||{},this.nested){var r=s({},e),n=!0,a=!1,o=void 0;try{for(var u,l=Object.entries(this.nested)[Symbol.iterator]();!(n=(u=l.next()).done);n=!0){var f=u.value,c=i(f,2),v=c[0],d=new(0,c[1])(e[v]);r[v]=d.renderValues()}}catch(e){a=!0,o=e}finally{try{!n&&l.return&&l.return()}finally{if(a)throw o}}e=r}if(this.multi){var h=s({},e=e||{}),y=!0,p=!1,m=void 0;try{for(var b,g=this.multi[Symbol.iterator]();!(y=(b=g.next()).done);y=!0){var w=b.value;h=s({},h,(new w).renderValues(h))}}catch(e){p=!0,m=e}finally{try{!y&&g.return&&g.return()}finally{if(p)throw m}}e=h}else e=e||{}}return e}},{key:"validate",value:function(e){this.forceValidation=e;var t=new f.default,r=this.getValues();if(e){if(Array.isArray(r)){var n=this.nested[0];r=r.map(function(t){var r=new n(t);return r.validate(e),r.state})}else{var a=!0,o=!1,u=void 0;try{for(var l,c=Object.entries(this.nested)[Symbol.iterator]();!(a=(l=c.next()).done);a=!0){var v=l.value,d=i(v,2),h=d[0],y=d[1];if(void 0!==r[h]){var p=new y(r[h]);p.validate(e),r[h]=p.state}}}catch(e){o=!0,u=e}finally{try{!a&&c.return&&c.return()}finally{if(o)throw u}}}this.state=s({},this.state,{values:r})}this.validateFields(t),this.validateForm(t);var m=!0,b=!1,g=void 0;try{for(var w,O=(this.multi||[])[Symbol.iterator]();!(m=(w=O.next()).done);m=!0){var j=new(0,w.value)(r);j.validate(e),t.errors=f.default.merge(t.errors,j.state.errors)}}catch(e){b=!0,g=e}finally{try{!m&&O.return&&O.return()}finally{if(b)throw g}}if(Array.isArray(r)){var _=!0,x=!1,k=void 0;try{for(var P,A=r[Symbol.iterator]();!(_=(P=A.next()).done);_=!0){var F=P.value.errors,S=void 0===F?{}:F,E=!0,C=!1,R=void 0;try{for(var M,T=Object.entries(S.counts||{})[Symbol.iterator]();!(E=(M=T.next()).done);E=!0){var V=M.value,z=i(V,2),I=z[0],U=z[1];t.updateCounts(I,U)}}catch(e){C=!0,R=e}finally{try{!E&&T.return&&T.return()}finally{if(C)throw R}}}}catch(e){x=!0,k=e}finally{try{!_&&A.return&&A.return()}finally{if(x)throw k}}}else{var N=!0,W=!1,q=void 0;try{for(var D,L=Object.keys(this.nested)[Symbol.iterator]();!(N=(D=L.next()).done);N=!0){var G=(r[D.value]||{}).errors,J=void 0===G?{}:G,K=!0,B=!1,H=void 0;try{for(var Q,X=Object.entries(J.counts||{})[Symbol.iterator]();!(K=(Q=X.next()).done);K=!0){var Y=Q.value,Z=i(Y,2),$=Z[0],ee=Z[1];t.updateCounts($,ee)}}catch(e){B=!0,H=e}finally{try{!K&&X.return&&X.return()}finally{if(B)throw H}}}}catch(e){W=!0,q=e}finally{try{!N&&L.return&&L.return()}finally{if(W)throw q}}}this.state=s({},this.state,{errors:t.errors})}},{key:"validateFields",value:function(e){var t=!0,r=!1,n=void 0;try{for(var a,o=this.iterFieldValidators()[Symbol.iterator]();!(t=(a=o.next()).done);t=!0){var u=a.value,s=i(u,2),l=s[0],f=s[1];this.isTouched(l)&&f(e,l,this)}}catch(e){r=!0,n=e}finally{try{!t&&o.return&&o.return()}finally{if(r)throw n}}}},{key:"validateForm",value:function(e){}},{key:"iterFieldValidators",value:regeneratorRuntime.mark(function e(){var t,r,n,a,o,u,s,l,f,c,v,d,h,y,p;return regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:t=!0,r=!1,n=void 0,e.prev=3,a=Object.entries(this.fieldValidators)[Symbol.iterator]();case 5:if(t=(o=a.next()).done){e.next=39;break}u=o.value,s=i(u,2),l=s[0],f=s[1],c=!0,v=!1,d=void 0,e.prev=13,h=f[Symbol.iterator]();case 15:if(c=(y=h.next()).done){e.next=22;break}return p=y.value,e.next=19,[l,p];case 19:c=!0,e.next=15;break;case 22:e.next=28;break;case 24:e.prev=24,e.t0=e.catch(13),v=!0,d=e.t0;case 28:e.prev=28,e.prev=29,!c&&h.return&&h.return();case 31:if(e.prev=31,!v){e.next=34;break}throw d;case 34:return e.finish(31);case 35:return e.finish(28);case 36:t=!0,e.next=5;break;case 39:e.next=45;break;case 41:e.prev=41,e.t1=e.catch(3),r=!0,n=e.t1;case 45:e.prev=45,e.prev=46,!t&&a.return&&a.return();case 48:if(e.prev=48,!r){e.next=51;break}throw n;case 51:return e.finish(48);case 52:return e.finish(45);case 53:case"end":return e.stop()}},e,this,[[3,41,45,53],[13,24,28,36],[29,,31,35],[46,,48,52]])})},{key:"updateValue",value:function(t,r){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},a=this.getValues(),o=this.state.touched,i=void 0;if(Array.isArray(a)){var u=void 0;u=Array.isArray(t)?t:[t];var l=!0,h=!1,y=void 0;try{for(var p,m=u[Symbol.iterator]();!(l=(p=m.next()).done);l=!0){var b=p.value;null===b?a=[].concat(v(a),[e.normalize({values:r})]):(b=parseInt(b))>=0&&b<a.length&&(a=[].concat(v(a.slice(0,b)),[n.merge?d(a[b],r):r],v(a.slice(b+1))))}}catch(e){h=!0,y=e}finally{try{!l&&m.return&&m.return()}finally{if(h)throw y}}a=a.filter(function(e){return null!==e})}else t===e.MULTI?(a=s({},a,r.values),o=s({},o,r.touched||{}),i=r.errors):(a=s({},a,c({},t,r)),o=this.updateTouched(t,r,this.state.touched));this.state=s({},this.state,{values:a,touched:o}),this.state.errors=this.validate(),i&&(this.state.errors=f.default.merge(this.state.errors,i))}},{key:"updateTouched",value:function(e,t,r){var n=this.getInitial();return r=this.getTouched(r),s({},r,c({},e,!(!r[e]&&!t&&n[e]==t)))}},{key:"isTouched",value:function(e,t){var r=this.lookupForm(e),n=i(r,2),a=n[0],o=n[1];return this.forceValidation&&(a.touched[o]=!0),a.touched[o]||!1}},{key:"getTouched",value:function(e){return this._getState(e,"touched",{})}},{key:"getInitial",value:function(e){return this._getState(e,"initial",{})}},{key:"getValues",value:function(e){return this._getState(e,"values",this.defaultValues)}},{key:"getValue",value:function(e){var t=this.lookupForm(e),r=i(t,2),n=r[0],a=r[1];return e?n.values[a]:n.values}},{key:"lookupForm",value:function(e){var t=this.state;if(e){var r=e.split("."),n=!0,a=!1,o=void 0;try{for(var i,u=r.slice(0,r.length-1)[Symbol.iterator]();!(n=(i=u.next()).done);n=!0){var s=i.value;t=t.values[s]}}catch(e){a=!0,o=e}finally{try{!n&&u.return&&u.return()}finally{if(a)throw o}}e=r[r.length-1]}return[t,e]}},{key:"_getState",value:function(e,t,r){return void 0===e&&(e=this.state[t]||r),e}}]),e}(),n.ERROR="error",n.WARNING="warning",n.MULTI="",n.normalizeErrors=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};return s({fields:{},form:[],counts:{}},e)},n.normalize=function(e){return s({touched:{},errors:h.normalizeErrors(),values:{}},e)},a);t.default=h},qgJu:function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.emptyWarning=t.required=void 0;var n,a=r("pR90"),o=(n=a)&&n.__esModule?n:{default:n},i=r("Al62");var u=function(e,t,r,n,a,o){var u=r.getValue(t);(0,i.isNil)(u)&&e.addFieldError(t,function(e,t,r,n){return e instanceof Function?e(t,r):e||n}(n,t,r,o),a)};(t.required=function e(t){return function(r,n,a){return u(r,n,a,t,o.default.ERROR,e.DEFAULT_MESSAGE)}}).DEFAULT_MESSAGE="This field is required.";t.emptyWarning=function(e){return function(t,r,n){return u(t,r,n,e,o.default.WARNING)}}},tjUo:function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.Form=t.asFormWizard=t.asForm=void 0;var n=r("qgJu");Object.keys(n).forEach(function(e){"default"!==e&&"__esModule"!==e&&Object.defineProperty(t,e,{enumerable:!0,get:function(){return n[e]}})});var a=u(r("yK9j")),o=u(r("JE77")),i=u(r("pR90"));function u(e){return e&&e.__esModule?e:{default:e}}t.asForm=a.default,t.asFormWizard=o.default,t.Form=i.default},yK9j:function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var n=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e},a=function(){function e(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}return function(t,r,n){return r&&e(t.prototype,r),n&&e(t,n),t}}(),o=s(r("cDcd")),i=s(r("pR90")),u=r("Al62");function s(e){return e&&e.__esModule?e:{default:e}}t.default=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};return function(t){var r,s;return s=r=function(r){function s(t){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,s);var r=function(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t}(this,(s.__proto__||Object.getPrototypeOf(s)).call(this,t));if(r.getFormClass=function(){return r.props.Form||e.form},r.makeForm=function(e){var t=new(r.getFormClass())(r.getFormState());return e&&t.validate(!0),t},r.getFormState=function(){var e=r.props.form,t=void 0===e?{}:e;(t||{}).values||(t=(new(r.getFormClass())).parse(r.props.initial));return i.default.normalize(n({},t))},r.handleChange=function(e,t,n){var a=r.makeForm();a.updateValue(e,t,n);var o=r.props,s=o.onChange,l=o.formPrefix;s&&(l!==i.default.MULTI&&(0,u.isNil)(l)?s(a.state,e,t):s(l,a.state))},r.validate=function(e){var t=r.makeForm().validate();r.setState({errors:t})},!t.form){var a=r.makeForm();t.onChange&&t.onChange(a.state)}return r}return function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)}(s,o.default.Component),a(s,[{key:"shouldComponentUpdate",value:function(e){return(e.form||{}).values!=(this.props.form||{}).values||(e.form||{}).errors!=(this.props.form||{}).errors}},{key:"render",value:function(){var e=this.props,r=e.form,a=(e.Form,function(e,t){var r={};for(var n in e)t.indexOf(n)>=0||Object.prototype.hasOwnProperty.call(e,n)&&(r[n]=e[n]);return r}(e,["form","Form"]));return(0,u.isNil)(r)&&(r=this.makeForm(this.props.forceValidation).state),o.default.createElement(t,n({},a,{form:r,onChange:this.handleChange}))}}]),s}(),r.Form=e.form,s}}}})}); | ||
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t(require("react")):"function"==typeof define&&define.amd?define(["react"],t):"object"==typeof exports?exports["react-nested-validation"]=t(require("react")):e["react-nested-validation"]=t(e.react)}(window,function(e){return function(e){var t={};function r(n){if(t[n])return t[n].exports;var a=t[n]={i:n,l:!1,exports:{}};return e[n].call(a.exports,a,a.exports,r),a.l=!0,a.exports}return r.m=e,r.c=t,r.d=function(e,t,n){r.o(e,t)||Object.defineProperty(e,t,{configurable:!1,enumerable:!0,get:n})},r.r=function(e){Object.defineProperty(e,"__esModule",{value:!0})},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,"a",t),t},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.p="",r(r.s=0)}({0:function(e,t,r){e.exports=r("tjUo")},Al62:function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0});t.isNil=function(e){return void 0===e||null===e||""===e},t.isFunc=function(e){return"function"==typeof e},t.capitalize=function(e){return""+e[0].toUpperCase()+e.slice(1)}},"D+7e":function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.default=void 0;var n,a=function(){return function(e,t){if(Array.isArray(e))return e;if(Symbol.iterator in Object(e))return function(e,t){var r=[],n=!0,a=!1,i=void 0;try{for(var o,u=e[Symbol.iterator]();!(n=(o=u.next()).done)&&(r.push(o.value),!t||r.length!==t);n=!0);}catch(e){a=!0,i=e}finally{try{!n&&u.return&&u.return()}finally{if(a)throw i}}return r}(e,t);throw new TypeError("Invalid attempt to destructure non-iterable instance")}}(),i=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e},o=function(){function e(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}return function(t,r,n){return r&&e(t.prototype,r),n&&e(t,n),t}}(),u=r("pR90"),s=(n=u)&&n.__esModule?n:{default:n};function l(e){if(Array.isArray(e)){for(var t=0,r=Array(e.length);t<e.length;t++)r[t]=e[t];return r}return Array.from(e)}var f=function(){function e(){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this.errors=s.default.normalizeErrors()}return o(e,[{key:"addFieldError",value:function(e,t){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:s.default.ERROR,n=this.errors.fields[e]||[];n.push({type:r,message:t}),this.errors.fields[e]=n,this.updateCounts(r)}},{key:"addFormError",value:function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:s.default.ERROR,r=this.errors.form||[];r.push({type:t,message:e}),this.errors.form=r,this.updateCounts(t)}},{key:"updateCounts",value:function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:1;this.errors.counts[e]=(this.errors.counts[e]||0)+t}}],[{key:"merge",value:function(e,t){return{fields:i({},e.fields,t.fields),form:[].concat(l(e.form),l(t.form)),counts:this._mergeCounts(e.counts,t.counts)}}},{key:"_mergeCounts",value:function(e,t){var r=i({},e),n=!0,o=!1,u=void 0;try{for(var s,l=Object.entries(t)[Symbol.iterator]();!(n=(s=l.next()).done);n=!0){var f=s.value,c=a(f,2),v=c[0],d=c[1];r[v]=(r[v]||0)+d}}catch(e){o=!0,u=e}finally{try{!n&&l.return&&l.return()}finally{if(o)throw u}}return r}}]),e}();t.default=f},JE77:function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var n=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e},a=function(){function e(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}return function(t,r,n){return r&&e(t.prototype,r),n&&e(t,n),t}}(),i=s(r("cDcd")),o=r("Al62"),u=s(r("yK9j"));function s(e){return e&&e.__esModule?e:{default:e}}function l(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function f(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t}t.default=function(e){return function(t){var r,s,c,v=e.initialPage,d=function(e,t){var r={};for(var n in e)t.indexOf(n)>=0||Object.prototype.hasOwnProperty.call(e,n)&&(r[n]=e[n]);return r}(e,["initialPage"]);return(0,u.default)(d)((c=s=function(e){function r(){var e,t,a;!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,r);for(var i=arguments.length,u=Array(i),s=0;s<i;s++)u[s]=arguments[s];return t=a=f(this,(e=r.__proto__||Object.getPrototypeOf(r)).call.apply(e,[this].concat(u))),a.state={page:(0,o.isFunc)(v)?v(a.props):v,history:[],visited:{}},a.nextPage=function(e,t,r){var i=a.state,u=i.page,s=i.visited;e&&e.validate(!0);var f=!s[u];e&&(e.state.errors.counts.error>0||e.state.errors.counts.warning>0&&f)?(r&&r(u,e),a.setState({visited:n({},s,l({},u,!0))})):(0,o.isFunc)(t)?t():a.setState({page:t,visited:n({},s,l({},u,!0)),history:[].concat(function(e){if(Array.isArray(e)){for(var t=0,r=Array(e.length);t<e.length;t++)r[t]=e[t];return r}return Array.from(e)}(a.state.history),[u])})},a.previousPage=function(){var e=a.state.history;e.length>0&&a.setState({page:e[e.length-1],history:e.slice(0,e.length-1)})},a.renderPage=function(e){var t=a.state.page;return e["render"+(0,o.capitalize)(t)]()},f(a,t)}return function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)}(r,i.default.Component),a(r,[{key:"render",value:function(){return i.default.createElement(t,n({},this.props,{nextPage:this.nextPage,previousPage:this.previousPage,finishWizard:this.finishWizard,renderPage:this.renderPage}))}}]),r}(),s.Form=e.form,r=c))||r}}},cDcd:function(t,r){t.exports=e},pR90:function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.default=void 0;var n,a,i,o=function(){return function(e,t){if(Array.isArray(e))return e;if(Symbol.iterator in Object(e))return function(e,t){var r=[],n=!0,a=!1,i=void 0;try{for(var o,u=e[Symbol.iterator]();!(n=(o=u.next()).done)&&(r.push(o.value),!t||r.length!==t);n=!0);}catch(e){a=!0,i=e}finally{try{!n&&u.return&&u.return()}finally{if(a)throw i}}return r}(e,t);throw new TypeError("Invalid attempt to destructure non-iterable instance")}}(),u=function(){function e(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}return function(t,r,n){return r&&e(t.prototype,r),n&&e(t,n),t}}(),s=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e},l=r("D+7e"),f=(i=l)&&i.__esModule?i:{default:i},c=r("Al62");function v(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function d(e){if(Array.isArray(e)){for(var t=0,r=Array(e.length);t<e.length;t++)r[t]=e[t];return r}return Array.from(e)}function h(e,t,r){var n=new e(t=s({},t,{values:s({},t.values||{},r)}));return n.validate(),n.state}var y=(a=n=function(){function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this.state=e.normalize(t),this.nested={},this.fieldValidators={},this.defaultValues={}}return u(e,[{key:"parse",value:function(e,t){var r=this,n=e;if(Array.isArray(this.nested))e=(e||[]).map(function(e){return(new r.nested[0]).parse(e,t)}),e=[].concat(d(this.state.values||[]),d(e));else{e=e||{};var a=void 0;if(this.fields){a={};var i=!0,u=!1,l=void 0;try{for(var f,v=this.fields[Symbol.iterator]();!(i=(f=v.next()).done);i=!0){var h=f.value;a[h]=e[h]}}catch(e){u=!0,l=e}finally{try{!i&&v.return&&v.return()}finally{if(u)throw l}}}else a=s({},e);if(this.nested){var y=!0,p=!1,m=void 0;try{for(var b,g=Object.entries(this.nested)[Symbol.iterator]();!(y=(b=g.next()).done);y=!0){var w=b.value,O=o(w,2),j=O[0],_=O[1],k=void 0,A=this["initial"+(0,c.capitalize)(j)];k=A?A(e,j):e;var x=new _;a[j]=x.parse(k,t)}}catch(e){p=!0,m=e}finally{try{!y&&g.return&&g.return()}finally{if(p)throw m}}}e=s({},this.state.values||{},a)}return this.parseValues&&(e=this.parseValues(e)),this.state=s({},this.state,{values:e,touched:{},initial:n}),this.render(),this.validate(t),this.state}},{key:"render",value:function(){var e=this.state.values,t=void 0;if(Array.isArray(this.nested))t=e.map(function(e){return e.flat});else{if(this.fields){t={};var r=!0,n=!1,a=void 0;try{for(var i,o=this.fields[Symbol.iterator]();!(r=(i=o.next()).done);r=!0){var u=i.value;t[u]=e[u]}}catch(e){n=!0,a=e}finally{try{!r&&o.return&&o.return()}finally{if(n)throw a}}}else t=s({},e);var l=!0,f=!1,v=void 0;try{for(var d,h=Object.keys(this.nested)[Symbol.iterator]();!(l=(d=h.next()).done);l=!0){var y=d.value,p=this["render"+(0,c.capitalize)(y)],m=this.state.values[y].flat;p?t=p(t,m):Array.isArray(m)?t[y]=m:t=s({},t,m)}}catch(e){f=!0,v=e}finally{try{!l&&h.return&&h.return()}finally{if(f)throw v}}}return this.state.flat=this.renderValues(t),t}},{key:"renderValues",value:function(e){return e}},{key:"validate",value:function(e){this.forceValidation=e;var t=new f.default,r=this.getValues();if(e){if(Array.isArray(r)){var n=this.nested[0];r=r.map(function(t){var r=new n(t);return r.validate(e),r.state})}else{var a=!0,i=!1,u=void 0;try{for(var l,c=Object.entries(this.nested)[Symbol.iterator]();!(a=(l=c.next()).done);a=!0){var v=l.value,d=o(v,2),h=d[0],y=d[1];if(void 0!==r[h]){var p=new y(r[h]);p.validate(e),r[h]=p.state}}}catch(e){i=!0,u=e}finally{try{!a&&c.return&&c.return()}finally{if(i)throw u}}}this.state=s({},this.state,{values:r})}if(this.validateFields(t),this.validateForm(t),Array.isArray(r)){var m=!0,b=!1,g=void 0;try{for(var w,O=r[Symbol.iterator]();!(m=(w=O.next()).done);m=!0){var j=w.value.errors,_=void 0===j?{}:j,k=!0,A=!1,x=void 0;try{for(var P,F=Object.entries(_.counts||{})[Symbol.iterator]();!(k=(P=F.next()).done);k=!0){var S=P.value,E=o(S,2),C=E[0],R=E[1];t.updateCounts(C,R)}}catch(e){A=!0,x=e}finally{try{!k&&F.return&&F.return()}finally{if(A)throw x}}}}catch(e){b=!0,g=e}finally{try{!m&&O.return&&O.return()}finally{if(b)throw g}}}else{var V=!0,M=!1,T=void 0;try{for(var z,I=Object.keys(this.nested)[Symbol.iterator]();!(V=(z=I.next()).done);V=!0){var N=(r[z.value]||{}).errors,W=void 0===N?{}:N,q=!0,D=!1,U=void 0;try{for(var G,J=Object.entries(W.counts||{})[Symbol.iterator]();!(q=(G=J.next()).done);q=!0){var K=G.value,L=o(K,2),B=L[0],H=L[1];t.updateCounts(B,H)}}catch(e){D=!0,U=e}finally{try{!q&&J.return&&J.return()}finally{if(D)throw U}}}}catch(e){M=!0,T=e}finally{try{!V&&I.return&&I.return()}finally{if(M)throw T}}}return this.state=s({},this.state,{errors:t.errors}),this.state}},{key:"validateFields",value:function(e){var t=!0,r=!1,n=void 0;try{for(var a,i=this.iterFieldValidators()[Symbol.iterator]();!(t=(a=i.next()).done);t=!0){var u=a.value,s=o(u,2),l=s[0],f=s[1];this.isTouched(l)&&f(e,l,this)}}catch(e){r=!0,n=e}finally{try{!t&&i.return&&i.return()}finally{if(r)throw n}}}},{key:"validateForm",value:function(e){}},{key:"iterFieldValidators",value:regeneratorRuntime.mark(function e(){var t,r,n,a,i,u,s,l,f,c,v,d,h,y,p;return regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:t=!0,r=!1,n=void 0,e.prev=3,a=Object.entries(this.fieldValidators)[Symbol.iterator]();case 5:if(t=(i=a.next()).done){e.next=39;break}u=i.value,s=o(u,2),l=s[0],f=s[1],c=!0,v=!1,d=void 0,e.prev=13,h=f[Symbol.iterator]();case 15:if(c=(y=h.next()).done){e.next=22;break}return p=y.value,e.next=19,[l,p];case 19:c=!0,e.next=15;break;case 22:e.next=28;break;case 24:e.prev=24,e.t0=e.catch(13),v=!0,d=e.t0;case 28:e.prev=28,e.prev=29,!c&&h.return&&h.return();case 31:if(e.prev=31,!v){e.next=34;break}throw d;case 34:return e.finish(31);case 35:return e.finish(28);case 36:t=!0,e.next=5;break;case 39:e.next=45;break;case 41:e.prev=41,e.t1=e.catch(3),r=!0,n=e.t1;case 45:e.prev=45,e.prev=46,!t&&a.return&&a.return();case 48:if(e.prev=48,!r){e.next=51;break}throw n;case 51:return e.finish(48);case 52:return e.finish(45);case 53:case"end":return e.stop()}},e,this,[[3,41,45,53],[13,24,28,36],[29,,31,35],[46,,48,52]])})},{key:"updateIn",value:function(e,t){if(void 0===t?t=[]:Array.isArray(t)||(t=t.split(".")),t.length>0){var r=void 0,n=void 0;if(Array.isArray(this.nested))r=parseInt(t[0]),n=new(0,this.nested[0])(this.state.values[r]);else r=t[0],n=new(0,this.nested[r])(this.state.values[r]);n.updateIn(e,t.slice(1)),this.updateValue(r,n.state)}else if(Array.isArray(this.nested)&&Array.isArray(e)){var a=!0,i=!1,u=void 0;try{for(var s,l=e[Symbol.iterator]();!(a=(s=l.next()).done);a=!0){var f=s.value;this.updateValue(null,f)}}catch(e){i=!0,u=e}finally{try{!a&&l.return&&l.return()}finally{if(i)throw u}}}else{var c=!0,d=!1,h=void 0;try{for(var y,p=Object.entries(e)[Symbol.iterator]();!(c=(y=p.next()).done);c=!0){var m=y.value,b=o(m,2),g=b[0],w=b[1];if(g.includes(".")){var O=g.split(".");this.updateIn(v({},O[O.length-1],w),O.slice(0,O.length-1))}else this.updateValue(g,w)}}catch(e){d=!0,h=e}finally{try{!c&&p.return&&p.return()}finally{if(d)throw h}}}this.render()}},{key:"updateValue",value:function(e,t){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},n=this.getValues(),a=this.state.touched;if(Array.isArray(n)){var i=this.nested[0],o=void 0;o=Array.isArray(e)?e:[e];var u=!0,l=!1,f=void 0;try{for(var c,y=o[Symbol.iterator]();!(u=(c=y.next()).done);u=!0){var p=c.value;if(null===p){var m=new i;m.parse(t),n=[].concat(d(n),[m.state])}else(p=parseInt(p))>=0&&p<n.length&&(n=[].concat(d(n.slice(0,p)),[r.merge?h(this.nested[0],n[p],t):t],d(n.slice(p+1))))}}catch(e){l=!0,f=e}finally{try{!u&&y.return&&y.return()}finally{if(l)throw f}}n=n.filter(function(e){return null!==e})}else n=s({},n,v({},e,t)),a=this.updateTouched(e,t,this.state.touched);this.state=s({},this.state,{values:n,touched:a}),this.state.errors=this.validate()}},{key:"updateTouched",value:function(e,t,r){var n=this.getInitial();return r=this.getTouched(r),s({},r,v({},e,!(!r[e]&&!t&&n[e]==t)))}},{key:"isTouched",value:function(e,t){var r=this.lookupForm(e),n=o(r,2),a=n[0],i=n[1];return this.forceValidation&&(a.touched[i]=!0),a.touched[i]||!1}},{key:"getTouched",value:function(e){return this._getState(e,"touched",{})}},{key:"getInitial",value:function(e){return this._getState(e,"initial",{})}},{key:"getValues",value:function(e){return this._getState(e,"values",this.defaultValues)}},{key:"getValue",value:function(e){var t=this.lookupForm(e),r=o(t,2),n=r[0],a=r[1];return e?n.values[a]:n.values}},{key:"lookupForm",value:function(e){var t=this.state;if(e){var r=e.split("."),n=!0,a=!1,i=void 0;try{for(var o,u=r.slice(0,r.length-1)[Symbol.iterator]();!(n=(o=u.next()).done);n=!0){var s=o.value;t=t.values[s]}}catch(e){a=!0,i=e}finally{try{!n&&u.return&&u.return()}finally{if(a)throw i}}e=r[r.length-1]}return[t,e]}},{key:"_getState",value:function(e,t,r){return void 0===e&&(e=this.state[t]||r),e}}]),e}(),n.ERROR="error",n.WARNING="warning",n.normalizeErrors=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};return s({fields:{},form:[],counts:{}},e)},n.normalize=function(e){return s({touched:{},errors:y.normalizeErrors(),values:{}},e)},a);t.default=y},qgJu:function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.emptyWarning=t.required=void 0;var n,a=r("pR90"),i=(n=a)&&n.__esModule?n:{default:n},o=r("Al62");var u=function(e,t,r,n,a,i){var u=r.getValue(t);(0,o.isNil)(u)&&e.addFieldError(t,function(e,t,r,n){return e instanceof Function?e(t,r):e||n}(n,t,r,i),a)};(t.required=function e(t){return function(r,n,a){return u(r,n,a,t,i.default.ERROR,e.DEFAULT_MESSAGE)}}).DEFAULT_MESSAGE="This field is required.";t.emptyWarning=function(e){return function(t,r,n){return u(t,r,n,e,i.default.WARNING)}}},tjUo:function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.Form=t.asFormWizard=t.asForm=void 0;var n=r("qgJu");Object.keys(n).forEach(function(e){"default"!==e&&"__esModule"!==e&&Object.defineProperty(t,e,{enumerable:!0,get:function(){return n[e]}})});var a=u(r("yK9j")),i=u(r("JE77")),o=u(r("pR90"));function u(e){return e&&e.__esModule?e:{default:e}}t.asForm=a.default,t.asFormWizard=i.default,t.Form=o.default},yK9j:function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var n=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e},a=function(){function e(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}return function(t,r,n){return r&&e(t.prototype,r),n&&e(t,n),t}}(),i=s(r("cDcd")),o=s(r("pR90")),u=r("Al62");function s(e){return e&&e.__esModule?e:{default:e}}t.default=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};return function(t){var r,s;return s=r=function(r){function s(t){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,s);var r=function(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t}(this,(s.__proto__||Object.getPrototypeOf(s)).call(this,t));if(r.getFormClass=function(){return r.props.Form||e.form},r.makeForm=function(e){var t=new(r.getFormClass())(r.getFormState());return e&&t.validate(!0),t},r.getFormState=function(){var e=r.props.form,t=void 0===e?{}:e;(t||{}).values||(t=(new(r.getFormClass())).parse(r.props.initial));return o.default.normalize(n({},t))},r.handleChange2=function(e,t){var n=r.props,a=n.onChange,i=n.formPrefix;if((0,u.isNil)(i)){var o=r.makeForm();o.updateIn(e,t),o.state,a&&a({state:o.state,values:o.flat,payload:e,path:t})}else t=(0,u.isNil)(t)?i:i+"."+t,a&&a(e,t)},r.handleChange=function(e,t,n){var a=r.makeForm();a.updateValue(e,t,n);var i=r.props,o=i.onChange,s=i.formPrefix;o&&((0,u.isNil)(s)?o(a.state,e,t):o(s,a.state))},r.validate=function(e){var t=r.makeForm().validate();r.setState({errors:t})},!t.form){var a=r.makeForm();t.onChange&&t.onChange({state:a.state})}return r}return function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)}(s,i.default.Component),a(s,[{key:"shouldComponentUpdate",value:function(e){return(e.form||{}).values!=(this.props.form||{}).values||(e.form||{}).errors!=(this.props.form||{}).errors}},{key:"render",value:function(){var e=this.props,r=e.form,a=(e.Form,function(e,t){var r={};for(var n in e)t.indexOf(n)>=0||Object.prototype.hasOwnProperty.call(e,n)&&(r[n]=e[n]);return r}(e,["form","Form"]));return(0,u.isNil)(r)&&(r=this.makeForm(this.props.forceValidation).state),i.default.createElement(t,n({},a,{form:r,onChange:this.handleChange2}))}}]),s}(),r.Form=e.form,s}}}})}); |
{ | ||
"name": "react-nested-validation", | ||
"version": "0.1.2", | ||
"version": "0.2.0", | ||
"description": "A toolkit for performing nested validation of React forms.", | ||
@@ -5,0 +5,0 @@ "esnext": "src/index.js", |
@@ -18,3 +18,3 @@ import React from 'react' | ||
if (props.onChange) { | ||
props.onChange(form.state) | ||
props.onChange({state: form.state}) | ||
} | ||
@@ -59,2 +59,32 @@ } | ||
handleChange2 = (payload, path) => { | ||
const {onChange, formPrefix} = this.props | ||
if (!isNil(formPrefix)) { | ||
console.debug('Deferring form update with prefix and path: ', formPrefix, path) | ||
if (!isNil(path)) { | ||
path = `${formPrefix}.${path}` | ||
} | ||
else { | ||
path = formPrefix | ||
} | ||
if (onChange) { | ||
onChange(payload, path) | ||
} | ||
} | ||
else { | ||
console.debug('Form changed with path and payload: ', path, payload) | ||
const form = this.makeForm() | ||
form.updateIn(payload, path) | ||
console.debug(' New state: ', form.state) | ||
if (onChange) { | ||
onChange({ | ||
state: form.state, | ||
values: form.flat, | ||
payload, | ||
path | ||
}) | ||
} | ||
} | ||
} | ||
handleChange = (name, value, options) => { | ||
@@ -65,3 +95,3 @@ const form = this.makeForm() | ||
if (onChange) { | ||
if (formPrefix === Form.MULTI || !isNil(formPrefix)) { | ||
if (!isNil(formPrefix)) { | ||
onChange(formPrefix, form.state) | ||
@@ -100,3 +130,3 @@ } | ||
form={form} | ||
onChange={this.handleChange} | ||
onChange={this.handleChange2} | ||
/> | ||
@@ -103,0 +133,0 @@ ) |
214
src/Form.js
import ErrorSet from './ErrorSet' | ||
import {capitalize} from './utils' | ||
@@ -8,10 +9,13 @@ /** | ||
*/ | ||
function mergeValues(form, values) { | ||
return { | ||
...form, | ||
function mergeValues(Form, state, values) { | ||
state = { | ||
...state, | ||
values: { | ||
...(form.values || {}), | ||
...(state.values || {}), | ||
...values | ||
} | ||
} | ||
const form = new Form(state) | ||
form.validate() | ||
return form.state | ||
} | ||
@@ -30,4 +34,2 @@ | ||
static MULTI = '' | ||
/** | ||
@@ -99,21 +101,51 @@ * Ensure an errors object has all standard fields. | ||
else { | ||
if (this.nested) { | ||
values = values || {} | ||
let newValues = { | ||
values = values || {} | ||
let newValues | ||
// If we've been given a set of fields to copy, use them. | ||
// Otherwise use all available fields. | ||
if (this.fields) { | ||
newValues = {} | ||
for (const fldName of this.fields) { | ||
newValues[fldName] = values[fldName] | ||
} | ||
} | ||
else { | ||
newValues = { | ||
...values | ||
} | ||
} | ||
if (this.nested) { | ||
for (const [fldName, Sub] of Object.entries(this.nested)) { | ||
// Check for a nested parser and use that if it exists. If | ||
// not, use the same flat values. | ||
let subValues | ||
const func = this[`initial${capitalize(fldName)}`] | ||
if (func) { | ||
subValues = func(values, fldName) | ||
} | ||
else { | ||
subValues = values | ||
} | ||
const sub = new Sub() | ||
newValues[fldName] = sub.parse(values[fldName], forceValidation) | ||
newValues[fldName] = sub.parse(subValues, forceValidation) | ||
} | ||
values = newValues | ||
} | ||
else { | ||
values = values || {} | ||
} | ||
values = { | ||
...(this.state.values || {}), | ||
...values | ||
...newValues | ||
} | ||
} | ||
// Call for any overloaded parsing. This is useful for things like | ||
// conversion to form select objects. This is preferable to | ||
// overlading `parse` as we want to do a single validation at the | ||
// end of value parsing. | ||
if (this.parseValues) { | ||
values = this.parseValues(values) | ||
} | ||
this.state = { | ||
@@ -126,7 +158,5 @@ ...this.state, | ||
// Handle multi-forms. Apply each multi-form to the same state we've | ||
// just calculated. | ||
for (const MultiForm of (this.multi || [])) { | ||
this.state = new MultiForm(this.state).parse(values, forceValidation) | ||
} | ||
// Render my flat version of values. Do this after we've recursed | ||
// so as to have all my values already rendered. | ||
this.render() | ||
@@ -137,39 +167,44 @@ this.validate(forceValidation) | ||
renderValues(values) { | ||
if (values === undefined) { | ||
values = this.state.values | ||
} | ||
render() { | ||
const {values} = this.state | ||
let flat | ||
if (Array.isArray(this.nested)) { | ||
const Sub = this.nested[0] | ||
values = (values || []).map(x => new Sub(x).renderValues()) | ||
flat = values.map(sub => sub.flat) | ||
} | ||
else { | ||
values = values || {} | ||
if (this.nested) { | ||
let newValues = { | ||
...values | ||
if (this.fields) { | ||
flat = {} | ||
for (const fldName of this.fields) { | ||
flat[fldName] = values[fldName] | ||
} | ||
for (const [fldName, Sub] of Object.entries(this.nested)) { | ||
const sub = new Sub(values[fldName]) | ||
newValues[fldName] = sub.renderValues() | ||
} | ||
values = newValues | ||
} | ||
if(this.multi) { | ||
values = values || {} | ||
let newValues = { | ||
else { | ||
flat = { | ||
...values | ||
} | ||
for (const Multi of this.multi) { | ||
newValues = { | ||
...newValues, | ||
...new Multi().renderValues(newValues) | ||
} | ||
for (const fldName of Object.keys(this.nested)) { | ||
const func = this[`render${capitalize(fldName)}`] | ||
const subFlat = this.state.values[fldName].flat | ||
if (func) { | ||
flat = func(flat, subFlat) | ||
} | ||
else { | ||
if (Array.isArray(subFlat)) { | ||
flat[fldName] = subFlat | ||
} | ||
else { | ||
flat = { | ||
...flat, | ||
...subFlat | ||
} | ||
} | ||
} | ||
values = newValues | ||
} | ||
else { | ||
values = values || {} | ||
} | ||
} | ||
this.state.flat = this.renderValues(flat) | ||
return flat | ||
} | ||
renderValues(values) { | ||
return values | ||
@@ -214,9 +249,2 @@ } | ||
// Perform any multi-form validation. | ||
for (const SubForm of this.multi || []) { | ||
const sub = new SubForm(values) | ||
sub.validate(force) | ||
errorSet.errors = ErrorSet.merge(errorSet.errors, sub.state.errors) | ||
} | ||
// If the form value is an array add up counts from the sub- | ||
@@ -247,2 +275,4 @@ // forms in the array. Otherwise check if any sub-form fields | ||
} | ||
return this.state | ||
} | ||
@@ -270,2 +300,51 @@ | ||
/** | ||
* TODO: This method has gotten a little crazy. | ||
*/ | ||
updateIn(payload, path) { | ||
if (path === undefined) { | ||
path = [] | ||
} else if (!Array.isArray(path)) { | ||
path = path.split('.') | ||
} | ||
if (path.length > 0) { | ||
let subKey | ||
let subForm | ||
if (Array.isArray(this.nested)) { | ||
subKey = parseInt(path[0]) | ||
const SubForm = this.nested[0] | ||
subForm = new SubForm(this.state.values[subKey]) | ||
} | ||
else { | ||
subKey = path[0] | ||
const SubForm = this.nested[subKey] | ||
subForm = new SubForm(this.state.values[subKey]) | ||
} | ||
subForm.updateIn(payload, path.slice(1)) | ||
this.updateValue(subKey, subForm.state) | ||
} | ||
else { | ||
if (Array.isArray(this.nested) && Array.isArray(payload)) { | ||
for (const item of payload) { | ||
this.updateValue(null, item) | ||
} | ||
} | ||
else { | ||
for (const [key, value] of Object.entries(payload)) { | ||
if (key.includes('.')) { | ||
const splitKey = key.split('.') | ||
this.updateIn( | ||
{[splitKey[splitKey.length - 1]]: value}, | ||
splitKey.slice(0, splitKey.length - 1) | ||
) | ||
} | ||
else { | ||
this.updateValue(key, value) | ||
} | ||
} | ||
} | ||
} | ||
this.render() | ||
} | ||
updateValue(name, value, options = {}) { | ||
@@ -276,2 +355,4 @@ let values = this.getValues() | ||
if (Array.isArray(values)) { | ||
const SubForm = this.nested[0] | ||
// TODO: Manage touched when removing an array item. | ||
@@ -289,5 +370,7 @@ let names | ||
if (name === null) { | ||
const subForm = new SubForm() | ||
subForm.parse(value) | ||
values = [ | ||
...values, | ||
Form.normalize({values: value}) | ||
subForm.state | ||
] | ||
@@ -304,3 +387,3 @@ } | ||
...values.slice(0, name), | ||
options.merge ? mergeValues(values[name], value) : value, | ||
options.merge ? mergeValues(this.nested[0], values[name], value) : value, | ||
...values.slice(name + 1) | ||
@@ -314,20 +397,7 @@ ] | ||
else { | ||
if (name === Form.MULTI) { | ||
values = { | ||
...values, | ||
...value.values | ||
} | ||
touched = { | ||
...touched, | ||
...(value.touched || {}) | ||
} | ||
errors = value.errors | ||
values = { | ||
...values, | ||
[name]: value | ||
} | ||
else { | ||
values = { | ||
...values, | ||
[name]: value | ||
} | ||
touched = this.updateTouched(name, value, this.state.touched) | ||
} | ||
touched = this.updateTouched(name, value, this.state.touched) | ||
} | ||
@@ -334,0 +404,0 @@ this.state = { |
@@ -8,5 +8,11 @@ import Form from '../src/Form' | ||
class ParentForm extends Form { | ||
nested = { | ||
sub: SubForm | ||
} | ||
initialSub(values) { | ||
return values.sub | ||
} | ||
} | ||
@@ -21,18 +27,23 @@ | ||
class FormA extends Form { | ||
nested = { | ||
a: ArrayForm | ||
} | ||
initialA(values) { | ||
return values.a | ||
} | ||
} | ||
class FormB extends Form { | ||
nested = { | ||
b: ArrayForm | ||
} | ||
} | ||
class MultiForm extends Form { | ||
multi = [ | ||
FormA, | ||
FormB | ||
] | ||
initialA(values) { | ||
return values.b | ||
} | ||
} | ||
@@ -54,4 +65,3 @@ | ||
FormB, | ||
MultiForm, | ||
RequiredFieldForm | ||
} |
@@ -5,3 +5,3 @@ import {expect} from 'code' | ||
import {ArrayForm, ParentForm, RequiredFieldForm} from './forms' | ||
import {FormA, ArrayForm, ParentForm, RequiredFieldForm} from './forms' | ||
@@ -136,2 +136,64 @@ describe('Form', function() { | ||
describe('updateIn', function() { | ||
it('works for basic updates', function() { | ||
let form = new FormA() | ||
form.parse({ | ||
f0: 'v0', | ||
a: [{f1: 'v1'}] | ||
}) | ||
form.updateIn({f0: 'v2'}) | ||
form.updateIn({f1: 'v3'}, 'a.0') | ||
form.updateIn({f2: 'v4'}, 'a.0') | ||
expect(form.state.flat).to.equal({ | ||
f0: 'v2', | ||
a: [{f1: 'v3', f2: 'v4'}] | ||
}) | ||
}) | ||
it('works for array insertion', function() { | ||
let form = new FormA() | ||
form.parse({ | ||
f0: 'v0', | ||
a: [ | ||
{f1: 'v1'}, | ||
{f2: 'v2'} | ||
] | ||
}) | ||
form.updateIn([{f3: 'v3'}, {f4: 'v4'}], 'a') | ||
expect(form.state.flat).to.equal({ | ||
f0: 'v0', | ||
a: [ | ||
{f1: 'v1'}, | ||
{f2: 'v2'}, | ||
{f3: 'v3'}, | ||
{f4: 'v4'} | ||
] | ||
}) | ||
}) | ||
it('works for array spread', function() { | ||
let form = new FormA() | ||
form.parse({ | ||
f0: 'v0', | ||
a: [ | ||
{f1: 'v1'}, | ||
{f2: 'v2'} | ||
] | ||
}) | ||
form.updateIn({ | ||
'0.f1': 'v3', | ||
'1.f3': 'v4' | ||
}, 'a') | ||
expect(form.state.flat).to.equal({ | ||
f0: 'v0', | ||
a: [ | ||
{f1: 'v3'}, | ||
{f2: 'v2', f3: 'v4'} | ||
] | ||
}) | ||
}) | ||
}) | ||
}) |
@@ -34,4 +34,4 @@ import React from 'react' | ||
handleChange = form => { | ||
this.setState({form}) | ||
handleChange = ({state}) => { | ||
this.setState({form: state}) | ||
} | ||
@@ -38,0 +38,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1635508
9419
36