redux-form
Version:
A higher order component decorator for forms using Redux and React
94 lines (89 loc) • 3.35 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports["default"] = createFieldArrayProps;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));
function createFieldArrayProps(_ref2, name, form, sectionPrefix, getValue, _ref) {
var getIn = _ref2.getIn;
var arrayInsert = _ref.arrayInsert,
arrayMove = _ref.arrayMove,
arrayPop = _ref.arrayPop,
arrayPush = _ref.arrayPush,
arrayRemove = _ref.arrayRemove,
arrayRemoveAll = _ref.arrayRemoveAll,
arrayShift = _ref.arrayShift,
arraySplice = _ref.arraySplice,
arraySwap = _ref.arraySwap,
arrayUnshift = _ref.arrayUnshift,
asyncError = _ref.asyncError,
dirty = _ref.dirty,
length = _ref.length,
pristine = _ref.pristine,
submitError = _ref.submitError,
state = _ref.state,
submitFailed = _ref.submitFailed,
submitting = _ref.submitting,
syncError = _ref.syncError,
syncWarning = _ref.syncWarning,
value = _ref.value,
props = _ref.props,
rest = (0, _objectWithoutPropertiesLoose2["default"])(_ref, ["arrayInsert", "arrayMove", "arrayPop", "arrayPush", "arrayRemove", "arrayRemoveAll", "arrayShift", "arraySplice", "arraySwap", "arrayUnshift", "asyncError", "dirty", "length", "pristine", "submitError", "state", "submitFailed", "submitting", "syncError", "syncWarning", "value", "props"]);
var error = syncError || asyncError || submitError;
var warning = syncWarning;
var fieldName = sectionPrefix ? name.replace(sectionPrefix + ".", '') : name;
var finalProps = (0, _extends2["default"])({
fields: {
_isFieldArray: true,
forEach: function forEach(callback) {
return (value || []).forEach(function (item, index) {
return callback(fieldName + "[" + index + "]", index, finalProps.fields);
});
},
get: getValue,
getAll: function getAll() {
return value;
},
insert: arrayInsert,
length: length,
map: function map(callback) {
return (value || []).map(function (item, index) {
return callback(fieldName + "[" + index + "]", index, finalProps.fields);
});
},
move: arrayMove,
name: name,
pop: function pop() {
arrayPop();
return getIn(value, String(length - 1));
},
push: arrayPush,
reduce: function reduce(callback, initial) {
return (value || []).reduce(function (accumulator, item, index) {
return callback(accumulator, fieldName + "[" + index + "]", index, finalProps.fields);
}, initial);
},
remove: arrayRemove,
removeAll: arrayRemoveAll,
shift: function shift() {
arrayShift();
return getIn(value, '0');
},
splice: arraySplice,
swap: arraySwap,
unshift: arrayUnshift
},
meta: {
dirty: dirty,
error: error,
form: form,
warning: warning,
invalid: !!error,
pristine: pristine,
submitting: submitting,
submitFailed: submitFailed,
valid: !error
}
}, props, {}, rest);
return finalProps;
}