@reactables/forms 0.6.0-alpha.0 → 0.7.0-alpha.1

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.
Files changed (38) hide show
  1. package/README.md +208 -33
  2. package/dist/Helpers/addAsyncValidationEffects.d.ts +2 -1
  3. package/dist/Helpers/buildFormState.d.ts +3 -2
  4. package/dist/Helpers/buildHub2Source.d.ts +2 -2
  5. package/dist/Models/Configs.d.ts +3 -3
  6. package/dist/Models/Payloads.d.ts +4 -4
  7. package/dist/Models/Validators.d.ts +2 -2
  8. package/dist/Models/index.d.ts +1 -1
  9. package/dist/Reducers/Hub1/addControl.d.ts +3 -2
  10. package/dist/Reducers/Hub1/getErrors.d.ts +2 -1
  11. package/dist/Reducers/Hub1/markControlAsTouched.d.ts +2 -2
  12. package/dist/Reducers/Hub1/pushControl.d.ts +3 -2
  13. package/dist/Reducers/Hub1/removeControl.d.ts +2 -1
  14. package/dist/Reducers/Hub1/resetControl.d.ts +2 -1
  15. package/dist/Reducers/Hub1/updateAncestorValues.d.ts +3 -2
  16. package/dist/Reducers/Hub1/updateAncestorValuesAddControl.d.ts +3 -2
  17. package/dist/Reducers/Hub1/updateAncestorValuesRemoveControl.d.ts +2 -1
  18. package/dist/Reducers/Hub1/updateValues.d.ts +3 -2
  19. package/dist/RxForm/RxForm.d.ts +34 -16
  20. package/dist/RxForm/Tests/customReducers.test.d.ts +1 -0
  21. package/dist/RxForm/Tests/initialization.test.d.ts +1 -0
  22. package/dist/RxForm/Tests/load.test.d.ts +1 -0
  23. package/dist/RxForm/Tests/markControlAsPristine.test.d.ts +1 -0
  24. package/dist/RxForm/Tests/markControlTouchStatus.test.d.ts +1 -0
  25. package/dist/RxForm/Tests/pushControl.test.d.ts +1 -0
  26. package/dist/RxForm/Tests/removeControl.test.d.ts +1 -0
  27. package/dist/RxForm/Tests/resetControl.test.d.ts +1 -0
  28. package/dist/RxForm/Tests/updateValues.test.d.ts +1 -0
  29. package/dist/RxForm/index.d.ts +1 -1
  30. package/dist/Testing/Models/initialState.d.ts +667 -0
  31. package/dist/Testing/Validators.d.ts +10 -0
  32. package/dist/Testing/asyncConfig.d.ts +0 -8
  33. package/dist/Testing/config.d.ts +0 -8
  34. package/dist/Validators/Validators.d.ts +0 -1
  35. package/dist/index.js +163 -89
  36. package/package.json +2 -2
  37. package/dist/Helpers/FormBuilder.d.ts +0 -10
  38. /package/dist/RxForm/{RxForm.test.d.ts → Tests/addControl.test.d.ts} +0 -0
package/dist/index.js CHANGED
@@ -39,18 +39,18 @@ var required = function (value) {
39
39
  };
40
40
  var email = function (value) {
41
41
  return value && !/^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/g.test(value) ? { email: true } : { email: false };
42
- };
43
- var phoneNumber = function (value) {
44
- return value && !/^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$/.test(value)
45
- ? { phoneNumber: true }
46
- : { phoneNumber: false };
47
42
  };
48
43
 
44
+ var Validators = /*#__PURE__*/Object.freeze({
45
+ __proto__: null,
46
+ required: required,
47
+ email: email
48
+ });
49
+
49
50
  var index = /*#__PURE__*/Object.freeze({
50
51
  __proto__: null,
51
52
  required: required,
52
- email: email,
53
- phoneNumber: phoneNumber
53
+ email: email
54
54
  });
55
55
 
