@reactables/forms 1.3.0-beta.3 → 2.0.0-beta.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 (83) hide show
  1. package/dist/Helpers/addAsyncValidationEffects.d.ts +9 -6
  2. package/dist/Helpers/buildFormState.d.ts +6 -6
  3. package/dist/Helpers/buildHub2Source.d.ts +4 -4
  4. package/dist/Helpers/controlRefCheck.d.ts +5 -5
  5. package/dist/Helpers/controlRefCheck.test.d.ts +1 -1
  6. package/dist/Helpers/generateKey.d.ts +1 -1
  7. package/dist/Helpers/getAncestorControls.d.ts +3 -3
  8. package/dist/Helpers/getAncestorControls.test.d.ts +1 -1
  9. package/dist/Helpers/getArrayItems.d.ts +3 -3
  10. package/dist/Helpers/getArrayItems.test.d.ts +1 -1
  11. package/dist/Helpers/getControl.d.ts +3 -3
  12. package/dist/Helpers/getControlBranch.d.ts +3 -3
  13. package/dist/Helpers/getControlBranch.test.d.ts +1 -1
  14. package/dist/Helpers/getDescendantControls.d.ts +3 -3
  15. package/dist/Helpers/getDescendantControls.test.d.ts +1 -1
  16. package/dist/Helpers/getFormKey.d.ts +2 -2
  17. package/dist/Helpers/getValueFromControlConfig.d.ts +2 -2
  18. package/dist/Helpers/getValueFromControlConfig.test.d.ts +1 -1
  19. package/dist/Helpers/index.d.ts +4 -4
  20. package/dist/Helpers/isChildRef.d.ts +2 -2
  21. package/dist/Helpers/reverseObjectKeys.d.ts +2 -2
  22. package/dist/Models/Configs.d.ts +22 -22
  23. package/dist/Models/ControlRef.d.ts +1 -1
  24. package/dist/Models/Controls.d.ts +48 -48
  25. package/dist/Models/FormErrors.d.ts +3 -3
  26. package/dist/Models/Payloads.d.ts +24 -24
  27. package/dist/Models/Validators.d.ts +5 -5
  28. package/dist/Models/index.d.ts +6 -6
  29. package/dist/Reducers/Hub1/addControl.d.ts +5 -5
  30. package/dist/Reducers/Hub1/getErrors.d.ts +4 -4
  31. package/dist/Reducers/Hub1/index.d.ts +6 -7
  32. package/dist/Reducers/Hub1/markControlAsPristine.d.ts +4 -4
  33. package/dist/Reducers/Hub1/markControlAsTouched.d.ts +4 -4
  34. package/dist/Reducers/Hub1/markControlAsUntouched.d.ts +4 -4
  35. package/dist/Reducers/Hub1/pushControl.d.ts +5 -5
  36. package/dist/Reducers/Hub1/removeControl.d.ts +5 -5
  37. package/dist/Reducers/Hub1/resetControl.d.ts +5 -5
  38. package/dist/Reducers/Hub1/updateAncestorPristineValues.d.ts +5 -5
  39. package/dist/Reducers/Hub1/updateAncestorValues.d.ts +6 -6
  40. package/dist/Reducers/Hub1/updateAncestorValuesAddControl.d.ts +6 -6
  41. package/dist/Reducers/Hub1/updateAncestorValuesRemoveControl.d.ts +6 -6
  42. package/dist/Reducers/Hub1/updateValues.d.ts +5 -5
  43. package/dist/Reducers/Hub2/asyncValidation.d.ts +4 -4
  44. package/dist/Reducers/Hub2/asyncValidationResponse.d.ts +4 -0
  45. package/dist/Reducers/Hub2/formChange.d.ts +3 -3
  46. package/dist/Reducers/Hub2/index.d.ts +3 -3
  47. package/dist/Reducers/Hub2/mergeBranchErrors.d.ts +3 -3
  48. package/dist/Reducers/Hub2/mergeControls.d.ts +5 -5
  49. package/dist/Reducers/Hub2/mergeErrors.d.ts +2 -2
  50. package/dist/Reducers/Hub2/mergeRemoveControl.d.ts +6 -6
  51. package/dist/Reducers/index.d.ts +2 -2
  52. package/dist/RxForm/RxForm.d.ts +103 -99
  53. package/dist/RxForm/Tests/addControl.test.d.ts +1 -1
  54. package/dist/RxForm/Tests/combinedTest.test.d.ts +1 -1
  55. package/dist/RxForm/Tests/customReducers.test.d.ts +1 -1
  56. package/dist/RxForm/Tests/initialization.test.d.ts +1 -1
  57. package/dist/RxForm/Tests/load.test.d.ts +1 -1
  58. package/dist/RxForm/Tests/markControlAsPristine.test.d.ts +1 -1
  59. package/dist/RxForm/Tests/markControlTouchStatus.test.d.ts +1 -1
  60. package/dist/RxForm/Tests/pushControl.test.d.ts +1 -1
  61. package/dist/RxForm/Tests/removeControl.test.d.ts +1 -1
  62. package/dist/RxForm/Tests/resetControl.test.d.ts +1 -1
  63. package/dist/RxForm/Tests/updateValues.test.d.ts +1 -1
  64. package/dist/RxForm/index.d.ts +1 -1
  65. package/dist/Testing/AsyncValidators.d.ts +9 -8
  66. package/dist/Testing/Models/Contact.d.ts +11 -11
  67. package/dist/Testing/Models/DoctorInfo.d.ts +6 -6
  68. package/dist/Testing/Models/EmergencyContact.d.ts +6 -6
  69. package/dist/Testing/Models/initialState.d.ts +667 -667
  70. package/dist/Testing/Validators.d.ts +10 -10
  71. package/dist/Testing/asyncConfig.d.ts +3 -3
  72. package/dist/Testing/config.d.ts +3 -3
  73. package/dist/Validators/Validators.d.ts +4 -4
  74. package/dist/Validators/index.d.ts +1 -1
  75. package/dist/index.cjs +1213 -0
  76. package/dist/index.cjs.map +1 -0
  77. package/dist/index.d.ts +4 -4
  78. package/dist/index.js +1064 -1075
  79. package/dist/index.js.map +1 -0
  80. package/package.json +10 -3
  81. package/dist/Reducers/Hub2/asyncValidationResponseSuccess.d.ts +0 -4
  82. package/dist/Reducers/Hub2/hub2Reducer.d.ts +0 -3
  83. package/dist/Testing/Effects.d.ts +0 -10
