Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

immutable-form

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

immutable-form - npm Package Compare versions

Comparing version 0.0.6 to 0.0.7

2

lib/index.js

@@ -1,1 +0,1 @@

!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t(require("immutable"),require("lodash"),require("redux-logger"),require("react"),require("redux"),require("redux-immutable")):"function"==typeof define&&define.amd?define(["immutable","lodash","redux-logger","react","redux","redux-immutable"],t):"object"==typeof exports?exports["immutable-form"]=t(require("immutable"),require("lodash"),require("redux-logger"),require("react"),require("redux"),require("redux-immutable")):e["immutable-form"]=t(e.immutable,e.lodash,e["redux-logger"],e.react,e.redux,e["redux-immutable"])}(this,function(e,t,r,n,o,i){return function(e){function t(n){if(r[n])return r[n].exports;var o=r[n]={exports:{},id:n,loaded:!1};return e[n].call(o.exports,o,o.exports,t),o.loaded=!0,o.exports}var r={};return t.m=e,t.c=r,t.p="",t(0)}([function(e,t,r){"use strict";function n(e){return e&&e.__esModule?e:{default:e}}Object.defineProperty(t,"__esModule",{value:!0}),t.connectForm=t.FormCollection=t.Form=void 0;var o=r(5),i=n(o),a=r(1),u=n(a),s=r(6),l=n(s);t.Form=i.default,t.FormCollection=u.default,t.connectForm=l.default},function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var n=r(2),o={forms:(0,n.Map)({}),add:function(e){this.forms=this.forms.set(e.name,e)},remove:function(e){this.forms=this.forms.delete(e)},get:function(e){return this.forms.get(e)},reset:function(){this.forms=(0,n.Map)({})}};t.default=o},function(e,t){e.exports=require("immutable")},function(e,t){e.exports=require("lodash")},function(e,t){e.exports=require("redux-logger")},function(e,t,r){"use strict";function n(e){return e&&e.__esModule?e:{default:e}}function o(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function i(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}Object.defineProperty(t,"__esModule",{value:!0}),t.filterValidate=void 0;var 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}}(),u=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},s="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},l=r(2),f=r(3),c=r(1),d=n(c),h=r(7),p=n(h),v="immutable-form/",m=v+"RESET_FORM",y=v+"CLEAR_ERRORS",b=v+"ADD_ERROR",g=v+"SET_FIELD",S=v+"REMOVE_FIELD",k=v+"/RESET_FIELD",x={addToFormCollection:!0,logger:!1,store:null},E=(0,l.Map)({fields:(0,l.Map)(),errors:(0,l.Stack)()}),F=(0,l.Map)({value:"",errors:(0,l.Stack)()}),_=function(e){return function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:e,r=arguments[1];switch(r.type){case g:var n=function(){var e=r.payload,n=e.field,o=e.value,i=e.error,a=["fields",n],u=t.hasIn(a)?t:t.setIn(a,F);return(o||""===o)&&(u=u.setIn([].concat(a,["value"]),o)),i?u=u.hasIn([].concat(a,["errors"]))?u.updateIn([].concat(a,["errors"]),function(e){return e.push(i)}):u.setIn([].concat(a,["errors"]),l.Stack.of(i)):null===i&&(u=u.setIn([].concat(a,["errors"]),(0,l.Stack)())),{v:u}}();if("object"===("undefined"==typeof n?"undefined":s(n)))return n.v;case k:var o=r.payload.field;return t.setIn(["fields",o],F);case S:var i=r.payload.field;return t.deleteIn(["fields",i]);case b:var a=r.payload.error,u=t.has("errors")?t.get("errors").push(a):l.Stack.of(a);return t.set("errors",u);case y:return t.set("errors",(0,l.Stack)());case m:return t.update(function(){return r.payload.initialState})}return t}},O=function(e){var t={},r=u({},e),n={},o=[];(0,f.has)(r,"fields")&&(t=(0,f.keys)(r.fields).reduce(function(e,t){return(0,f.hasIn)(r,["fields",t,"validate"])&&(n[t]=Array.isArray(r.fields[t].validate)?r.fields[t].validate:[r.fields[t].validate],delete r.fields[t].validate),u({},e,i({},t,r.fields[t]))},{})),(0,f.has)(r,"validate")&&(o=Array.isArray(r.validate)?r.validate:[r.validate],delete r.validate);var a=u({},r,{fields:t});return{filteredState:a,fieldValidators:n,formValidators:o}};t.filterValidate=O;var w=function(){function e(t,n){var i=this,a=arguments.length>2&&void 0!==arguments[2]?arguments[2]:x;if(o(this,e),this.name=t,this.options=a,this.fieldValidators={},this.formValidators=[],"string"!=typeof this.name||0===this.name.trim().length)throw new Error("Form must have a name");var u=[];if(this.options.logger){var s=r(4);u.push(s())}var c=void 0,h=(0,f.cloneDeep)(n);(0,f.isEmpty)(h)?c=E:!function(){var e=O(h),t=e.filteredState,r=e.fieldValidators,n=e.formValidators;i.fieldValidators=r,i.formValidators=n;var o=(0,f.has)(t,"fields")?(0,f.keys)(t.fields).reduce(function(e,r){return e.set(r,(0,l.Map)({value:(0,f.has)(t.fields[r],"value")?t.fields[r].value:"",errors:(0,f.has)(t.fields[r],"errors")?(0,l.Stack)(t.fields[r].errors):(0,l.Stack)()}))},(0,l.Map)()):(0,l.Map)();c=(0,l.Map)({fields:o,errors:(0,f.has)(t,"errors")?(0,l.Stack)(t.errors):(0,l.Stack)()})}(),this.initialState=(0,f.cloneDeep)(c),this.store=x.store||(0,p.default)({reducers:{form:_(c)},middleware:u}),a.addToFormCollection&&d.default.add(this)}return a(e,[{key:"getState",value:function(){return this.store.getState().get("form")}},{key:"setField",value:function(e,t,r){this.store.dispatch({type:g,payload:{field:e,value:t,error:r}})}},{key:"getField",value:function(e){return this.store.getState().getIn(["form","fields",e],F)}},{key:"removeField",value:function(e){this.store.dispatch({type:S,payload:{field:e}})}},{key:"resetField",value:function(e){this.store.dispatch({type:k,payload:{field:e}})}},{key:"addError",value:function(e){this.store.dispatch({type:b,payload:{error:e}})}},{key:"clearErrors",value:function(){var e=this;this.getFieldValues().forEach(function(t,r){return e.setField(r,null,null)}),this.store.dispatch({type:y})}},{key:"hasErrors",value:function(){var e=this.getState();return e.get("fields").reduce(function(e,t){return e||e===!1&&t.has("errors")&&t.get("errors").size>0},!1)||e.has("errors")&&e.get("errors").size>0}},{key:"reset",value:function(){this.store.dispatch({type:m,payload:{initialState:this.initialState}})}},{key:"saveInitialState",value:function(){this.initialState=this.getState()}},{key:"getFields",value:function(){return this.getState().get("fields",(0,l.Map)())}},{key:"getFieldValues",value:function(){var e=this.getState().get("fields",(0,l.Map)()).map(function(e){return e.has("value")?e.get("value"):""});return e}},{key:"getFieldErrors",value:function(){var e=this.getState().get("fields",(0,l.Map)()).map(function(e){return e.has("errors")?e.get("errors"):(0,l.Stack)()});return e}},{key:"validate",value:function(){var e=this;return this.formValidators.forEach(function(t){var r=t(e.getState(),{form:e});(0,f.isString)(r)&&e.addError(r)}),(0,f.keys)(this.fieldValidators).forEach(function(t){e.fieldValidators[t].forEach(function(r){var n=e.getField(t).get("value",""),o=r(n,{key:t,form:e});(0,f.isString)(o)&&e.setField(t,null,o)})}),!this.hasErrors()}},{key:"setLoad",value:function(e){return this.loadPromise="function"==typeof e?e:function(){return e},this}},{key:"load",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:this.loadPromise(this);return e}},{key:"setSubmit",value:function(e){return this.submitPromise="function"==typeof e?e:function(){return e},this}},{key:"setOnSuccess",value:function(e){return this.onSuccess=e,this}},{key:"setOnFailure",value:function(e){return this.onFailure=e,this}},{key:"submit",value:function(){var e=this,t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:this.submitPromise(this);return this.clearErrors(),new Promise(function(r,n){e.validate()?t.then(function(t){e.onSuccess&&e.onSuccess(t,e),r(t)}).catch(function(t){e.onFailure&&e.onFailure(t,e),n(t)}):n("Validation failed")})}}]),e}();t.default=w},function(e,t,r){"use strict";function n(e){return e&&e.__esModule?e:{default:e}}function o(e){return function(){var t=e.apply(this,arguments);return new Promise(function(e,r){function n(o,i){try{var a=t[o](i),u=a.value}catch(e){return void r(e)}return a.done?void e(u):Promise.resolve(u).then(function(e){n("next",e)},function(e){n("throw",e)})}return n("next")})}}function i(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function a(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}function u(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)}Object.defineProperty(t,"__esModule",{value:!0});var 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=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}}(),f=r(8),c=n(f),d=r(3),h=r(1),p=n(h),v=function(e){return function(t){var r=function(r){function n(e,t){i(this,n);var r=a(this,(n.__proto__||Object.getPrototypeOf(n)).call(this,e,t));return r.state={loaded:!1},r}return u(n,r),l(n,[{key:"componentDidMount",value:function(){function t(){return r.apply(this,arguments)}var r=o(regeneratorRuntime.mark(function t(){var r=this;return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:if(this.form=((0,d.isFunction)(e)?e:function(){return e})(),this.unsubscribe=this.form.store.subscribe(function(){r.forceUpdate()}),!(0,d.isFunction)(this.form.loadPromise)){t.next=8;break}return t.next=5,this.form.load();case 5:this.setState({loaded:!0}),t.next=9;break;case 8:this.setState({loaded:!0});case 9:case"end":return t.stop()}},t,this)}));return t}()},{key:"componentWillUnmount",value:function(){this.unsubscribe(),p.default.remove(this.form.name),this.setState({loaded:!1})}},{key:"render",value:function(){if(!this.state.loaded)return null;var e=this.form.getFields();return c.default.cloneElement(t,s({},this.props,{fields:e,form:this.form}))}}]),n}(c.default.Component);return r}};t.default=v},function(e,t,r){"use strict";function n(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)}Object.defineProperty(t,"__esModule",{value:!0});var o=r(9),i=r(2),a=r(10);t.default=function(e){var t=e.reducers,r=void 0===t?{}:t,u=e.middleware,s=void 0===u?[]:u,l=e.state,f=void 0===l?(0,i.fromJS)({}):l;return(0,o.createStore)((0,a.combineReducers)(r),f,(0,o.compose)(o.applyMiddleware.apply(void 0,n(s))))}},function(e,t){e.exports=require("react")},function(e,t){e.exports=require("redux")},function(e,t){e.exports=require("redux-immutable")}])});
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t(require("immutable"),require("lodash"),require("redux-logger"),require("react"),require("redux"),require("redux-immutable")):"function"==typeof define&&define.amd?define(["immutable","lodash","redux-logger","react","redux","redux-immutable"],t):"object"==typeof exports?exports["immutable-form"]=t(require("immutable"),require("lodash"),require("redux-logger"),require("react"),require("redux"),require("redux-immutable")):e["immutable-form"]=t(e.immutable,e.lodash,e["redux-logger"],e.react,e.redux,e["redux-immutable"])}(this,function(e,t,r,n,i,o){return function(e){function t(n){if(r[n])return r[n].exports;var i=r[n]={exports:{},id:n,loaded:!1};return e[n].call(i.exports,i,i.exports,t),i.loaded=!0,i.exports}var r={};return t.m=e,t.c=r,t.p="",t(0)}([function(e,t,r){"use strict";function n(e){return e&&e.__esModule?e:{default:e}}Object.defineProperty(t,"__esModule",{value:!0}),t.connectForm=t.FormCollection=t.Form=void 0;var i=r(5),o=n(i),a=r(1),u=n(a),s=r(6),l=n(s);t.Form=o.default,t.FormCollection=u.default,t.connectForm=l.default},function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var n=r(2),i={forms:(0,n.Map)({}),add:function(e){this.forms=this.forms.set(e.name,e)},remove:function(e){this.forms=this.forms.delete(e)},get:function(e){return this.forms.get(e)},reset:function(){this.forms=(0,n.Map)({})}};t.default=i},function(e,t){e.exports=require("immutable")},function(e,t){e.exports=require("lodash")},function(e,t){e.exports=require("redux-logger")},function(e,t,r){"use strict";function n(e){return e&&e.__esModule?e:{default:e}}function i(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function o(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}Object.defineProperty(t,"__esModule",{value:!0}),t.filterValidate=void 0;var 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}}(),u=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},s="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},l=r(2),f=r(3),c=r(1),d=n(c),h=r(7),p=n(h),v="immutable-form/",m=v+"RESET_FORM",y=v+"CLEAR_ERRORS",b=v+"ADD_ERROR",g=v+"SET_FIELD",S=v+"REMOVE_FIELD",k=v+"/RESET_FIELD",x={addToFormCollection:!0,logger:!1,store:null},F=(0,l.Map)({fields:(0,l.Map)(),errors:(0,l.Stack)()}),E=(0,l.Map)({value:"",errors:(0,l.Stack)()}),_=function(e){return function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:e,r=arguments[1];switch(r.type){case g:var n=function(){var e=r.payload,n=e.field,i=e.value,o=e.error,a=["fields",n],u=t.hasIn(a)?t:t.setIn(a,E);return(i||""===i)&&(u=u.setIn([].concat(a,["value"]),i)),o?u=u.hasIn([].concat(a,["errors"]))?u.updateIn([].concat(a,["errors"]),function(e){return e.push(o)}):u.setIn([].concat(a,["errors"]),l.Stack.of(o)):null===o&&(u=u.setIn([].concat(a,["errors"]),(0,l.Stack)())),{v:u}}();if("object"===("undefined"==typeof n?"undefined":s(n)))return n.v;case k:var i=r.payload.field;return t.setIn(["fields",i],E);case S:var o=r.payload.field;return t.deleteIn(["fields",o]);case b:var a=r.payload.error,u=t.has("errors")?t.get("errors").push(a):l.Stack.of(a);return t.set("errors",u);case y:return t.set("errors",(0,l.Stack)());case m:return t.update(function(){return r.payload.initialState})}return t}},w=function(e){var t={},r=u({},e),n={},i=[];(0,f.has)(r,"fields")&&(t=(0,f.keys)(r.fields).reduce(function(e,t){return(0,f.hasIn)(r,["fields",t,"validate"])&&(n[t]=Array.isArray(r.fields[t].validate)?r.fields[t].validate:[r.fields[t].validate],delete r.fields[t].validate),u({},e,o({},t,r.fields[t]))},{})),(0,f.has)(r,"validate")&&(i=Array.isArray(r.validate)?r.validate:[r.validate],delete r.validate);var a=u({},r,{fields:t});return{filteredState:a,fieldValidators:n,formValidators:i}};t.filterValidate=w;var O=function(){function e(t,n){var o=this,a=arguments.length>2&&void 0!==arguments[2]?arguments[2]:x;if(i(this,e),this.name=t,this.options=a,this.fieldValidators={},this.formValidators=[],"string"!=typeof this.name||0===this.name.trim().length)throw new Error("Form must have a name");var u=[];if(this.options.logger){var s=r(4);u.push(s())}var c=void 0,h=(0,f.cloneDeep)(n);(0,f.isEmpty)(h)?c=F:!function(){var e=w(h),t=e.filteredState,r=e.fieldValidators,n=e.formValidators;o.fieldValidators=r,o.formValidators=n;var i=(0,f.has)(t,"fields")?(0,f.keys)(t.fields).reduce(function(e,r){return e.set(r,(0,l.Map)({value:(0,f.has)(t.fields[r],"value")?t.fields[r].value:"",errors:(0,f.has)(t.fields[r],"errors")?(0,l.Stack)(t.fields[r].errors):(0,l.Stack)()}))},(0,l.Map)()):(0,l.Map)();c=(0,l.Map)({fields:i,errors:(0,f.has)(t,"errors")?(0,l.Stack)(t.errors):(0,l.Stack)()})}(),this.initialState=(0,f.cloneDeep)(c),this.store=x.store||(0,p.default)({reducers:{form:_(c)},middleware:u}),a.addToFormCollection&&d.default.add(this)}return a(e,[{key:"getState",value:function(){return this.store.getState().get("form")}},{key:"setField",value:function(e,t,r){this.store.dispatch({type:g,payload:{field:e,value:t,error:r}})}},{key:"addFieldValidator",value:function(e,t){this.fieldValidators[e]||(this.fieldValidators[e]=[]),this.fieldValidators[e].push(t)}},{key:"removeFieldValidator",value:function(e,t){var r=this.fieldValidators[e];if(r){var n=r.indexOf(t);n!==-1&&r.splice(n,1)}}},{key:"getField",value:function(e){return this.store.getState().getIn(["form","fields",e],E)}},{key:"removeField",value:function(e){this.store.dispatch({type:S,payload:{field:e}})}},{key:"resetField",value:function(e){this.store.dispatch({type:k,payload:{field:e}})}},{key:"addError",value:function(e){this.store.dispatch({type:b,payload:{error:e}})}},{key:"clearErrors",value:function(){var e=this;this.getFieldValues().forEach(function(t,r){return e.setField(r,null,null)}),this.store.dispatch({type:y})}},{key:"hasErrors",value:function(){var e=this.getState();return e.get("fields").reduce(function(e,t){return e||e===!1&&t.has("errors")&&t.get("errors").size>0},!1)||e.has("errors")&&e.get("errors").size>0}},{key:"reset",value:function(){this.store.dispatch({type:m,payload:{initialState:this.initialState}})}},{key:"saveInitialState",value:function(){this.initialState=this.getState()}},{key:"getFields",value:function(){return this.getState().get("fields",(0,l.Map)())}},{key:"getFieldValues",value:function(){var e=this.getState().get("fields",(0,l.Map)()).map(function(e){return e.has("value")?e.get("value"):""});return e}},{key:"getFieldErrors",value:function(){var e=this.getState().get("fields",(0,l.Map)()).map(function(e){return e.has("errors")?e.get("errors"):(0,l.Stack)()});return e}},{key:"validate",value:function(){var e=this;return this.formValidators.forEach(function(t){var r=t(e.getState(),{form:e});(0,f.isString)(r)&&e.addError(r)}),(0,f.keys)(this.fieldValidators).forEach(function(t){e.fieldValidators[t].forEach(function(r){var n=e.getField(t).get("value",""),i=r(n,{key:t,form:e});(0,f.isString)(i)&&e.setField(t,null,i)})}),!this.hasErrors()}},{key:"setLoad",value:function(e){return this.loadPromise="function"==typeof e?e:function(){return e},this}},{key:"load",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:this.loadPromise(this);return e}},{key:"setSubmit",value:function(e){return this.submitPromise="function"==typeof e?e:function(){return e},this}},{key:"setOnSuccess",value:function(e){return this.onSuccess=e,this}},{key:"setOnFailure",value:function(e){return this.onFailure=e,this}},{key:"submit",value:function(){var e=this,t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:this.submitPromise(this);return this.clearErrors(),new Promise(function(r,n){e.validate()?t.then(function(t){e.onSuccess&&e.onSuccess(t,e),r(t)}).catch(function(t){e.onFailure&&e.onFailure(t,e),n(t)}):n("Validation failed")})}}]),e}();t.default=O},function(e,t,r){"use strict";function n(e){return e&&e.__esModule?e:{default:e}}function i(e){return function(){var t=e.apply(this,arguments);return new Promise(function(e,r){function n(i,o){try{var a=t[i](o),u=a.value}catch(e){return void r(e)}return a.done?void e(u):Promise.resolve(u).then(function(e){n("next",e)},function(e){n("throw",e)})}return n("next")})}}function o(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function a(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}function u(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)}Object.defineProperty(t,"__esModule",{value:!0});var 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=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}}(),f=r(8),c=n(f),d=r(3),h=r(1),p=n(h),v=function(e){return function(t){var r=function(r){function n(e,t){o(this,n);var r=a(this,(n.__proto__||Object.getPrototypeOf(n)).call(this,e,t));return r.state={loaded:!1},r}return u(n,r),l(n,[{key:"componentDidMount",value:function(){function e(){return t.apply(this,arguments)}var t=i(regeneratorRuntime.mark(function e(){return regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this.reloadForm(this.props);case 2:case"end":return e.stop()}},e,this)}));return e}()},{key:"componentWillReceiveProps",value:function(){function t(e){return r.apply(this,arguments)}var r=i(regeneratorRuntime.mark(function t(r){return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:if(!(0,d.isFunction)(e)){t.next=4;break}return this.setState({loaded:!1}),t.next=4,this.reloadForm(r);case 4:case"end":return t.stop()}},t,this)}));return t}()},{key:"componentWillUnmount",value:function(){this.unsubscribe(),p.default.remove(this.form.name),this.setState({loaded:!1})}},{key:"reloadForm",value:function(){function t(e){return r.apply(this,arguments)}var r=i(regeneratorRuntime.mark(function t(r){var n=this;return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:if(this.form=((0,d.isFunction)(e)?e:function(){return e})(r),this.unsubscribe=this.form.store.subscribe(function(){n.forceUpdate()}),!(0,d.isFunction)(this.form.loadPromise)){t.next=5;break}return t.next=5,this.form.load();case 5:this.setState({loaded:!0});case 6:case"end":return t.stop()}},t,this)}));return t}()},{key:"render",value:function(){if(!this.state.loaded)return null;var e=this.form.getFields();return c.default.cloneElement(t,s({},this.props,{fields:e,form:this.form}))}}]),n}(c.default.Component);return r}};t.default=v},function(e,t,r){"use strict";function n(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)}Object.defineProperty(t,"__esModule",{value:!0});var i=r(9),o=r(2),a=r(10);t.default=function(e){var t=e.reducers,r=void 0===t?{}:t,u=e.middleware,s=void 0===u?[]:u,l=e.state,f=void 0===l?(0,o.fromJS)({}):l;return(0,i.createStore)((0,a.combineReducers)(r),f,(0,i.compose)(i.applyMiddleware.apply(void 0,n(s))))}},function(e,t){e.exports=require("react")},function(e,t){e.exports=require("redux")},function(e,t){e.exports=require("redux-immutable")}])});
{
"name": "immutable-form",
"version": "0.0.6",
"version": "0.0.7",
"description": "",

@@ -5,0 +5,0 @@ "main": "lib/index.js",

@@ -40,8 +40,12 @@ # immutable-form

const createUserPromise = Promise.resolve({
userId: '123'
})
userId: '123',
});
const userForm = new Form('userForm', {
// These props could be anything. This function returns a new Form as a function of props.
const mapPropsToForm = ({
hasUsername,
hasPassword,
}) => new Form('userForm', {
fields: {
username: {
username: (hasUsername || undefined) && {
// All these fields are optional,

@@ -52,10 +56,10 @@ value: 'some value', // Set an initial value

username => username.trim().length === 0 && 'Username cannot be be empty',
]
}
password: {
],
},
password: (hasPassword || undefined) && {
validate: [
password => password.trim().length < 6 && 'Password must be longer than 6 characters'
]
}
}
password => password.trim().length < 6 && 'Password must be longer than 6 characters',
],
},
},
})

