final-form-arrays
Advanced tools
Comparing version 1.1.2 to 1.2.0
@@ -5,15 +5,2 @@ 'use strict'; | ||
// | ||
var insert = function insert(_ref, state, _ref2) { | ||
var name = _ref[0], | ||
index = _ref[1], | ||
value = _ref[2]; | ||
var changeValue = _ref2.changeValue; | ||
changeValue(state, name, function (array) { | ||
var copy = [].concat(array || []); | ||
copy.splice(index, 0, value); | ||
return copy; | ||
}); | ||
}; | ||
function createCommonjsModule(fn, module) { | ||
@@ -46,2 +33,47 @@ return module = { exports: {} }, fn(module, module.exports), module.exports; | ||
// | ||
var insert = function insert(_ref, state, _ref2) { | ||
var name = _ref[0], | ||
index = _ref[1], | ||
value = _ref[2]; | ||
var changeValue = _ref2.changeValue; | ||
changeValue(state, name, function (array) { | ||
var copy = [].concat(array || []); | ||
copy.splice(index, 0, value); | ||
return copy; | ||
}); // now we have increment any higher indexes | ||
var pattern = new RegExp("^" + name + "\\[(\\d+)\\](.*)"); | ||
var changes = {}; | ||
Object.keys(state.fields).forEach(function (key) { | ||
var tokens = pattern.exec(key); | ||
if (tokens) { | ||
var fieldIndex = Number(tokens[1]); | ||
if (fieldIndex >= index) { | ||
// inc index one higher | ||
var incrementedKey = name + "[" + (fieldIndex + 1) + "]" + tokens[2]; | ||
changes[incrementedKey] = state.fields[key]; | ||
changes[incrementedKey].name = incrementedKey; | ||
} | ||
if (fieldIndex === index) { | ||
delete state.fields[key]; | ||
} | ||
} | ||
}); | ||
state.fields = _extends_1({}, state.fields, changes); | ||
}; | ||
// | ||
var concat = function concat(_ref, state, _ref2) { | ||
var name = _ref[0], | ||
value = _ref[1]; | ||
var changeValue = _ref2.changeValue; | ||
changeValue(state, name, function (array) { | ||
return array ? [].concat(array, value) : value; | ||
}); | ||
}; | ||
// | ||
var move = function move(_ref, state, _ref2) { | ||
@@ -107,6 +139,8 @@ var name = _ref[0], | ||
name: destKey, | ||
change: state.fields[destKey].change, | ||
// prevent functions from being overwritten | ||
blur: state.fields[destKey].blur, | ||
focus: state.fields[destKey].focus, | ||
// if the state.fields[destKey] does not exist, it will be created | ||
// when that field gets registered, with its own change/blur/focus callbacks | ||
change: state.fields[destKey] && state.fields[destKey].change, | ||
blur: state.fields[destKey] && state.fields[destKey].blur, | ||
focus: state.fields[destKey] && state.fields[destKey].focus, | ||
lastFieldState: undefined // clearing lastFieldState forces renotification | ||
@@ -157,2 +191,26 @@ | ||
return copy; | ||
}); // now we have to remove any subfields for our index, | ||
// and decrement all higher indexes. | ||
var pattern = new RegExp("^" + name + "\\[(\\d+)\\](.*)"); | ||
var backup = _extends_1({}, state.fields); | ||
Object.keys(state.fields).forEach(function (key) { | ||
var tokens = pattern.exec(key); | ||
if (tokens) { | ||
var fieldIndex = Number(tokens[1]); | ||
if (fieldIndex === index) { | ||
// delete any subfields for this array item | ||
delete state.fields[key]; | ||
} else if (fieldIndex > index) { | ||
// shift all higher ones down | ||
delete state.fields[key]; | ||
var decrementedKey = name + "[" + (fieldIndex - 1) + "]" + tokens[2]; | ||
state.fields[decrementedKey] = backup[key]; | ||
state.fields[decrementedKey].name = decrementedKey; | ||
} | ||
} | ||
}); | ||
@@ -163,20 +221,73 @@ return returnValue; | ||
// | ||
var shift = function shift(_ref, state, _ref2) { | ||
var name = _ref[0]; | ||
var countBelow = function countBelow(array, value) { | ||
return array.reduce(function (count, item) { | ||
return item < value ? count + 1 : count; | ||
}, 0); | ||
}; | ||
var removeBatch = function removeBatch(_ref, state, _ref2) { | ||
var name = _ref[0], | ||
indexes = _ref[1]; | ||
var changeValue = _ref2.changeValue; | ||
var result; | ||
var sortedIndexes = [].concat(indexes); | ||
sortedIndexes.sort(); // remove duplicates | ||
for (var i = 0; i < sortedIndexes.length; i++) { | ||
if (i > 0 && sortedIndexes[i] === sortedIndexes[i - 1]) { | ||
sortedIndexes.splice(i--, 1); | ||
} | ||
} | ||
var returnValue = []; | ||
changeValue(state, name, function (array) { | ||
if (array) { | ||
if (!array.length) { | ||
return []; | ||
} | ||
// use original order of indexes for return value | ||
returnValue = indexes.map(function (index) { | ||
return array && array[index]; | ||
}); | ||
result = array[0]; | ||
return array.slice(1, array.length); | ||
if (!array || !sortedIndexes.length) { | ||
return array; | ||
} | ||
var copy = [].concat(array); | ||
var removed = []; | ||
sortedIndexes.forEach(function (index) { | ||
copy.splice(index - removed.length, 1); | ||
removed.push(array && array[index]); | ||
}); | ||
return copy; | ||
}); // now we have to remove any subfields for our indexes, | ||
// and decrement all higher indexes. | ||
var pattern = new RegExp("^" + name + "\\[(\\d+)\\](.*)"); | ||
var newFields = {}; | ||
Object.keys(state.fields).forEach(function (key) { | ||
var tokens = pattern.exec(key); | ||
if (tokens) { | ||
var fieldIndex = Number(tokens[1]); | ||
if (!~sortedIndexes.indexOf(fieldIndex)) { | ||
// not one of the removed indexes | ||
// shift all higher ones down | ||
var decrementedKey = name + "[" + (fieldIndex - countBelow(sortedIndexes, fieldIndex)) + "]" + tokens[2]; | ||
newFields[decrementedKey] = state.fields[key]; | ||
newFields[decrementedKey].name = decrementedKey; | ||
} | ||
} else { | ||
newFields[key] = state.fields[key]; | ||
} | ||
}); | ||
return result; | ||
state.fields = newFields; | ||
return returnValue; | ||
}; | ||
// | ||
var shift = function shift(_ref, state, tools) { | ||
var name = _ref[0]; | ||
return remove([name, 0], state, tools); | ||
}; | ||
// | ||
var swap = function swap(_ref, state, _ref2) { | ||
@@ -235,9 +346,7 @@ var name = _ref[0], | ||
// | ||
var unshift = function unshift(_ref, state, _ref2) { | ||
var unshift = function unshift(_ref, state, tools) { | ||
var name = _ref[0], | ||
value = _ref[1]; | ||
var changeValue = _ref2.changeValue; | ||
changeValue(state, name, function (array) { | ||
return array ? [value].concat(array) : [value]; | ||
}); | ||
return insert([name, 0, value], state, tools); | ||
}; | ||
@@ -261,2 +370,3 @@ | ||
insert: insert, | ||
concat: concat, | ||
move: move, | ||
@@ -266,2 +376,3 @@ pop: pop, | ||
remove: remove, | ||
removeBatch: removeBatch, | ||
shift: shift, | ||
@@ -268,0 +379,0 @@ swap: swap, |
@@ -1,14 +0,1 @@ | ||
// | ||
var insert = function insert(_ref, state, _ref2) { | ||
var name = _ref[0], | ||
index = _ref[1], | ||
value = _ref[2]; | ||
var changeValue = _ref2.changeValue; | ||
changeValue(state, name, function (array) { | ||
var copy = [].concat(array || []); | ||
copy.splice(index, 0, value); | ||
return copy; | ||
}); | ||
}; | ||
function _extends() { | ||
@@ -33,2 +20,47 @@ _extends = Object.assign || function (target) { | ||
// | ||
var insert = function insert(_ref, state, _ref2) { | ||
var name = _ref[0], | ||
index = _ref[1], | ||
value = _ref[2]; | ||
var changeValue = _ref2.changeValue; | ||
changeValue(state, name, function (array) { | ||
var copy = [].concat(array || []); | ||
copy.splice(index, 0, value); | ||
return copy; | ||
}); // now we have increment any higher indexes | ||
var pattern = new RegExp("^" + name + "\\[(\\d+)\\](.*)"); | ||
var changes = {}; | ||
Object.keys(state.fields).forEach(function (key) { | ||
var tokens = pattern.exec(key); | ||
if (tokens) { | ||
var fieldIndex = Number(tokens[1]); | ||
if (fieldIndex >= index) { | ||
// inc index one higher | ||
var incrementedKey = name + "[" + (fieldIndex + 1) + "]" + tokens[2]; | ||
changes[incrementedKey] = state.fields[key]; | ||
changes[incrementedKey].name = incrementedKey; | ||
} | ||
if (fieldIndex === index) { | ||
delete state.fields[key]; | ||
} | ||
} | ||
}); | ||
state.fields = _extends({}, state.fields, changes); | ||
}; | ||
// | ||
var concat = function concat(_ref, state, _ref2) { | ||
var name = _ref[0], | ||
value = _ref[1]; | ||
var changeValue = _ref2.changeValue; | ||
changeValue(state, name, function (array) { | ||
return array ? [].concat(array, value) : value; | ||
}); | ||
}; | ||
// | ||
var move = function move(_ref, state, _ref2) { | ||
@@ -94,6 +126,8 @@ var name = _ref[0], | ||
name: destKey, | ||
change: state.fields[destKey].change, | ||
// prevent functions from being overwritten | ||
blur: state.fields[destKey].blur, | ||
focus: state.fields[destKey].focus, | ||
// if the state.fields[destKey] does not exist, it will be created | ||
// when that field gets registered, with its own change/blur/focus callbacks | ||
change: state.fields[destKey] && state.fields[destKey].change, | ||
blur: state.fields[destKey] && state.fields[destKey].blur, | ||
focus: state.fields[destKey] && state.fields[destKey].focus, | ||
lastFieldState: undefined // clearing lastFieldState forces renotification | ||
@@ -144,2 +178,26 @@ | ||
return copy; | ||
}); // now we have to remove any subfields for our index, | ||
// and decrement all higher indexes. | ||
var pattern = new RegExp("^" + name + "\\[(\\d+)\\](.*)"); | ||
var backup = _extends({}, state.fields); | ||
Object.keys(state.fields).forEach(function (key) { | ||
var tokens = pattern.exec(key); | ||
if (tokens) { | ||
var fieldIndex = Number(tokens[1]); | ||
if (fieldIndex === index) { | ||
// delete any subfields for this array item | ||
delete state.fields[key]; | ||
} else if (fieldIndex > index) { | ||
// shift all higher ones down | ||
delete state.fields[key]; | ||
var decrementedKey = name + "[" + (fieldIndex - 1) + "]" + tokens[2]; | ||
state.fields[decrementedKey] = backup[key]; | ||
state.fields[decrementedKey].name = decrementedKey; | ||
} | ||
} | ||
}); | ||
@@ -150,20 +208,73 @@ return returnValue; | ||
// | ||
var shift = function shift(_ref, state, _ref2) { | ||
var name = _ref[0]; | ||
var countBelow = function countBelow(array, value) { | ||
return array.reduce(function (count, item) { | ||
return item < value ? count + 1 : count; | ||
}, 0); | ||
}; | ||
var removeBatch = function removeBatch(_ref, state, _ref2) { | ||
var name = _ref[0], | ||
indexes = _ref[1]; | ||
var changeValue = _ref2.changeValue; | ||
var result; | ||
var sortedIndexes = [].concat(indexes); | ||
sortedIndexes.sort(); // remove duplicates | ||
for (var i = 0; i < sortedIndexes.length; i++) { | ||
if (i > 0 && sortedIndexes[i] === sortedIndexes[i - 1]) { | ||
sortedIndexes.splice(i--, 1); | ||
} | ||
} | ||
var returnValue = []; | ||
changeValue(state, name, function (array) { | ||
if (array) { | ||
if (!array.length) { | ||
return []; | ||
} | ||
// use original order of indexes for return value | ||
returnValue = indexes.map(function (index) { | ||
return array && array[index]; | ||
}); | ||
result = array[0]; | ||
return array.slice(1, array.length); | ||
if (!array || !sortedIndexes.length) { | ||
return array; | ||
} | ||
var copy = [].concat(array); | ||
var removed = []; | ||
sortedIndexes.forEach(function (index) { | ||
copy.splice(index - removed.length, 1); | ||
removed.push(array && array[index]); | ||
}); | ||
return copy; | ||
}); // now we have to remove any subfields for our indexes, | ||
// and decrement all higher indexes. | ||
var pattern = new RegExp("^" + name + "\\[(\\d+)\\](.*)"); | ||
var newFields = {}; | ||
Object.keys(state.fields).forEach(function (key) { | ||
var tokens = pattern.exec(key); | ||
if (tokens) { | ||
var fieldIndex = Number(tokens[1]); | ||
if (!~sortedIndexes.indexOf(fieldIndex)) { | ||
// not one of the removed indexes | ||
// shift all higher ones down | ||
var decrementedKey = name + "[" + (fieldIndex - countBelow(sortedIndexes, fieldIndex)) + "]" + tokens[2]; | ||
newFields[decrementedKey] = state.fields[key]; | ||
newFields[decrementedKey].name = decrementedKey; | ||
} | ||
} else { | ||
newFields[key] = state.fields[key]; | ||
} | ||
}); | ||
return result; | ||
state.fields = newFields; | ||
return returnValue; | ||
}; | ||
// | ||
var shift = function shift(_ref, state, tools) { | ||
var name = _ref[0]; | ||
return remove([name, 0], state, tools); | ||
}; | ||
// | ||
var swap = function swap(_ref, state, _ref2) { | ||
@@ -222,9 +333,7 @@ var name = _ref[0], | ||
// | ||
var unshift = function unshift(_ref, state, _ref2) { | ||
var unshift = function unshift(_ref, state, tools) { | ||
var name = _ref[0], | ||
value = _ref[1]; | ||
var changeValue = _ref2.changeValue; | ||
changeValue(state, name, function (array) { | ||
return array ? [value].concat(array) : [value]; | ||
}); | ||
return insert([name, 0, value], state, tools); | ||
}; | ||
@@ -248,2 +357,3 @@ | ||
insert: insert, | ||
concat: concat, | ||
move: move, | ||
@@ -253,2 +363,3 @@ pop: pop, | ||
remove: remove, | ||
removeBatch: removeBatch, | ||
shift: shift, | ||
@@ -255,0 +366,0 @@ swap: swap, |
@@ -7,15 +7,2 @@ (function (global, factory) { | ||
// | ||
var insert = function insert(_ref, state, _ref2) { | ||
var name = _ref[0], | ||
index = _ref[1], | ||
value = _ref[2]; | ||
var changeValue = _ref2.changeValue; | ||
changeValue(state, name, function (array) { | ||
var copy = [].concat(array || []); | ||
copy.splice(index, 0, value); | ||
return copy; | ||
}); | ||
}; | ||
function _extends() { | ||
@@ -40,2 +27,47 @@ _extends = Object.assign || function (target) { | ||
// | ||
var insert = function insert(_ref, state, _ref2) { | ||
var name = _ref[0], | ||
index = _ref[1], | ||
value = _ref[2]; | ||
var changeValue = _ref2.changeValue; | ||
changeValue(state, name, function (array) { | ||
var copy = [].concat(array || []); | ||
copy.splice(index, 0, value); | ||
return copy; | ||
}); // now we have increment any higher indexes | ||
var pattern = new RegExp("^" + name + "\\[(\\d+)\\](.*)"); | ||
var changes = {}; | ||
Object.keys(state.fields).forEach(function (key) { | ||
var tokens = pattern.exec(key); | ||
if (tokens) { | ||
var fieldIndex = Number(tokens[1]); | ||
if (fieldIndex >= index) { | ||
// inc index one higher | ||
var incrementedKey = name + "[" + (fieldIndex + 1) + "]" + tokens[2]; | ||
changes[incrementedKey] = state.fields[key]; | ||
changes[incrementedKey].name = incrementedKey; | ||
} | ||
if (fieldIndex === index) { | ||
delete state.fields[key]; | ||
} | ||
} | ||
}); | ||
state.fields = _extends({}, state.fields, changes); | ||
}; | ||
// | ||
var concat = function concat(_ref, state, _ref2) { | ||
var name = _ref[0], | ||
value = _ref[1]; | ||
var changeValue = _ref2.changeValue; | ||
changeValue(state, name, function (array) { | ||
return array ? [].concat(array, value) : value; | ||
}); | ||
}; | ||
// | ||
var move = function move(_ref, state, _ref2) { | ||
@@ -101,6 +133,8 @@ var name = _ref[0], | ||
name: destKey, | ||
change: state.fields[destKey].change, | ||
// prevent functions from being overwritten | ||
blur: state.fields[destKey].blur, | ||
focus: state.fields[destKey].focus, | ||
// if the state.fields[destKey] does not exist, it will be created | ||
// when that field gets registered, with its own change/blur/focus callbacks | ||
change: state.fields[destKey] && state.fields[destKey].change, | ||
blur: state.fields[destKey] && state.fields[destKey].blur, | ||
focus: state.fields[destKey] && state.fields[destKey].focus, | ||
lastFieldState: undefined // clearing lastFieldState forces renotification | ||
@@ -151,2 +185,26 @@ | ||
return copy; | ||
}); // now we have to remove any subfields for our index, | ||
// and decrement all higher indexes. | ||
var pattern = new RegExp("^" + name + "\\[(\\d+)\\](.*)"); | ||
var backup = _extends({}, state.fields); | ||
Object.keys(state.fields).forEach(function (key) { | ||
var tokens = pattern.exec(key); | ||
if (tokens) { | ||
var fieldIndex = Number(tokens[1]); | ||
if (fieldIndex === index) { | ||
// delete any subfields for this array item | ||
delete state.fields[key]; | ||
} else if (fieldIndex > index) { | ||
// shift all higher ones down | ||
delete state.fields[key]; | ||
var decrementedKey = name + "[" + (fieldIndex - 1) + "]" + tokens[2]; | ||
state.fields[decrementedKey] = backup[key]; | ||
state.fields[decrementedKey].name = decrementedKey; | ||
} | ||
} | ||
}); | ||
@@ -157,20 +215,73 @@ return returnValue; | ||
// | ||
var shift = function shift(_ref, state, _ref2) { | ||
var name = _ref[0]; | ||
var countBelow = function countBelow(array, value) { | ||
return array.reduce(function (count, item) { | ||
return item < value ? count + 1 : count; | ||
}, 0); | ||
}; | ||
var removeBatch = function removeBatch(_ref, state, _ref2) { | ||
var name = _ref[0], | ||
indexes = _ref[1]; | ||
var changeValue = _ref2.changeValue; | ||
var result; | ||
var sortedIndexes = [].concat(indexes); | ||
sortedIndexes.sort(); // remove duplicates | ||
for (var i = 0; i < sortedIndexes.length; i++) { | ||
if (i > 0 && sortedIndexes[i] === sortedIndexes[i - 1]) { | ||
sortedIndexes.splice(i--, 1); | ||
} | ||
} | ||
var returnValue = []; | ||
changeValue(state, name, function (array) { | ||
if (array) { | ||
if (!array.length) { | ||
return []; | ||
} | ||
// use original order of indexes for return value | ||
returnValue = indexes.map(function (index) { | ||
return array && array[index]; | ||
}); | ||
result = array[0]; | ||
return array.slice(1, array.length); | ||
if (!array || !sortedIndexes.length) { | ||
return array; | ||
} | ||
var copy = [].concat(array); | ||
var removed = []; | ||
sortedIndexes.forEach(function (index) { | ||
copy.splice(index - removed.length, 1); | ||
removed.push(array && array[index]); | ||
}); | ||
return copy; | ||
}); // now we have to remove any subfields for our indexes, | ||
// and decrement all higher indexes. | ||
var pattern = new RegExp("^" + name + "\\[(\\d+)\\](.*)"); | ||
var newFields = {}; | ||
Object.keys(state.fields).forEach(function (key) { | ||
var tokens = pattern.exec(key); | ||
if (tokens) { | ||
var fieldIndex = Number(tokens[1]); | ||
if (!~sortedIndexes.indexOf(fieldIndex)) { | ||
// not one of the removed indexes | ||
// shift all higher ones down | ||
var decrementedKey = name + "[" + (fieldIndex - countBelow(sortedIndexes, fieldIndex)) + "]" + tokens[2]; | ||
newFields[decrementedKey] = state.fields[key]; | ||
newFields[decrementedKey].name = decrementedKey; | ||
} | ||
} else { | ||
newFields[key] = state.fields[key]; | ||
} | ||
}); | ||
return result; | ||
state.fields = newFields; | ||
return returnValue; | ||
}; | ||
// | ||
var shift = function shift(_ref, state, tools) { | ||
var name = _ref[0]; | ||
return remove([name, 0], state, tools); | ||
}; | ||
// | ||
var swap = function swap(_ref, state, _ref2) { | ||
@@ -229,9 +340,7 @@ var name = _ref[0], | ||
// | ||
var unshift = function unshift(_ref, state, _ref2) { | ||
var unshift = function unshift(_ref, state, tools) { | ||
var name = _ref[0], | ||
value = _ref[1]; | ||
var changeValue = _ref2.changeValue; | ||
changeValue(state, name, function (array) { | ||
return array ? [value].concat(array) : [value]; | ||
}); | ||
return insert([name, 0, value], state, tools); | ||
}; | ||
@@ -255,2 +364,3 @@ | ||
insert: insert, | ||
concat: concat, | ||
move: move, | ||
@@ -260,2 +370,3 @@ pop: pop, | ||
remove: remove, | ||
removeBatch: removeBatch, | ||
shift: shift, | ||
@@ -262,0 +373,0 @@ swap: swap, |
@@ -1,2 +0,2 @@ | ||
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((e=e||self)["final-form-arrays"]={})}(this,function(e){"use strict";function d(){return(d=Object.assign||function(e){for(var n=1;n<arguments.length;n++){var t=arguments[n];for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r])}return e}).apply(this,arguments)}var n={insert:function(e,n,t){var r=e[0],c=e[1],i=e[2];(0,t.changeValue)(n,r,function(e){var n=[].concat(e||[]);return n.splice(c,0,i),n})},move:function(e,u,n){var a=e[0],f=e[1],s=e[2],t=n.changeValue;if(f!==s){t(u,a,function(e){var n=[].concat(e||[]),t=n[f];return n.splice(f,1),n.splice(s,0,t),n});var o=a+"["+f+"]";Object.keys(u.fields).forEach(function(e){if(e.substring(0,o.length)===o){var n=e.substring(o.length),t=o+n,r=u.fields[t];if(f<s)for(var c=f;c<s;c++)l({destKey:a+"["+c+"]"+n,source:u.fields[a+"["+(c+1)+"]"+n]});else for(var i=f;s<i;i--)l({destKey:a+"["+i+"]"+n,source:u.fields[a+"["+(i-1)+"]"+n]});l({destKey:a+"["+s+"]"+n,source:r})}})}function l(e){var n=e.destKey,t=e.source;u.fields[n]=d({},t,{name:n,change:u.fields[n].change,blur:u.fields[n].blur,focus:u.fields[n].focus,lastFieldState:void 0})}},pop:function(e,n,t){var r,c=e[0];return(0,t.changeValue)(n,c,function(e){if(e)return e.length?(r=e[e.length-1],e.slice(0,e.length-1)):[]}),r},push:function(e,n,t){var r=e[0],c=e[1];(0,t.changeValue)(n,r,function(e){return e?[].concat(e,[c]):[c]})},remove:function(e,n,t){var r,c=e[0],i=e[1];return(0,t.changeValue)(n,c,function(e){var n=[].concat(e||[]);return r=n[i],n.splice(i,1),n}),r},shift:function(e,n,t){var r,c=e[0];return(0,t.changeValue)(n,c,function(e){if(e)return e.length?(r=e[0],e.slice(1,e.length)):[]}),r},swap:function(e,i,n){var t=e[0],r=e[1],c=e[2],u=n.changeValue;if(r!==c){u(i,t,function(e){var n=[].concat(e||[]),t=n[r];return n[r]=n[c],n[c]=t,n});var a=t+"["+r+"]",f=t+"["+c+"]";Object.keys(i.fields).forEach(function(e){if(e.substring(0,a.length)===a){var n=e.substring(a.length),t=a+n,r=f+n,c=i.fields[t];s({destKey:t,source:i.fields[r]}),s({destKey:r,source:c})}})}function s(e){var n=e.destKey,t=e.source;i.fields[n]=d({},t,{name:n,change:i.fields[n].change,blur:i.fields[n].blur,focus:i.fields[n].focus,lastFieldState:void 0})}},unshift:function(e,n,t){var r=e[0],c=e[1];(0,t.changeValue)(n,r,function(e){return e?[c].concat(e):[c]})},update:function(e,n,t){var r=e[0],c=e[1],i=e[2];(0,t.changeValue)(n,r,function(e){var n=[].concat(e||[]);return n.splice(c,1,i),n})}};e.default=n,Object.defineProperty(e,"__esModule",{value:!0})}); | ||
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((e=e||self)["final-form-arrays"]={})}(this,function(e){"use strict";function d(){return(d=Object.assign||function(e){for(var n=1;n<arguments.length;n++){var r=arguments[n];for(var t in r)Object.prototype.hasOwnProperty.call(r,t)&&(e[t]=r[t])}return e}).apply(this,arguments)}var f=function(e,i,n){var f=e[0],c=e[1],r=e[2];(0,n.changeValue)(i,f,function(e){var n=[].concat(e||[]);return n.splice(c,0,r),n});var a=new RegExp("^"+f+"\\[(\\d+)\\](.*)"),s={};Object.keys(i.fields).forEach(function(e){var n=a.exec(e);if(n){var r=Number(n[1]);if(c<=r){var t=f+"["+(r+1)+"]"+n[2];s[t]=i.fields[e],s[t].name=t}r===c&&delete i.fields[e]}}),i.fields=d({},i.fields,s)},i=function(e,i,n){var r,f=e[0],c=e[1];(0,n.changeValue)(i,f,function(e){var n=[].concat(e||[]);return r=n[c],n.splice(c,1),n});var a=new RegExp("^"+f+"\\[(\\d+)\\](.*)"),s=d({},i.fields);return Object.keys(i.fields).forEach(function(e){var n=a.exec(e);if(n){var r=Number(n[1]);if(r===c)delete i.fields[e];else if(c<r){delete i.fields[e];var t=f+"["+(r-1)+"]"+n[2];i.fields[t]=s[e],i.fields[t].name=t}}}),r},n={insert:f,concat:function(e,n,r){var t=e[0],i=e[1];(0,r.changeValue)(n,t,function(e){return e?[].concat(e,i):i})},move:function(e,c,n){var a=e[0],s=e[1],u=e[2],r=n.changeValue;if(s!==u){r(c,a,function(e){var n=[].concat(e||[]),r=n[s];return n.splice(s,1),n.splice(u,0,r),n});var o=a+"["+s+"]";Object.keys(c.fields).forEach(function(e){if(e.substring(0,o.length)===o){var n=e.substring(o.length),r=o+n,t=c.fields[r];if(s<u)for(var i=s;i<u;i++)l({destKey:a+"["+i+"]"+n,source:c.fields[a+"["+(i+1)+"]"+n]});else for(var f=s;u<f;f--)l({destKey:a+"["+f+"]"+n,source:c.fields[a+"["+(f-1)+"]"+n]});l({destKey:a+"["+u+"]"+n,source:t})}})}function l(e){var n=e.destKey,r=e.source;c.fields[n]=d({},r,{name:n,change:c.fields[n]&&c.fields[n].change,blur:c.fields[n]&&c.fields[n].blur,focus:c.fields[n]&&c.fields[n].focus,lastFieldState:void 0})}},pop:function(e,n,r){var t,i=e[0];return(0,r.changeValue)(n,i,function(e){if(e)return e.length?(t=e[e.length-1],e.slice(0,e.length-1)):[]}),t},push:function(e,n,r){var t=e[0],i=e[1];(0,r.changeValue)(n,t,function(e){return e?[].concat(e,[i]):[i]})},remove:i,removeBatch:function(e,f,n){var c=e[0],i=e[1],r=n.changeValue,a=[].concat(i);a.sort();for(var t=0;t<a.length;t++)0<t&&a[t]===a[t-1]&&a.splice(t--,1);var s=[];r(f,c,function(n){if(s=i.map(function(e){return n&&n[e]}),!n||!a.length)return n;var r=[].concat(n),t=[];return a.forEach(function(e){r.splice(e-t.length,1),t.push(n&&n[e])}),r});var u=new RegExp("^"+c+"\\[(\\d+)\\](.*)"),o={};return Object.keys(f.fields).forEach(function(e){var r,n=u.exec(e);if(n){var t=Number(n[1]);if(!~a.indexOf(t)){var i=c+"["+(t-(r=t,a.reduce(function(e,n){return n<r?e+1:e},0)))+"]"+n[2];o[i]=f.fields[e],o[i].name=i}}else o[e]=f.fields[e]}),f.fields=o,s},shift:function(e,n,r){var t=e[0];return i([t,0],n,r)},swap:function(e,f,n){var r=e[0],t=e[1],i=e[2],c=n.changeValue;if(t!==i){c(f,r,function(e){var n=[].concat(e||[]),r=n[t];return n[t]=n[i],n[i]=r,n});var a=r+"["+t+"]",s=r+"["+i+"]";Object.keys(f.fields).forEach(function(e){if(e.substring(0,a.length)===a){var n=e.substring(a.length),r=a+n,t=s+n,i=f.fields[r];u({destKey:r,source:f.fields[t]}),u({destKey:t,source:i})}})}function u(e){var n=e.destKey,r=e.source;f.fields[n]=d({},r,{name:n,change:f.fields[n].change,blur:f.fields[n].blur,focus:f.fields[n].focus,lastFieldState:void 0})}},unshift:function(e,n,r){var t=e[0],i=e[1];return f([t,0,i],n,r)},update:function(e,n,r){var t=e[0],i=e[1],f=e[2];(0,r.changeValue)(n,t,function(e){var n=[].concat(e||[]);return n.splice(i,1,f),n})}};e.default=n,Object.defineProperty(e,"__esModule",{value:!0})}); | ||
//# sourceMappingURL=final-form-arrays.umd.min.js.map |
import { Mutator } from 'final-form' | ||
export const insert: Mutator | ||
export const concat: Mutator | ||
export const move: Mutator | ||
export const pop: Mutator | ||
export const push: Mutator | ||
export const removeBatch: Mutator | ||
export const remove: Mutator | ||
@@ -15,5 +17,7 @@ export const shift: Mutator | ||
insert: Mutator | ||
concat: Mutator | ||
move: Mutator | ||
pop: Mutator | ||
push: Mutator | ||
removeBatch: Mutator | ||
remove: Mutator | ||
@@ -32,2 +36,3 @@ shift: Mutator | ||
insert: (name: string, index: number, value: any) => void | ||
concat: (name: string, value: Array<any>) => void | ||
move: (name: string, from: number, to: number) => void | ||
@@ -37,2 +42,3 @@ pop: (name: string) => any | ||
remove: (name: string, index: number) => any | ||
removeBatch: (name: string, indexes: Array<number>) => any | ||
shift: (name: string) => any | ||
@@ -39,0 +45,0 @@ swap: (name: string, indexA: number, indexB: number) => void |
{ | ||
"name": "final-form-arrays", | ||
"version": "1.1.2", | ||
"version": "1.2.0", | ||
"description": "Array Mutators for 🏁 Final Form", | ||
@@ -5,0 +5,0 @@ "main": "dist/final-form-arrays.cjs.js", |
@@ -52,2 +52,3 @@ # 🏁 Final Form Arrays | ||
- [Mutators](#mutators) | ||
- [`form.mutators.concat(name: string, value: Array<any>) => void`](#formmutatorsconcatname-string-value-arrayany--void) | ||
- [`form.mutators.insert(name: string, index: number, value: any) => undefined`](#formmutatorsinsertname-string-index-number-value-any--undefined) | ||
@@ -58,2 +59,3 @@ - [`form.mutators.move(name: string, from: number, to: number) => undefined`](#formmutatorsmovename-string-from-number-to-number--undefined) | ||
- [`form.mutators.remove(name: string, index: number) => any`](#formmutatorsremovename-string-index-number--any) | ||
- [`form.mutators.removeBatch(name: string, indexes: Array<number>) => undefined`](#formmutatorsremovebatchname-string-indexes-arraynumber--undefined) | ||
- [`form.mutators.shift(name: string) => any`](#formmutatorsshiftname-string--any) | ||
@@ -68,2 +70,6 @@ - [`form.mutators.swap(name: string, indexA: number, indexB: number) => void`](#formmutatorsswapname-string-indexa-number-indexb-number--void) | ||
### `form.mutators.concat(name: string, value: Array<any>) => void` | ||
Concatenates an array at the end of the array field. | ||
### `form.mutators.insert(name: string, index: number, value: any) => undefined` | ||
@@ -90,2 +96,6 @@ | ||
### `form.mutators.removeBatch(name: string, indexes: Array<number>) => undefined` | ||
Removes the values at the specified indexes of the array field. | ||
### `form.mutators.shift(name: string) => any` | ||
@@ -92,0 +102,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
45919
1021
112