56
56
  /******************************************************************************
@@ -144,12 +144,18 @@ var generateKey = function (length) {
144
144
  return result;
145
145
  };
146
146
 
147
- var getErrors = function (control, value) {
148
- var _a;
149
- return ((_a = control.config.validators) === null || _a === void 0 ? void 0 : _a.reduce(function (acc, validator) { return (__assign(__assign({}, acc), validator(value))); }, {})) || {};
147
+ var getErrors = function (control, value, _a) {
148
+ var _b;
149
+ var validators = _a.validators;
150
+ return ((_b = control.config.validators) === null || _b === void 0 ? void 0 : _b.reduce(function (acc, validator) {
151
+ if (!validators[validator]) {
152
+ throw "You have not provided a validator for \"".concat(validator, "\"");
153
+ }
154
+ return __assign(__assign({}, acc), validators[validator](value));
155
+ }, {})) || {};
150
156
  };
151
157
 
152
- var buildState = function (config, form, controlRef) {
158
+ var buildState = function (config, form, controlRef, providers) {
153
159
  var _a;
154
160
  if (form === void 0) { form = { root: null }; }
155
161
  if (controlRef === void 0) { controlRef = []; }
@@ -164,40 +170,45 @@ var buildState = function (config, form, controlRef) {
164
170
  config: config,
165
171
  key: generateKey(5)
166
172
  };
167
- var newForm = __assign(__assign({}, form), (_a = {}, _a[getFormKey(controlRef)] = __assign(__assign({}, control), { validatorErrors: getErrors(control, value) }), _a));
173
+ var newForm = __assign(__assign({}, form), (_a = {}, _a[getFormKey(controlRef)] = __assign(__assign({}, control), { validatorErrors: getErrors(control, value, providers) }), _a));
168
174
  var controls = config.controls;
169
175
  // Adding controls for Form Group
170
176
  if (controls && !(controls instanceof Array)) {
171
177
  newForm = Object.entries(config.controls).reduce(function (acc, _a) {
172
178
  var key = _a[0], controlConfig = _a[1];
173
- return buildState(controlConfig, acc, controlRef.concat(key));
179
+ return buildState(controlConfig, acc, controlRef.concat(key), providers);
174
180
  }, newForm);
175
181
  }
176
182
  else if (controls && controls instanceof Array) {
177
183
  // Adding controls for Form Array
178
- newForm = config.controls.reduce(function (acc, controlConfig, index) { return buildState(controlConfig, acc, controlRef.concat(index)); }, newForm);
184
+ newForm = config.controls.reduce(function (acc, controlConfig, index) {
185
+ return buildState(controlConfig, acc, controlRef.concat(index), providers);
186
+ }, newForm);
179
187
  }
180
188
  return newForm;
181
189
  };
182
- var buildFormState = function (config, form, controlRef) {
190
+ var buildFormState = function (config, form, controlRef, providers) {
183
191
  if (form === void 0) { form = { root: null }; }
184
192
  if (controlRef === void 0) { controlRef = []; }
185
193
  return {
186
- form: buildState(config, form, controlRef)
194
+ form: buildState(config, form, controlRef, providers)
187
195
  };
188
196
  };
189
197
 
190
- var getScopedEffectsForControl = function (formControl) {
198
+ var getScopedEffectsForControl = function (formControl, providers) {
191
199
  var config = formControl.config, key = formControl.key;
192
200
  var asyncValidators = config.asyncValidators;
193
201
  var scopedEffects = [];
194
202
  if (asyncValidators && asyncValidators.length) {
195
- scopedEffects = asyncValidators.reduce(function (acc, validator, validatorIndex) {
203
+ scopedEffects = asyncValidators.reduce(function (acc, asyncValidator, validatorIndex) {
196
204
  var effect = function (actions$) {
205
+ if (!providers.asyncValidators[asyncValidator]) {
206
+ throw "You have not provided an asyncValidator for \"".concat(asyncValidator, "\"");
207
+ }
197
208
  return actions$.pipe(operators.map(function (_a) {
198
209
  var control = _a.payload;
199
210
  return control;
200
- }), validator, operators.map(function (errors) { return ({
211
+ }), providers.asyncValidators[asyncValidator], operators.map(function (errors) { return ({
201
212
  type: 'asyncValidationResponseSuccess',
202
213
  payload: {
203
214
  key: key,
@@ -213,16 +224,16 @@ var getScopedEffectsForControl = function (formControl) {
213
224
  };
214
225
  var getAsyncValidationActions = function (formControls) {
215
226
  return formControls.reduce(function (acc, control) {
216
- var effects = getScopedEffectsForControl(control);
217
- if (!effects.length)
227
+ var _a;
228
+ if (!((_a = control.config.asyncValidators) === null || _a === void 0 ? void 0 : _a.length))
218
229
  return acc;
219
230
  var action = { type: 'asyncValidation', payload: control };
220
231
  return acc.concat(action);
221
232
  }, []);
222
233
  };
223
234
 
224
- var buildHub2Source = function (rx) {
225
- var hub1StateMapped$ = rx.state$.pipe(operators.map(function (payload) { return ({ type: 'formChange', payload: payload }); }));
235
+ var buildHub2Source = function (hub1State$) {
236
+ var hub1StateMapped$ = hub1State$.pipe(operators.map(function (payload) { return ({ type: 'formChange', payload: payload }); }));
226
237
  var sourceForHub2$ = hub1StateMapped$.pipe(operators.mergeMap(function (formChangeAction) {
227
238
  var changedControls = formChangeAction.payload.changedControls;
228
239
  var controlsToCheck = changedControls ? Object.values(changedControls) : [];
@@ -237,7 +248,7 @@ var getControl = function (controlRef, form) {
237
248
  };
238
249
 
239
250
  var UPDATE_ANCESTOR_VALUES = 'UPDATE_ANCESTOR_VALUES';
240
- var updateAncestorValues = function (form, _a) {
251
+ var updateAncestorValues = function (form, _a, providers) {
241
252
  var _b, _c;
242
253
  var _d = _a.payload, controlRef = _d.controlRef, value = _d.value;
243
254
  if (controlRef.length) {
@@ -256,11 +267,11 @@ var updateAncestorValues = function (form, _a) {
256
267
  // If parent is a Form Group
257
268
  newValue = __assign(__assign({}, parentControl.value), (_b = {}, _b[childKey_1] = value, _b));
258
269
  }
259
- var newParentControl = __assign(__assign({}, parentControl), { validatorErrors: getErrors(parentControl, newValue), value: newValue, dirty: !isEqual__default["default"](newValue, parentControl.pristineValue) });
270
+ var newParentControl = __assign(__assign({}, parentControl), { validatorErrors: getErrors(parentControl, newValue, providers), value: newValue, dirty: !isEqual__default["default"](newValue, parentControl.pristineValue) });
260
271
  return updateAncestorValues(__assign(__assign({}, form), (_c = {}, _c[parentFormKey] = newParentControl, _c)), {
261
272
  type: UPDATE_ANCESTOR_VALUES,
262
273
  payload: { controlRef: parentRef, value: newValue }
263
- });
274
+ }, providers);
264
275
  }
265
276
  return form;
266
277
  };
@@ -321,7 +332,7 @@ var getAncestorControls = function (controlRef, form, excludeSelf) {
321
332
  };
322
333
 
323
334
  var UPDATE_DESCENDANT_VALUES = 'UPDATE_DESCENDANT_VALUES';
324
- var updateDescendants = function (state, _a) {
335
+ var updateDescendants = function (state, _a, providers) {
325
336
  var _b = _a.payload, controlRef = _b.controlRef, value = _b.value;
326
337
  var descendants = getDescendantControls(controlRef, state.form, true).map(function (control) { return [getFormKey(control.controlRef), control]; });
327
338
  var result = descendants.reduce(function (acc, _a) {
@@ -329,7 +340,7 @@ var updateDescendants = function (state, _a) {
329
340
  var key = _a[0], control = _a[1];
330
341
  if (isChildRef(control.controlRef, controlRef)) {
331
342
  var childValue = value[control.controlRef.at(-1)];
332
- var validatorErrors = getErrors(control, value);
343
+ var validatorErrors = getErrors(control, value, providers);
333
344
  var newControl = __assign(__assign({}, control), { value: childValue, validatorErrors: validatorErrors, dirty: !isEqual__default["default"](childValue, control.pristineValue) });
334
345
  acc = {
335
346
  form: __assign(__assign({}, acc.form), (_b = {}, _b[key] = newControl, _b)),
@@ -340,7 +351,7 @@ var updateDescendants = function (state, _a) {
340
351
  acc = updateDescendants(acc, {
341
352
  type: UPDATE_DESCENDANT_VALUES,
342
353
  payload: { controlRef: control.controlRef, value: childValue }
343
- });
354
+ }, providers);
344
355
  }
345
356
  }
346
357
  return acc;
@@ -351,37 +362,48 @@ var updateDescendants = function (state, _a) {
351
362
  };
352
363
  };
353
364
  // Will only update child controls that are present.
354
- // Use AddControl/RemoveControl action reducers to add/remove control
355
- var updateValues = function (_a, action, mergeChanges) {
365
+ // Use AddControlPayload/RemoveControl action reducers to add/remove control
366
+ var updateValues = function (_a, action, providers, mergeChanges) {
356
367
  var _b, _c;
357
368
  var form = _a.form, _d = _a.changedControls, changedControls = _d === void 0 ? {} : _d, _e = _a.removedControls, removedControls = _e === void 0 ? {} : _e;
358
369
  if (mergeChanges === void 0) { mergeChanges = false; }
370
+ var normalizers = providers.normalizers;
359
371
  var _f = action.payload, controlRef = _f.controlRef, value = _f.value;
360
372
  // Update its own value
361
373
  var ctrlKey = getFormKey(controlRef);
362
- var validatorErrors = getErrors(form[ctrlKey], value);
363
- var newControl = __assign(__assign({}, form[ctrlKey]), { validatorErrors: validatorErrors, dirty: !isEqual__default["default"](value, form[ctrlKey].pristineValue), value: value });
374
+ var newValue = value;
375
+ var config = form[ctrlKey].config;
376
+ if (config.normalizers) {
377
+ newValue = config.normalizers.reduce(function (acc, normalizer) {
378
+ if (!normalizers[normalizer]) {
379
+ throw "You have not provided a normalizer for \"".concat(normalizer, "\"");
380
+ }
381
+ return normalizers[normalizer](acc);
382
+ }, value);
383
+ }
384
+ var validatorErrors = getErrors(form[ctrlKey], newValue, providers);
385
+ var newControl = __assign(__assign({}, form[ctrlKey]), { validatorErrors: validatorErrors, dirty: !isEqual__default["default"](value, form[ctrlKey].pristineValue), value: newValue });
364
386
  var result = {
365
387
  form: __assign(__assign({}, form), (_b = {}, _b[ctrlKey] = newControl, _b)),
366
388
  changedControls: (_c = {}, _c[newControl.key] = newControl, _c)
367
389
  };
368
- var configControls = form[ctrlKey].config.controls;
390
+ var configControls = config.controls;
369
391
  // Update its children
370
392
  if (configControls) {
371
393
  result = updateDescendants(result, {
372
394
  type: UPDATE_DESCENDANT_VALUES,
373
395
  payload: {
374
396
  controlRef: controlRef,
375
- value: value
397
+ value: newValue
376
398
  }
377
- });
399
+ }, providers);
378
400
  }
379
401
  // Update its Ancestors
380
402
  if (controlRef.length) {
381
403
  result = __assign(__assign({}, result), { form: updateAncestorValues(result.form, {
382
404
  type: UPDATE_ANCESTOR_VALUES,
383
- payload: { controlRef: controlRef, value: value }
384
- }) });
405
+ payload: { controlRef: controlRef, value: newValue }
406
+ }, providers) });
385
407
  }
386
408
  var changedAncestorControls = getAncestorControls(controlRef, result.form).reduce(function (acc, control) {
387
409
  var _a;
@@ -392,7 +414,7 @@ var updateValues = function (_a, action, mergeChanges) {
392
414
  };
393
415
 
394
416
  var UPDATE_ANCESTOR_VALUES_REMOVE_CONTROL = 'UPDATE_ANCESTOR_VALUES_REMOVE_CONTROL';
395
- var updateAncestorValuesRemoveControl = function (form, _a) {
417
+ var updateAncestorValuesRemoveControl = function (form, _a, providers) {
396
418
  var _b;
397
419
  var controlRef = _a.payload;
398
420
  if (controlRef.length) {
@@ -410,16 +432,16 @@ var updateAncestorValuesRemoveControl = function (form, _a) {
410
432
  newValue = __assign({}, parentControl.value);
411
433
  delete newValue[childKey_1];
412
434
  }
413
- var newParentControl = __assign(__assign({}, parentControl), { value: newValue, validatorErrors: getErrors(parentControl, newValue), dirty: !isEqual__default["default"](newValue, parentControl.pristineValue) });
435
+ var newParentControl = __assign(__assign({}, parentControl), { value: newValue, validatorErrors: getErrors(parentControl, newValue, providers), dirty: !isEqual__default["default"](newValue, parentControl.pristineValue) });
414
436
  return updateAncestorValues(__assign(__assign({}, form), (_b = {}, _b[parentFormKey] = newParentControl, _b)), {
415
437
  type: UPDATE_ANCESTOR_VALUES,
416
438
  payload: { controlRef: parentRef, value: newValue }
417
- });
439
+ }, providers);
418
440
  }
419
441
  return form;
420
442
  };
421
443
 
422
- var removeControl = function (state, action, mergeChanges) {
444
+ var removeControl = function (state, action, providers, mergeChanges) {
423
445
  var _a;
424
446
  if (mergeChanges === void 0) { mergeChanges = false; }
425
447
  var form = state.form;
@@ -468,7 +490,7 @@ var removeControl = function (state, action, mergeChanges) {
468
490
  var result = updateAncestorValuesRemoveControl(controlRemoved, {
469
491
  type: UPDATE_ANCESTOR_VALUES_REMOVE_CONTROL,
470
492
  payload: controlRef
471
- });
493
+ }, providers);
472
494
  var changedControls = __assign(__assign({}, (mergeChanges ? state.changedControls || {} : undefined)), getAncestorControls(controlRef.slice(0, -1), result).reduce(function (acc, control) {
473
495
  var _a;
474
496
  return (__assign(__assign({}, acc), (_a = {}, _a[control.key] = control, _a)));
@@ -513,7 +535,7 @@ var removeControl = function (state, action, mergeChanges) {
513
535
  };
514
536
 
515
537
  var UPDATE_ANCESTOR_VALUES_ADD_CONTROL = 'UPDATE_ANCESTOR_VALUES_ADD_CONTROL';
516
- var updateAncestorValuesAddControl = function (form, _a) {
538
+ var updateAncestorValuesAddControl = function (form, _a, providers) {
517
539
  var _b, _c;
518
540
  var _d = _a.payload, controlRef = _d.controlRef, value = _d.value;
519
541
  if (controlRef.length) {
@@ -530,11 +552,11 @@ var updateAncestorValuesAddControl = function (form, _a) {
530
552
  // If parent is a Form Group
531
553
  newValue = __assign(__assign({}, form[parentFormKey].value), (_b = {}, _b[childKey] = value, _b));
532
554
  }
533
- var newParentControl = __assign(__assign({}, parentControl), { value: newValue, validatorErrors: getErrors(parentControl, newValue), dirty: !isEqual__default["default"](newValue, parentControl.pristineValue) });
555
+ var newParentControl = __assign(__assign({}, parentControl), { value: newValue, validatorErrors: getErrors(parentControl, newValue, providers), dirty: !isEqual__default["default"](newValue, parentControl.pristineValue) });
534
556
  return updateAncestorValues(__assign(__assign({}, form), (_c = {}, _c[parentFormKey] = newParentControl, _c)), {
535
557
  type: UPDATE_ANCESTOR_VALUES,
536
558
  payload: { controlRef: parentRef, value: newValue }
537
- });
559
+ }, providers);
538
560
  }
539
561
  return form;
540
562
  };
@@ -545,19 +567,19 @@ var getControlBranch = function (controlRef, form) {
545
567
  return ancestors.concat(childControls).sort(function (a, b) { return a.controlRef.length - b.controlRef.length; });
546
568
  };
547
569
 
548
- var addControl = function (state, action, mergeChanges) {
570
+ var addControl = function (state, action, providers, mergeChanges) {
549
571
  if (mergeChanges === void 0) { mergeChanges = false; }
550
572
  var _a = action.payload, config = _a.config, controlRef = _a.controlRef;
551
573
  // If controlRef does not exist we are adding control to a Form Group
552
574
  if (!getControl(controlRef.slice(0, -1), state.form)) {
553
575
  throw 'You are attempting to add a control to a non-existent form group';
554
576
  }
555
- var newForm = buildState(config, state.form, controlRef);
577
+ var newForm = buildState(config, state.form, controlRef, providers);
556
578
  var newValue = getControl(controlRef, newForm).value;
557
579
  var ancestorsUpdated = updateAncestorValuesAddControl(newForm, {
558
580
  type: UPDATE_ANCESTOR_VALUES_ADD_CONTROL,
559
581
  payload: { controlRef: controlRef, value: newValue }
560
- });
582
+ }, providers);
561
583
  var changedControls = getControlBranch(controlRef, ancestorsUpdated).reduce(function (acc, control) {
562
584
  var _a;
563
585
  return (__assign(__assign({}, acc), (_a = {}, _a[control.key] = control, _a)));
@@ -569,7 +591,7 @@ var addControl = function (state, action, mergeChanges) {
569
591
  };
570
592
  };
571
593
 
572
- var pushControl = function (state, action, mergeChanges) {
594
+ var pushControl = function (state, action, providers, mergeChanges) {
573
595
  if (mergeChanges === void 0) { mergeChanges = false; }
574
596
  var newControlRef;
575
597
  var _a = action.payload, config = _a.config, controlRef = _a.controlRef;
@@ -583,12 +605,12 @@ var pushControl = function (state, action, mergeChanges) {
583
605
  else {
584
606
  throw 'You are attempting to push to a control that is not a Form Array';
585
607
  }
586
- var newForm = buildState(config, state.form, newControlRef);
608
+ var newForm = buildState(config, state.form, newControlRef, providers);
587
609
  var newValue = getControl(newControlRef, newForm).value;
588
610
  var ancestorsUpdated = updateAncestorValuesAddControl(newForm, {
589
611
  type: UPDATE_ANCESTOR_VALUES_ADD_CONTROL,
590
612
  payload: { controlRef: newControlRef, value: newValue }
591
- });
613
+ }, providers);
592
614
  var changedControls = getControlBranch(newControlRef, ancestorsUpdated).reduce(function (acc, control) {
593
615
  var _a;
594
616
  return (__assign(__assign({}, acc), (_a = {}, _a[control.key] = control, _a)));
@@ -714,7 +736,7 @@ var markControlAsUntouched = function (state, action, mergeChanges) {
714
736
  };
715
737
  };
716
738
 
717
- var resetControl = function (state, action, mergeChanges) {
739
+ var resetControl = function (state, action, providers, mergeChanges) {
718
740
  var _a;
719
741
  if (mergeChanges === void 0) { mergeChanges = false; }
720
742
  var form = state.form;
@@ -730,12 +752,12 @@ var resetControl = function (state, action, mergeChanges) {
730
752
  delete descendantsRemoved[key];
731
753
  });
732
754
  // Remove all descendants
733
- var restoredControls = buildState(controlToReset.config, descendantsRemoved, controlToReset.controlRef);
755
+ var restoredControls = buildState(controlToReset.config, descendantsRemoved, controlToReset.controlRef, providers);
734
756
  var restoredControlValue = getControl(controlRef, restoredControls).value;
735
757
  var result = updateAncestorValues(restoredControls, {
736
758
  type: UPDATE_ANCESTOR_VALUES,
737
759
  payload: { controlRef: controlRef, value: restoredControlValue }
738
- });
760
+ }, providers);
739
761
  var changedControls = __assign(__assign({}, (mergeChanges ? state.changedControls || {} : undefined)), getControlBranch(controlRef, result).reduce(function (acc, control) {
740
762
  var _a;
741
763
  return (__assign(__assign({}, acc), (_a = {}, _a[control.key] = control, _a)));
@@ -988,64 +1010,115 @@ var control = function (config) {
988
1010
  };
989
1011
  var array = function (config) { return config; };
990
1012
  var group = function (config) { return config; };
991
- var reducerTools = {
992
- updateValues: function (state, action) {
993
- return updateValues(state, action, true);
1013
+ var reducerTools = function (providers) { return ({
1014
+ updateValues: function (state, payload) {
1015
+ return updateValues(state, { payload: payload }, providers, true);
994
1016
  },
995
- removeControl: function (state, action) {
996
- return removeControl(state, action, true);
1017
+ removeControl: function (state, payload) {
1018
+ return removeControl(state, { payload: payload }, providers, true);
997
1019
  },
998
- pushControl: function (state, action) {
999
- return pushControl(state, action, true);
1020
+ pushControl: function (state, payload) {
1021
+ return pushControl(state, { payload: payload }, providers, true);
1000
1022
  },
1001
- addControl: function (state, action) {
1002
- return addControl(state, action, true);
1023
+ addControl: function (state, payload) {
1024
+ return addControl(state, { payload: payload }, providers, true);
1003
1025
  },
1004
- markControlAsPristine: function (state, action) {
1005
- return markControlAsPristine(state, action, true);
1026
+ resetControl: function (state, payload) {
1027
+ return resetControl(state, { payload: payload }, providers, true);
1006
1028
  },
1007
- markControlAsTouched: function (state, action) {
1008
- return markControlAsTouched(state, action, true);
1029
+ markControlAsPristine: function (state, payload) {
1030
+ return markControlAsPristine(state, { payload: payload }, true);
1009
1031
  },
1010
- markControlAsUntouched: function (state, action) {
1011
- return markControlAsUntouched(state, action, true);
1032
+ markControlAsTouched: function (state, payload) {
1033
+ return markControlAsTouched(state, { payload: payload }, true);
1012
1034
  },
1013
- resetControl: function (state, action) {
1014
- return resetControl(state, action, true);
1035
+ markControlAsUntouched: function (state, payload) {
1036
+ return markControlAsUntouched(state, { payload: payload }, true);
1015
1037
  }
1016
- };
1038
+ }); };
1017
1039
  var build = function (config, options) {
1040
+ var _a, _b, _c;
1018
1041
  if (options === void 0) { options = {}; }
1019
- var initialState = buildFormState(config);
1042
+ var providers = {
1043
+ normalizers: __assign({}, (_a = options.providers) === null || _a === void 0 ? void 0 : _a.normalizers),
1044
+ validators: __assign(__assign({}, Validators), (_b = options.providers) === null || _b === void 0 ? void 0 : _b.validators),
1045
+ asyncValidators: __assign({}, (_c = options.providers) === null || _c === void 0 ? void 0 : _c.asyncValidators)
1046
+ };
1047
+ var initialState = buildFormState(config, undefined, undefined, providers);
1048
+ return createReactable(initialState, options);
1049
+ };
1050
+ var load = function (state, options) {
1051
+ if (options === void 0) { options = {}; }
1052
+ var baseFormState = {
1053
+ form: Object.entries(state).reduce(function (acc, _a) {
1054
+ var _b;
1055
+ var key = _a[0], control = _a[1];
1056
+ return __assign(__assign({}, acc), (_b = {}, _b[key] = Object.entries(control)
1057
+ .filter(function (_a) {
1058
+ var key = _a[0];
1059
+ return !Object.keys(DEFAULT_HUB2_FIELDS).includes(key);
1060
+ })
1061
+ .reduce(function (acc, _a) {
1062
+ var _b;
1063
+ var key = _a[0], value = _a[1];
1064
+ return (__assign(__assign({}, acc), (_b = {}, _b[key] = value, _b)));
1065
+ }, {}), _b));
1066
+ }, {})
1067
+ };
1068
+ return createReactable(baseFormState, options);
1069
+ };
1070
+ var createReactable = function (initialBaseState, options, initialFormState) {
1071
+ var _a, _b, _c;
1072
+ if (options === void 0) { options = {}; }
1073
+ var providers = {
1074
+ normalizers: __assign({}, (_a = options.providers) === null || _a === void 0 ? void 0 : _a.normalizers),
1075
+ validators: __assign(__assign({}, Validators), (_b = options.providers) === null || _b === void 0 ? void 0 : _b.validators),
1076
+ asyncValidators: __assign({}, (_c = options.providers) === null || _c === void 0 ? void 0 : _c.asyncValidators)
1077
+ };
1020
1078
  var reducers = options.reducers, otherOptions = __rest(options, ["reducers"]);
1021
1079
  var customReducers = Object.entries(reducers || {}).reduce(function (acc, _a) {
1022
1080
  var _b;
1023
- var key = _a[0], reducer = _a[1];
1024
- return (__assign(__assign({}, acc), (_b = {}, _b[key] = function (_a, action) {
1025
- var form = _a.form;
1026
- return reducer(reducerTools, { form: form }, action);
1027
- }, _b)));
1081
+ var key = _a[0], _case = _a[1];
1082
+ var _reducer = typeof _case === 'function' ? _case : _case.reducer;
1083
+ var effects = typeof _case === 'function' ? [] : _case.effects;
1084
+ return __assign(__assign({}, acc), (_b = {}, _b[key] = {
1085
+ reducer: function (_a, action) {
1086
+ var form = _a.form;
1087
+ return _reducer(reducerTools(providers), { form: form }, action);
1088
+ },
1089
+ effects: effects
1090
+ }, _b));
1028
1091
  }, {});
1029
- var rxHub1 = core.RxBuilder(__assign({ initialState: initialState, reducers: __assign({ updateValues: updateValues, removeControl: removeControl, addControl: addControl, pushControl: pushControl, markControlAsPristine: markControlAsPristine, markControlAsTouched: markControlAsTouched, markControlAsUntouched: markControlAsUntouched, resetControl: resetControl }, customReducers) }, otherOptions));
1030
- var rxHub2 = core.RxBuilder({
1031
- sources: [buildHub2Source(rxHub1)],
1032
- initialState: null,
1092
+ var _d = core.RxBuilder(__assign({ initialState: initialBaseState, reducers: __assign({ updateValues: function (state, action, mergeChanges) {
1093
+ return updateValues(state, action, providers, mergeChanges);
1094
+ }, removeControl: function (state, action, mergeChanges) {
1095
+ return removeControl(state, action, providers, mergeChanges);
1096
+ }, addControl: function (state, action, mergeChanges) {
1097
+ return addControl(state, action, providers, mergeChanges);
1098
+ }, pushControl: function (state, action, mergeChanges) {
1099
+ return pushControl(state, action, providers, mergeChanges);
1100
+ }, resetControl: function (state, action, mergeChanges) {
1101
+ return resetControl(state, action, providers, mergeChanges);
1102
+ }, markControlAsPristine: markControlAsPristine, markControlAsTouched: markControlAsTouched, markControlAsUntouched: markControlAsUntouched }, customReducers) }, otherOptions)), hub1State$ = _d[0], hub1Actions = _d[1];
1103
+ var state$ = core.RxBuilder({
1104
+ sources: [buildHub2Source(hub1State$).pipe(operators.skip(initialFormState ? 1 : 0))],
1105
+ initialState: initialFormState || null,
1033
1106
  reducers: {
1034
1107
  formChange: formChange,
1035
1108
  asyncValidation: {
1036
1109
  reducer: asyncValidation,
1037
1110
  effects: function (control) { return ({
1038
1111
  key: control.key,
1039
- effects: getScopedEffectsForControl(control)
1112
+ effects: getScopedEffectsForControl(control, providers)
1040
1113
  }); }
1041
1114
  },
1042
1115
  asyncValidationResponseSuccess: asyncValidationResponseSuccess
1043
1116
  }
1044
- });
1045
- return {
1046
- state$: rxHub2.state$.pipe(operators.filter(function (form) { return form !== null; })),
1047
- actions: rxHub1.actions
1048
- };
1117
+ })[0];
1118
+ return [
1119
+ state$.pipe(operators.filter(function (form) { return form !== null; })),
1120
+ hub1Actions,
1121
+ ];
1049
1122
  };
1050
1123
 
1051
1124
  var getArrayItems = function (controlRef, form) {
@@ -1064,3 +1137,4 @@ exports.build = build;
1064
1137
  exports.control = control;
1065
1138
  exports.getArrayItems = getArrayItems;
1066
1139
  exports.group = group;
1140
+ exports.load = load;
package/package.json CHANGED
@@ -14,7 +14,7 @@
14
14
  "author": "David Lai",
15
15
  "license": "ISC",
16
16
  "dependencies": {
17
- "@reactables/core": "^0.6.0-alpha.0",
17
+ "@reactables/core": "^0.7.0-alpha.1",
18
18
  "lodash.isequal": "^4.5.0"
19
19
  },
20
20
  "peerDependencies": {
@@ -23,5 +23,5 @@
23
23
  "devDependencies": {
24
24
  "lodash.clonedeep": "^4.5.0"
25
25
  },
26
- "version": "0.6.0-alpha.0"
26
+ "version": "0.7.0-alpha.1"
27
27
  }
@@ -1,10 +0,0 @@
1
- import { FormControlConfig, FormArrayConfig, FormGroupConfig, AbstractControlConfig } from '../Models/Configs';
2
- import { ValidatorFn, ValidatorAsyncFn } from '../Models/Validators';
3
- type FbControl<T> = [T, (ValidatorFn | ValidatorFn[])?, (ValidatorAsyncFn | ValidatorAsyncFn[])?];
4
- export declare const FormBuilder: {
5
- control: <T>(config: FormControlConfig<T> | FbControl<T>) => FormControlConfig<T>;
6
- array: (config: FormArrayConfig) => FormArrayConfig;
7
- group: (config: FormGroupConfig) => FormGroupConfig;
8
- build: (config: AbstractControlConfig, hub?: any) => any;
9
- };
10
- export {};