package/dist/index.js CHANGED
@@ -1,61 +1,53 @@
1
- 'use strict';
1
+ import { RxBuilder } from '@reactables/core';
2
+ import { map, switchMap, catchError, startWith, pairwise, mergeMap, skip, filter } from 'rxjs/operators';
3
+ import { concat, of } from 'rxjs';
4
+ import isEqual from 'lodash.isequal';
2
5
 
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var core = require('@reactables/core');
6
- var operators = require('rxjs/operators');
7
- var rxjs = require('rxjs');
8
- var isEqual = require('lodash.isequal');
9
-
10
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
11
-
12
- var isEqual__default = /*#__PURE__*/_interopDefaultLegacy(isEqual);
13
-
14
- var DEFAULT_HUB2_FIELDS = {
15
- asyncValidatorErrors: {},
16
- asyncValidateInProgress: {},
17
- pending: false,
18
- valid: null,
19
- childrenValid: null,
20
- errors: null
6
+ var DEFAULT_HUB2_FIELDS = {
7
+ asyncValidatorErrors: {},
8
+ asyncValidateInProgress: {},
9
+ pending: false,
10
+ valid: null,
11
+ childrenValid: null,
12
+ errors: null
21
13
  };
22
14
 
23
15
  var Controls = /*#__PURE__*/Object.freeze({
24
- __proto__: null,
25
- DEFAULT_HUB2_FIELDS: DEFAULT_HUB2_FIELDS
16
+ __proto__: null,
17
+ DEFAULT_HUB2_FIELDS: DEFAULT_HUB2_FIELDS
26
18
  });
27
19
 
28
- var required = function (value) {
29
- if (typeof value === 'string' || typeof value === 'object') {
30
- return { required: !Boolean(value) };
31
- }
32
- if (typeof value === 'number') {
33
- return { required: !(value !== undefined && value !== null) };
34
- }
35
- if (typeof value === 'boolean') {
36
- return { required: !value };
37
- }
38
- return { required: false };
39
- };
40
- var arrayNotEmpty = function (value) { return ({
41
- arrayNotEmpty: !Boolean(value.length)
42
- }); };
43
- var email = function (value) {
44
- return value && !/^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/g.test(value) ? { email: true } : { email: false };
20
+ var required = function (value) {
21
+ if (typeof value === 'string' || typeof value === 'object') {
22
+ return { required: !Boolean(value) };
23
+ }
24
+ if (typeof value === 'number') {
25
+ return { required: !(value !== undefined && value !== null) };
26
+ }
27
+ if (typeof value === 'boolean') {
28
+ return { required: !value };
29
+ }
30
+ return { required: false };
31
+ };
32
+ var arrayNotEmpty = function (value) { return ({
33
+ arrayNotEmpty: !Boolean(value.length)
34
+ }); };
35
+ var email = function (value) {
36
+ return value && !/^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/g.test(value) ? { email: true } : { email: false };
45
37
  };
46
38
 
47
39
  var Validators = /*#__PURE__*/Object.freeze({
48
- __proto__: null,
49
- required: required,
50
- arrayNotEmpty: arrayNotEmpty,
51
- email: email
40
+ __proto__: null,
41
+ arrayNotEmpty: arrayNotEmpty,
42
+ email: email,
43
+ required: required
52
44
  });
53
45
 
54
46
  var index = /*#__PURE__*/Object.freeze({
55
- __proto__: null,
56
- required: required,
57
- arrayNotEmpty: arrayNotEmpty,
58
- email: email
47
+ __proto__: null,
48
+ arrayNotEmpty: arrayNotEmpty,
49
+ email: email,
50
+ required: required
59
51
  });
60
52
 
61
53
  /******************************************************************************
@@ -72,6 +64,8 @@ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
72
64
  OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
73
65
  PERFORMANCE OF THIS SOFTWARE.
74
66
  ***************************************************************************** */
67
+ /* global Reflect, Promise, SuppressedError, Symbol, Iterator */
68
+
75
69
 
76
70
  var __assign = function() {
77
71
  __assign = Object.assign || function __assign(t) {
@@ -111,1102 +105,1097 @@ typeof SuppressedError === "function" ? SuppressedError : function (error, suppr
111
105
  return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
112
106
  };
113
107
 
114
- var getValueFromControlConfig = function (controlConfig) {
115
- var controls = controlConfig.controls;
116
- if (controls && !(controls instanceof Array)) {
117
- var result = {};
118
- for (var key in controlConfig.controls) {
119
- var control = controlConfig.controls[key];
120
- result[key] = getValueFromControlConfig(control);
121
- }
122
- return result;
123
- }
124
- else if (controls && controls instanceof Array) {
125
- var configs = controlConfig.controls;
126
- var result = configs
127
- ? configs.map(function (controlConfig) { return getValueFromControlConfig(controlConfig); })
128
- : [];
129
- return result;
130
- }
131
- else {
132
- return controlConfig.initialValue;
133
- }
108
+ var getValueFromControlConfig = function (controlConfig) {
109
+ var controls = controlConfig.controls;
110
+ if (controls && !(controls instanceof Array)) {
111
+ var result = {};
112
+ for (var key in controlConfig.controls) {
113
+ var control = controlConfig.controls[key];
114
+ result[key] = getValueFromControlConfig(control);
115
+ }
116
+ return result;
117
+ }
118
+ else if (controls && controls instanceof Array) {
119
+ var configs = controlConfig.controls;
120
+ var result = configs
121
+ ? configs.map(function (controlConfig) { return getValueFromControlConfig(controlConfig); })
122
+ : [];
123
+ return result;
124
+ }
125
+ else {
126
+ return controlConfig.initialValue;
127
+ }
134
128
  };
135
129
 
136
- var getFormKey = function (controlRef) {
137
- return controlRef.length ? controlRef.join('.') : 'root';
130
+ var getFormKey = function (controlRef) {
131
+ return controlRef.length ? controlRef.join('.') : 'root';
138
132
  };
139
133
 
140
- var generateKey = function (length) {
141
- var result = '';
142
- var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
143
- var charactersLength = characters.length;
144
- var counter = 0;
145
- while (counter < length) {
146
- result += characters.charAt(Math.floor(Math.random() * charactersLength));
147
- counter += 1;
148
- }
149
- return result;
134
+ var generateKey = function (length) {
135
+ var result = '';
136
+ var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
137
+ var charactersLength = characters.length;
138
+ var counter = 0;
139
+ while (counter < length) {
140
+ result += characters.charAt(Math.floor(Math.random() * charactersLength));
141
+ counter += 1;
142
+ }
143
+ return result;
150
144
  };
151
145
 
152
- var getErrors = function (control, value, _a) {
153
- var _b;
154
- var validators = _a.validators;
155
- return ((_b = control.config.validators) === null || _b === void 0 ? void 0 : _b.reduce(function (acc, validator) {
156
- if (!validators[validator]) {
157
- throw "You have not provided a validator for \"".concat(validator, "\"");
158
- }
159
- return __assign(__assign({}, acc), validators[validator](value));
160
- }, {})) || {};
146
+ var getErrors = function (control, value, _a) {
147
+ var _b;
148
+ var validators = _a.validators;
149
+ return ((_b = control.config.validators) === null || _b === void 0 ? void 0 : _b.reduce(function (acc, validator) {
150
+ if (!validators[validator]) {
151
+ throw "You have not provided a validator for \"".concat(validator, "\"");
152
+ }
153
+ return __assign(__assign({}, acc), validators[validator](value));
154
+ }, {})) || {};
161
155
  };
162
156
 
163
- var buildState = function (config, form, controlRef, providers) {
164
- var _a;
165
- if (form === void 0) { form = { root: null }; }
166
- if (controlRef === void 0) { controlRef = []; }
167
- var value = getValueFromControlConfig(config);
168
- var control = {
169
- pristineValue: value,
170
- dirty: false,
171
- touched: false,
172
- value: value,
173
- controlRef: controlRef,
174
- validatorErrors: {},
175
- config: config,
176
- key: generateKey(5)
177
- };
178
- var newForm = __assign(__assign({}, form), (_a = {}, _a[getFormKey(controlRef)] = __assign(__assign({}, control), { validatorErrors: getErrors(control, value, providers) }), _a));
179
- var controls = config.controls;
180
- // Adding controls for Form Group
181
- if (controls && !(controls instanceof Array)) {
182
- newForm = Object.entries(config.controls).reduce(function (acc, _a) {
183
- var key = _a[0], controlConfig = _a[1];
184
- return buildState(controlConfig, acc, controlRef.concat(key), providers);
185
- }, newForm);
186
- }
187
- else if (controls && controls instanceof Array) {
188
- // Adding controls for Form Array
189
- newForm = config.controls.reduce(function (acc, controlConfig, index) {
190
- return buildState(controlConfig, acc, controlRef.concat(index), providers);
191
- }, newForm);
192
- }
193
- return newForm;
194
- };
195
- var buildFormState = function (config, form, controlRef, providers) {
196
- if (form === void 0) { form = { root: null }; }
197
- if (controlRef === void 0) { controlRef = []; }
198
- return {
199
- form: buildState(config, form, controlRef, providers)
200
- };
157
+ var buildState = function (config, form, controlRef, providers) {
158
+ var _a;
159
+ if (form === void 0) { form = { root: null }; }
160
+ if (controlRef === void 0) { controlRef = []; }
161
+ var value = getValueFromControlConfig(config);
162
+ var control = {
163
+ pristineValue: value,
164
+ dirty: false,
165
+ touched: false,
166
+ value: value,
167
+ controlRef: controlRef,
168
+ validatorErrors: {},
169
+ config: config,
170
+ key: generateKey(5)
171
+ };
172
+ var newForm = __assign(__assign({}, form), (_a = {}, _a[getFormKey(controlRef)] = __assign(__assign({}, control), { validatorErrors: getErrors(control, value, providers) }), _a));
173
+ var controls = config.controls;
174
+ // Adding controls for Form Group
175
+ if (controls && !(controls instanceof Array)) {
176
+ newForm = Object.entries(config.controls).reduce(function (acc, _a) {
177
+ var key = _a[0], controlConfig = _a[1];
178
+ return buildState(controlConfig, acc, controlRef.concat(key), providers);
179
+ }, newForm);
180
+ }
181
+ else if (controls && controls instanceof Array) {
182
+ // Adding controls for Form Array
183
+ newForm = config.controls.reduce(function (acc, controlConfig, index) {
184
+ return buildState(controlConfig, acc, controlRef.concat(index), providers);
185
+ }, newForm);
186
+ }
187
+ return newForm;
188
+ };
189
+ var buildFormState = function (config, form, controlRef, providers) {
190
+ if (form === void 0) { form = { root: null }; }
191
+ if (controlRef === void 0) { controlRef = []; }
192
+ return {
193
+ form: buildState(config, form, controlRef, providers)
194
+ };
201
195
  };
202
196
 
203
- var getScopedEffectsForControl = function (formControl, providers) {
204
- var config = formControl.config, key = formControl.key;
205
- var asyncValidators = config.asyncValidators;
206
- var scopedEffects = [];
207
- if (asyncValidators && asyncValidators.length) {
208
- scopedEffects = asyncValidators.reduce(function (acc, asyncValidator, validatorIndex) {
209
- var effect = function (actions$) {
210
- if (!providers.asyncValidators[asyncValidator]) {
211
- throw "You have not provided an asyncValidator for \"".concat(asyncValidator, "\"");
212
- }
213
- return actions$.pipe(operators.map(function (_a) {
214
- var control = _a.payload;
215
- return control;
216
- }), providers.asyncValidators[asyncValidator], operators.map(function (errors) { return ({
217
- type: 'asyncValidationResponseSuccess',
218
- payload: {
219
- key: key,
220
- errors: errors,
221
- validatorIndex: validatorIndex
222
- }
223
- }); }));
224
- };
225
- return acc.concat(effect);
226
- }, []);
227
- }
228
- return scopedEffects;
229
- };
230
- var getAsyncValidationActions = function (formControls) {
231
- return formControls.reduce(function (acc, control) {
232
- var _a;
233
- if (!((_a = control.config.asyncValidators) === null || _a === void 0 ? void 0 : _a.length))
234
- return acc;
235
- var action = { type: 'asyncValidation', payload: control };
236
- return acc.concat(action);
237
- }, []);
197
+ var getScopedEffectsForControl = function (formControl, providers) {
198
+ var config = formControl.config, key = formControl.key, controlRef = formControl.controlRef;
199
+ var asyncValidators = config.asyncValidators;
200
+ var scopedEffects = [];
201
+ if (asyncValidators && asyncValidators.length) {
202
+ scopedEffects = asyncValidators.reduce(function (acc, asyncValidator, validatorIndex) {
203
+ var effect = function (actions$) {
204
+ if (!providers.asyncValidators[asyncValidator]) {
205
+ throw "You have not provided an asyncValidator for \"".concat(asyncValidator, "\"");
206
+ }
207
+ return actions$.pipe(map(function (_a) {
208
+ var control = _a.payload;
209
+ return control;
210
+ }), providers.asyncValidators[asyncValidator], switchMap(function (errors$) {
211
+ return concat(of({ type: 'asyncValidation', payload: controlRef }), errors$.pipe(map(function (errors) { return ({
212
+ type: 'asyncValidationResponse',
213
+ payload: {
214
+ key: key,
215
+ errors: errors,
216
+ validatorIndex: validatorIndex
217
+ }
218
+ }); }), catchError(function () {
219
+ return of({
220
+ type: 'asyncValidationResponse',
221
+ payload: {
222
+ key: key,
223
+ errors: { asyncValidationApiError: true },
224
+ validatorIndex: validatorIndex
225
+ }
226
+ });
227
+ })));
228
+ }));
229
+ };
230
+ return acc.concat(effect);
231
+ }, []);
232
+ }
233
+ return scopedEffects;
234
+ };
235
+ var getAsyncValidationEffects = function (formControls) {
236
+ return formControls.reduce(function (acc, control) {
237
+ var _a;
238
+ if (!((_a = control.config.asyncValidators) === null || _a === void 0 ? void 0 : _a.length))
239
+ return acc;
240
+ var action = { type: 'asyncValidationEffect', payload: control };
241
+ return acc.concat(action);
242
+ }, []);
238
243
  };
239
244
 
240
- var buildHub2Source = function (hub1State$, initialBaseState) {
241
- var hub1StateMapped$ = hub1State$.pipe(operators.map(function (payload) { return ({ type: 'formChange', payload: payload }); }));
242
- var initialAction = { type: 'formChange', payload: initialBaseState };
243
- var sourceForHub2$ = hub1StateMapped$.pipe(operators.startWith(initialAction), operators.pairwise(), operators.mergeMap(function (_a) {
244
- var prevForm = _a[0].payload.form, currAction = _a[1];
245
- var _b = currAction.payload, _changedControls = _b._changedControls, currentForm = _b.form;
246
- var valueChanged = !isEqual__default["default"](prevForm.root.value, currentForm.root.value);
247
- var controlsToCheck = _changedControls && valueChanged ? Object.values(_changedControls) : [];
248
- var asyncValidationActions = getAsyncValidationActions(controlsToCheck);
249
- return rxjs.of.apply(void 0, __spreadArray([currAction], asyncValidationActions, false));
250
- }));
251
- return sourceForHub2$;
245
+ var buildHub2Source = function (hub1State$, initialBaseState) {
246
+ var hub1StateMapped$ = hub1State$.pipe(map(function (payload) { return ({ type: 'formChange', payload: payload }); }));
247
+ var initialAction = { type: 'formChange', payload: initialBaseState };
248
+ var sourceForHub2$ = hub1StateMapped$.pipe(startWith(initialAction), pairwise(), mergeMap(function (_a) {
249
+ var prevForm = _a[0].payload.form, currAction = _a[1];
250
+ var _b = currAction.payload, _changedControls = _b._changedControls, currentForm = _b.form;
251
+ var valueChanged = !isEqual(prevForm.root.value, currentForm.root.value);
252
+ var controlsToCheck = _changedControls && valueChanged ? Object.values(_changedControls) : [];
253
+ var asyncValidationEffectActions = getAsyncValidationEffects(controlsToCheck);
254
+ return of.apply(void 0, __spreadArray([currAction], asyncValidationEffectActions, false));
255
+ }));
256
+ return sourceForHub2$;
252
257
  };
253
258
 
254
- var getControl = function (controlRef, form) {
255
- return form[getFormKey(controlRef)];
259
+ var getControl = function (controlRef, form) {
260
+ return form[getFormKey(controlRef)];
256
261
  };
257
262
 
258
- var UPDATE_ANCESTOR_VALUES = 'UPDATE_ANCESTOR_VALUES';
259
- var updateAncestorValues = function (form, _a, providers) {
260
- var _b, _c;
261
- var _d = _a.payload, controlRef = _d.controlRef, value = _d.value;
262
- if (controlRef.length) {
263
- var parentRef = controlRef.slice(0, -1);
264
- var parentControl = getControl(parentRef, form);
265
- var parentFormKey = getFormKey(parentControl.controlRef);
266
- var childKey_1 = controlRef.slice(-1)[0];
267
- var newValue = void 0;
268
- // If parent is a Form Array
269
- if (Array.isArray(parentControl.value)) {
270
- newValue = parentControl.value.map(function (item, index) {
271
- return index === childKey_1 ? value : item;
272
- });
273
- }
274
- else {
275
- // If parent is a Form Group
276
- newValue = __assign(__assign({}, parentControl.value), (_b = {}, _b[childKey_1] = value, _b));
277
- }
278
- var newParentControl = __assign(__assign({}, parentControl), { validatorErrors: getErrors(parentControl, newValue, providers), value: newValue, dirty: !isEqual__default["default"](newValue, parentControl.pristineValue) });
279
- return updateAncestorValues(__assign(__assign({}, form), (_c = {}, _c[parentFormKey] = newParentControl, _c)), {
280
- type: UPDATE_ANCESTOR_VALUES,
281
- payload: { controlRef: parentRef, value: newValue }
282
- }, providers);
283
- }
284
- return form;
263
+ var updateAncestorValues = function (form, _a, providers) {
264
+ var _b, _c;
265
+ var _d = _a.payload, controlRef = _d.controlRef, value = _d.value;
266
+ if (controlRef.length) {
267
+ var parentRef = controlRef.slice(0, -1);
268
+ var parentControl = getControl(parentRef, form);
269
+ var parentFormKey = getFormKey(parentControl.controlRef);
270
+ var childKey_1 = controlRef.slice(-1)[0];
271
+ var newValue = void 0;
272
+ // If parent is a Form Array
273
+ if (Array.isArray(parentControl.value)) {
274
+ newValue = parentControl.value.map(function (item, index) {
275
+ return index === childKey_1 ? value : item;
276
+ });
277
+ }
278
+ else {
279
+ // If parent is a Form Group
280
+ newValue = __assign(__assign({}, parentControl.value), (_b = {}, _b[childKey_1] = value, _b));
281
+ }
282
+ var newParentControl = __assign(__assign({}, parentControl), { validatorErrors: getErrors(parentControl, newValue, providers), value: newValue, dirty: !isEqual(newValue, parentControl.pristineValue) });
283
+ return updateAncestorValues(__assign(__assign({}, form), (_c = {}, _c[parentFormKey] = newParentControl, _c)), {
284
+ payload: { controlRef: parentRef, value: newValue }
285
+ }, providers);
286
+ }
287
+ return form;
285
288
  };
286
289
 
287
- var isChildRef = function (controlRef, parentRef) {
288
- return (parentRef.every(function (key, index) { return controlRef[index] === key; }) &&
289
- controlRef.length === parentRef.length + 1);
290
+ var isChildRef = function (controlRef, parentRef) {
291
+ return (parentRef.every(function (key, index) { return controlRef[index] === key; }) &&
292
+ controlRef.length === parentRef.length + 1);
290
293
  };
291
294
 
292
- // Includes the original control of interest unless excludeSelf === true
293
- var getDescendantControls = function (controlRef, form, excludeSelf) {
294
- if (excludeSelf === void 0) { excludeSelf = false; }
295
- if (!controlRef.length) {
296
- return Object.values(form).filter(function (_a) {
297
- var controlRef = _a.controlRef;
298
- return excludeSelf ? controlRef.length !== 0 : true;
299
- });
300
- }
301
- var control = getControl(controlRef, form);
302
- var value = control.value, config = control.config;
303
- var descendants;
304
- if (Array.isArray(config.controls)) {
305
- // If control is a Form Array
306
- descendants = value.reduce(function (acc, item, index) {
307
- return acc.concat(getDescendantControls(controlRef.concat(index), form));
308
- }, []);
309
- }
310
- else if (config.controls) {
311
- // If control is a Form Group
312
- descendants = Object.keys(value).reduce(function (acc, key) {
313
- return acc.concat(getDescendantControls(controlRef.concat(key), form));
314
- }, []);
315
- }
316
- if (excludeSelf)
317
- return descendants;
318
- return [control].concat(descendants || []);
295
+ // Includes the original control of interest unless excludeSelf === true
296
+ var getDescendantControls = function (controlRef, form, excludeSelf) {
297
+ if (excludeSelf === void 0) { excludeSelf = false; }
298
+ if (!controlRef.length) {
299
+ return Object.values(form).filter(function (_a) {
300
+ var controlRef = _a.controlRef;
301
+ return excludeSelf ? controlRef.length !== 0 : true;
302
+ });
303
+ }
304
+ var control = getControl(controlRef, form);
305
+ var value = control.value, config = control.config;
306
+ var descendants;
307
+ if (Array.isArray(config.controls)) {
308
+ // If control is a Form Array
309
+ descendants = value.reduce(function (acc, item, index) {
310
+ return acc.concat(getDescendantControls(controlRef.concat(index), form));
311
+ }, []);
312
+ }
313
+ else if (config.controls) {
314
+ // If control is a Form Group
315
+ descendants = Object.keys(value).reduce(function (acc, key) {
316
+ return acc.concat(getDescendantControls(controlRef.concat(key), form));
317
+ }, []);
318
+ }
319
+ if (excludeSelf)
320
+ return descendants;
321
+ return [control].concat(descendants || []);
319
322
  };
320
323
 
321
- // Includes the original control of interest unless excludeSelf === true
322
- var getAncestorControls = function (controlRef, form, excludeSelf) {
323
- if (excludeSelf === void 0) { excludeSelf = false; }
324
- var formControls = controlRef.reduce(function (acc, key) {
325
- var currentRef = acc.currentRef.concat(key);
326
- var formControls = acc.formControls.concat(getControl(currentRef, form));
327
- return {
328
- currentRef: currentRef,
329
- formControls: formControls
330
- };
331
- }, {
332
- currentRef: [],
333
- formControls: []
334
- }).formControls;
335
- var root = form['root'];
336
- var result = [root].concat(formControls);
337
- return result.filter(function (control) {
338
- return excludeSelf ? getFormKey(control.controlRef) !== getFormKey(controlRef) : true;
339
- });
324
+ // Includes the original control of interest unless excludeSelf === true
325
+ var getAncestorControls = function (controlRef, form, excludeSelf) {
326
+ if (excludeSelf === void 0) { excludeSelf = false; }
327
+ var formControls = controlRef.reduce(function (acc, key) {
328
+ var currentRef = acc.currentRef.concat(key);
329
+ var formControls = acc.formControls.concat(getControl(currentRef, form));
330
+ return {
331
+ currentRef: currentRef,
332
+ formControls: formControls
333
+ };
334
+ }, {
335
+ currentRef: [],
336
+ formControls: []
337
+ }).formControls;
338
+ var root = form['root'];
339
+ var result = [root].concat(formControls);
340
+ return result.filter(function (control) {
341
+ return excludeSelf ? getFormKey(control.controlRef) !== getFormKey(controlRef) : true;
342
+ });
340
343
  };
341
344
 
342
- /**
343
- * @description see if any keys in a ControlRef has a "." probably a mistake by developer
344
- */
345
- var controlRefCheck = function (ref) {
346
- try {
347
- var hasError = ref.some(function (key) {
348
- if (typeof key === 'string') {
349
- return key.includes('.');
350
- }
351
- return false;
352
- });
353
- if (hasError) {
354
- var refString = "[".concat(ref.reduce(function (acc, key, index) {
355
- if (index > 0) {
356
- acc = acc.concat(', ');
357
- }
358
- if (typeof key === 'number') {
359
- return acc.concat(key.toString());
360
- }
361
- return acc.concat("'".concat(key, "'"));
362
- }, ''), "]");
363
- var suggestion = "[".concat(ref.reduce(function (acc, key, index) {
364
- if (index > 0) {
365
- acc = acc.concat(', ');
366
- }
367
- if (typeof key === 'number') {
368
- return acc.concat(key.toString());
369
- }
370
- var splitted = key
371
- .split('.')
372
- .map(function (key) { return "'".concat(key, "'"); })
373
- .join(', ');
374
- return acc.concat("".concat(splitted));
375
- }, ''), "]");
376
- console.warn("You provided ".concat(refString, ". Did you mean ").concat(suggestion, "?"));
377
- }
378
- }
379
- catch (_a) { }
345
+ /**
346
+ * @description see if any keys in a ControlRef has a "." probably a mistake by developer
347
+ */
348
+ var controlRefCheck = function (ref) {
349
+ try {
350
+ var hasError = ref.some(function (key) {
351
+ if (typeof key === 'string') {
352
+ return key.includes('.');
353
+ }
354
+ return false;
355
+ });
356
+ if (hasError) {
357
+ var refString = "[".concat(ref.reduce(function (acc, key, index) {
358
+ if (index > 0) {
359
+ acc = acc.concat(', ');
360
+ }
361
+ if (typeof key === 'number') {
362
+ return acc.concat(key.toString());
363
+ }
364
+ return acc.concat("'".concat(key, "'"));
365
+ }, ''), "]");
366
+ var suggestion = "[".concat(ref.reduce(function (acc, key, index) {
367
+ if (index > 0) {
368
+ acc = acc.concat(', ');
369
+ }
370
+ if (typeof key === 'number') {
371
+ return acc.concat(key.toString());
372
+ }
373
+ var splitted = key
374
+ .split('.')
375
+ .map(function (key) { return "'".concat(key, "'"); })
376
+ .join(', ');
377
+ return acc.concat("".concat(splitted));
378
+ }, ''), "]");
379
+ console.warn("You provided ".concat(refString, ". Did you mean ").concat(suggestion, "?"));
380
+ }
381
+ }
382
+ catch (_a) { }
380
383
  };
381
384
 
382
- var UPDATE_DESCENDANT_VALUES = 'UPDATE_DESCENDANT_VALUES';
383
- var updateDescendantValues = function (form, _a, providers) {
384
- var _b = _a.payload, controlRef = _b.controlRef, value = _b.value;
385
- var descendants = getDescendantControls(controlRef, form, true).map(function (control) { return [getFormKey(control.controlRef), control]; });
386
- var result = descendants.reduce(function (acc, _a) {
387
- var _b;
388
- var key = _a[0], control = _a[1];
389
- if (isChildRef(control.controlRef, controlRef)) {
390
- var childValue = value[control.controlRef.at(-1)];
391
- var validatorErrors = getErrors(control, childValue, providers);
392
- var newControl = __assign(__assign({}, control), { value: childValue, validatorErrors: validatorErrors, dirty: !isEqual__default["default"](childValue, control.pristineValue) });
393
- acc = __assign(__assign({}, acc), (_b = {}, _b[key] = newControl, _b));
394
- var configControls = control.config.controls;
395
- if (configControls) {
396
- acc = updateDescendantValues(acc, {
397
- type: UPDATE_DESCENDANT_VALUES,
398
- payload: { controlRef: control.controlRef, value: childValue }
399
- }, providers);
400
- }
401
- }
402
- return acc;
403
- }, form);
404
- return result;
405
- };
406
- // Will only update child controls that are present.
407
- // Use AddControlPayload/RemoveControl action reducers to add/remove control
408
- var updateValues = function (_a, action, providers, mergeChanges) {
409
- var _b, _c;
410
- var form = _a.form, _d = _a._changedControls, _changedControls = _d === void 0 ? {} : _d, _e = _a._removedControls, _removedControls = _e === void 0 ? {} : _e;
411
- if (mergeChanges === void 0) { mergeChanges = false; }
412
- var normalizers = providers.normalizers;
413
- var _f = action.payload, controlRef = _f.controlRef, value = _f.value;
414
- controlRefCheck(controlRef);
415
- // Update its own value
416
- var ctrlKey = getFormKey(controlRef);
417
- var newValue = value;
418
- var config = form[ctrlKey].config;
419
- if (config.normalizers) {
420
- newValue = config.normalizers.reduce(function (acc, normalizer) {
421
- if (!normalizers[normalizer]) {
422
- throw "You have not provided a normalizer for \"".concat(normalizer, "\"");
423
- }
424
- return normalizers[normalizer](acc);
425
- }, value);
426
- }
427
- var validatorErrors = getErrors(form[ctrlKey], newValue, providers);
428
- var newControl = __assign(__assign({}, form[ctrlKey]), { validatorErrors: validatorErrors, dirty: !isEqual__default["default"](value, form[ctrlKey].pristineValue), value: newValue });
429
- var result = {
430
- form: __assign(__assign({}, form), (_b = {}, _b[ctrlKey] = newControl, _b)),
431
- _changedControls: (_c = {}, _c[newControl.key] = newControl, _c)
432
- };
433
- var configControls = config.controls;
434
- // Update its descendants
435
- if (configControls) {
436
- var updatedDescendants = updateDescendantValues(result.form, {
437
- type: UPDATE_DESCENDANT_VALUES,
438
- payload: {
439
- controlRef: controlRef,
440
- value: newValue
441
- }
442
- }, providers);
443
- var changedDescendantControls = getDescendantControls(controlRef, updatedDescendants).reduce(function (acc, control) {
444
- var _a;
445
- return (__assign(__assign({}, acc), (_a = {}, _a[control.key] = control, _a)));
446
- }, {});
447
- result = __assign(__assign({}, result), { form: updatedDescendants, _changedControls: __assign(__assign({}, result._changedControls), changedDescendantControls) });
448
- }
449
- // Update its Ancestors
450
- if (controlRef.length) {
451
- result = __assign(__assign({}, result), { form: updateAncestorValues(result.form, {
452
- type: UPDATE_ANCESTOR_VALUES,
453
- payload: { controlRef: controlRef, value: newValue }
454
- }, providers) });
455
- }
456
- var changedAncestorControls = getAncestorControls(controlRef, result.form).reduce(function (acc, control) {
457
- var _a;
458
- return (__assign(__assign({}, acc), (_a = {}, _a[control.key] = control, _a)));
459
- }, {});
460
- var mergedResult = __assign(__assign({}, result), { _changedControls: __assign(__assign(__assign({}, (mergeChanges ? _changedControls : undefined)), changedAncestorControls), result._changedControls), _removedControls: mergeChanges ? _removedControls : undefined });
461
- return mergedResult;
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.';
388
+ }
389
+ };
390
+ var updateDescendantValues = function (form, _a, providers) {
391
+ var _b = _a.payload, controlRef = _b.controlRef, value = _b.value;
392
+ var descendants = getDescendantControls(controlRef, form, true).map(function (control) { return [getFormKey(control.controlRef), control]; });
393
+ var result = descendants.reduce(function (acc, _a) {
394
+ var _b;
395
+ var key = _a[0], control = _a[1];
396
+ if (isChildRef(control.controlRef, controlRef)) {
397
+ var newChildValue = value[control.controlRef.at(-1)];
398
+ var validatorErrors = getErrors(control, newChildValue, providers);
399
+ var oldChildValue = control.value;
400
+ var newControl = __assign(__assign({}, control), { value: newChildValue, validatorErrors: validatorErrors, dirty: !isEqual(newChildValue, control.pristineValue) });
401
+ acc = __assign(__assign({}, acc), (_b = {}, _b[key] = newControl, _b));
402
+ var configControls = control.config.controls;
403
+ if (configControls) {
404
+ checkKeys(oldChildValue, newChildValue);
405
+ acc = updateDescendantValues(acc, {
406
+ payload: { controlRef: control.controlRef, value: newChildValue }
407
+ }, providers);
408
+ }
409
+ }
410
+ return acc;
411
+ }, form);
412
+ return result;
413
+ };
414
+ // Will only update child controls that are present.
415
+ // Use AddControlPayload/RemoveControl action reducers to add/remove control
416
+ var updateValues = function (_a, action, providers, mergeChanges) {
417
+ 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
+ if (mergeChanges === void 0) { mergeChanges = false; }
420
+ var normalizers = providers.normalizers;
421
+ var _f = action.payload, controlRef = _f.controlRef, value = _f.value;
422
+ controlRefCheck(controlRef);
423
+ // Update its own value
424
+ var ctrlKey = getFormKey(controlRef);
425
+ var newValue = value;
426
+ var oldValue = form[ctrlKey].value;
427
+ var config = form[ctrlKey].config;
428
+ if (config.normalizers) {
429
+ newValue = config.normalizers.reduce(function (acc, normalizer) {
430
+ if (!normalizers[normalizer]) {
431
+ throw "You have not provided a normalizer for \"".concat(normalizer, "\"");
432
+ }
433
+ return normalizers[normalizer](acc);
434
+ }, value);
435
+ }
436
+ var validatorErrors = getErrors(form[ctrlKey], newValue, providers);
437
+ var newControl = __assign(__assign({}, form[ctrlKey]), { validatorErrors: validatorErrors, dirty: !isEqual(value, form[ctrlKey].pristineValue), value: newValue });
438
+ var result = {
439
+ form: __assign(__assign({}, form), (_b = {}, _b[ctrlKey] = newControl, _b)),
440
+ _changedControls: (_c = {}, _c[newControl.key] = newControl, _c)
441
+ };
442
+ var configControls = config.controls;
443
+ // Update its descendants
444
+ if (configControls) {
445
+ checkKeys(oldValue, newValue);
446
+ var updatedDescendants = updateDescendantValues(result.form, {
447
+ payload: {
448
+ controlRef: controlRef,
449
+ value: newValue
450
+ }
451
+ }, providers);
452
+ var changedDescendantControls = getDescendantControls(controlRef, updatedDescendants).reduce(function (acc, control) {
453
+ var _a;
454
+ return (__assign(__assign({}, acc), (_a = {}, _a[control.key] = control, _a)));
455
+ }, {});
456
+ result = __assign(__assign({}, result), { form: updatedDescendants, _changedControls: __assign(__assign({}, result._changedControls), changedDescendantControls) });
457
+ }
458
+ // Update its Ancestors
459
+ if (controlRef.length) {
460
+ result = __assign(__assign({}, result), { form: updateAncestorValues(result.form, {
461
+ payload: { controlRef: controlRef, value: newValue }
462
+ }, providers) });
463
+ }
464
+ var changedAncestorControls = getAncestorControls(controlRef, result.form).reduce(function (acc, control) {
465
+ var _a;
466
+ return (__assign(__assign({}, acc), (_a = {}, _a[control.key] = control, _a)));
467
+ }, {});
468
+ var mergedResult = __assign(__assign({}, result), { _changedControls: __assign(__assign(__assign({}, (mergeChanges ? _changedControls : undefined)), changedAncestorControls), result._changedControls), _removedControls: mergeChanges ? _removedControls : undefined });
469
+ return mergedResult;
462
470
  };
463
471
 
464
- var UPDATE_ANCESTOR_VALUES_REMOVE_CONTROL = 'UPDATE_ANCESTOR_VALUES_REMOVE_CONTROL';
465
- var updateAncestorValuesRemoveControl = function (form, _a, providers) {
466
- var _b;
467
- var controlRef = _a.payload;
468
- if (controlRef.length) {
469
- var parentRef = controlRef.slice(0, -1);
470
- var parentControl = getControl(parentRef, form);
471
- var parentFormKey = getFormKey(parentControl.controlRef);
472
- var childKey_1 = controlRef.slice(-1)[0];
473
- var newValue = void 0;
474
- // If parent is a Form Array
475
- if (Array.isArray(parentControl.value)) {
476
- newValue = parentControl.value.filter(function (item, index) { return index !== childKey_1; });
477
- }
478
- else {
479
- // If parent is a Form Group
480
- newValue = __assign({}, parentControl.value);
481
- delete newValue[childKey_1];
482
- }
483
- var newParentControl = __assign(__assign({}, parentControl), { value: newValue, validatorErrors: getErrors(parentControl, newValue, providers), dirty: !isEqual__default["default"](newValue, parentControl.pristineValue) });
484
- return updateAncestorValues(__assign(__assign({}, form), (_b = {}, _b[parentFormKey] = newParentControl, _b)), {
485
- type: UPDATE_ANCESTOR_VALUES,
486
- payload: { controlRef: parentRef, value: newValue }
487
- }, providers);
488
- }
489
- return form;
472
+ var updateAncestorValuesRemoveControl = function (form, _a, providers) {
473
+ var _b;
474
+ var controlRef = _a.payload;
475
+ if (controlRef.length) {
476
+ var parentRef = controlRef.slice(0, -1);
477
+ var parentControl = getControl(parentRef, form);
478
+ var parentFormKey = getFormKey(parentControl.controlRef);
479
+ var childKey_1 = controlRef.slice(-1)[0];
480
+ var newValue = void 0;
481
+ // If parent is a Form Array
482
+ if (Array.isArray(parentControl.value)) {
483
+ newValue = parentControl.value.filter(function (item, index) { return index !== childKey_1; });
484
+ }
485
+ else {
486
+ // If parent is a Form Group
487
+ newValue = __assign({}, parentControl.value);
488
+ delete newValue[childKey_1];
489
+ }
490
+ var newParentControl = __assign(__assign({}, parentControl), { value: newValue, validatorErrors: getErrors(parentControl, newValue, providers), dirty: !isEqual(newValue, parentControl.pristineValue) });
491
+ return updateAncestorValues(__assign(__assign({}, form), (_b = {}, _b[parentFormKey] = newParentControl, _b)), {
492
+ payload: { controlRef: parentRef, value: newValue }
493
+ }, providers);
494
+ }
495
+ return form;
490
496
  };
491
497
 
492
- var removeControl = function (state, action, providers, mergeChanges) {
493
- var _a;
494
- if (mergeChanges === void 0) { mergeChanges = false; }
495
- var form = state.form;
496
- var controlRef = action.payload;
497
- controlRefCheck(controlRef);
498
- var controlToRemove = getControl(controlRef, form);
499
- if (!controlToRemove) {
500
- throw 'Control not found';
501
- }
502
- // Can't remove the root of the form
503
- if (!controlRef.length)
504
- return { form: form };
505
- var parentRef = controlRef.slice(0, -1);
506
- var parentIsFormArray = Array.isArray(getControl(parentRef, form).config.controls);
507
- var descendants = getDescendantControls(controlRef, form);
508
- var descendantkeys = descendants.map(function (_a) {
509
- var controlRef = _a.controlRef;
510
- return getFormKey(controlRef);
511
- });
512
- var controlsRemoved = __assign({}, form);
513
- descendantkeys.forEach(function (key) {
514
- delete controlsRemoved[key];
515
- });
516
- // Remove control and all descendants
517
- var controlRemoved = Object.entries(controlsRemoved).reduce(function (acc, _a) {
518
- var _b, _c;
519
- var key = _a[0], control = _a[1];
520
- // May need to reindex array items of removed control
521
- // if it was part of a Form Array.
522
- if (parentIsFormArray) {
523
- var oldIndex = control.controlRef.at(parentRef.length);
524
- if (
525
- // If control is descendant.
526
- parentRef.every(function (ref, index) { return control.controlRef[index] === ref; }) &&
527
- control.controlRef.length > parentRef.length &&
528
- // If the array item index was greater than the index of item removed
529
- // we need to decrement its index by 1.
530
- oldIndex > controlRef.at(-1)) {
531
- var newRef = parentRef
532
- .concat(oldIndex - 1)
533
- .concat(control.controlRef.slice(parentRef.length + 1));
534
- return __assign(__assign({}, acc), (_b = {}, _b[getFormKey(newRef)] = __assign(__assign({}, control), { controlRef: newRef }), _b));
535
- }
536
- }
537
- return __assign(__assign({}, acc), (_c = {}, _c[key] = control, _c));
538
- }, {});
539
- var result = updateAncestorValuesRemoveControl(controlRemoved, {
540
- type: UPDATE_ANCESTOR_VALUES_REMOVE_CONTROL,
541
- payload: controlRef
542
- }, providers);
543
- var _changedControls = __assign(__assign({}, (mergeChanges ? state._changedControls || {} : undefined)), getAncestorControls(controlRef.slice(0, -1), result).reduce(function (acc, control) {
544
- var _a;
545
- return (__assign(__assign({}, acc), (_a = {}, _a[control.key] = control, _a)));
546
- }, {}));
547
- // Check for reindexing for changed controls
548
- if (parentIsFormArray) {
549
- _changedControls = Object.entries(_changedControls).reduce(function (acc, _a) {
550
- var _b, _c;
551
- var key = _a[0], control = _a[1];
552
- var oldIndex = control.controlRef.at(parentRef.length);
553
- if (
554
- // If control is descendant.
555
- parentRef.every(function (ref, index) { return control.controlRef[index] === ref; }) &&
556
- control.controlRef.length > parentRef.length &&
557
- // If the array item index was greater than the index of item removed
558
- // we need to decrement its index by 1.
559
- oldIndex > controlRef.at(-1)) {
560
- var newRef = parentRef
561
- .concat(oldIndex - 1)
562
- .concat(control.controlRef.slice(parentRef.length + 1));
563
- return __assign(__assign({}, acc), (_b = {}, _b[getFormKey(newRef)] = __assign(__assign({}, control), { controlRef: newRef }), _b));
564
- }
565
- else {
566
- return __assign(__assign({}, acc), (_c = {}, _c[key] = control, _c));
567
- }
568
- }, {});
569
- }
570
- var _removedControls = __assign(__assign({}, (mergeChanges ? state._removedControls || {} : undefined)), (_a = {}, _a[controlToRemove.key] = controlToRemove, _a));
571
- descendants
572
- .map(function (_a) {
573
- var key = _a.key;
574
- return key;
575
- })
576
- .forEach(function (key) {
577
- delete _changedControls[key];
578
- });
579
- return {
580
- form: result,
581
- _changedControls: _changedControls,
582
- _removedControls: _removedControls
583
- };
498
+ var removeControl = function (state, action, providers, mergeChanges) {
499
+ var _a;
500
+ if (mergeChanges === void 0) { mergeChanges = false; }
501
+ var form = state.form;
502
+ var controlRef = action.payload;
503
+ controlRefCheck(controlRef);
504
+ var controlToRemove = getControl(controlRef, form);
505
+ if (!controlToRemove) {
506
+ throw 'Control not found';
507
+ }
508
+ // Can't remove the root of the form
509
+ if (!controlRef.length)
510
+ return { form: form };
511
+ var parentRef = controlRef.slice(0, -1);
512
+ var parentIsFormArray = Array.isArray(getControl(parentRef, form).config.controls);
513
+ var descendants = getDescendantControls(controlRef, form);
514
+ var descendantkeys = descendants.map(function (_a) {
515
+ var controlRef = _a.controlRef;
516
+ return getFormKey(controlRef);
517
+ });
518
+ var controlsRemoved = __assign({}, form);
519
+ descendantkeys.forEach(function (key) {
520
+ delete controlsRemoved[key];
521
+ });
522
+ // Remove control and all descendants
523
+ var controlRemoved = Object.entries(controlsRemoved).reduce(function (acc, _a) {
524
+ var _b, _c;
525
+ var key = _a[0], control = _a[1];
526
+ // May need to reindex array items of removed control
527
+ // if it was part of a Form Array.
528
+ if (parentIsFormArray) {
529
+ var oldIndex = control.controlRef.at(parentRef.length);
530
+ if (
531
+ // If control is descendant.
532
+ parentRef.every(function (ref, index) { return control.controlRef[index] === ref; }) &&
533
+ control.controlRef.length > parentRef.length &&
534
+ // If the array item index was greater than the index of item removed
535
+ // we need to decrement its index by 1.
536
+ oldIndex > controlRef.at(-1)) {
537
+ var newRef = parentRef
538
+ .concat(oldIndex - 1)
539
+ .concat(control.controlRef.slice(parentRef.length + 1));
540
+ return __assign(__assign({}, acc), (_b = {}, _b[getFormKey(newRef)] = __assign(__assign({}, control), { controlRef: newRef }), _b));
541
+ }
542
+ }
543
+ return __assign(__assign({}, acc), (_c = {}, _c[key] = control, _c));
544
+ }, {});
545
+ var result = updateAncestorValuesRemoveControl(controlRemoved, {
546
+ payload: controlRef
547
+ }, providers);
548
+ var _changedControls = __assign(__assign({}, (mergeChanges ? state._changedControls || {} : undefined)), getAncestorControls(controlRef.slice(0, -1), result).reduce(function (acc, control) {
549
+ var _a;
550
+ return (__assign(__assign({}, acc), (_a = {}, _a[control.key] = control, _a)));
551
+ }, {}));
552
+ // Check for reindexing for changed controls
553
+ if (parentIsFormArray) {
554
+ _changedControls = Object.entries(_changedControls).reduce(function (acc, _a) {
555
+ var _b, _c;
556
+ var key = _a[0], control = _a[1];
557
+ var oldIndex = control.controlRef.at(parentRef.length);
558
+ if (
559
+ // If control is descendant.
560
+ parentRef.every(function (ref, index) { return control.controlRef[index] === ref; }) &&
561
+ control.controlRef.length > parentRef.length &&
562
+ // If the array item index was greater than the index of item removed
563
+ // we need to decrement its index by 1.
564
+ oldIndex > controlRef.at(-1)) {
565
+ var newRef = parentRef
566
+ .concat(oldIndex - 1)
567
+ .concat(control.controlRef.slice(parentRef.length + 1));
568
+ return __assign(__assign({}, acc), (_b = {}, _b[getFormKey(newRef)] = __assign(__assign({}, control), { controlRef: newRef }), _b));
569
+ }
570
+ else {
571
+ return __assign(__assign({}, acc), (_c = {}, _c[key] = control, _c));
572
+ }
573
+ }, {});
574
+ }
575
+ var _removedControls = __assign(__assign({}, (mergeChanges ? state._removedControls || {} : undefined)), (_a = {}, _a[controlToRemove.key] = controlToRemove, _a));
576
+ descendants
577
+ .map(function (_a) {
578
+ var key = _a.key;
579
+ return key;
580
+ })
581
+ .forEach(function (key) {
582
+ delete _changedControls[key];
583
+ });
584
+ return {
585
+ form: result,
586
+ _changedControls: _changedControls,
587
+ _removedControls: _removedControls
588
+ };
584
589
  };
585
590
 
586
- var UPDATE_ANCESTOR_VALUES_ADD_CONTROL = 'UPDATE_ANCESTOR_VALUES_ADD_CONTROL';
587
- var updateAncestorValuesAddControl = function (form, _a, providers) {
588
- var _b, _c;
589
- var _d = _a.payload, controlRef = _d.controlRef, value = _d.value;
590
- if (controlRef.length) {
591
- var parentRef = controlRef.slice(0, -1);
592
- var parentControl = getControl(parentRef, form);
593
- var parentFormKey = getFormKey(parentControl.controlRef);
594
- var childKey = controlRef.slice(-1)[0];
595
- var newValue = void 0;
596
- // If parent is a Form Array
597
- if (Array.isArray(parentControl.value)) {
598
- newValue = parentControl.value.concat(value);
599
- }
600
- else {
601
- // If parent is a Form Group
602
- newValue = __assign(__assign({}, form[parentFormKey].value), (_b = {}, _b[childKey] = value, _b));
603
- }
604
- var newParentControl = __assign(__assign({}, parentControl), { value: newValue, validatorErrors: getErrors(parentControl, newValue, providers), dirty: !isEqual__default["default"](newValue, parentControl.pristineValue) });
605
- return updateAncestorValues(__assign(__assign({}, form), (_c = {}, _c[parentFormKey] = newParentControl, _c)), {
606
- type: UPDATE_ANCESTOR_VALUES,
607
- payload: { controlRef: parentRef, value: newValue }
608
- }, providers);
609
- }
610
- return form;
591
+ var updateAncestorValuesAddControl = function (form, _a, providers) {
592
+ var _b, _c;
593
+ var _d = _a.payload, controlRef = _d.controlRef, value = _d.value;
594
+ if (controlRef.length) {
595
+ var parentRef = controlRef.slice(0, -1);
596
+ var parentControl = getControl(parentRef, form);
597
+ var parentFormKey = getFormKey(parentControl.controlRef);
598
+ var childKey = controlRef.slice(-1)[0];
599
+ var newValue = void 0;
600
+ // If parent is a Form Array
601
+ if (Array.isArray(parentControl.value)) {
602
+ newValue = parentControl.value.concat(value);
603
+ }
604
+ else {
605
+ // If parent is a Form Group
606
+ newValue = __assign(__assign({}, form[parentFormKey].value), (_b = {}, _b[childKey] = value, _b));
607
+ }
608
+ var newParentControl = __assign(__assign({}, parentControl), { value: newValue, validatorErrors: getErrors(parentControl, newValue, providers), dirty: !isEqual(newValue, parentControl.pristineValue) });
609
+ return updateAncestorValues(__assign(__assign({}, form), (_c = {}, _c[parentFormKey] = newParentControl, _c)), {
610
+ payload: { controlRef: parentRef, value: newValue }
611
+ }, providers);
612
+ }
613
+ return form;
611
614
  };
612
615
 
613
- var getControlBranch = function (controlRef, form) {
614
- var ancestors = getAncestorControls(controlRef, form);
615
- var childControls = getDescendantControls(controlRef, form).slice(1);
616
- return ancestors.concat(childControls).sort(function (a, b) { return a.controlRef.length - b.controlRef.length; });
616
+ var getControlBranch = function (controlRef, form) {
617
+ var ancestors = getAncestorControls(controlRef, form);
618
+ var childControls = getDescendantControls(controlRef, form).slice(1);
619
+ return ancestors.concat(childControls).sort(function (a, b) { return a.controlRef.length - b.controlRef.length; });
617
620
  };
618
621
 
619
- var addControl = function (state, action, providers, mergeChanges) {
620
- if (mergeChanges === void 0) { mergeChanges = false; }
621
- var _a = action.payload, config = _a.config, controlRef = _a.controlRef;
622
- controlRefCheck(controlRef);
623
- // If controlRef does not exist we are adding control to a Form Group
624
- if (!getControl(controlRef.slice(0, -1), state.form)) {
625
- throw 'You are attempting to add a control to a non-existent form group';
626
- }
627
- var newForm = buildState(config, state.form, controlRef, providers);
628
- var newValue = getControl(controlRef, newForm).value;
629
- var ancestorsUpdated = updateAncestorValuesAddControl(newForm, {
630
- type: UPDATE_ANCESTOR_VALUES_ADD_CONTROL,
631
- payload: { controlRef: controlRef, value: newValue }
632
- }, providers);
633
- var _changedControls = getControlBranch(controlRef, ancestorsUpdated).reduce(function (acc, control) {
634
- var _a;
635
- return (__assign(__assign({}, acc), (_a = {}, _a[control.key] = control, _a)));
636
- }, {});
637
- return {
638
- form: ancestorsUpdated,
639
- _changedControls: __assign(__assign({}, (mergeChanges ? state._changedControls || {} : undefined)), _changedControls),
640
- _removedControls: mergeChanges ? state._removedControls || {} : undefined
641
- };
622
+ var addControl = function (state, action, providers, mergeChanges) {
623
+ if (mergeChanges === void 0) { mergeChanges = false; }
624
+ var _a = action.payload, config = _a.config, controlRef = _a.controlRef;
625
+ controlRefCheck(controlRef);
626
+ // If controlRef does not exist we are adding control to a Form Group
627
+ if (!getControl(controlRef.slice(0, -1), state.form)) {
628
+ throw 'You are attempting to add a control to a non-existent form group';
629
+ }
630
+ var newForm = buildState(config, state.form, controlRef, providers);
631
+ var newValue = getControl(controlRef, newForm).value;
632
+ var ancestorsUpdated = updateAncestorValuesAddControl(newForm, {
633
+ payload: { controlRef: controlRef, value: newValue }
634
+ }, providers);
635
+ var _changedControls = getControlBranch(controlRef, ancestorsUpdated).reduce(function (acc, control) {
636
+ var _a;
637
+ return (__assign(__assign({}, acc), (_a = {}, _a[control.key] = control, _a)));
638
+ }, {});
639
+ return {
640
+ form: ancestorsUpdated,
641
+ _changedControls: __assign(__assign({}, (mergeChanges ? state._changedControls || {} : undefined)), _changedControls),
642
+ _removedControls: mergeChanges ? state._removedControls || {} : undefined
643
+ };
642
644
  };
643
645
 
644
- var pushControl = function (state, action, providers, mergeChanges) {
645
- if (mergeChanges === void 0) { mergeChanges = false; }
646
- var newControlRef;
647
- var _a = action.payload, config = _a.config, controlRef = _a.controlRef;
648
- controlRefCheck(controlRef);
649
- var existingControl = getControl(controlRef, state.form);
650
- if (!existingControl)
651
- throw 'You are attempting to push to a control that does not exist';
652
- // If controlRef exists we are adding control to a Form Array
653
- if (Array.isArray(existingControl.config.controls)) {
654
- newControlRef = controlRef.concat(existingControl.value.length);
655
- }
656
- else {
657
- throw 'You are attempting to push to a control that is not a Form Array';
658
- }
659
- var newForm = buildState(config, state.form, newControlRef, providers);
660
- var newValue = getControl(newControlRef, newForm).value;
661
- var ancestorsUpdated = updateAncestorValuesAddControl(newForm, {
662
- type: UPDATE_ANCESTOR_VALUES_ADD_CONTROL,
663
- payload: { controlRef: newControlRef, value: newValue }
664
- }, providers);
665
- var _changedControls = getControlBranch(newControlRef, ancestorsUpdated).reduce(function (acc, control) {
666
- var _a;
667
- return (__assign(__assign({}, acc), (_a = {}, _a[control.key] = control, _a)));
668
- }, {});
669
- return {
670
- form: ancestorsUpdated,
671
- _changedControls: __assign(__assign({}, (mergeChanges ? state._changedControls || {} : undefined)), _changedControls),
672
- _removedControls: mergeChanges ? state._removedControls || {} : undefined
673
- };
646
+ var pushControl = function (state, action, providers, mergeChanges) {
647
+ if (mergeChanges === void 0) { mergeChanges = false; }
648
+ var newControlRef;
649
+ var _a = action.payload, config = _a.config, controlRef = _a.controlRef;
650
+ controlRefCheck(controlRef);
651
+ var existingControl = getControl(controlRef, state.form);
652
+ if (!existingControl)
653
+ throw 'You are attempting to push to a control that does not exist';
654
+ // If controlRef exists we are adding control to a Form Array
655
+ if (Array.isArray(existingControl.config.controls)) {
656
+ newControlRef = controlRef.concat(existingControl.value.length);
657
+ }
658
+ else {
659
+ throw 'You are attempting to push to a control that is not a Form Array';
660
+ }
661
+ var newForm = buildState(config, state.form, newControlRef, providers);
662
+ var newValue = getControl(newControlRef, newForm).value;
663
+ var ancestorsUpdated = updateAncestorValuesAddControl(newForm, {
664
+ payload: { controlRef: newControlRef, value: newValue }
665
+ }, providers);
666
+ var _changedControls = getControlBranch(newControlRef, ancestorsUpdated).reduce(function (acc, control) {
667
+ var _a;
668
+ return (__assign(__assign({}, acc), (_a = {}, _a[control.key] = control, _a)));
669
+ }, {});
670
+ return {
671
+ form: ancestorsUpdated,
672
+ _changedControls: __assign(__assign({}, (mergeChanges ? state._changedControls || {} : undefined)), _changedControls),
673
+ _removedControls: mergeChanges ? state._removedControls || {} : undefined
674
+ };
674
675
  };
675
676
 
676
- // Same implementation as updateAncestor values except updating pristine values
677
- var UPDATE_ANCESTOR_PRISTINE_VALUES = 'UPDATE_ANCESTOR_PRISTINE_VALUES';
678
- var updateAncestorPristineValues = function (form, _a) {
679
- var _b;
680
- var controlRef = _a.payload;
681
- if (controlRef.length) {
682
- var parentRef = controlRef.slice(0, -1);
683
- var parentKey = getFormKey(parentRef);
684
- var siblingControls = getDescendantControls(parentRef, form).filter(
685
- // Out of descendants we only need the siblings
686
- function (control) { return control.controlRef.length === controlRef.length; });
687
- var newValue = void 0;
688
- // If parent is a Form Array
689
- if (Array.isArray(form[parentKey].value)) {
690
- newValue = siblingControls
691
- .slice()
692
- .sort(function (a, b) { return a.controlRef.at(-1) - b.controlRef.at(-1); })
693
- .map(function (control) { return control.pristineValue; });
694
- }
695
- else {
696
- // If parent is a Form Group
697
- newValue = siblingControls.reduce(function (acc, _a) {
698
- var _b;
699
- var controlRef = _a.controlRef, pristineValue = _a.pristineValue;
700
- return __assign(__assign({}, acc), (_b = {}, _b[controlRef.at(-1)] = pristineValue, _b));
701
- }, {});
702
- }
703
- var newParentControl = __assign(__assign({}, form[parentKey]), { pristineValue: newValue, dirty: isEqual__default["default"](form[parentKey].value, newValue) });
704
- return updateAncestorPristineValues(__assign(__assign({}, form), (_b = {}, _b[parentKey] = newParentControl, _b)), {
705
- type: UPDATE_ANCESTOR_PRISTINE_VALUES,
706
- payload: parentRef
707
- });
708
- }
709
- return form;
677
+ var updateAncestorPristineValues = function (form, _a) {
678
+ var _b;
679
+ var controlRef = _a.payload;
680
+ if (controlRef.length) {
681
+ var parentRef = controlRef.slice(0, -1);
682
+ var parentKey = getFormKey(parentRef);
683
+ var siblingControls = getDescendantControls(parentRef, form).filter(
684
+ // Out of descendants we only need the siblings
685
+ function (control) { return control.controlRef.length === controlRef.length; });
686
+ var newValue = void 0;
687
+ // If parent is a Form Array
688
+ if (Array.isArray(form[parentKey].value)) {
689
+ newValue = siblingControls
690
+ .slice()
691
+ .sort(function (a, b) { return a.controlRef.at(-1) - b.controlRef.at(-1); })
692
+ .map(function (control) { return control.pristineValue; });
693
+ }
694
+ else {
695
+ // If parent is a Form Group
696
+ newValue = siblingControls.reduce(function (acc, _a) {
697
+ var _b;
698
+ var controlRef = _a.controlRef, pristineValue = _a.pristineValue;
699
+ return __assign(__assign({}, acc), (_b = {}, _b[controlRef.at(-1)] = pristineValue, _b));
700
+ }, {});
701
+ }
702
+ var newParentControl = __assign(__assign({}, form[parentKey]), { pristineValue: newValue, dirty: isEqual(form[parentKey].value, newValue) });
703
+ return updateAncestorPristineValues(__assign(__assign({}, form), (_b = {}, _b[parentKey] = newParentControl, _b)), {
704
+ payload: parentRef
705
+ });
706
+ }
707
+ return form;
710
708
  };
711
709
 
712
- var markControlAsPristine = function (state, action, mergeChanges) {
713
- if (mergeChanges === void 0) { mergeChanges = false; }
714
- var form = state.form;
715
- var controlRef = action.payload;
716
- controlRefCheck(controlRef);
717
- var descendants = getDescendantControls(controlRef, form).reduce(function (acc, control) {
718
- var _a;
719
- return (__assign(__assign({}, acc), (_a = {}, _a[getFormKey(control.controlRef)] = __assign(__assign({}, control), { dirty: false, pristineValue: control.value }), _a)));
720
- }, {});
721
- var result = __assign(__assign({}, form), descendants);
722
- if (controlRef.length) {
723
- result = updateAncestorPristineValues(result, {
724
- type: UPDATE_ANCESTOR_PRISTINE_VALUES,
725
- payload: controlRef
726
- });
727
- }
728
- var _changedControls = getControlBranch(controlRef, result).reduce(function (acc, control) {
729
- var _a;
730
- return (__assign(__assign({}, acc), (_a = {}, _a[control.key] = control, _a)));
731
- }, {});
732
- return {
733
- form: result,
734
- _changedControls: __assign(__assign({}, (mergeChanges ? state._changedControls || {} : undefined)), _changedControls),
735
- _removedControls: mergeChanges ? state._removedControls || {} : undefined
736
- };
710
+ var markControlAsPristine = function (state, action, mergeChanges) {
711
+ if (mergeChanges === void 0) { mergeChanges = false; }
712
+ var form = state.form;
713
+ var controlRef = action.payload;
714
+ controlRefCheck(controlRef);
715
+ var descendants = getDescendantControls(controlRef, form).reduce(function (acc, control) {
716
+ var _a;
717
+ return (__assign(__assign({}, acc), (_a = {}, _a[getFormKey(control.controlRef)] = __assign(__assign({}, control), { dirty: false, pristineValue: control.value }), _a)));
718
+ }, {});
719
+ var result = __assign(__assign({}, form), descendants);
720
+ if (controlRef.length) {
721
+ result = updateAncestorPristineValues(result, {
722
+ payload: controlRef
723
+ });
724
+ }
725
+ var _changedControls = getControlBranch(controlRef, result).reduce(function (acc, control) {
726
+ var _a;
727
+ return (__assign(__assign({}, acc), (_a = {}, _a[control.key] = control, _a)));
728
+ }, {});
729
+ return {
730
+ form: result,
731
+ _changedControls: __assign(__assign({}, (mergeChanges ? state._changedControls || {} : undefined)), _changedControls),
732
+ _removedControls: mergeChanges ? state._removedControls || {} : undefined
733
+ };
737
734
  };
738
735
 
739
- var markControlAsTouched = function (state, action, mergeChanges) {
740
- if (mergeChanges === void 0) { mergeChanges = false; }
741
- var form = state.form;
742
- var _a = action.payload, controlRef = _a.controlRef, _b = _a.markAll, markAll = _b === void 0 ? false : _b;
743
- controlRefCheck(controlRef);
744
- var controls = (markAll ? getControlBranch(controlRef, form) : getAncestorControls(controlRef, form)).reduce(function (acc, control) {
745
- var _a;
746
- return (__assign(__assign({}, acc), (_a = {}, _a[getFormKey(control.controlRef)] = __assign(__assign({}, control), { touched: true }), _a)));
747
- }, {});
748
- var result = {
749
- form: __assign(__assign({}, form), controls),
750
- action: action
751
- };
752
- var _changedControls = getControlBranch(controlRef, result.form).reduce(function (acc, control) {
753
- var _a;
754
- return (__assign(__assign({}, acc), (_a = {}, _a[control.key] = control, _a)));
755
- }, {});
756
- return {
757
- form: __assign(__assign({}, form), controls),
758
- _changedControls: __assign(__assign({}, (mergeChanges ? state._changedControls || {} : undefined)), _changedControls),
759
- _removedControls: mergeChanges ? state._removedControls || {} : undefined
760
- };
736
+ var markControlAsTouched = function (state, action, mergeChanges) {
737
+ if (mergeChanges === void 0) { mergeChanges = false; }
738
+ var form = state.form;
739
+ var _a = action.payload, controlRef = _a.controlRef, _b = _a.markAll, markAll = _b === void 0 ? false : _b;
740
+ controlRefCheck(controlRef);
741
+ var controls = (markAll ? getControlBranch(controlRef, form) : getAncestorControls(controlRef, form)).reduce(function (acc, control) {
742
+ var _a;
743
+ return (__assign(__assign({}, acc), (_a = {}, _a[getFormKey(control.controlRef)] = __assign(__assign({}, control), { touched: true }), _a)));
744
+ }, {});
745
+ var result = {
746
+ form: __assign(__assign({}, form), controls)};
747
+ var _changedControls = getControlBranch(controlRef, result.form).reduce(function (acc, control) {
748
+ var _a;
749
+ return (__assign(__assign({}, acc), (_a = {}, _a[control.key] = control, _a)));
750
+ }, {});
751
+ return {
752
+ form: __assign(__assign({}, form), controls),
753
+ _changedControls: __assign(__assign({}, (mergeChanges ? state._changedControls || {} : undefined)), _changedControls),
754
+ _removedControls: mergeChanges ? state._removedControls || {} : undefined
755
+ };
761
756
  };
762
757
 
763
- var markControlAsUntouched = function (state, action, mergeChanges) {
764
- var _a;
765
- if (mergeChanges === void 0) { mergeChanges = false; }
766
- var form = state.form;
767
- var controlRef = action.payload;
768
- controlRefCheck(controlRef);
769
- var result = getDescendantControls(controlRef, form).reduce(function (acc, control) {
770
- var _a;
771
- return (__assign(__assign({}, acc), (_a = {}, _a[getFormKey(control.controlRef)] = __assign(__assign({}, control), { touched: false }), _a)));
772
- }, {});
773
- result = __assign(__assign({}, form), result);
774
- // Update ancestors
775
- var currentRef = controlRef;
776
- var key;
777
- while (currentRef.length > 0) {
778
- currentRef = currentRef.slice(0, -1);
779
- key = getFormKey(currentRef);
780
- result = __assign(__assign({}, result), (_a = {}, _a[key] = __assign(__assign({}, result[key]), { touched: getDescendantControls(currentRef, result, true).some(function (control) { return control.touched; }) }), _a));
781
- }
782
- var _changedControls = getControlBranch(controlRef, result).reduce(function (acc, control) {
783
- var _a;
784
- return (__assign(__assign({}, acc), (_a = {}, _a[control.key] = control, _a)));
785
- }, {});
786
- return {
787
- form: result,
788
- _changedControls: __assign(__assign({}, (mergeChanges ? state._changedControls || {} : undefined)), _changedControls),
789
- _removedControls: mergeChanges ? state._removedControls || {} : undefined
790
- };
758
+ var markControlAsUntouched = function (state, action, mergeChanges) {
759
+ var _a;
760
+ if (mergeChanges === void 0) { mergeChanges = false; }
761
+ var form = state.form;
762
+ var controlRef = action.payload;
763
+ controlRefCheck(controlRef);
764
+ var result = getDescendantControls(controlRef, form).reduce(function (acc, control) {
765
+ var _a;
766
+ return (__assign(__assign({}, acc), (_a = {}, _a[getFormKey(control.controlRef)] = __assign(__assign({}, control), { touched: false }), _a)));
767
+ }, {});
768
+ result = __assign(__assign({}, form), result);
769
+ // Update ancestors
770
+ var currentRef = controlRef;
771
+ var key;
772
+ while (currentRef.length > 0) {
773
+ currentRef = currentRef.slice(0, -1);
774
+ key = getFormKey(currentRef);
775
+ result = __assign(__assign({}, result), (_a = {}, _a[key] = __assign(__assign({}, result[key]), { touched: getDescendantControls(currentRef, result, true).some(function (control) { return control.touched; }) }), _a));
776
+ }
777
+ var _changedControls = getControlBranch(controlRef, result).reduce(function (acc, control) {
778
+ var _a;
779
+ return (__assign(__assign({}, acc), (_a = {}, _a[control.key] = control, _a)));
780
+ }, {});
781
+ return {
782
+ form: result,
783
+ _changedControls: __assign(__assign({}, (mergeChanges ? state._changedControls || {} : undefined)), _changedControls),
784
+ _removedControls: mergeChanges ? state._removedControls || {} : undefined
785
+ };
791
786
  };
792
787
 
793
- var resetControl = function (state, action, providers, mergeChanges) {
794
- var _a;
795
- if (mergeChanges === void 0) { mergeChanges = false; }
796
- var form = state.form;
797
- var controlRef = action.payload;
798
- controlRefCheck(controlRef);
799
- var controlToReset = getControl(controlRef, form);
800
- var descendantsRemoved = __assign({}, form);
801
- var descendants = getDescendantControls(controlRef, form);
802
- var descendantKeys = descendants.map(function (_a) {
803
- var controlRef = _a.controlRef;
804
- return getFormKey(controlRef);
805
- });
806
- descendantKeys.forEach(function (key) {
807
- delete descendantsRemoved[key];
808
- });
809
- // Remove all descendants
810
- var restoredControls = buildState(controlToReset.config, descendantsRemoved, controlToReset.controlRef, providers);
811
- var restoredControlValue = getControl(controlRef, restoredControls).value;
812
- var result = updateAncestorValues(restoredControls, {
813
- type: UPDATE_ANCESTOR_VALUES,
814
- payload: { controlRef: controlRef, value: restoredControlValue }
815
- }, providers);
816
- var _changedControls = __assign(__assign({}, (mergeChanges ? state._changedControls || {} : undefined)), getControlBranch(controlRef, result).reduce(function (acc, control) {
817
- var _a;
818
- return (__assign(__assign({}, acc), (_a = {}, _a[control.key] = control, _a)));
819
- }, {}));
820
- var _removedControls = __assign(__assign({}, (mergeChanges ? state._removedControls || {} : undefined)), (_a = {}, _a[controlToReset.key] = controlToReset, _a));
821
- // If control is removed, we can delete it from the _changedControls check
822
- descendants
823
- .map(function (_a) {
824
- var key = _a.key;
825
- return key;
826
- })
827
- .forEach(function (key) {
828
- delete _changedControls[key];
829
- });
830
- return {
831
- form: result,
832
- _changedControls: _changedControls,
833
- _removedControls: _removedControls
834
- };
788
+ var resetControl = function (state, action, providers, mergeChanges) {
789
+ var _a;
790
+ if (mergeChanges === void 0) { mergeChanges = false; }
791
+ var form = state.form;
792
+ var controlRef = action.payload;
793
+ controlRefCheck(controlRef);
794
+ var controlToReset = getControl(controlRef, form);
795
+ var descendantsRemoved = __assign({}, form);
796
+ var descendants = getDescendantControls(controlRef, form);
797
+ var descendantKeys = descendants.map(function (_a) {
798
+ var controlRef = _a.controlRef;
799
+ return getFormKey(controlRef);
800
+ });
801
+ descendantKeys.forEach(function (key) {
802
+ delete descendantsRemoved[key];
803
+ });
804
+ // Remove all descendants
805
+ var restoredControls = buildState(controlToReset.config, descendantsRemoved, controlToReset.controlRef, providers);
806
+ var restoredControlValue = getControl(controlRef, restoredControls).value;
807
+ var result = updateAncestorValues(restoredControls, {
808
+ payload: { controlRef: controlRef, value: restoredControlValue }
809
+ }, providers);
810
+ var _changedControls = __assign(__assign({}, (mergeChanges ? state._changedControls || {} : undefined)), getControlBranch(controlRef, result).reduce(function (acc, control) {
811
+ var _a;
812
+ return (__assign(__assign({}, acc), (_a = {}, _a[control.key] = control, _a)));
813
+ }, {}));
814
+ var _removedControls = __assign(__assign({}, (mergeChanges ? state._removedControls || {} : undefined)), (_a = {}, _a[controlToReset.key] = controlToReset, _a));
815
+ // If control is removed, we can delete it from the _changedControls check
816
+ descendants
817
+ .map(function (_a) {
818
+ var key = _a.key;
819
+ return key;
820
+ })
821
+ .forEach(function (key) {
822
+ delete _changedControls[key];
823
+ });
824
+ return {
825
+ form: result,
826
+ _changedControls: _changedControls,
827
+ _removedControls: _removedControls
828
+ };
835
829
  };
836
830
 
837
- var asyncValidation = function (form, _a) {
838
- var controlRef = _a.payload.controlRef;
839
- var updatedSelfAndAncestors = getAncestorControls(controlRef, form).reduce(function (acc, control) {
840
- var _a;
841
- var isChangedControl = getFormKey(control.controlRef) === getFormKey(controlRef);
842
- return __assign(__assign({}, acc), (_a = {}, _a[getFormKey(control.controlRef)] = __assign(__assign({}, control), { pending: true, valid: false, asyncValidateInProgress: isChangedControl
843
- ? __assign({}, control.config.asyncValidators.reduce(function (acc, _, index) {
844
- var _a;
845
- return (__assign(__assign({}, acc), (_a = {}, _a[index] = true, _a)));
846
- }, {})) : control.asyncValidateInProgress }), _a));
847
- }, {});
848
- return __assign(__assign({}, form), updatedSelfAndAncestors);
831
+ var asyncValidation = function (form, _a) {
832
+ var controlRef = _a.payload;
833
+ var updatedSelfAndAncestors = getAncestorControls(controlRef, form).reduce(function (acc, control) {
834
+ var _a;
835
+ var isChangedControl = getFormKey(control.controlRef) === getFormKey(controlRef);
836
+ 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) {
838
+ var _a;
839
+ return (__assign(__assign({}, acc), (_a = {}, _a[index] = true, _a)));
840
+ }, {})) : control.asyncValidateInProgress }), _a));
841
+ }, {});
842
+ return __assign(__assign({}, form), updatedSelfAndAncestors);
849
843
  };
850
844
 
851
- var reverseObjectKeys = function (form) {
852
- return Object.keys(form)
853
- .reverse()
854
- .reduce(function (acc, key) {
855
- acc[key] = form[key];
856
- return acc;
857
- }, {});
845
+ var reverseObjectKeys = function (form) {
846
+ return Object.keys(form)
847
+ .reverse()
848
+ .reduce(function (acc, key) {
849
+ acc[key] = form[key];
850
+ return acc;
851
+ }, {});
858
852
  };
859
853
 
860
- var hasErrors$3 = function (errors) {
861
- return Object.values(errors).some(function (hasError) { return hasError; });
862
- };
863
- var mergeBranchErrors = function (form, controlRef) {
864
- var controlBranch = getControlBranch(controlRef, form).reduce(function (acc, ctrl) {
865
- var _a;
866
- return __assign(__assign({}, acc), (_a = {}, _a[getFormKey(ctrl.controlRef)] = ctrl, _a));
867
- }, {});
868
- var errorsMerged = Object.entries(controlBranch)
869
- .reverse()
870
- .reduce(function (acc, _a) {
871
- var _b;
872
- var key = _a[0], control = _a[1];
873
- var errors = __assign(__assign({}, control.validatorErrors), control.asyncValidatorErrors);
874
- var selfValid = !hasErrors$3(errors);
875
- var childrenValid = true;
876
- if (Array.isArray(control.config.controls)) {
877
- // If control is a FormArray
878
- childrenValid = control.value.every(function (item, index) {
879
- var formKey = getFormKey(control.controlRef.concat(index));
880
- var valid = acc[formKey] === undefined ? form[formKey].valid : acc[formKey].valid;
881
- return valid;
882
- });
883
- }
884
- else if (control.config.controls) {
885
- // If control is a FormGroup
886
- childrenValid = Object.keys(control.value).every(function (childKey) {
887
- var formKey = getFormKey(control.controlRef.concat(childKey));
888
- var valid = acc[formKey] === undefined ? form[formKey].valid : acc[formKey].valid;
889
- return valid;
890
- });
891
- }
892
- return __assign(__assign({}, acc), (_b = {}, _b[key] = __assign(__assign({}, control), { errors: errors, valid: selfValid && childrenValid && !control.pending, childrenValid: childrenValid }), _b));
893
- }, {});
894
- var errorsMergedOrderRestored = reverseObjectKeys(errorsMerged);
895
- return __assign(__assign({}, form), errorsMergedOrderRestored);
854
+ var hasErrors$3 = function (errors) {
855
+ return Object.values(errors).some(function (hasError) { return hasError; });
856
+ };
857
+ var mergeBranchErrors = function (form, controlRef) {
858
+ var controlBranch = getControlBranch(controlRef, form).reduce(function (acc, ctrl) {
859
+ var _a;
860
+ return __assign(__assign({}, acc), (_a = {}, _a[getFormKey(ctrl.controlRef)] = ctrl, _a));
861
+ }, {});
862
+ var errorsMerged = Object.entries(controlBranch)
863
+ .reverse()
864
+ .reduce(function (acc, _a) {
865
+ var _b;
866
+ var key = _a[0], control = _a[1];
867
+ var errors = __assign(__assign({}, control.validatorErrors), control.asyncValidatorErrors);
868
+ var selfValid = !hasErrors$3(errors);
869
+ var childrenValid = true;
870
+ if (Array.isArray(control.config.controls)) {
871
+ // If control is a FormArray
872
+ childrenValid = control.value.every(function (item, index) {
873
+ var formKey = getFormKey(control.controlRef.concat(index));
874
+ var valid = acc[formKey] === undefined ? form[formKey].valid : acc[formKey].valid;
875
+ return valid;
876
+ });
877
+ }
878
+ else if (control.config.controls) {
879
+ // If control is a FormGroup
880
+ childrenValid = Object.keys(control.value).every(function (childKey) {
881
+ var formKey = getFormKey(control.controlRef.concat(childKey));
882
+ var valid = acc[formKey] === undefined ? form[formKey].valid : acc[formKey].valid;
883
+ return valid;
884
+ });
885
+ }
886
+ return __assign(__assign({}, acc), (_b = {}, _b[key] = __assign(__assign({}, control), { errors: errors, valid: selfValid && childrenValid, childrenValid: childrenValid }), _b));
887
+ }, {});
888
+ var errorsMergedOrderRestored = reverseObjectKeys(errorsMerged);
889
+ return __assign(__assign({}, form), errorsMergedOrderRestored);
896
890
  };
897
891
 
898
- var isControlValidating = function (control) {
899
- if (!control.asyncValidateInProgress)
900
- return false;
901
- return Object.values(control.asyncValidateInProgress).some(function (pending) { return pending; });
902
- };
903
- var getControlByKey = function (key, form) {
904
- return Object.values(form).find(function (control) { return control.key === key; });
905
- };
906
- var asyncValidationResponseSuccess = function (form, _a) {
907
- var _b, _c;
908
- var _d = _a.payload, key = _d.key, validatorIndex = _d.validatorIndex, errors = _d.errors;
909
- var control = getControlByKey(key, form);
910
- if (!control) {
911
- return form;
912
- }
913
- var controlUpdated = __assign(__assign({}, form), (_b = {}, _b[getFormKey(control.controlRef)] = __assign(__assign({}, control), { asyncValidateInProgress: __assign(__assign({}, control.asyncValidateInProgress), (_c = {}, _c[validatorIndex] = false, _c)), asyncValidatorErrors: __assign(__assign({}, control.asyncValidatorErrors), errors) }), _b));
914
- var ancestors = getAncestorControls(control.controlRef, controlUpdated);
915
- var ancestorsUpdated = Object.entries(controlUpdated).reduce(function (acc, _a) {
916
- var _b, _c;
917
- var key = _a[0], control = _a[1];
918
- if (ancestors.includes(control)) {
919
- var descendants = getDescendantControls(control.controlRef, controlUpdated);
920
- var pending_1 = descendants.some(function (control) { return isControlValidating(control); });
921
- return __assign(__assign({}, acc), (_b = {}, _b[key] = __assign(__assign({}, control), { pending: pending_1 }), _b));
922
- }
923
- return __assign(__assign({}, acc), (_c = {}, _c[key] = control, _c));
924
- }, {});
925
- return mergeBranchErrors(ancestorsUpdated, control.controlRef);
892
+ var isControlValidating = function (control) {
893
+ if (!control.asyncValidateInProgress)
894
+ return false;
895
+ return Object.values(control.asyncValidateInProgress).some(function (pending) { return pending; });
896
+ };
897
+ var getControlByKey = function (key, form) {
898
+ return Object.values(form).find(function (control) { return control.key === key; });
899
+ };
900
+ var asyncValidationResponse = function (form, _a) {
901
+ var _b, _c;
902
+ var _d = _a.payload, key = _d.key, validatorIndex = _d.validatorIndex, errors = _d.errors;
903
+ var control = getControlByKey(key, form);
904
+ if (!control) {
905
+ return form;
906
+ }
907
+ var controlUpdated = __assign(__assign({}, form), (_b = {}, _b[getFormKey(control.controlRef)] = __assign(__assign({}, control), { asyncValidateInProgress: __assign(__assign({}, control.asyncValidateInProgress), (_c = {}, _c[validatorIndex] = false, _c)), asyncValidatorErrors: __assign(__assign({}, control.asyncValidatorErrors), errors) }), _b));
908
+ var ancestors = getAncestorControls(control.controlRef, controlUpdated);
909
+ var ancestorsUpdated = Object.entries(controlUpdated).reduce(function (acc, _a) {
910
+ var _b, _c;
911
+ var key = _a[0], control = _a[1];
912
+ if (ancestors.includes(control)) {
913
+ var descendants = getDescendantControls(control.controlRef, controlUpdated);
914
+ var pending_1 = descendants.some(function (control) { return isControlValidating(control); });
915
+ return __assign(__assign({}, acc), (_b = {}, _b[key] = __assign(__assign({}, control), { pending: pending_1 }), _b));
916
+ }
917
+ return __assign(__assign({}, acc), (_c = {}, _c[key] = control, _c));
918
+ }, {});
919
+ return mergeBranchErrors(ancestorsUpdated, control.controlRef);
926
920
  };
927
921
 
928
- var hasErrors$2 = function (errors) {
929
- return Object.values(errors).some(function (hasError) { return hasError; });
930
- };
931
- // TODO: update merge errors to not do the entire form but just a subset of it
932
- // TODO: separation of concern to update validity of control and ancestors
933
- var mergeErrors = function (form) {
934
- var errorsMerged = Object.entries(form)
935
- .reverse()
936
- .reduce(function (acc, _a) {
937
- var _b;
938
- var key = _a[0], control = _a[1];
939
- var errors = __assign(__assign({}, control.validatorErrors), control.asyncValidatorErrors);
940
- var selfValid = !hasErrors$2(errors);
941
- var childrenValid = true;
942
- if (Array.isArray(control.config.controls)) {
943
- // If control is a FormArray
944
- childrenValid = control.value.every(function (item, index) { return acc[getFormKey(control.controlRef.concat(index))].valid; });
945
- }
946
- else if (control.config.controls) {
947
- // If control is a FormGroup
948
- childrenValid = Object.keys(control.value).every(function (childKey) { return acc[getFormKey(control.controlRef.concat(childKey))].valid; });
949
- }
950
- return __assign(__assign({}, acc), (_b = {}, _b[key] = __assign(__assign({}, control), { errors: errors, valid: selfValid && childrenValid && !control.pending, childrenValid: childrenValid }), _b));
951
- }, {});
952
- var restoredOrder = reverseObjectKeys(errorsMerged);
953
- return restoredOrder;
922
+ var hasErrors$2 = function (errors) {
923
+ return Object.values(errors).some(function (hasError) { return hasError; });
924
+ };
925
+ // TODO: update merge errors to not do the entire form but just a subset of it
926
+ // TODO: separation of concern to update validity of control and ancestors
927
+ var mergeErrors = function (form) {
928
+ var errorsMerged = Object.entries(form)
929
+ .reverse()
930
+ .reduce(function (acc, _a) {
931
+ var _b;
932
+ var key = _a[0], control = _a[1];
933
+ var errors = __assign(__assign({}, control.validatorErrors), control.asyncValidatorErrors);
934
+ var selfValid = !hasErrors$2(errors);
935
+ var childrenValid = true;
936
+ if (Array.isArray(control.config.controls)) {
937
+ // If control is a FormArray
938
+ childrenValid = control.value.every(function (item, index) { return acc[getFormKey(control.controlRef.concat(index))].valid; });
939
+ }
940
+ else if (control.config.controls) {
941
+ // If control is a FormGroup
942
+ childrenValid = Object.keys(control.value).every(function (childKey) { return acc[getFormKey(control.controlRef.concat(childKey))].valid; });
943
+ }
944
+ return __assign(__assign({}, acc), (_b = {}, _b[key] = __assign(__assign({}, control), { errors: errors, valid: selfValid && childrenValid, childrenValid: childrenValid }), _b));
945
+ }, {});
946
+ var restoredOrder = reverseObjectKeys(errorsMerged);
947
+ return restoredOrder;
954
948
  };
955
949
 
956
- var hasErrors$1 = function (errors) {
957
- return Object.values(errors).some(function (hasError) { return hasError; });
958
- };
959
- var mergeRemoveControl = function (state, form, controlRef) {
960
- var parentRef = controlRef.slice(0, -1);
961
- var existingBranch = getControlBranch(parentRef, state);
962
- var updatedControlBranch = getControlBranch(parentRef, form)
963
- .reverse()
964
- .reduce(function (acc, baseControl) {
965
- var _a;
966
- var key = getFormKey(baseControl.controlRef);
967
- var existingControl = existingBranch.find(function (control) { return baseControl.key === control.key; }) ||
968
- structuredClone(DEFAULT_HUB2_FIELDS);
969
- var errors = __assign(__assign({}, baseControl.validatorErrors), existingControl.asyncValidatorErrors);
970
- var selfValid = !hasErrors$1(errors);
971
- var childrenValid = true;
972
- if (Array.isArray(baseControl.config.controls)) {
973
- // If control is a FormArray
974
- childrenValid = baseControl.value.every(function (item, index) {
975
- var formKey = getFormKey(baseControl.controlRef.concat(index));
976
- var valid = acc[formKey] === undefined ? state[formKey].valid : acc[formKey].valid;
977
- return valid;
978
- });
979
- }
980
- else if (baseControl.config.controls) {
981
- // If control is a FormGroup
982
- childrenValid = Object.keys(baseControl.value).every(function (childKey) {
983
- var formKey = getFormKey(baseControl.controlRef.concat(childKey));
984
- var valid = acc[formKey] === undefined ? state[formKey].valid : acc[formKey].valid;
985
- return valid;
986
- });
987
- }
988
- return __assign(__assign({}, acc), (_a = {}, _a[key] = __assign(__assign(__assign({}, existingControl), baseControl), { errors: errors, valid: selfValid && childrenValid && !existingControl.pending, childrenValid: childrenValid }), _a));
989
- }, {});
990
- var updatedControlBranchOrderRestored = reverseObjectKeys(updatedControlBranch);
991
- var descendants = existingBranch.filter(function (control) { return control.controlRef.length > parentRef.length; });
992
- var _removedControls = __assign({}, state);
993
- descendants.forEach(function (control) {
994
- delete _removedControls[getFormKey(control.controlRef)];
995
- });
996
- delete _removedControls[getFormKey(controlRef)];
997
- return __assign(__assign({}, _removedControls), updatedControlBranchOrderRestored);
950
+ var hasErrors$1 = function (errors) {
951
+ return Object.values(errors).some(function (hasError) { return hasError; });
952
+ };
953
+ var mergeRemoveControl = function (state, form, controlRef) {
954
+ var parentRef = controlRef.slice(0, -1);
955
+ var existingBranch = getControlBranch(parentRef, state);
956
+ var updatedControlBranch = getControlBranch(parentRef, form)
957
+ .reverse()
958
+ .reduce(function (acc, baseControl) {
959
+ var _a;
960
+ var key = getFormKey(baseControl.controlRef);
961
+ var existingControl = existingBranch.find(function (control) { return baseControl.key === control.key; }) ||
962
+ structuredClone(DEFAULT_HUB2_FIELDS);
963
+ var errors = __assign(__assign({}, baseControl.validatorErrors), existingControl.asyncValidatorErrors);
964
+ var selfValid = !hasErrors$1(errors);
965
+ var childrenValid = true;
966
+ if (Array.isArray(baseControl.config.controls)) {
967
+ // If control is a FormArray
968
+ childrenValid = baseControl.value.every(function (item, index) {
969
+ var formKey = getFormKey(baseControl.controlRef.concat(index));
970
+ var valid = acc[formKey] === undefined ? state[formKey].valid : acc[formKey].valid;
971
+ return valid;
972
+ });
973
+ }
974
+ else if (baseControl.config.controls) {
975
+ // If control is a FormGroup
976
+ childrenValid = Object.keys(baseControl.value).every(function (childKey) {
977
+ var formKey = getFormKey(baseControl.controlRef.concat(childKey));
978
+ var valid = acc[formKey] === undefined ? state[formKey].valid : acc[formKey].valid;
979
+ return valid;
980
+ });
981
+ }
982
+ return __assign(__assign({}, acc), (_a = {}, _a[key] = __assign(__assign(__assign({}, existingControl), baseControl), { errors: errors, valid: selfValid && childrenValid, childrenValid: childrenValid }), _a));
983
+ }, {});
984
+ var updatedControlBranchOrderRestored = reverseObjectKeys(updatedControlBranch);
985
+ var descendants = existingBranch.filter(function (control) { return control.controlRef.length > parentRef.length; });
986
+ var _removedControls = __assign({}, state);
987
+ descendants.forEach(function (control) {
988
+ delete _removedControls[getFormKey(control.controlRef)];
989
+ });
990
+ delete _removedControls[getFormKey(controlRef)];
991
+ return __assign(__assign({}, _removedControls), updatedControlBranchOrderRestored);
998
992
  };
999
993
 
1000
- var hasErrors = function (errors) {
1001
- return Object.values(errors).some(function (hasError) { return hasError; });
1002
- };
1003
- var mergeControls = function (state, _a) {
1004
- var form = _a.form, _b = _a._changedControls, _changedControls = _b === void 0 ? {} : _b, _removedControls = _a._removedControls;
1005
- var controlsRemoved = _removedControls
1006
- ? Object.values(_removedControls).reduce(function (acc, _a) {
1007
- var controlRef = _a.controlRef;
1008
- return mergeRemoveControl(acc, form, controlRef);
1009
- }, state)
1010
- : state;
1011
- var updatedBranch = Object.values(_changedControls)
1012
- .reverse()
1013
- .reduce(function (acc, control) {
1014
- var _a;
1015
- var formKey = getFormKey(control.controlRef);
1016
- var existingControl = controlsRemoved && controlsRemoved[formKey];
1017
- var newControl = __assign(__assign({}, (existingControl
1018
- ? existingControl
1019
- : structuredClone(DEFAULT_HUB2_FIELDS))), control);
1020
- var errors = __assign(__assign({}, newControl.validatorErrors), newControl.asyncValidatorErrors);
1021
- var selfValid = !hasErrors(errors);
1022
- var childrenValid = true;
1023
- if (Array.isArray(control.config.controls)) {
1024
- // If control is a FormArray
1025
- childrenValid = control.value.every(function (item, index) {
1026
- var formKey = getFormKey(control.controlRef.concat(index));
1027
- var valid = acc[formKey] === undefined ? controlsRemoved[formKey].valid : acc[formKey].valid;
1028
- return valid;
1029
- });
1030
- }
1031
- else if (control.config.controls) {
1032
- // If control is a FormGroup
1033
- childrenValid = Object.keys(control.value).every(function (childKey) {
1034
- var formKey = getFormKey(control.controlRef.concat(childKey));
1035
- var valid = acc[formKey] === undefined ? controlsRemoved[formKey].valid : acc[formKey].valid;
1036
- return valid;
1037
- });
1038
- }
1039
- return __assign(__assign({}, acc), (_a = {}, _a[formKey] = __assign(__assign({}, newControl), { errors: errors, valid: selfValid && childrenValid && !newControl.pending, childrenValid: childrenValid }), _a));
1040
- }, {});
1041
- var orderRestored = reverseObjectKeys(updatedBranch);
1042
- var result = __assign(__assign({}, controlsRemoved), orderRestored);
1043
- return result;
994
+ var hasErrors = function (errors) {
995
+ return Object.values(errors).some(function (hasError) { return hasError; });
996
+ };
997
+ var mergeControls = function (state, _a) {
998
+ var form = _a.form, _b = _a._changedControls, _changedControls = _b === void 0 ? {} : _b, _removedControls = _a._removedControls;
999
+ var controlsRemoved = _removedControls
1000
+ ? Object.values(_removedControls).reduce(function (acc, _a) {
1001
+ var controlRef = _a.controlRef;
1002
+ return mergeRemoveControl(acc, form, controlRef);
1003
+ }, state)
1004
+ : state;
1005
+ var updatedBranch = Object.values(_changedControls)
1006
+ .reverse()
1007
+ .reduce(function (acc, control) {
1008
+ var _a;
1009
+ var formKey = getFormKey(control.controlRef);
1010
+ var existingControl = controlsRemoved && controlsRemoved[formKey];
1011
+ var newControl = __assign(__assign({}, (existingControl
1012
+ ? existingControl
1013
+ : structuredClone(DEFAULT_HUB2_FIELDS))), control);
1014
+ var errors = __assign(__assign({}, newControl.validatorErrors), newControl.asyncValidatorErrors);
1015
+ var selfValid = !hasErrors(errors);
1016
+ var childrenValid = true;
1017
+ if (Array.isArray(control.config.controls)) {
1018
+ // If control is a FormArray
1019
+ childrenValid = control.value.every(function (item, index) {
1020
+ var formKey = getFormKey(control.controlRef.concat(index));
1021
+ var valid = acc[formKey] === undefined ? controlsRemoved[formKey].valid : acc[formKey].valid;
1022
+ return valid;
1023
+ });
1024
+ }
1025
+ else if (control.config.controls) {
1026
+ // If control is a FormGroup
1027
+ childrenValid = Object.keys(control.value).every(function (childKey) {
1028
+ var formKey = getFormKey(control.controlRef.concat(childKey));
1029
+ var valid = acc[formKey] === undefined ? controlsRemoved[formKey].valid : acc[formKey].valid;
1030
+ return valid;
1031
+ });
1032
+ }
1033
+ return __assign(__assign({}, acc), (_a = {}, _a[formKey] = __assign(__assign({}, newControl), { errors: errors, valid: selfValid && childrenValid, childrenValid: childrenValid }), _a));
1034
+ }, {});
1035
+ var orderRestored = reverseObjectKeys(updatedBranch);
1036
+ var result = __assign(__assign({}, controlsRemoved), orderRestored);
1037
+ return result;
1044
1038
  };
1045
1039
 
1046
- var formChange = function (state, _a) {
1047
- if (state === void 0) { state = null; }
1048
- var payload = _a.payload;
1049
- var form = payload.form;
1050
- if (state === null) {
1051
- return mergeErrors(Object.entries(form).reduce(function (acc, _a) {
1052
- var _b;
1053
- var dictKey = _a[0], baseControl = _a[1];
1054
- return __assign(__assign({}, acc), (_b = {}, _b[dictKey] = __assign(__assign({}, structuredClone(DEFAULT_HUB2_FIELDS)), baseControl), _b));
1055
- }, {}));
1056
- }
1057
- return mergeControls(state, payload);
1040
+ var formChange = function (state, _a) {
1041
+ if (state === void 0) { state = null; }
1042
+ var payload = _a.payload;
1043
+ var form = payload.form;
1044
+ if (state === null) {
1045
+ return mergeErrors(Object.entries(form).reduce(function (acc, _a) {
1046
+ var _b;
1047
+ var dictKey = _a[0], baseControl = _a[1];
1048
+ return __assign(__assign({}, acc), (_b = {}, _b[dictKey] = __assign(__assign({}, structuredClone(DEFAULT_HUB2_FIELDS)), baseControl), _b));
1049
+ }, {}));
1050
+ }
1051
+ return mergeControls(state, payload);
1058
1052
  };
1059
1053
 
1060
- var control = function (config) {
1061
- if (Array.isArray(config)) {
1062
- return config.reduce(function (acc, item, index) {
1063
- var _a;
1064
- var indexMap = {
1065
- 0: 'initialValue',
1066
- 1: 'validators',
1067
- 2: 'asyncValidators'
1068
- };
1069
- return __assign(__assign({}, acc), (_a = {}, _a[indexMap[index]] = index < 1 ? item : [].concat(item || []), _a));
1070
- }, {});
1071
- }
1072
- return config;
1073
- };
1074
- var array = function (config) { return config; };
1075
- var group = function (config) { return config; };
1076
- var reducerTools = function (providers) { return ({
1077
- updateValues: function (state, payload) {
1078
- return updateValues(state, { payload: payload }, providers, true);
1079
- },
1080
- removeControl: function (state, payload) {
1081
- return removeControl(state, { payload: payload }, providers, true);
1082
- },
1083
- pushControl: function (state, payload) {
1084
- return pushControl(state, { payload: payload }, providers, true);
1085
- },
1086
- addControl: function (state, payload) {
1087
- return addControl(state, { payload: payload }, providers, true);
1088
- },
1089
- resetControl: function (state, payload) {
1090
- return resetControl(state, { payload: payload }, providers, true);
1091
- },
1092
- markControlAsPristine: function (state, payload) {
1093
- return markControlAsPristine(state, { payload: payload }, true);
1094
- },
1095
- markControlAsTouched: function (state, payload) {
1096
- return markControlAsTouched(state, { payload: payload }, true);
1097
- },
1098
- markControlAsUntouched: function (state, payload) {
1099
- return markControlAsUntouched(state, { payload: payload }, true);
1100
- }
1101
- }); };
1102
- var build = function (config, options) {
1103
- var _a, _b, _c;
1104
- if (options === void 0) { options = {}; }
1105
- var providers = {
1106
- normalizers: __assign({}, (_a = options.providers) === null || _a === void 0 ? void 0 : _a.normalizers),
1107
- validators: __assign(__assign({}, Validators), (_b = options.providers) === null || _b === void 0 ? void 0 : _b.validators),
1108
- asyncValidators: __assign({}, (_c = options.providers) === null || _c === void 0 ? void 0 : _c.asyncValidators)
1109
- };
1110
- var initialState = buildFormState(config, undefined, undefined, providers);
1111
- return createReactable(initialState, options);
1112
- };
1113
- var load = function (state, options) {
1114
- if (options === void 0) { options = {}; }
1115
- var baseFormState = {
1116
- form: Object.entries(state).reduce(function (acc, _a) {
1117
- var _b;
1118
- var key = _a[0], control = _a[1];
1119
- return __assign(__assign({}, acc), (_b = {}, _b[key] = Object.entries(control)
1120
- .filter(function (_a) {
1121
- var key = _a[0];
1122
- return !Object.keys(DEFAULT_HUB2_FIELDS).includes(key);
1123
- })
1124
- .reduce(function (acc, _a) {
1125
- var _b;
1126
- var key = _a[0], value = _a[1];
1127
- return (__assign(__assign({}, acc), (_b = {}, _b[key] = value, _b)));
1128
- }, {}), _b));
1129
- }, {})
1130
- };
1131
- return createReactable(baseFormState, options);
1132
- };
1133
- var createReactable = function (initialBaseState, options, initialFormState) {
1134
- var _a, _b, _c;
1135
- if (options === void 0) { options = {}; }
1136
- var providers = {
1137
- normalizers: __assign({}, (_a = options.providers) === null || _a === void 0 ? void 0 : _a.normalizers),
1138
- validators: __assign(__assign({}, Validators), (_b = options.providers) === null || _b === void 0 ? void 0 : _b.validators),
1139
- asyncValidators: __assign({}, (_c = options.providers) === null || _c === void 0 ? void 0 : _c.asyncValidators)
1140
- };
1141
- var reducers = options.reducers, debug = options.debug, name = options.name, storeValue = options.storeValue, otherOptions = __rest(options, ["reducers", "debug", "name", "storeValue"]);
1142
- var customReducers = Object.entries(reducers || {}).reduce(function (acc, _a) {
1143
- var _b;
1144
- var key = _a[0], _case = _a[1];
1145
- var _reducer = typeof _case === 'function' ? _case : _case.reducer;
1146
- var effects = typeof _case === 'function' ? [] : _case.effects;
1147
- return __assign(__assign({}, acc), (_b = {}, _b[key] = {
1148
- reducer: function (_a, action) {
1149
- var form = _a.form;
1150
- return _reducer(reducerTools(providers), { form: form }, action);
1151
- },
1152
- effects: effects
1153
- }, _b));
1154
- }, {});
1155
- var _d = core.RxBuilder(__assign({ initialState: initialBaseState, name: "Stage 1 ".concat(name ? name : 'rxForm'), debug: debug, reducers: __assign({ updateValues: function (state, action) {
1156
- return updateValues(state, action, providers);
1157
- }, removeControl: function (state, action) { return removeControl(state, action, providers); }, addControl: function (state, action) {
1158
- return addControl(state, action, providers);
1159
- }, pushControl: function (state, action) {
1160
- return pushControl(state, action, providers);
1161
- }, resetControl: function (state, action) { return resetControl(state, action, providers); }, markControlAsPristine: function (state, action) {
1162
- return markControlAsPristine(state, action);
1163
- }, markControlAsTouched: function (state, action) {
1164
- return markControlAsTouched(state, action);
1165
- }, markControlAsUntouched: function (state, action) {
1166
- return markControlAsUntouched(state, action);
1167
- } }, customReducers) }, otherOptions)), hub1State$ = _d[0], hub1Actions = _d[1], hub1Actions$ = _d[2];
1168
- var state$ = core.RxBuilder({
1169
- sources: [buildHub2Source(hub1State$, initialBaseState).pipe(operators.skip(initialFormState ? 1 : 0))],
1170
- initialState: initialFormState || null,
1171
- name: "Stage 2 ".concat(name ? name : 'rxForm'),
1172
- debug: debug,
1173
- storeValue: storeValue,
1174
- reducers: {
1175
- formChange: formChange,
1176
- asyncValidation: {
1177
- reducer: asyncValidation,
1178
- effects: function (control) { return ({
1179
- key: control.key,
1180
- effects: getScopedEffectsForControl(control, providers)
1181
- }); }
1182
- },
1183
- asyncValidationResponseSuccess: asyncValidationResponseSuccess
1184
- }
1185
- })[0];
1186
- return [
1187
- state$.pipe(operators.filter(function (form) { return form !== null; })),
1188
- hub1Actions,
1189
- hub1Actions$,
1190
- ];
1054
+ var control = function (config) {
1055
+ if (Array.isArray(config)) {
1056
+ return config.reduce(function (acc, item, index) {
1057
+ var _a;
1058
+ var indexMap = {
1059
+ 0: 'initialValue',
1060
+ 1: 'validators',
1061
+ 2: 'asyncValidators'
1062
+ };
1063
+ return __assign(__assign({}, acc), (_a = {}, _a[indexMap[index]] = index < 1 ? item : [].concat(item || []), _a));
1064
+ }, {});
1065
+ }
1066
+ return config;
1067
+ };
1068
+ var array = function (config) { return config; };
1069
+ var group = function (config) { return config; };
1070
+ var reducerTools = function (providers) { return ({
1071
+ updateValues: function (state, payload) {
1072
+ return updateValues(state, { payload: payload }, providers, true);
1073
+ },
1074
+ removeControl: function (state, payload) {
1075
+ return removeControl(state, { payload: payload }, providers, true);
1076
+ },
1077
+ pushControl: function (state, payload) {
1078
+ return pushControl(state, { payload: payload }, providers, true);
1079
+ },
1080
+ addControl: function (state, payload) {
1081
+ return addControl(state, { payload: payload }, providers, true);
1082
+ },
1083
+ resetControl: function (state, payload) {
1084
+ return resetControl(state, { payload: payload }, providers, true);
1085
+ },
1086
+ markControlAsPristine: function (state, payload) {
1087
+ return markControlAsPristine(state, { payload: payload }, true);
1088
+ },
1089
+ markControlAsTouched: function (state, payload) {
1090
+ return markControlAsTouched(state, { payload: payload }, true);
1091
+ },
1092
+ markControlAsUntouched: function (state, payload) {
1093
+ return markControlAsUntouched(state, { payload: payload }, true);
1094
+ }
1095
+ }); };
1096
+ var build = function (config, options) {
1097
+ var _a, _b, _c;
1098
+ if (options === void 0) { options = {}; }
1099
+ var providers = {
1100
+ normalizers: __assign({}, (_a = options.providers) === null || _a === void 0 ? void 0 : _a.normalizers),
1101
+ validators: __assign(__assign({}, Validators), (_b = options.providers) === null || _b === void 0 ? void 0 : _b.validators),
1102
+ asyncValidators: __assign({}, (_c = options.providers) === null || _c === void 0 ? void 0 : _c.asyncValidators)
1103
+ };
1104
+ var initialState = buildFormState(config, undefined, undefined, providers);
1105
+ return createReactable(initialState, options);
1106
+ };
1107
+ var load = function (state, options) {
1108
+ if (options === void 0) { options = {}; }
1109
+ var baseFormState = {
1110
+ form: Object.entries(state).reduce(function (acc, _a) {
1111
+ var _b;
1112
+ var key = _a[0], control = _a[1];
1113
+ return __assign(__assign({}, acc), (_b = {}, _b[key] = Object.entries(control)
1114
+ .filter(function (_a) {
1115
+ var key = _a[0];
1116
+ return !Object.keys(DEFAULT_HUB2_FIELDS).includes(key);
1117
+ })
1118
+ .reduce(function (acc, _a) {
1119
+ var _b;
1120
+ var key = _a[0], value = _a[1];
1121
+ return (__assign(__assign({}, acc), (_b = {}, _b[key] = value, _b)));
1122
+ }, {}), _b));
1123
+ }, {})
1124
+ };
1125
+ return createReactable(baseFormState, options);
1126
+ };
1127
+ var createReactable = function (initialBaseState, options, initialFormState) {
1128
+ var _a, _b, _c;
1129
+ if (options === void 0) { options = {}; }
1130
+ var providers = {
1131
+ normalizers: __assign({}, (_a = options.providers) === null || _a === void 0 ? void 0 : _a.normalizers),
1132
+ validators: __assign(__assign({}, Validators), (_b = options.providers) === null || _b === void 0 ? void 0 : _b.validators),
1133
+ asyncValidators: __assign({}, (_c = options.providers) === null || _c === void 0 ? void 0 : _c.asyncValidators)
1134
+ };
1135
+ var reducers = options.reducers, debug = options.debug, name = options.name, otherOptions = __rest(options, ["reducers", "debug", "name"]);
1136
+ var customReducers = Object.entries(reducers || {}).reduce(function (acc, _a) {
1137
+ var _b;
1138
+ var key = _a[0], _case = _a[1];
1139
+ var _reducer = typeof _case === 'function' ? _case : _case.reducer;
1140
+ var effects = typeof _case === 'function' ? [] : _case.effects;
1141
+ return __assign(__assign({}, acc), (_b = {}, _b[key] = {
1142
+ reducer: function (_a, action) {
1143
+ var form = _a.form;
1144
+ return _reducer(reducerTools(providers), { form: form }, action);
1145
+ },
1146
+ effects: effects
1147
+ }, _b));
1148
+ }, {});
1149
+ var _d = RxBuilder(__assign({ initialState: initialBaseState, name: "Stage 1 ".concat(name ? name : 'rxForm'), debug: debug, reducers: __assign({ updateValues: function (state, action) {
1150
+ return updateValues(state, action, providers);
1151
+ }, removeControl: function (state, action) { return removeControl(state, action, providers); }, addControl: function (state, action) {
1152
+ return addControl(state, action, providers);
1153
+ }, pushControl: function (state, action) {
1154
+ return pushControl(state, action, providers);
1155
+ }, resetControl: function (state, action) { return resetControl(state, action, providers); }, markControlAsPristine: function (state, action) {
1156
+ return markControlAsPristine(state, action);
1157
+ }, markControlAsTouched: function (state, action) {
1158
+ return markControlAsTouched(state, action);
1159
+ }, markControlAsUntouched: function (state, action) {
1160
+ return markControlAsUntouched(state, action);
1161
+ } }, customReducers) }, otherOptions)), hub1State$ = _d[0], hub1Actions = _d[1], hub1Actions$ = _d[2];
1162
+ var _e = RxBuilder({
1163
+ sources: [buildHub2Source(hub1State$, initialBaseState).pipe(skip(0))],
1164
+ initialState: null,
1165
+ name: "Stage 2 ".concat(name ? name : 'rxForm'),
1166
+ debug: debug,
1167
+ reducers: {
1168
+ formChange: formChange,
1169
+ asyncValidationEffect: {
1170
+ reducer: function (state) { return state; },
1171
+ effects: function (control) { return ({
1172
+ key: control.key,
1173
+ effects: getScopedEffectsForControl(control, providers)
1174
+ }); }
1175
+ },
1176
+ asyncValidation: asyncValidation,
1177
+ asyncValidationResponse: asyncValidationResponse
1178
+ }
1179
+ }), state$ = _e[0], hub2Actions = _e[1];
1180
+ var destroy = function () {
1181
+ hub1Actions.destroy();
1182
+ hub2Actions.destroy();
1183
+ };
1184
+ return [
1185
+ state$.pipe(filter(function (form) { return form !== null; })),
1186
+ __assign(__assign({}, hub1Actions), { destroy: destroy }),
1187
+ hub1Actions$,
1188
+ ];
1191
1189
  };
1192
1190
 
1193
- var getArrayItems = function (controlRef, form) {
1194
- var control = getControl(controlRef, form);
1195
- if (!Array.isArray(control.config.controls)) {
1196
- throw "".concat(controlRef.join('.'), " is not a Form Array control");
1197
- }
1198
- var result = control.value.map(function (_, index) { return form[getFormKey(controlRef.concat(index))]; });
1199
- return result;
1191
+ var getArrayItems = function (controlRef, form) {
1192
+ var control = getControl(controlRef, form);
1193
+ if (!Array.isArray(control.config.controls)) {
1194
+ throw "".concat(controlRef.join('.'), " is not a Form Array control");
1195
+ }
1196
+ var result = control.value.map(function (_, index) { return form[getFormKey(controlRef.concat(index))]; });
1197
+ return result;
1200
1198
  };
1201
1199
 
1202
- exports.ControlModels = Controls;
1203
- exports.Validators = index;
1204
- exports.array = array;
1205
- exports.build = build;
1206
- exports.control = control;
1207
- exports.getAncestorControls = getAncestorControls;
1208
- exports.getArrayItems = getArrayItems;
1209
- exports.getDescendantControls = getDescendantControls;
1210
- exports.getValueFromControlConfig = getValueFromControlConfig;
1211
- exports.group = group;
1212
- exports.load = load;
1200
+ export { Controls as ControlModels, index as Validators, array, build, control, getAncestorControls, getArrayItems, getDescendantControls, getValueFromControlConfig, group, load };
1201
+ //# sourceMappingURL=index.js.map