@@ -67,5 +71,4 @@ // Send a request to the server

// If the promise is rejected, do something.
.setOnFailure((err, form) => err)
.setOnFailure((err, form) => err);
const UserForm = ({

@@ -75,4 +78,4 @@ // fields is a an Immutable Map

// This is the `Form` object, you can use it do extra actions such submitting the form.
form
}) =>
form,
}) => (
<div>

@@ -93,4 +96,5 @@ <input

</div>
);
export default connectForm(userForm)(UserForm);
export default connectForm(mapPropsToForm)(UserForm);

@@ -110,2 +114,3 @@ ```

// Note that the below example does not use a function to create a new Form. See above for that use case.
const userForm = new Form('userForm', {

@@ -116,10 +121,10 @@ fields: {

username => username.trim().length === 0 && 'Username cannot be be empty',
]
}
],
},
password: {
validate: [
password => password.trim().length < 6 && 'Password must be longer than 6 characters'
]
}
}
password => password.trim().length < 6 && 'Password must be longer than 6 characters',
],
},
},
})

@@ -146,5 +151,5 @@ .setLoad(form => loadFormPromise.then(({ username, password }) => {

const Form = new Form('form', {}, {
logger: true, // Will enable redux-logger (false by default)
addToFormCollection: true, // Refrains from adding a reference to FormCollection, (true by default)
})
logger: true, // Will enable redux-logger (false by default)
addToFormCollection: true, // Refrains from adding a reference to FormCollection, (true by default)
});
```
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc