diff --git a/packages/drivers/form/README.md b/packages/drivers/form/README.md index 2d95118..074d135 100644 --- a/packages/drivers/form/README.md +++ b/packages/drivers/form/README.md @@ -52,3 +52,8 @@ const mapStore = inject((store, { formName, field }, drivers) => { export default mapStore(Component) ``` + +## Helpers + - `drivers.form.getUpdatedValues(action)` will return an _object_ of all updated pair (field name -> field value) for the given action. + - `drivers.form.getUpdatedEntries(action)` will return an _array_ of all updated pair (field name -> field value) for the given action. + - `drivers.form.getUpdatedFieldNames(action)` will return an _array_ of all updated field names for the given action. diff --git a/packages/drivers/form/__snapshots__/index.spec.js.snap b/packages/drivers/form/__snapshots__/index.spec.js.snap index 1f51862..1ca545f 100644 --- a/packages/drivers/form/__snapshots__/index.spec.js.snap +++ b/packages/drivers/form/__snapshots__/index.spec.js.snap @@ -1021,6 +1021,39 @@ Object { } `; +exports[`drivers/form bulk mode should returns updated field entries 1`] = ` +Object { + "entries": Array [ + Array [ + "first", + true, + ], + Array [ + "second", + "ok", + ], + ], +} +`; + +exports[`drivers/form bulk mode should returns updated field names 1`] = ` +Object { + "fieldNames": Array [ + "first", + "second", + ], +} +`; + +exports[`drivers/form bulk mode should returns updated field names and values 1`] = ` +Object { + "fieldValues": Object { + "first": true, + "second": "ok", + }, +} +`; + exports[`drivers/form bulk mode should set errors 1`] = ` Object { "state": Object { diff --git a/packages/drivers/form/dist/index.es.js b/packages/drivers/form/dist/index.es.js index 9048aa3..23f4e12 100644 --- a/packages/drivers/form/dist/index.es.js +++ b/packages/drivers/form/dist/index.es.js @@ -1 +1 @@ -import{types}from"k-ramel";function _defineProperty(e,r,t){return r in e?Object.defineProperty(e,r,{value:t,enumerable:!0,configurable:!0,writable:!0}):e[r]=t,e}function _objectSpread(e){for(var r=1;r0&&void 0!==arguments[0]?arguments[0]:{};return e[o].addOrUpdate(_objectSpread({},i,(_defineProperty(u={},r,n),_defineProperty(u,t,Object.keys(i)),u)))}};return{set:o("values"),setErrors:o("errors"),update:function(o){return function(u){var i;return e.values.update((_defineProperty(i={},r,n),_defineProperty(i,t,[o]),_defineProperty(i,o,u),i))}},clearErrors:function(){return e.errors.reset(n)},remove:function(){e.values.remove(n),e.errors.remove(n)}}}}},bulkActions=function(e){var r=e.keyName,t=e.keyFields;return function(e){var n=function(n){return function(o){return e[n].addOrUpdate(o.map(function(e){var n,o=e.name,u=e.values;return _objectSpread({},u,(_defineProperty(n={},r,o),_defineProperty(n,t,Object.keys(u)),n))}))}};return{set:n("values"),setErrors:n("errors"),clearErrors:e.errors.remove,remove:function(r){e.values.remove(r),e.errors.remove(r)}}}},selectors=function(e){var r=e.keyName;return function(e){return function(t){var n=function(n){return function(o){var u=e[n].get(t);if(!u)return o?"":{};if(!o&&u){var i=_objectSpread({},u);return delete i[r],i}var a=u[o];return null==a?"":a}};return{exists:function(){return!!e.values.get(t)},get:n("values"),getErrors:n("errors")}}}},utils=function(e){return{find:function(r){return e.values.getKeys().filter(function(e){return e.match(r)})}}},form=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},r=e.path,t=void 0===r?"form":r,n=e.getState,o=void 0===n?function(e){return e.form}:n,u=e.key,i=void 0===u?"@@form-name":u,a=i,c="@@form";"@@form-name"!==i&&(c=i);var f="".concat(c,"-fields");return{getReducer:function(){return{path:t,reducer:{values:types.keyValue({key:a}),errors:types.keyValue({key:a})}}},getDriver:function(e){var r=o(e);return Object.assign(function(e){return _objectSpread({},actions({keyName:a,keyFields:f})(r)(e),selectors({keyName:a,keyFields:f})(r)(e))},bulkActions({keyName:a,keyFields:f})(r),utils(r))}}};export default form; +import{types}from"k-ramel";function _defineProperty(e,r,t){return r in e?Object.defineProperty(e,r,{value:t,enumerable:!0,configurable:!0,writable:!0}):e[r]=t,e}function _objectSpread(e){for(var r=1;r0&&void 0!==arguments[0]?arguments[0]:{};return e[o].addOrUpdate(_objectSpread({},a,(_defineProperty(u={},r,n),_defineProperty(u,t,Object.keys(a)),u)))}};return{set:o("values"),setErrors:o("errors"),update:function(o){return function(u){var a;return e.values.update((_defineProperty(a={},r,n),_defineProperty(a,t,[o]),_defineProperty(a,o,u),a))}},clearErrors:function(){return e.errors.reset(n)},remove:function(){e.values.remove(n),e.errors.remove(n)}}}}},bulkActions=function(e){var r=e.keyName,t=e.keyFields;return function(e){var n=function(n){return function(o){return e[n].addOrUpdate(o.map(function(e){var n,o=e.name,u=e.values;return _objectSpread({},u,(_defineProperty(n={},r,o),_defineProperty(n,t,Object.keys(u)),n))}))}};return{set:n("values"),setErrors:n("errors"),clearErrors:e.errors.remove,remove:function(r){e.values.remove(r),e.errors.remove(r)}}}},selectors=function(e){var r=e.keyName;return function(e){return function(t){var n=function(n){return function(o){var u=e[n].get(t);if(!u)return o?"":{};if(!o&&u){var a=_objectSpread({},u);return delete a[r],a}var i=u[o];return null==i?"":i}};return{exists:function(){return!!e.values.get(t)},get:n("values"),getErrors:n("errors")}}}},utils=function(e){return{find:function(r){return e.values.getKeys().filter(function(e){return e.match(r)})}}},getUpdatedValues=function(e){var r=e.keyName,t=e.keyFields;return function(e){var n=_objectSpread({},e.payload);return delete n[r],delete n[t],n}},getUpdatedFieldNames=function(e){var r=e.keyFields;return function(e){return e.payload[r]}},getUpdatedEntries=function(e){return function(r){return Object.entries(getUpdatedValues(e)(r))}},helpers=function(e){return{getUpdatedFieldNames:getUpdatedFieldNames(e),getUpdatedValues:getUpdatedValues(e),getUpdatedEntries:getUpdatedEntries(e)}},form=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},r=e.path,t=void 0===r?"form":r,n=e.getState,o=void 0===n?function(e){return e.form}:n,u=e.key,a=void 0===u?"@@form-name":u,i=a,c="@@form";"@@form-name"!==a&&(c=a);var s="".concat(c,"-fields");return{getReducer:function(){return{path:t,reducer:{values:types.keyValue({key:i}),errors:types.keyValue({key:i})}}},getDriver:function(e){var r=o(e);return Object.assign(function(e){return _objectSpread({},actions({keyName:i,keyFields:s})(r)(e),selectors({keyName:i,keyFields:s})(r)(e))},bulkActions({keyName:i,keyFields:s})(r),utils(r),helpers({keyName:i,keyFields:s}))}}};export default form; diff --git a/packages/drivers/form/dist/index.umd.js b/packages/drivers/form/dist/index.umd.js index 196ba8a..fc31111 100644 --- a/packages/drivers/form/dist/index.umd.js +++ b/packages/drivers/form/dist/index.umd.js @@ -1 +1 @@ -!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?module.exports=r(require("k-ramel")):"function"==typeof define&&define.amd?define(["k-ramel"],r):(e=e||self)["@k-ramel/driver-form"]=r(e.kRamel)}(this,function(e){"use strict";function r(e,r,n){return r in e?Object.defineProperty(e,r,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[r]=n,e}function n(e){for(var n=1;n0&&void 0!==arguments[0]?arguments[0]:{},u=t.path,o=void 0===u?"form":u,i=t.getState,a=void 0===i?function(e){return e.form}:i,f=t.key,c=void 0===f?"@@form-name":f,s=c,l="@@form";"@@form-name"!==c&&(l=c);var v="".concat(l,"-fields");return{getReducer:function(){return{path:o,reducer:{values:e.types.keyValue({key:s}),errors:e.types.keyValue({key:s})}}},getDriver:function(e){var t=a(e);return Object.assign(function(e){return n({},function(e){var t=e.keyName,u=e.keyFields;return function(e){return function(o){var i=function(i){return function(){var a,f=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};return e[i].addOrUpdate(n({},f,(r(a={},t,o),r(a,u,Object.keys(f)),a)))}};return{set:i("values"),setErrors:i("errors"),update:function(n){return function(i){var a;return e.values.update((r(a={},t,o),r(a,u,[n]),r(a,n,i),a))}},clearErrors:function(){return e.errors.reset(o)},remove:function(){e.values.remove(o),e.errors.remove(o)}}}}}({keyName:s,keyFields:v})(t)(e),function(e){var r=e.keyName;return function(e){return function(t){var u=function(u){return function(o){var i=e[u].get(t);if(!i)return o?"":{};if(!o&&i){var a=n({},i);return delete a[r],a}var f=i[o];return null==f?"":f}};return{exists:function(){return!!e.values.get(t)},get:u("values"),getErrors:u("errors")}}}}({keyName:s,keyFields:v})(t)(e))},function(e){var t=e.keyName,u=e.keyFields;return function(e){var o=function(o){return function(i){return e[o].addOrUpdate(i.map(function(e){var o,i=e.name,a=e.values;return n({},a,(r(o={},t,i),r(o,u,Object.keys(a)),o))}))}};return{set:o("values"),setErrors:o("errors"),clearErrors:e.errors.remove,remove:function(r){e.values.remove(r),e.errors.remove(r)}}}}({keyName:s,keyFields:v})(t),function(e){return{find:function(r){return e.values.getKeys().filter(function(e){return e.match(r)})}}}(t))}}}}); +!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?module.exports=r(require("k-ramel")):"function"==typeof define&&define.amd?define(["k-ramel"],r):(e=e||self)["@k-ramel/driver-form"]=r(e.kRamel)}(this,function(e){"use strict";function r(e,r,t){return r in e?Object.defineProperty(e,r,{value:t,enumerable:!0,configurable:!0,writable:!0}):e[r]=t,e}function t(e){for(var t=1;t0&&void 0!==arguments[0]?arguments[0]:{},i=o.path,a=void 0===i?"form":i,f=o.getState,c=void 0===f?function(e){return e.form}:f,s=o.key,l=void 0===s?"@@form-name":s,d=l,v="@@form";"@@form-name"!==l&&(v=l);var y="".concat(v,"-fields");return{getReducer:function(){return{path:a,reducer:{values:e.types.keyValue({key:d}),errors:e.types.keyValue({key:d})}}},getDriver:function(e){var o=c(e);return Object.assign(function(e){return t({},function(e){var n=e.keyName,u=e.keyFields;return function(e){return function(o){var i=function(i){return function(){var a,f=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};return e[i].addOrUpdate(t({},f,(r(a={},n,o),r(a,u,Object.keys(f)),a)))}};return{set:i("values"),setErrors:i("errors"),update:function(t){return function(i){var a;return e.values.update((r(a={},n,o),r(a,u,[t]),r(a,t,i),a))}},clearErrors:function(){return e.errors.reset(o)},remove:function(){e.values.remove(o),e.errors.remove(o)}}}}}({keyName:d,keyFields:y})(o)(e),function(e){var r=e.keyName;return function(e){return function(n){var u=function(u){return function(o){var i=e[u].get(n);if(!i)return o?"":{};if(!o&&i){var a=t({},i);return delete a[r],a}var f=i[o];return null==f?"":f}};return{exists:function(){return!!e.values.get(n)},get:u("values"),getErrors:u("errors")}}}}({keyName:d,keyFields:y})(o)(e))},function(e){var n=e.keyName,u=e.keyFields;return function(e){var o=function(o){return function(i){return e[o].addOrUpdate(i.map(function(e){var o,i=e.name,a=e.values;return t({},a,(r(o={},n,i),r(o,u,Object.keys(a)),o))}))}};return{set:o("values"),setErrors:o("errors"),clearErrors:e.errors.remove,remove:function(r){e.values.remove(r),e.errors.remove(r)}}}}({keyName:d,keyFields:y})(o),function(e){return{find:function(r){return e.values.getKeys().filter(function(e){return e.match(r)})}}}(o),function(e){return{getUpdatedFieldNames:(r=e,t=r.keyFields,function(e){return e.payload[t]}),getUpdatedValues:n(e),getUpdatedEntries:u(e)};var r,t}({keyName:d,keyFields:y}))}}}}); diff --git a/packages/drivers/form/misc/test-suite.js b/packages/drivers/form/misc/test-suite.js index 6dc0c79..9ff5ea4 100644 --- a/packages/drivers/form/misc/test-suite.js +++ b/packages/drivers/form/misc/test-suite.js @@ -355,6 +355,66 @@ export default (driver) => { state, }).toMatchSnapshot() }) + + it('should returns updated field names', () => { + // run implementation + let fieldNames + runReaction()((action, store, { form }) => { + fieldNames = form.getUpdatedFieldNames({ + payload: { + '@@form-name': 'myForm', + '@@form-fields': ['first', 'second'], + first: true, + second: 'ok', + }, + }) + }) + + // assert + expect({ + fieldNames, + }).toMatchSnapshot() + }) + + it('should returns updated field names and values', () => { + // run implementation + let fieldValues + runReaction()((action, store, { form }) => { + fieldValues = form.getUpdatedValues({ + payload: { + '@@form-name': 'myForm', + '@@form-fields': ['first', 'second'], + first: true, + second: 'ok', + }, + }) + }) + + // assert + expect({ + fieldValues, + }).toMatchSnapshot() + }) + + it('should returns updated field entries', () => { + // run implementation + let entries + runReaction()((action, store, { form }) => { + entries = form.getUpdatedEntries({ + payload: { + '@@form-name': 'myForm', + '@@form-fields': ['first', 'second'], + first: true, + second: 'ok', + }, + }) + }) + + // assert + expect({ + entries, + }).toMatchSnapshot() + }) }) }) } diff --git a/packages/drivers/form/src/__snapshots__/index.spec.js.snap b/packages/drivers/form/src/__snapshots__/index.spec.js.snap index 1f51862..1ca545f 100644 --- a/packages/drivers/form/src/__snapshots__/index.spec.js.snap +++ b/packages/drivers/form/src/__snapshots__/index.spec.js.snap @@ -1021,6 +1021,39 @@ Object { } `; +exports[`drivers/form bulk mode should returns updated field entries 1`] = ` +Object { + "entries": Array [ + Array [ + "first", + true, + ], + Array [ + "second", + "ok", + ], + ], +} +`; + +exports[`drivers/form bulk mode should returns updated field names 1`] = ` +Object { + "fieldNames": Array [ + "first", + "second", + ], +} +`; + +exports[`drivers/form bulk mode should returns updated field names and values 1`] = ` +Object { + "fieldValues": Object { + "first": true, + "second": "ok", + }, +} +`; + exports[`drivers/form bulk mode should set errors 1`] = ` Object { "state": Object { diff --git a/packages/drivers/form/src/form.js b/packages/drivers/form/src/form.js index cb9251d..95a956a 100644 --- a/packages/drivers/form/src/form.js +++ b/packages/drivers/form/src/form.js @@ -3,6 +3,7 @@ import actions from './actions' import bulkActions from './bulk.actions' import selectors from './selectors' import utils from './utils' +import helpers from './helpers' export default ({ path = 'form', @@ -36,6 +37,7 @@ export default ({ }), bulkActions({ keyName, keyFields })(state), utils(state), + helpers({ keyName, keyFields }), ) }, } diff --git a/packages/drivers/form/src/helpers.js b/packages/drivers/form/src/helpers.js new file mode 100644 index 0000000..74cdda2 --- /dev/null +++ b/packages/drivers/form/src/helpers.js @@ -0,0 +1,21 @@ +const getUpdatedValues = ({ keyName, keyFields }) => (action) => { + const values = { ...action.payload } + delete values[keyName] + delete values[keyFields] + + return values +} + +const getUpdatedFieldNames = ({ keyFields }) => action => ( + action.payload[keyFields] +) + +const getUpdatedEntries = keys => action => ( + Object.entries(getUpdatedValues(keys)(action)) +) + +export default keys => ({ + getUpdatedFieldNames: getUpdatedFieldNames(keys), + getUpdatedValues: getUpdatedValues(keys), + getUpdatedEntries: getUpdatedEntries(keys), +})