@reactables/forms 2.0.0-beta.1 → 2.0.0-beta.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -9,7 +9,7 @@ var DEFAULT_HUB2_FIELDS = {
9
9
  pending: false,
10
10
  valid: null,
11
11
  childrenValid: null,
12
- errors: null
12
+ errors: {}
13
13
  };
14
14
 
15
15
  var Controls = /*#__PURE__*/Object.freeze({
@@ -147,16 +147,15 @@ var getErrors = function (control, value, _a) {
147
147
  var _b;
148
148
  var validators = _a.validators;
149
149
  return ((_b = control.config.validators) === null || _b === void 0 ? void 0 : _b.reduce(function (acc, validator) {
150
- if (!validators[validator]) {
150
+ if (!(validators === null || validators === void 0 ? void 0 : validators[validator])) {
151
151
  throw "You have not provided a validator for \"".concat(validator, "\"");
152
152
  }
153
153
  return __assign(__assign({}, acc), validators[validator](value));
154
154
  }, {})) || {};
155
155
  };
156
156
 
157
- var buildState = function (config, form, controlRef, providers) {
157
+ var buildState = function (config, controlRef, providers, form) {
158
158
  var _a;
159
- if (form === void 0) { form = { root: null }; }
160
159
  if (controlRef === void 0) { controlRef = []; }
161
160
  var value = getValueFromControlConfig(config);
162
161
  var control = {
@@ -175,22 +174,21 @@ var buildState = function (config, form, controlRef, providers) {
175
174
  if (controls && !(controls instanceof Array)) {
176
175
  newForm = Object.entries(config.controls).reduce(function (acc, _a) {
177
176
  var key = _a[0], controlConfig = _a[1];
178
- return buildState(controlConfig, acc, controlRef.concat(key), providers);
177
+ return buildState(controlConfig, controlRef.concat(key), providers, acc);
179
178
  }, newForm);
180
179
  }
181
180
  else if (controls && controls instanceof Array) {
182
181
  // Adding controls for Form Array
183
182
  newForm = config.controls.reduce(function (acc, controlConfig, index) {
184
- return buildState(controlConfig, acc, controlRef.concat(index), providers);
183
+ return buildState(controlConfig, controlRef.concat(index), providers, acc);
185
184
  }, newForm);
186
185
  }
187
186
  return newForm;
188
187
  };
189
- var buildFormState = function (config, form, controlRef, providers) {
190
- if (form === void 0) { form = { root: null }; }
188
+ var buildFormState = function (config, controlRef, providers, form) {
191
189
  if (controlRef === void 0) { controlRef = []; }
192
190
  return {
193
- form: buildState(config, form, controlRef, providers)
191
+ form: buildState(config, controlRef, providers, form)
194
192
  };
195
193
  };
196
194
 
@@ -201,7 +199,8 @@ var getScopedEffectsForControl = function (formControl, providers) {
201
199
  if (asyncValidators && asyncValidators.length) {
202
200
  scopedEffects = asyncValidators.reduce(function (acc, asyncValidator, validatorIndex) {
203
201
  var effect = function (actions$) {
204
- if (!providers.asyncValidators[asyncValidator]) {
202
+ var _a;
203
+ if (!((_a = providers.asyncValidators) === null || _a === void 0 ? void 0 : _a[asyncValidator])) {
205
204
  throw "You have not provided an asyncValidator for \"".concat(asyncValidator, "\"");
206
205
  }
207
206
  return actions$.pipe(map(function (_a) {
@@ -303,7 +302,7 @@ var getDescendantControls = function (controlRef, form, excludeSelf) {
303
302
  }
304
303
  var control = getControl(controlRef, form);
305
304
  var value = control.value, config = control.config;
306
- var descendants;
305
+ var descendants = [];
307
306
  if (Array.isArray(config.controls)) {
308
307
  // If control is a Form Array
309
308
  descendants = value.reduce(function (acc, item, index) {
@@ -382,9 +381,11 @@ var controlRefCheck = function (ref) {
382
381
  catch (_a) { }
383
382
  };
384
383
 
385
- var checkKeys = function (obj1, obj2) {
386
- if (Object.keys(obj1).slice().sort().join() !== Object.keys(obj2).slice().sort().join()) {
387
- throw 'Can not update value because keys are not the same.';
384
+ var checkKeys = function (oldObj, newObj) {
385
+ var oldKeys = Object.keys(oldObj).slice().sort().join();
386
+ var newKeys = Object.keys(newObj).slice().sort().join();
387
+ if (oldKeys !== newKeys) {
388
+ console.warn("You are trying to update multiple controls but the keys don't match. It is recommended your keys match. Old keys: ".concat(oldKeys, ". New Keys: ").concat(newKeys, " "));
388
389
  }
389
390
  };
390
391
  var updateDescendantValues = function (form, _a, providers) {
@@ -415,10 +416,11 @@ var updateDescendantValues = function (form, _a, providers) {
415
416
  // Use AddControlPayload/RemoveControl action reducers to add/remove control
416
417
  var updateValues = function (_a, action, providers, mergeChanges) {
417
418
  var _b, _c;
418
- var form = _a.form, _d = _a._changedControls, _changedControls = _d === void 0 ? {} : _d, _e = _a._removedControls, _removedControls = _e === void 0 ? {} : _e;
419
+ var _d;
420
+ var form = _a.form, _e = _a._changedControls, _changedControls = _e === void 0 ? {} : _e, _f = _a._removedControls, _removedControls = _f === void 0 ? {} : _f;
419
421
  if (mergeChanges === void 0) { mergeChanges = false; }
420
422
  var normalizers = providers.normalizers;
421
- var _f = action.payload, controlRef = _f.controlRef, value = _f.value;
423
+ var _g = action.payload, controlRef = _g.controlRef, value = _g.value;
422
424
  controlRefCheck(controlRef);
423
425
  // Update its own value
424
426
  var ctrlKey = getFormKey(controlRef);
@@ -426,8 +428,8 @@ var updateValues = function (_a, action, providers, mergeChanges) {
426
428
  var oldValue = form[ctrlKey].value;
427
429
  var config = form[ctrlKey].config;
428
430
  if (config.normalizers) {
429
- newValue = config.normalizers.reduce(function (acc, normalizer) {
430
- if (!normalizers[normalizer]) {
431
+ newValue = (_d = config.normalizers) === null || _d === void 0 ? void 0 : _d.reduce(function (acc, normalizer) {
432
+ if (!(normalizers === null || normalizers === void 0 ? void 0 : normalizers[normalizer])) {
431
433
  throw "You have not provided a normalizer for \"".concat(normalizer, "\"");
432
434
  }
433
435
  return normalizers[normalizer](acc);
@@ -627,7 +629,7 @@ var addControl = function (state, action, providers, mergeChanges) {
627
629
  if (!getControl(controlRef.slice(0, -1), state.form)) {
628
630
  throw 'You are attempting to add a control to a non-existent form group';
629
631
  }
630
- var newForm = buildState(config, state.form, controlRef, providers);
632
+ var newForm = buildState(config, controlRef, providers, state.form);
631
633
  var newValue = getControl(controlRef, newForm).value;
632
634
  var ancestorsUpdated = updateAncestorValuesAddControl(newForm, {
633
635
  payload: { controlRef: controlRef, value: newValue }
@@ -658,7 +660,7 @@ var pushControl = function (state, action, providers, mergeChanges) {
658
660
  else {
659
661
  throw 'You are attempting to push to a control that is not a Form Array';
660
662
  }
661
- var newForm = buildState(config, state.form, newControlRef, providers);
663
+ var newForm = buildState(config, newControlRef, providers, state.form);
662
664
  var newValue = getControl(newControlRef, newForm).value;
663
665
  var ancestorsUpdated = updateAncestorValuesAddControl(newForm, {
664
666
  payload: { controlRef: newControlRef, value: newValue }
@@ -802,7 +804,7 @@ var resetControl = function (state, action, providers, mergeChanges) {
802
804
  delete descendantsRemoved[key];
803
805
  });
804
806
  // Remove all descendants
805
- var restoredControls = buildState(controlToReset.config, descendantsRemoved, controlToReset.controlRef, providers);
807
+ var restoredControls = buildState(controlToReset.config, controlToReset.controlRef, providers, descendantsRemoved);
806
808
  var restoredControlValue = getControl(controlRef, restoredControls).value;
807
809
  var result = updateAncestorValues(restoredControls, {
808
810
  payload: { controlRef: controlRef, value: restoredControlValue }
@@ -832,12 +834,14 @@ var asyncValidation = function (form, _a) {
832
834
  var controlRef = _a.payload;
833
835
  var updatedSelfAndAncestors = getAncestorControls(controlRef, form).reduce(function (acc, control) {
834
836
  var _a;
837
+ var _b;
835
838
  var isChangedControl = getFormKey(control.controlRef) === getFormKey(controlRef);
836
839
  return __assign(__assign({}, acc), (_a = {}, _a[getFormKey(control.controlRef)] = __assign(__assign({}, control), { pending: true, asyncValidateInProgress: isChangedControl
837
- ? __assign({}, control.config.asyncValidators.reduce(function (acc, _, index) {
840
+ ? __assign({}, (_b = control.config.asyncValidators) === null || _b === void 0 ? void 0 : _b.reduce(function (acc, _, index) {
838
841
  var _a;
839
842
  return (__assign(__assign({}, acc), (_a = {}, _a[index] = true, _a)));
840
- }, {})) : control.asyncValidateInProgress }), _a));
843
+ }, {}))
844
+ : control.asyncValidateInProgress }), _a));
841
845
  }, {});
842
846
  return __assign(__assign({}, form), updatedSelfAndAncestors);
843
847
  };
@@ -1101,7 +1105,7 @@ var build = function (config, options) {
1101
1105
  validators: __assign(__assign({}, Validators), (_b = options.providers) === null || _b === void 0 ? void 0 : _b.validators),
1102
1106
  asyncValidators: __assign({}, (_c = options.providers) === null || _c === void 0 ? void 0 : _c.asyncValidators)
1103
1107
  };
1104
- var initialState = buildFormState(config, undefined, undefined, providers);
1108
+ var initialState = buildFormState(config, undefined, providers);
1105
1109
  return createReactable(initialState, options);
1106
1110
  };
1107
1111
  var load = function (state, options) {
@@ -1181,9 +1185,10 @@ var createReactable = function (initialBaseState, options, initialFormState) {
1181
1185
  hub1Actions.destroy();
1182
1186
  hub2Actions.destroy();
1183
1187
  };
1188
+ var actions = __assign(__assign({}, hub1Actions), { destroy: destroy });
1184
1189
  return [
1185
1190
  state$.pipe(filter(function (form) { return form !== null; })),
1186
- __assign(__assign({}, hub1Actions), { destroy: destroy }),
1191
+ actions,
1187
1192
  hub1Actions$,
1188
1193
  ];
1189
1194
  };