final-form-arrays
Advanced tools
Comparing version 3.0.1 to 3.0.2
@@ -37,2 +37,3 @@ 'use strict'; | ||
delete state.fields[source.name]; | ||
state.fields[destKey] = _extends_1({}, source, { | ||
@@ -49,4 +50,22 @@ name: destKey, | ||
}); | ||
if (!state.fields[destKey].change) { | ||
delete state.fields[destKey].change; | ||
} | ||
if (!state.fields[destKey].blur) { | ||
delete state.fields[destKey].blur; | ||
} | ||
if (!state.fields[destKey].focus) { | ||
delete state.fields[destKey].focus; | ||
} | ||
} | ||
// | ||
// From MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#Escaping | ||
var escapeRegexTokens = function escapeRegexTokens(string) { | ||
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); | ||
}; // $& means the whole matched string | ||
var insert = function insert(_ref, state, _ref2) { | ||
@@ -62,9 +81,11 @@ var name = _ref[0], | ||
return copy; | ||
}); // now we have increment any higher indexes | ||
}); | ||
var pattern = new RegExp("^" + name + "\\[(\\d+)\\](.*)"); | ||
var backup = _extends_1({}, state.fields); // now we have increment any higher indexes | ||
var backup = _extends_1({}, state.fields); | ||
Object.keys(state.fields).forEach(function (key) { | ||
var pattern = new RegExp("^" + escapeRegexTokens(name) + "\\[(\\d+)\\](.*)"); // we need to increment high indices first so | ||
// lower indices won't overlap | ||
Object.keys(state.fields).sort().reverse().forEach(function (key) { | ||
var tokens = pattern.exec(key); | ||
@@ -80,6 +101,2 @@ | ||
} | ||
if (fieldIndex === index) { | ||
resetFieldState(key); | ||
} | ||
} | ||
@@ -101,2 +118,37 @@ }); | ||
function moveFields(name, matchPrefix, destIndex, state) { | ||
Object.keys(state.fields).forEach(function (key) { | ||
if (key.substring(0, matchPrefix.length) === matchPrefix) { | ||
var suffix = key.substring(matchPrefix.length); | ||
var destKey = name + "[" + destIndex + "]" + suffix; | ||
moveFieldState(state, state.fields[key], destKey); | ||
} | ||
}); | ||
} | ||
// | ||
function restoreFunctions(state, backupState) { | ||
Object.keys(state.fields).forEach(function (key) { | ||
state.fields[key] = _extends_1({}, state.fields[key], { | ||
change: state.fields[key].change || backupState.fields[key] && backupState.fields[key].change, | ||
blur: state.fields[key].blur || backupState.fields[key] && backupState.fields[key].blur, | ||
focus: state.fields[key].focus || backupState.fields[key] && backupState.fields[key].focus | ||
}); | ||
if (!state.fields[key].change) { | ||
delete state.fields[key].change; | ||
} | ||
if (!state.fields[key].blur) { | ||
delete state.fields[key].blur; | ||
} | ||
if (!state.fields[key].focus) { | ||
delete state.fields[key].focus; | ||
} | ||
}); | ||
} | ||
var TMP = 'tmp'; | ||
var move = function move(_ref, state, _ref2) { | ||
@@ -118,34 +170,37 @@ var name = _ref[0], | ||
return copy; | ||
}); //make a copy of a state for further functions restore | ||
var backupState = _extends_1({}, state, { | ||
fields: _extends_1({}, state.fields) // move this row to tmp index | ||
}); | ||
var fromPrefix = name + "[" + from + "]"; | ||
Object.keys(state.fields).forEach(function (key) { | ||
if (key.substring(0, fromPrefix.length) === fromPrefix) { | ||
var suffix = key.substring(fromPrefix.length); | ||
var fromKey = fromPrefix + suffix; | ||
var backup = state.fields[fromKey]; | ||
moveFields(name, fromPrefix, TMP, state); | ||
if (from < to) { | ||
// moving to a higher index | ||
// decrement all indices between from and to | ||
for (var i = from; i < to; i++) { | ||
var destKey = name + "[" + i + "]" + suffix; | ||
moveFieldState(state, state.fields[name + "[" + (i + 1) + "]" + suffix], destKey); | ||
} | ||
} else { | ||
// moving to a lower index | ||
// increment all indices between to and from | ||
for (var _i = from; _i > to; _i--) { | ||
var _destKey = name + "[" + _i + "]" + suffix; | ||
if (from < to) { | ||
// moving to a higher index | ||
// decrement all indices between from and to | ||
for (var i = from + 1; i <= to; i++) { | ||
var innerFromPrefix = name + "[" + i + "]"; | ||
moveFields(name, innerFromPrefix, "" + (i - 1), state); | ||
} | ||
} else { | ||
// moving to a lower index | ||
// increment all indices between to and from | ||
for (var _i = from - 1; _i >= to; _i--) { | ||
var _innerFromPrefix = name + "[" + _i + "]"; | ||
moveFieldState(state, state.fields[name + "[" + (_i - 1) + "]" + suffix], _destKey); | ||
} | ||
} | ||
moveFields(name, _innerFromPrefix, "" + (_i + 1), state); | ||
} | ||
} // move from tmp index to destination | ||
var toKey = name + "[" + to + "]" + suffix; | ||
moveFieldState(state, backup, toKey); | ||
} | ||
}); | ||
var tmpPrefix = name + "[" + TMP + "]"; | ||
moveFields(name, tmpPrefix, to, state); | ||
restoreFunctions(state, backupState); | ||
}; | ||
// | ||
var pop = function pop(_ref, state, _ref2) { | ||
@@ -169,3 +224,3 @@ var name = _ref[0]; | ||
if (removedIndex !== undefined) { | ||
var pattern = new RegExp("^" + name + "\\[" + removedIndex + "].*"); | ||
var pattern = new RegExp("^" + escapeRegexTokens(name) + "\\[" + removedIndex + "].*"); | ||
Object.keys(state.fields).forEach(function (key) { | ||
@@ -194,3 +249,4 @@ if (pattern.test(key)) { | ||
index = _ref[1]; | ||
var changeValue = _ref2.changeValue; | ||
var changeValue = _ref2.changeValue, | ||
renameField = _ref2.renameField; | ||
var returnValue; | ||
@@ -205,3 +261,3 @@ changeValue(state, name, function (array) { | ||
var pattern = new RegExp("^" + name + "\\[(\\d+)\\](.*)"); | ||
var pattern = new RegExp("^" + escapeRegexTokens(name) + "\\[(\\d+)\\](.*)"); | ||
@@ -225,3 +281,9 @@ var backup = _extends_1({}, state, { | ||
var decrementedKey = name + "[" + (fieldIndex - 1) + "]" + tokens[2]; | ||
moveFieldState(state, backup.fields[key], decrementedKey, backup); | ||
if (backup.fields[decrementedKey]) { | ||
moveFieldState(state, backup.fields[key], decrementedKey, backup); | ||
} else { | ||
// take care of setting the correct change, blur, focus, validators on new field | ||
renameField(state, key, decrementedKey); | ||
} | ||
} | ||
@@ -273,3 +335,3 @@ } | ||
var pattern = new RegExp("^" + name + "\\[(\\d+)\\](.*)"); | ||
var pattern = new RegExp("^" + escapeRegexTokens(name) + "\\[(\\d+)\\](.*)"); | ||
@@ -307,3 +369,3 @@ var newState = _extends_1({}, state, { | ||
// | ||
var TMP$1 = 'tmp'; | ||
@@ -326,16 +388,16 @@ var swap = function swap(_ref, state, _ref2) { | ||
return copy; | ||
}); // swap all field state that begin with "name[indexA]" with that under "name[indexB]" | ||
}); //make a copy of a state for further functions restore | ||
var backupState = _extends_1({}, state, { | ||
fields: _extends_1({}, state.fields) // swap all field state that begin with "name[indexA]" with that under "name[indexB]" | ||
}); | ||
var aPrefix = name + "[" + indexA + "]"; | ||
var bPrefix = name + "[" + indexB + "]"; | ||
Object.keys(state.fields).forEach(function (key) { | ||
if (key.substring(0, aPrefix.length) === aPrefix) { | ||
var suffix = key.substring(aPrefix.length); | ||
var aKey = aPrefix + suffix; | ||
var bKey = bPrefix + suffix; | ||
var fieldA = state.fields[aKey]; | ||
moveFieldState(state, state.fields[bKey], aKey); | ||
moveFieldState(state, fieldA, bKey); | ||
} | ||
}); | ||
var tmpPrefix = name + "[" + TMP$1 + "]"; | ||
moveFields(name, aPrefix, TMP$1, state); | ||
moveFields(name, bPrefix, indexA, state); | ||
moveFields(name, tmpPrefix, indexB, state); | ||
restoreFunctions(state, backupState); | ||
}; | ||
@@ -342,0 +404,0 @@ |
@@ -25,2 +25,3 @@ function _extends() { | ||
delete state.fields[source.name]; | ||
state.fields[destKey] = _extends({}, source, { | ||
@@ -37,4 +38,22 @@ name: destKey, | ||
}); | ||
if (!state.fields[destKey].change) { | ||
delete state.fields[destKey].change; | ||
} | ||
if (!state.fields[destKey].blur) { | ||
delete state.fields[destKey].blur; | ||
} | ||
if (!state.fields[destKey].focus) { | ||
delete state.fields[destKey].focus; | ||
} | ||
} | ||
// | ||
// From MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#Escaping | ||
var escapeRegexTokens = function escapeRegexTokens(string) { | ||
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); | ||
}; // $& means the whole matched string | ||
var insert = function insert(_ref, state, _ref2) { | ||
@@ -50,9 +69,11 @@ var name = _ref[0], | ||
return copy; | ||
}); // now we have increment any higher indexes | ||
}); | ||
var pattern = new RegExp("^" + name + "\\[(\\d+)\\](.*)"); | ||
var backup = _extends({}, state.fields); // now we have increment any higher indexes | ||
var backup = _extends({}, state.fields); | ||
Object.keys(state.fields).forEach(function (key) { | ||
var pattern = new RegExp("^" + escapeRegexTokens(name) + "\\[(\\d+)\\](.*)"); // we need to increment high indices first so | ||
// lower indices won't overlap | ||
Object.keys(state.fields).sort().reverse().forEach(function (key) { | ||
var tokens = pattern.exec(key); | ||
@@ -68,6 +89,2 @@ | ||
} | ||
if (fieldIndex === index) { | ||
resetFieldState(key); | ||
} | ||
} | ||
@@ -89,2 +106,37 @@ }); | ||
function moveFields(name, matchPrefix, destIndex, state) { | ||
Object.keys(state.fields).forEach(function (key) { | ||
if (key.substring(0, matchPrefix.length) === matchPrefix) { | ||
var suffix = key.substring(matchPrefix.length); | ||
var destKey = name + "[" + destIndex + "]" + suffix; | ||
moveFieldState(state, state.fields[key], destKey); | ||
} | ||
}); | ||
} | ||
// | ||
function restoreFunctions(state, backupState) { | ||
Object.keys(state.fields).forEach(function (key) { | ||
state.fields[key] = _extends({}, state.fields[key], { | ||
change: state.fields[key].change || backupState.fields[key] && backupState.fields[key].change, | ||
blur: state.fields[key].blur || backupState.fields[key] && backupState.fields[key].blur, | ||
focus: state.fields[key].focus || backupState.fields[key] && backupState.fields[key].focus | ||
}); | ||
if (!state.fields[key].change) { | ||
delete state.fields[key].change; | ||
} | ||
if (!state.fields[key].blur) { | ||
delete state.fields[key].blur; | ||
} | ||
if (!state.fields[key].focus) { | ||
delete state.fields[key].focus; | ||
} | ||
}); | ||
} | ||
var TMP = 'tmp'; | ||
var move = function move(_ref, state, _ref2) { | ||
@@ -106,34 +158,37 @@ var name = _ref[0], | ||
return copy; | ||
}); //make a copy of a state for further functions restore | ||
var backupState = _extends({}, state, { | ||
fields: _extends({}, state.fields) // move this row to tmp index | ||
}); | ||
var fromPrefix = name + "[" + from + "]"; | ||
Object.keys(state.fields).forEach(function (key) { | ||
if (key.substring(0, fromPrefix.length) === fromPrefix) { | ||
var suffix = key.substring(fromPrefix.length); | ||
var fromKey = fromPrefix + suffix; | ||
var backup = state.fields[fromKey]; | ||
moveFields(name, fromPrefix, TMP, state); | ||
if (from < to) { | ||
// moving to a higher index | ||
// decrement all indices between from and to | ||
for (var i = from; i < to; i++) { | ||
var destKey = name + "[" + i + "]" + suffix; | ||
moveFieldState(state, state.fields[name + "[" + (i + 1) + "]" + suffix], destKey); | ||
} | ||
} else { | ||
// moving to a lower index | ||
// increment all indices between to and from | ||
for (var _i = from; _i > to; _i--) { | ||
var _destKey = name + "[" + _i + "]" + suffix; | ||
if (from < to) { | ||
// moving to a higher index | ||
// decrement all indices between from and to | ||
for (var i = from + 1; i <= to; i++) { | ||
var innerFromPrefix = name + "[" + i + "]"; | ||
moveFields(name, innerFromPrefix, "" + (i - 1), state); | ||
} | ||
} else { | ||
// moving to a lower index | ||
// increment all indices between to and from | ||
for (var _i = from - 1; _i >= to; _i--) { | ||
var _innerFromPrefix = name + "[" + _i + "]"; | ||
moveFieldState(state, state.fields[name + "[" + (_i - 1) + "]" + suffix], _destKey); | ||
} | ||
} | ||
moveFields(name, _innerFromPrefix, "" + (_i + 1), state); | ||
} | ||
} // move from tmp index to destination | ||
var toKey = name + "[" + to + "]" + suffix; | ||
moveFieldState(state, backup, toKey); | ||
} | ||
}); | ||
var tmpPrefix = name + "[" + TMP + "]"; | ||
moveFields(name, tmpPrefix, to, state); | ||
restoreFunctions(state, backupState); | ||
}; | ||
// | ||
var pop = function pop(_ref, state, _ref2) { | ||
@@ -157,3 +212,3 @@ var name = _ref[0]; | ||
if (removedIndex !== undefined) { | ||
var pattern = new RegExp("^" + name + "\\[" + removedIndex + "].*"); | ||
var pattern = new RegExp("^" + escapeRegexTokens(name) + "\\[" + removedIndex + "].*"); | ||
Object.keys(state.fields).forEach(function (key) { | ||
@@ -182,3 +237,4 @@ if (pattern.test(key)) { | ||
index = _ref[1]; | ||
var changeValue = _ref2.changeValue; | ||
var changeValue = _ref2.changeValue, | ||
renameField = _ref2.renameField; | ||
var returnValue; | ||
@@ -193,3 +249,3 @@ changeValue(state, name, function (array) { | ||
var pattern = new RegExp("^" + name + "\\[(\\d+)\\](.*)"); | ||
var pattern = new RegExp("^" + escapeRegexTokens(name) + "\\[(\\d+)\\](.*)"); | ||
@@ -213,3 +269,9 @@ var backup = _extends({}, state, { | ||
var decrementedKey = name + "[" + (fieldIndex - 1) + "]" + tokens[2]; | ||
moveFieldState(state, backup.fields[key], decrementedKey, backup); | ||
if (backup.fields[decrementedKey]) { | ||
moveFieldState(state, backup.fields[key], decrementedKey, backup); | ||
} else { | ||
// take care of setting the correct change, blur, focus, validators on new field | ||
renameField(state, key, decrementedKey); | ||
} | ||
} | ||
@@ -261,3 +323,3 @@ } | ||
var pattern = new RegExp("^" + name + "\\[(\\d+)\\](.*)"); | ||
var pattern = new RegExp("^" + escapeRegexTokens(name) + "\\[(\\d+)\\](.*)"); | ||
@@ -295,3 +357,3 @@ var newState = _extends({}, state, { | ||
// | ||
var TMP$1 = 'tmp'; | ||
@@ -314,16 +376,16 @@ var swap = function swap(_ref, state, _ref2) { | ||
return copy; | ||
}); // swap all field state that begin with "name[indexA]" with that under "name[indexB]" | ||
}); //make a copy of a state for further functions restore | ||
var backupState = _extends({}, state, { | ||
fields: _extends({}, state.fields) // swap all field state that begin with "name[indexA]" with that under "name[indexB]" | ||
}); | ||
var aPrefix = name + "[" + indexA + "]"; | ||
var bPrefix = name + "[" + indexB + "]"; | ||
Object.keys(state.fields).forEach(function (key) { | ||
if (key.substring(0, aPrefix.length) === aPrefix) { | ||
var suffix = key.substring(aPrefix.length); | ||
var aKey = aPrefix + suffix; | ||
var bKey = bPrefix + suffix; | ||
var fieldA = state.fields[aKey]; | ||
moveFieldState(state, state.fields[bKey], aKey); | ||
moveFieldState(state, fieldA, bKey); | ||
} | ||
}); | ||
var tmpPrefix = name + "[" + TMP$1 + "]"; | ||
moveFields(name, aPrefix, TMP$1, state); | ||
moveFields(name, bPrefix, indexA, state); | ||
moveFields(name, tmpPrefix, indexB, state); | ||
restoreFunctions(state, backupState); | ||
}; | ||
@@ -330,0 +392,0 @@ |
@@ -31,2 +31,3 @@ (function (global, factory) { | ||
delete state.fields[source.name]; | ||
state.fields[destKey] = _extends({}, source, { | ||
@@ -43,4 +44,22 @@ name: destKey, | ||
}); | ||
if (!state.fields[destKey].change) { | ||
delete state.fields[destKey].change; | ||
} | ||
if (!state.fields[destKey].blur) { | ||
delete state.fields[destKey].blur; | ||
} | ||
if (!state.fields[destKey].focus) { | ||
delete state.fields[destKey].focus; | ||
} | ||
} | ||
// | ||
// From MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#Escaping | ||
var escapeRegexTokens = function escapeRegexTokens(string) { | ||
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); | ||
}; // $& means the whole matched string | ||
var insert = function insert(_ref, state, _ref2) { | ||
@@ -56,9 +75,11 @@ var name = _ref[0], | ||
return copy; | ||
}); // now we have increment any higher indexes | ||
}); | ||
var pattern = new RegExp("^" + name + "\\[(\\d+)\\](.*)"); | ||
var backup = _extends({}, state.fields); // now we have increment any higher indexes | ||
var backup = _extends({}, state.fields); | ||
Object.keys(state.fields).forEach(function (key) { | ||
var pattern = new RegExp("^" + escapeRegexTokens(name) + "\\[(\\d+)\\](.*)"); // we need to increment high indices first so | ||
// lower indices won't overlap | ||
Object.keys(state.fields).sort().reverse().forEach(function (key) { | ||
var tokens = pattern.exec(key); | ||
@@ -74,6 +95,2 @@ | ||
} | ||
if (fieldIndex === index) { | ||
resetFieldState(key); | ||
} | ||
} | ||
@@ -95,2 +112,37 @@ }); | ||
function moveFields(name, matchPrefix, destIndex, state) { | ||
Object.keys(state.fields).forEach(function (key) { | ||
if (key.substring(0, matchPrefix.length) === matchPrefix) { | ||
var suffix = key.substring(matchPrefix.length); | ||
var destKey = name + "[" + destIndex + "]" + suffix; | ||
moveFieldState(state, state.fields[key], destKey); | ||
} | ||
}); | ||
} | ||
// | ||
function restoreFunctions(state, backupState) { | ||
Object.keys(state.fields).forEach(function (key) { | ||
state.fields[key] = _extends({}, state.fields[key], { | ||
change: state.fields[key].change || backupState.fields[key] && backupState.fields[key].change, | ||
blur: state.fields[key].blur || backupState.fields[key] && backupState.fields[key].blur, | ||
focus: state.fields[key].focus || backupState.fields[key] && backupState.fields[key].focus | ||
}); | ||
if (!state.fields[key].change) { | ||
delete state.fields[key].change; | ||
} | ||
if (!state.fields[key].blur) { | ||
delete state.fields[key].blur; | ||
} | ||
if (!state.fields[key].focus) { | ||
delete state.fields[key].focus; | ||
} | ||
}); | ||
} | ||
var TMP = 'tmp'; | ||
var move = function move(_ref, state, _ref2) { | ||
@@ -112,34 +164,37 @@ var name = _ref[0], | ||
return copy; | ||
}); //make a copy of a state for further functions restore | ||
var backupState = _extends({}, state, { | ||
fields: _extends({}, state.fields) // move this row to tmp index | ||
}); | ||
var fromPrefix = name + "[" + from + "]"; | ||
Object.keys(state.fields).forEach(function (key) { | ||
if (key.substring(0, fromPrefix.length) === fromPrefix) { | ||
var suffix = key.substring(fromPrefix.length); | ||
var fromKey = fromPrefix + suffix; | ||
var backup = state.fields[fromKey]; | ||
moveFields(name, fromPrefix, TMP, state); | ||
if (from < to) { | ||
// moving to a higher index | ||
// decrement all indices between from and to | ||
for (var i = from; i < to; i++) { | ||
var destKey = name + "[" + i + "]" + suffix; | ||
moveFieldState(state, state.fields[name + "[" + (i + 1) + "]" + suffix], destKey); | ||
} | ||
} else { | ||
// moving to a lower index | ||
// increment all indices between to and from | ||
for (var _i = from; _i > to; _i--) { | ||
var _destKey = name + "[" + _i + "]" + suffix; | ||
if (from < to) { | ||
// moving to a higher index | ||
// decrement all indices between from and to | ||
for (var i = from + 1; i <= to; i++) { | ||
var innerFromPrefix = name + "[" + i + "]"; | ||
moveFields(name, innerFromPrefix, "" + (i - 1), state); | ||
} | ||
} else { | ||
// moving to a lower index | ||
// increment all indices between to and from | ||
for (var _i = from - 1; _i >= to; _i--) { | ||
var _innerFromPrefix = name + "[" + _i + "]"; | ||
moveFieldState(state, state.fields[name + "[" + (_i - 1) + "]" + suffix], _destKey); | ||
} | ||
} | ||
moveFields(name, _innerFromPrefix, "" + (_i + 1), state); | ||
} | ||
} // move from tmp index to destination | ||
var toKey = name + "[" + to + "]" + suffix; | ||
moveFieldState(state, backup, toKey); | ||
} | ||
}); | ||
var tmpPrefix = name + "[" + TMP + "]"; | ||
moveFields(name, tmpPrefix, to, state); | ||
restoreFunctions(state, backupState); | ||
}; | ||
// | ||
var pop = function pop(_ref, state, _ref2) { | ||
@@ -163,3 +218,3 @@ var name = _ref[0]; | ||
if (removedIndex !== undefined) { | ||
var pattern = new RegExp("^" + name + "\\[" + removedIndex + "].*"); | ||
var pattern = new RegExp("^" + escapeRegexTokens(name) + "\\[" + removedIndex + "].*"); | ||
Object.keys(state.fields).forEach(function (key) { | ||
@@ -188,3 +243,4 @@ if (pattern.test(key)) { | ||
index = _ref[1]; | ||
var changeValue = _ref2.changeValue; | ||
var changeValue = _ref2.changeValue, | ||
renameField = _ref2.renameField; | ||
var returnValue; | ||
@@ -199,3 +255,3 @@ changeValue(state, name, function (array) { | ||
var pattern = new RegExp("^" + name + "\\[(\\d+)\\](.*)"); | ||
var pattern = new RegExp("^" + escapeRegexTokens(name) + "\\[(\\d+)\\](.*)"); | ||
@@ -219,3 +275,9 @@ var backup = _extends({}, state, { | ||
var decrementedKey = name + "[" + (fieldIndex - 1) + "]" + tokens[2]; | ||
moveFieldState(state, backup.fields[key], decrementedKey, backup); | ||
if (backup.fields[decrementedKey]) { | ||
moveFieldState(state, backup.fields[key], decrementedKey, backup); | ||
} else { | ||
// take care of setting the correct change, blur, focus, validators on new field | ||
renameField(state, key, decrementedKey); | ||
} | ||
} | ||
@@ -267,3 +329,3 @@ } | ||
var pattern = new RegExp("^" + name + "\\[(\\d+)\\](.*)"); | ||
var pattern = new RegExp("^" + escapeRegexTokens(name) + "\\[(\\d+)\\](.*)"); | ||
@@ -301,3 +363,3 @@ var newState = _extends({}, state, { | ||
// | ||
var TMP$1 = 'tmp'; | ||
@@ -320,16 +382,16 @@ var swap = function swap(_ref, state, _ref2) { | ||
return copy; | ||
}); // swap all field state that begin with "name[indexA]" with that under "name[indexB]" | ||
}); //make a copy of a state for further functions restore | ||
var backupState = _extends({}, state, { | ||
fields: _extends({}, state.fields) // swap all field state that begin with "name[indexA]" with that under "name[indexB]" | ||
}); | ||
var aPrefix = name + "[" + indexA + "]"; | ||
var bPrefix = name + "[" + indexB + "]"; | ||
Object.keys(state.fields).forEach(function (key) { | ||
if (key.substring(0, aPrefix.length) === aPrefix) { | ||
var suffix = key.substring(aPrefix.length); | ||
var aKey = aPrefix + suffix; | ||
var bKey = bPrefix + suffix; | ||
var fieldA = state.fields[aKey]; | ||
moveFieldState(state, state.fields[bKey], aKey); | ||
moveFieldState(state, fieldA, bKey); | ||
} | ||
}); | ||
var tmpPrefix = name + "[" + TMP$1 + "]"; | ||
moveFields(name, aPrefix, TMP$1, state); | ||
moveFields(name, bPrefix, indexA, state); | ||
moveFields(name, tmpPrefix, indexB, state); | ||
restoreFunctions(state, backupState); | ||
}; | ||
@@ -336,0 +398,0 @@ |
@@ -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 l(){return(l=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)}function v(e,n,r,t){void 0===t&&(t=e),e.fields[r]=l({},n,{name:r,change:t.fields[r]&&t.fields[r].change,blur:t.fields[r]&&t.fields[r].blur,focus:t.fields[r]&&t.fields[r].focus,lastFieldState:void 0})}function f(e,i,n){var f=e[0],c=e[1],r=e[2],t=n.changeValue,a=n.resetFieldState;t(i,f,function(e){var n=[].concat(e||[]);return n.splice(c,0,r),n});var u=new RegExp("^"+f+"\\[(\\d+)\\](.*)"),o=l({},i.fields);Object.keys(i.fields).forEach(function(e){var n=u.exec(e);if(n){var r=Number(n[1]);if(c<=r){var t=f+"["+(r+1)+"]"+n[2];v(i,o[e],t)}r===c&&a(e)}})}function i(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+)\\](.*)"),u=l({},i,{fields:l({},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];v(i,u.fields[e],t,u)}}}),r}var 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,u,n){var o=e[0],s=e[1],l=e[2],r=n.changeValue;if(s!==l){r(u,o,function(e){var n=[].concat(e||[]),r=n[s];return n.splice(s,1),n.splice(l,0,r),n});var d=o+"["+s+"]";Object.keys(u.fields).forEach(function(e){if(e.substring(0,d.length)===d){var n=e.substring(d.length),r=d+n,t=u.fields[r];if(s<l)for(var i=s;i<l;i++){var f=o+"["+i+"]"+n;v(u,u.fields[o+"["+(i+1)+"]"+n],f)}else for(var c=s;l<c;c--){var a=o+"["+c+"]"+n;v(u,u.fields[o+"["+(c-1)+"]"+n],a)}v(u,t,o+"["+l+"]"+n)}})}},pop:function(e,n,r){var t,i,f=e[0];if((0,r.changeValue)(n,f,function(e){if(e)return e.length?(i=e.length-1,t=e[i],e.slice(0,i)):[]}),void 0!==i){var c=new RegExp("^"+f+"\\["+i+"].*");Object.keys(n.fields).forEach(function(e){c.test(e)&&delete n.fields[e]})}return 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,i,n){var f=e[0],c=e[1],r=n.changeValue,a=[].concat(c);a.sort();for(var t=0;t<a.length;t++)0<t&&a[t]===a[t-1]&&a.splice(t--,1);var u=[];r(i,f,function(n){if(u=c.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 o=new RegExp("^"+f+"\\[(\\d+)\\](.*)"),s=l({},i,{fields:{}});return Object.keys(i.fields).forEach(function(e){var n=o.exec(e);if(n){var r=Number(n[1]);if(!~a.indexOf(r)){var t=f+"["+(r-function(e,r){return e.reduce(function(e,n){return n<r?e+1:e},0)}(a,r))+"]"+n[2];v(s,i.fields[e],t,i)}}else s.fields[e]=i.fields[e]}),i.fields=s.fields,u},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+"]",u=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=u+n,i=f.fields[r];v(f,f.fields[t],r),v(f,i,t)}})}},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})}); | ||
!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 o(){return(o=Object.assign||function(e){for(var n=1;n<arguments.length;n++){var f=arguments[n];for(var i in f)Object.prototype.hasOwnProperty.call(f,i)&&(e[i]=f[i])}return e}).apply(this,arguments)}function d(e,n,f,i){void 0===i&&(i=e),delete e.fields[n.name],e.fields[f]=o({},n,{name:f,change:i.fields[f]&&i.fields[f].change,blur:i.fields[f]&&i.fields[f].blur,focus:i.fields[f]&&i.fields[f].focus,lastFieldState:void 0}),e.fields[f].change||delete e.fields[f].change,e.fields[f].blur||delete e.fields[f].blur,e.fields[f].focus||delete e.fields[f].focus}function v(e){return e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}function r(e,t,n){var r=e[0],c=e[1],f=e[2],i=n.changeValue;n.resetFieldState,i(t,r,function(e){var n=[].concat(e||[]);return n.splice(c,0,f),n});var l=o({},t.fields),s=new RegExp("^"+v(r)+"\\[(\\d+)\\](.*)");Object.keys(t.fields).sort().reverse().forEach(function(e){var n=s.exec(e);if(n){var f=Number(n[1]);if(c<=f){var i=r+"["+(f+1)+"]"+n[2];d(t,l[e],i)}}})}function u(i,t,r,c){Object.keys(c.fields).forEach(function(e){if(e.substring(0,t.length)===t){var n=e.substring(t.length),f=i+"["+r+"]"+n;d(c,c.fields[e],f)}})}function h(n,f){Object.keys(n.fields).forEach(function(e){n.fields[e]=o({},n.fields[e],{change:n.fields[e].change||f.fields[e]&&f.fields[e].change,blur:n.fields[e].blur||f.fields[e]&&f.fields[e].blur,focus:n.fields[e].focus||f.fields[e]&&f.fields[e].focus}),n.fields[e].change||delete n.fields[e].change,n.fields[e].blur||delete n.fields[e].blur,n.fields[e].focus||delete n.fields[e].focus})}function t(e,t,n){var f,r=e[0],c=e[1],i=n.changeValue,l=n.renameField;i(t,r,function(e){var n=[].concat(e||[]);return f=n[c],n.splice(c,1),n});var s=new RegExp("^"+v(r)+"\\[(\\d+)\\](.*)"),a=o({},t,{fields:o({},t.fields)});return Object.keys(t.fields).forEach(function(e){var n=s.exec(e);if(n){var f=Number(n[1]);if(f===c)delete t.fields[e];else if(c<f){delete t.fields[e];var i=r+"["+(f-1)+"]"+n[2];a.fields[i]?d(t,a.fields[e],i,a):l(t,e,i)}}}),f}var n={insert:r,concat:function(e,n,f){var i=e[0],t=e[1];(0,f.changeValue)(n,i,function(e){return e?[].concat(e,t):t})},move:function(e,n,f){var i=e[0],t=e[1],r=e[2],c=f.changeValue;if(t!==r){c(n,i,function(e){var n=[].concat(e||[]),f=n[t];return n.splice(t,1),n.splice(r,0,f),n});var l=o({},n,{fields:o({},n.fields)});if(u(i,i+"["+t+"]","tmp",n),t<r)for(var s=t+1;s<=r;s++){u(i,i+"["+s+"]",""+(s-1),n)}else for(var a=t-1;r<=a;a--){u(i,i+"["+a+"]",""+(a+1),n)}u(i,i+"[tmp]",r,n),h(n,l)}},pop:function(e,n,f){var i,t,r=e[0];if((0,f.changeValue)(n,r,function(e){if(e)return e.length?(t=e.length-1,i=e[t],e.slice(0,t)):[]}),void 0!==t){var c=new RegExp("^"+v(r)+"\\["+t+"].*");Object.keys(n.fields).forEach(function(e){c.test(e)&&delete n.fields[e]})}return i},push:function(e,n,f){var i=e[0],t=e[1];(0,f.changeValue)(n,i,function(e){return e?[].concat(e,[t]):[t]})},remove:t,removeBatch:function(e,t,n){var r=e[0],c=e[1],f=n.changeValue,l=[].concat(c);l.sort();for(var i=0;i<l.length;i++)0<i&&l[i]===l[i-1]&&l.splice(i--,1);var s=[];f(t,r,function(n){if(s=c.map(function(e){return n&&n[e]}),!n||!l.length)return n;var f=[].concat(n),i=[];return l.forEach(function(e){f.splice(e-i.length,1),i.push(n&&n[e])}),f});var a=new RegExp("^"+v(r)+"\\[(\\d+)\\](.*)"),u=o({},t,{fields:{}});return Object.keys(t.fields).forEach(function(e){var n=a.exec(e);if(n){var f=Number(n[1]);if(!~l.indexOf(f)){var i=r+"["+(f-function(e,f){return e.reduce(function(e,n){return n<f?e+1:e},0)}(l,f))+"]"+n[2];d(u,t.fields[e],i,t)}}else u.fields[e]=t.fields[e]}),t.fields=u.fields,s},shift:function(e,n,f){var i=e[0];return t([i,0],n,f)},swap:function(e,n,f){var i=e[0],t=e[1],r=e[2],c=f.changeValue;if(t!==r){c(n,i,function(e){var n=[].concat(e||[]),f=n[t];return n[t]=n[r],n[r]=f,n});var l=o({},n,{fields:o({},n.fields)}),s=i+"["+r+"]",a=i+"[tmp]";u(i,i+"["+t+"]","tmp",n),u(i,s,t,n),u(i,a,r,n),h(n,l)}},unshift:function(e,n,f){var i=e[0],t=e[1];return r([i,0,t],n,f)},update:function(e,n,f){var i=e[0],t=e[1],r=e[2];(0,f.changeValue)(n,i,function(e){var n=[].concat(e||[]);return n.splice(t,1,r),n})}};e.default=n,Object.defineProperty(e,"__esModule",{value:!0})}); | ||
//# sourceMappingURL=final-form-arrays.umd.min.js.map |
@@ -15,14 +15,14 @@ import { Mutator } from 'final-form' | ||
export interface DefaultType { | ||
insert: Mutator | ||
concat: Mutator | ||
move: Mutator | ||
pop: Mutator | ||
push: Mutator | ||
removeBatch: Mutator | ||
remove: Mutator | ||
shift: Mutator | ||
swap: Mutator | ||
update: Mutator | ||
unshift: Mutator | ||
export interface DefaultType<FormValues = any> { | ||
insert: Mutator<FormValues> | ||
concat: Mutator<FormValues> | ||
move: Mutator<FormValues> | ||
pop: Mutator<FormValues> | ||
push: Mutator<FormValues> | ||
removeBatch: Mutator<FormValues> | ||
remove: Mutator<FormValues> | ||
shift: Mutator<FormValues> | ||
swap: Mutator<FormValues> | ||
update: Mutator<FormValues> | ||
unshift: Mutator<FormValues> | ||
} | ||
@@ -29,0 +29,0 @@ |
{ | ||
"name": "final-form-arrays", | ||
"version": "3.0.1", | ||
"version": "3.0.2", | ||
"description": "Array Mutators for 🏁 Final Form", | ||
@@ -5,0 +5,0 @@ "main": "dist/final-form-arrays.cjs.js", |
@@ -14,99 +14,4 @@ # 🏁 Final Form Arrays | ||
## Installation | ||
## [Getting Started](https://final-form.org/docs/final-form-arrays/getting-started) | ||
```bash | ||
npm install --save final-form-arrays | ||
``` | ||
or | ||
```bash | ||
yarn add final-form-arrays | ||
``` | ||
## Usage | ||
```js | ||
import { createForm } from 'final-form' | ||
import arrayMutators from 'final-form-arrays' | ||
// Create Form | ||
const form = createForm({ | ||
mutators: { ...arrayMutators }, | ||
onSubmit | ||
}) | ||
// push | ||
form.mutators.push('customers', { firstName: '', lastName: '' }) | ||
// pop | ||
const customer = form.mutators.pop('customers') | ||
``` | ||
## Table of Contents | ||
<!-- START doctoc generated TOC please keep comment here to allow auto update --> | ||
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --> | ||
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --> | ||
- [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) | ||
- [`form.mutators.move(name: string, from: number, to: number) => undefined`](#formmutatorsmovename-string-from-number-to-number--undefined) | ||
- [`form.mutators.pop(name: string) => any`](#formmutatorspopname-string--any) | ||
- [`form.mutators.push(name: string, value: any) => void`](#formmutatorspushname-string-value-any--void) | ||
- [`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) | ||
- [`form.mutators.swap(name: string, indexA: number, indexB: number) => void`](#formmutatorsswapname-string-indexa-number-indexb-number--void) | ||
- [`form.mutators.update(name: string, index: number, value: any) => void`](#formmutatorsupdatename-string-index-number-value-any--void) | ||
- [`form.mutators.unshift(name: string, value: any) => void`](#formmutatorsunshiftname-string-value-any--void) | ||
<!-- END doctoc generated TOC please keep comment here to allow auto update --> | ||
## Mutators | ||
### `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` | ||
Inserts a value into the specified index of the array field. | ||
### `form.mutators.move(name: string, from: number, to: number) => undefined` | ||
Moves a value from one index to another index in the array field. | ||
### `form.mutators.pop(name: string) => any` | ||
Pops a value off the end of an array field. Returns the value. | ||
### `form.mutators.push(name: string, value: any) => void` | ||
Pushes a value onto the end of an array field. | ||
### `form.mutators.remove(name: string, index: number) => any` | ||
Removes a value from the specified index of the array field. Returns the removed | ||
value. | ||
### `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` | ||
Removes a value from the beginning of the array field. Returns the value. | ||
### `form.mutators.swap(name: string, indexA: number, indexB: number) => void` | ||
Swaps the position of two values in the array field. | ||
### `form.mutators.update(name: string, index: number, value: any) => void` | ||
Updates a value of the specified index of the array field. | ||
### `form.mutators.unshift(name: string, value: any) => void` | ||
Inserts a value onto the beginning of the array field. | ||
## [API](https://final-form.org/docs/final-form-arrays/api) |
Sorry, the diff of this file is not supported yet
48163
1104
17