@reactables/forms 0.4.0-alpha.0

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 (63) hide show
  1. package/README.md +15 -0
  2. package/dist/Helpers/FormBuilder.d.ts +10 -0
  3. package/dist/Helpers/addAsyncValidationEffects.d.ts +5 -0
  4. package/dist/Helpers/buildFormState.d.ts +5 -0
  5. package/dist/Helpers/buildHub2Source.d.ts +4 -0
  6. package/dist/Helpers/generateKey.d.ts +1 -0
  7. package/dist/Helpers/getAncestorControls.d.ts +3 -0
  8. package/dist/Helpers/getAncestorControls.test.d.ts +1 -0
  9. package/dist/Helpers/getArrayItems.d.ts +3 -0
  10. package/dist/Helpers/getArrayItems.test.d.ts +1 -0
  11. package/dist/Helpers/getControl.d.ts +3 -0
  12. package/dist/Helpers/getControlBranch.d.ts +3 -0
  13. package/dist/Helpers/getControlBranch.test.d.ts +1 -0
  14. package/dist/Helpers/getDescendantControls.d.ts +3 -0
  15. package/dist/Helpers/getDescendantControls.test.d.ts +1 -0
  16. package/dist/Helpers/getFormKey.d.ts +2 -0
  17. package/dist/Helpers/getValueFromControlConfig.d.ts +2 -0
  18. package/dist/Helpers/getValueFromControlConfig.test.d.ts +1 -0
  19. package/dist/Helpers/index.d.ts +1 -0
  20. package/dist/Helpers/isChildRef.d.ts +2 -0
  21. package/dist/Models/Configs.d.ts +22 -0
  22. package/dist/Models/ControlRef.d.ts +1 -0
  23. package/dist/Models/Controls.d.ts +44 -0
  24. package/dist/Models/FormErrors.d.ts +3 -0
  25. package/dist/Models/Payloads.d.ts +20 -0
  26. package/dist/Models/Validators.d.ts +5 -0
  27. package/dist/Models/index.d.ts +6 -0
  28. package/dist/Reducers/Hub1/addControl.d.ts +4 -0
  29. package/dist/Reducers/Hub1/index.d.ts +9 -0
  30. package/dist/Reducers/Hub1/markControlAsPristine.d.ts +4 -0
  31. package/dist/Reducers/Hub1/markControlAsTouched.d.ts +4 -0
  32. package/dist/Reducers/Hub1/markControlAsUntouched.d.ts +4 -0
  33. package/dist/Reducers/Hub1/removeControl.d.ts +4 -0
  34. package/dist/Reducers/Hub1/resetControl.d.ts +4 -0
  35. package/dist/Reducers/Hub1/syncValidate.d.ts +2 -0
  36. package/dist/Reducers/Hub1/syncValidate.test.d.ts +1 -0
  37. package/dist/Reducers/Hub1/updateAncestorPristineValues.d.ts +5 -0
  38. package/dist/Reducers/Hub1/updateAncestorValues.d.ts +5 -0
  39. package/dist/Reducers/Hub1/updateDirty.d.ts +2 -0
  40. package/dist/Reducers/Hub1/updateDirty.test.d.ts +1 -0
  41. package/dist/Reducers/Hub1/updateValues.d.ts +4 -0
  42. package/dist/Reducers/Hub2/asyncValidation.d.ts +4 -0
  43. package/dist/Reducers/Hub2/asyncValidationResponseSuccess.d.ts +4 -0
  44. package/dist/Reducers/Hub2/formChange.d.ts +4 -0
  45. package/dist/Reducers/Hub2/hub2Reducer.d.ts +3 -0
  46. package/dist/Reducers/Hub2/index.d.ts +3 -0
  47. package/dist/Reducers/Hub2/mergeErrors.d.ts +2 -0
  48. package/dist/Reducers/index.d.ts +2 -0
  49. package/dist/RxForm/RxForm.d.ts +23 -0
  50. package/dist/RxForm/RxForm.test.d.ts +1 -0
  51. package/dist/RxForm/index.d.ts +1 -0
  52. package/dist/Testing/AsyncValidators.d.ts +6 -0
  53. package/dist/Testing/Effects.d.ts +10 -0
  54. package/dist/Testing/Models/Contact.d.ts +11 -0
  55. package/dist/Testing/Models/DoctorInfo.d.ts +6 -0
  56. package/dist/Testing/Models/EmergencyContact.d.ts +6 -0
  57. package/dist/Testing/asyncConfig.d.ts +11 -0
  58. package/dist/Testing/config.d.ts +11 -0
  59. package/dist/Validators/Validators.d.ts +4 -0
  60. package/dist/Validators/index.d.ts +1 -0
  61. package/dist/index.d.ts +4 -0
  62. package/dist/index.js +763 -0
  63. package/package.json +25 -0
package/dist/index.js ADDED
@@ -0,0 +1,763 @@
1
+ 'use strict';
2
+
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 Controls = /*#__PURE__*/Object.freeze({
15
+ __proto__: null
16
+ });
17
+
18
+ var required = function (value) {
19
+ if (Array.isArray(value)) {
20
+ return { required: !Boolean(value.length) };
21
+ }
22
+ if (typeof value === 'string' || typeof value === 'object') {
23
+ return { required: !Boolean(value) };
24
+ }
25
+ if (typeof value === 'number') {
26
+ return { required: !(value !== undefined && value !== null) };
27
+ }
28
+ return { required: false };
29
+ };
30
+ var email = function (value) {
31
+ return value && !/^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/g.test(value) ? { email: true } : { email: false };
32
+ };
33
+ var phoneNumber = function (value) {
34
+ return value && !/^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$/.test(value)
35
+ ? { phoneNumber: true }
36
+ : { phoneNumber: false };
37
+ };
38
+
39
+ var index = /*#__PURE__*/Object.freeze({
40
+ __proto__: null,
41
+ required: required,
42
+ email: email,
43
+ phoneNumber: phoneNumber
44
+ });
45
+
46
+ /******************************************************************************
47
+ Copyright (c) Microsoft Corporation.
48
+
49
+ Permission to use, copy, modify, and/or distribute this software for any
50
+ purpose with or without fee is hereby granted.
51
+
52
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
53
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
54
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
55
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
56
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
57
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
58
+ PERFORMANCE OF THIS SOFTWARE.
59
+ ***************************************************************************** */
60
+
61
+ var __assign = function() {
62
+ __assign = Object.assign || function __assign(t) {
63
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
64
+ s = arguments[i];
65
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
66
+ }
67
+ return t;
68
+ };
69
+ return __assign.apply(this, arguments);
70
+ };
71
+
72
+ function __spreadArray(to, from, pack) {
73
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
74
+ if (ar || !(i in from)) {
75
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
76
+ ar[i] = from[i];
77
+ }
78
+ }
79
+ return to.concat(ar || Array.prototype.slice.call(from));
80
+ }
81
+
82
+ typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
83
+ var e = new Error(message);
84
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
85
+ };
86
+
87
+ var getValueFromControlConfig = function (controlConfig) {
88
+ var controls = controlConfig.controls;
89
+ if (controls && !(controls instanceof Array)) {
90
+ var result = {};
91
+ for (var key in controlConfig.controls) {
92
+ var control = controlConfig.controls[key];
93
+ result[key] = getValueFromControlConfig(control);
94
+ }
95
+ return result;
96
+ }
97
+ else if (controls && controls instanceof Array) {
98
+ var configs = controlConfig.controls;
99
+ var result = configs
100
+ ? configs.map(function (controlConfig) { return getValueFromControlConfig(controlConfig); })
101
+ : [];
102
+ return result;
103
+ }
104
+ else {
105
+ return controlConfig.initialValue;
106
+ }
107
+ };
108
+
109
+ var getFormKey = function (controlRef) {
110
+ return controlRef.length ? controlRef.join('.') : 'root';
111
+ };
112
+
113
+ var generateKey = function (length) {
114
+ var result = '';
115
+ var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
116
+ var charactersLength = characters.length;
117
+ var counter = 0;
118
+ while (counter < length) {
119
+ result += characters.charAt(Math.floor(Math.random() * charactersLength));
120
+ counter += 1;
121
+ }
122
+ return result;
123
+ };
124
+
125
+ // Includes the original control of interest unless excludeSelf === true
126
+ var getDescendantControls = function (controlRef, form, excludeSelf) {
127
+ if (excludeSelf === void 0) { excludeSelf = false; }
128
+ var result = Object.entries(form)
129
+ .filter(function (_a) {
130
+ var key = _a[0];
131
+ if (!controlRef.length)
132
+ return true;
133
+ var childRef = key.split('.');
134
+ return controlRef.every(function (refKey, index) {
135
+ return refKey == childRef[index];
136
+ });
137
+ })
138
+ .map(function (entry) { return entry[1]; });
139
+ return result.filter(function (control) {
140
+ return excludeSelf ? getFormKey(control.controlRef) !== getFormKey(controlRef) : true;
141
+ });
142
+ };
143
+
144
+ var syncValidate = function (form) {
145
+ // First check each control for its own validation
146
+ var baseValidation = Object.entries(form).reduce(function (acc, _a) {
147
+ var _b;
148
+ var _c;
149
+ var key = _a[0], control = _a[1];
150
+ var validatorErrors = ((_c = control.config.validators) === null || _c === void 0 ? void 0 : _c.reduce(function (acc, validator) { return (__assign(__assign({}, acc), validator(control.value))); }, {})) || {};
151
+ var validatorsValid = !Object.values(validatorErrors).some(function (err) { return err; });
152
+ return __assign(__assign({}, acc), (_b = {}, _b[key] = __assign(__assign({}, control), { validatorErrors: validatorErrors, validatorsValid: validatorsValid }), _b));
153
+ }, {});
154
+ // Then check each controls children to see if there are errors
155
+ var validated = Object.entries(baseValidation).reduce(function (acc, _a) {
156
+ var _b;
157
+ var key = _a[0], control = _a[1];
158
+ var validatorsValid = getDescendantControls(control.controlRef, baseValidation).every(function (_a) {
159
+ var validatorsValid = _a.validatorsValid;
160
+ return validatorsValid;
161
+ });
162
+ return __assign(__assign({}, acc), (_b = {}, _b[key] = __assign(__assign({}, control), { validatorsValid: validatorsValid }), _b));
163
+ }, {});
164
+ return validated;
165
+ };
166
+
167
+ var buildState = function (config, form, controlRef) {
168
+ var _a;
169
+ if (form === void 0) { form = { root: null }; }
170
+ if (controlRef === void 0) { controlRef = []; }
171
+ var value = getValueFromControlConfig(config);
172
+ var control = {
173
+ pristineValue: value,
174
+ dirty: false,
175
+ touched: false,
176
+ value: value,
177
+ controlRef: controlRef,
178
+ validatorErrors: {},
179
+ validatorsValid: null,
180
+ config: config,
181
+ key: generateKey(5)
182
+ };
183
+ var newForm = __assign(__assign({}, form), (_a = {}, _a[getFormKey(controlRef)] = control, _a));
184
+ var controls = config.controls;
185
+ // Adding controls for Form Group
186
+ if (controls && !(controls instanceof Array)) {
187
+ newForm = Object.entries(config.controls).reduce(function (acc, _a) {
188
+ var key = _a[0], controlConfig = _a[1];
189
+ return buildState(controlConfig, acc, controlRef.concat(key));
190
+ }, newForm);
191
+ }
192
+ else if (controls && controls instanceof Array) {
193
+ // Adding controls for Form Array
194
+ newForm = config.controls.reduce(function (acc, controlConfig, index) { return buildState(controlConfig, acc, controlRef.concat(index)); }, newForm);
195
+ }
196
+ return syncValidate(newForm);
197
+ };
198
+ var buildFormState = function (config, form, controlRef) {
199
+ if (form === void 0) { form = { root: null }; }
200
+ if (controlRef === void 0) { controlRef = []; }
201
+ return {
202
+ form: buildState(config, form, controlRef),
203
+ action: null
204
+ };
205
+ };
206
+
207
+ var getControl = function (controlRef, form) {
208
+ return form[getFormKey(controlRef)];
209
+ };
210
+
211
+ // Includes the original control of interest unless excludeSelf === true
212
+ var getAncestorControls = function (controlRef, form, excludeSelf) {
213
+ if (excludeSelf === void 0) { excludeSelf = false; }
214
+ var formControls = controlRef.reduce(function (acc, key) {
215
+ var currentRef = acc.currentRef.concat(key);
216
+ var formControls = acc.formControls.concat(getControl(currentRef, form));
217
+ return {
218
+ currentRef: currentRef,
219
+ formControls: formControls
220
+ };
221
+ }, {
222
+ currentRef: [],
223
+ formControls: []
224
+ }).formControls;
225
+ var root = form['root'];
226
+ var result = [root].concat(formControls);
227
+ return result.filter(function (control) {
228
+ return excludeSelf ? getFormKey(control.controlRef) !== getFormKey(controlRef) : true;
229
+ });
230
+ };
231
+
232
+ var getControlBranch = function (controlRef, form) {
233
+ var ancestors = getAncestorControls(controlRef, form);
234
+ var childControls = getDescendantControls(controlRef, form).slice(1);
235
+ return ancestors.concat(childControls);
236
+ };
237
+
238
+ var getScopedEffectsForControl = function (formControl) {
239
+ var config = formControl.config, key = formControl.key;
240
+ var asyncValidators = config.asyncValidators;
241
+ var scopedEffects = [];
242
+ if (asyncValidators && asyncValidators.length) {
243
+ scopedEffects = asyncValidators.reduce(function (acc, validator, validatorIndex) {
244
+ var effect = function (actions$) {
245
+ return actions$.pipe(operators.map(function (_a) {
246
+ var control = _a.payload;
247
+ return control;
248
+ }), validator, operators.map(function (errors) { return ({
249
+ type: 'asyncValidationResponseSuccess',
250
+ payload: {
251
+ key: key,
252
+ errors: errors,
253
+ validatorIndex: validatorIndex
254
+ }
255
+ }); }));
256
+ };
257
+ return acc.concat(effect);
258
+ }, []);
259
+ }
260
+ return scopedEffects;
261
+ };
262
+ var getAsyncValidationActions = function (formControls) {
263
+ return formControls.reduce(function (acc, control) {
264
+ var effects = getScopedEffectsForControl(control);
265
+ if (!effects.length)
266
+ return acc;
267
+ var action = { type: 'asyncValidation', payload: control };
268
+ return acc.concat(action);
269
+ }, []);
270
+ };
271
+
272
+ var buildHub2Source = function (rx) {
273
+ var hub1StateMapped$ = rx.state$.pipe(operators.map(function (payload) { return ({ type: 'formChange', payload: payload }); }));
274
+ var sourceForHub2$ = hub1StateMapped$.pipe(operators.mergeMap(function (formChangeAction) {
275
+ var _a = formChangeAction.payload, form = _a.form, action = _a.action;
276
+ var newForm = form;
277
+ var controlsToCheck;
278
+ switch (action === null || action === void 0 ? void 0 : action.type) {
279
+ case 'updateValues':
280
+ controlsToCheck = getControlBranch(action.payload.controlRef, newForm);
281
+ break;
282
+ case 'addControl':
283
+ var changedControl = getControl(action.payload.controlRef, newForm);
284
+ if (Array.isArray(changedControl.config.controls)) {
285
+ var index = changedControl.value.length - 1;
286
+ var ref = action.payload.controlRef.concat(index);
287
+ controlsToCheck = getControlBranch(ref, newForm);
288
+ break;
289
+ }
290
+ else {
291
+ controlsToCheck = getControlBranch(action.payload.controlRef, newForm);
292
+ break;
293
+ }
294
+ case 'removeControl':
295
+ controlsToCheck = getAncestorControls(action.payload.slice(0, -1), newForm);
296
+ break;
297
+ case 'resetControl':
298
+ controlsToCheck = getControlBranch(action.payload, newForm);
299
+ break;
300
+ default:
301
+ controlsToCheck = [];
302
+ }
303
+ var asyncValidationActions = getAsyncValidationActions(controlsToCheck);
304
+ return rxjs.of.apply(void 0, __spreadArray([formChangeAction], asyncValidationActions, false));
305
+ }));
306
+ return sourceForHub2$;
307
+ };
308
+
309
+ var UPDATE_ANCESTOR_VALUES = 'UPDATE_ANCESTOR_VALUES';
310
+ var updateAncestorValues = function (form, _a) {
311
+ var _b;
312
+ var controlRef = _a.payload;
313
+ if (controlRef.length) {
314
+ var parentRef = controlRef.slice(0, -1);
315
+ var parentKey = getFormKey(parentRef);
316
+ var siblingControls = getDescendantControls(parentRef, form).filter(
317
+ // Out of descendants we only need the siblings
318
+ function (control) { return control.controlRef.length === controlRef.length; });
319
+ var newValue = void 0;
320
+ // If parent is a Form Array
321
+ if (Array.isArray(form[parentKey].value)) {
322
+ newValue = siblingControls
323
+ .slice()
324
+ .sort(function (a, b) { return a.controlRef.at(-1) - b.controlRef.at(-1); })
325
+ .map(function (control) { return control.value; });
326
+ }
327
+ else {
328
+ // If parent is a Form Group
329
+ newValue = siblingControls.reduce(function (acc, _a) {
330
+ var _b;
331
+ var controlRef = _a.controlRef, value = _a.value;
332
+ return __assign(__assign({}, acc), (_b = {}, _b[controlRef.at(-1)] = value, _b));
333
+ }, {});
334
+ }
335
+ var newParentControl = __assign(__assign({}, form[parentKey]), { value: newValue });
336
+ return updateAncestorValues(__assign(__assign({}, form), (_b = {}, _b[parentKey] = newParentControl, _b)), {
337
+ type: UPDATE_ANCESTOR_VALUES,
338
+ payload: parentRef
339
+ });
340
+ }
341
+ return form;
342
+ };
343
+
344
+ var updateDirty = function (form) {
345
+ return Object.entries(form).reduce(function (acc, _a) {
346
+ var _b;
347
+ var key = _a[0], control = _a[1];
348
+ return __assign(__assign({}, acc), (_b = {}, _b[key] = __assign(__assign({}, control), { dirty: !isEqual__default["default"](control.value, control.pristineValue) }), _b));
349
+ }, {});
350
+ };
351
+
352
+ var isChildRef = function (controlRef, parentRef) {
353
+ return (parentRef.every(function (key, index) { return controlRef[index] === key; }) &&
354
+ controlRef.length === parentRef.length + 1);
355
+ };
356
+
357
+ var UPDATE_DESCENDANT_VALUES = 'UPDATE_DESCENDANT_VALUES';
358
+ var updateDescendants = function (form, _a) {
359
+ var _b = _a.payload, controlRef = _b.controlRef, value = _b.value;
360
+ var result = Object.entries(form).reduce(function (acc, _a) {
361
+ var _b;
362
+ var key = _a[0], control = _a[1];
363
+ if (isChildRef(control.controlRef, controlRef)) {
364
+ var childValue = value[control.controlRef.at(-1)];
365
+ acc = __assign(__assign({}, acc), (_b = {}, _b[key] = __assign(__assign({}, control), { value: childValue }), _b));
366
+ var configControls = control.config.controls;
367
+ if (configControls) {
368
+ acc = updateDescendants(acc, {
369
+ type: UPDATE_DESCENDANT_VALUES,
370
+ payload: { controlRef: control.controlRef, value: childValue }
371
+ });
372
+ }
373
+ }
374
+ return acc;
375
+ }, form);
376
+ return result;
377
+ };
378
+ // Will only update child controls that are present.
379
+ // Use AddControl/RemoveControl action reducers to add/remove control
380
+ var updateValues = function (_a, action) {
381
+ var _b;
382
+ var form = _a.form;
383
+ var _c = action.payload, controlRef = _c.controlRef, value = _c.value;
384
+ // Update its own value
385
+ var ctrlKey = getFormKey(controlRef);
386
+ var result = __assign(__assign({}, form), (_b = {}, _b[ctrlKey] = __assign(__assign({}, form[ctrlKey]), { value: value }), _b));
387
+ var configControls = form[ctrlKey].config.controls;
388
+ // Update its children
389
+ if (configControls) {
390
+ result = updateDescendants(result, {
391
+ type: UPDATE_DESCENDANT_VALUES,
392
+ payload: {
393
+ controlRef: controlRef,
394
+ value: value
395
+ }
396
+ });
397
+ }
398
+ // Update its Ancestors
399
+ if (controlRef.length) {
400
+ result = updateAncestorValues(result, {
401
+ type: UPDATE_ANCESTOR_VALUES,
402
+ payload: controlRef
403
+ });
404
+ }
405
+ return { form: syncValidate(updateDirty(result)), action: action };
406
+ };
407
+
408
+ var removeControl = function (_a, action) {
409
+ var form = _a.form;
410
+ var controlRef = action.payload;
411
+ if (!getControl(controlRef, form)) {
412
+ throw 'Control not found';
413
+ }
414
+ // Can't remove the root of the form
415
+ if (!controlRef.length)
416
+ return { form: form, action: action };
417
+ var parentRef = controlRef.slice(0, -1);
418
+ var parentIsFormArray = Array.isArray(getControl(parentRef, form).config.controls);
419
+ // Remove control and all descendants
420
+ var controlRemoved = Object.entries(form)
421
+ .filter(function (_a) {
422
+ _a[0]; var control = _a[1];
423
+ return !(control.controlRef.length > parentRef.length &&
424
+ controlRef.every(function (key, index) { return key === control.controlRef[index]; }));
425
+ })
426
+ .reduce(function (acc, _a) {
427
+ var _b, _c;
428
+ var key = _a[0], control = _a[1];
429
+ // May need to reindex array items of removed control
430
+ // if it was part of a Form Array.
431
+ if (parentIsFormArray) {
432
+ var oldIndex = control.controlRef.at(parentRef.length);
433
+ if (
434
+ // If control is descendant.
435
+ parentRef.every(function (ref, index) { return control.controlRef[index] === ref; }) &&
436
+ control.controlRef.length > parentRef.length &&
437
+ // If the array item index was greater than the index of item removed
438
+ // we need to decrement its index by 1.
439
+ oldIndex > controlRef.at(-1)) {
440
+ var newRef = parentRef
441
+ .concat(oldIndex - 1)
442
+ .concat(control.controlRef.slice(parentRef.length + 1));
443
+ return __assign(__assign({}, acc), (_b = {}, _b[getFormKey(newRef)] = __assign(__assign({}, control), { controlRef: newRef }), _b));
444
+ }
445
+ }
446
+ return __assign(__assign({}, acc), (_c = {}, _c[key] = control, _c));
447
+ }, {});
448
+ return {
449
+ form: syncValidate(updateDirty(updateAncestorValues(controlRemoved, {
450
+ type: UPDATE_ANCESTOR_VALUES,
451
+ payload: controlRef
452
+ }))),
453
+ action: action
454
+ };
455
+ };
456
+
457
+ var addControl = function (state, action) {
458
+ var newControlRef;
459
+ var _a = action.payload, config = _a.config, controlRef = _a.controlRef;
460
+ var existingControl = getControl(controlRef, state.form);
461
+ // If controlRef exists we are adding control to a Form Array
462
+ if (existingControl && Array.isArray(existingControl.config.controls)) {
463
+ newControlRef = controlRef.concat(existingControl.value.length);
464
+ }
465
+ else {
466
+ // If controlRef does not exist we are adding control to a Form Group
467
+ if (!getControl(controlRef.slice(0, -1), state.form)) {
468
+ throw 'You are attempting to add a control to a non-existent form group';
469
+ }
470
+ newControlRef = controlRef;
471
+ }
472
+ var newForm = buildState(config, state.form, newControlRef);
473
+ var ancestorsUpdated = updateAncestorValues(newForm, {
474
+ type: UPDATE_ANCESTOR_VALUES,
475
+ payload: newControlRef
476
+ });
477
+ return { form: syncValidate(updateDirty(ancestorsUpdated)), action: action };
478
+ };
479
+
480
+ // Same implementation as updateAncestor values except updating pristine values
481
+ var UPDATE_ANCESTOR_PRISTINE_VALUES = 'UPDATE_ANCESTOR_PRISTINE_VALUES';
482
+ var updateAncestorPristineValues = function (form, _a) {
483
+ var _b;
484
+ var controlRef = _a.payload;
485
+ if (controlRef.length) {
486
+ var parentRef = controlRef.slice(0, -1);
487
+ var parentKey = getFormKey(parentRef);
488
+ var siblingControls = getDescendantControls(parentRef, form).filter(
489
+ // Out of descendants we only need the siblings
490
+ function (control) { return control.controlRef.length === controlRef.length; });
491
+ var newValue = void 0;
492
+ // If parent is a Form Array
493
+ if (Array.isArray(form[parentKey].value)) {
494
+ newValue = siblingControls
495
+ .slice()
496
+ .sort(function (a, b) { return a.controlRef.at(-1) - b.controlRef.at(-1); })
497
+ .map(function (control) { return control.pristineValue; });
498
+ }
499
+ else {
500
+ // If parent is a Form Group
501
+ newValue = siblingControls.reduce(function (acc, _a) {
502
+ var _b;
503
+ var controlRef = _a.controlRef, pristineValue = _a.pristineValue;
504
+ return __assign(__assign({}, acc), (_b = {}, _b[controlRef.at(-1)] = pristineValue, _b));
505
+ }, {});
506
+ }
507
+ var newParentControl = __assign(__assign({}, form[parentKey]), { pristineValue: newValue });
508
+ return updateAncestorPristineValues(__assign(__assign({}, form), (_b = {}, _b[parentKey] = newParentControl, _b)), {
509
+ type: UPDATE_ANCESTOR_PRISTINE_VALUES,
510
+ payload: parentRef
511
+ });
512
+ }
513
+ return form;
514
+ };
515
+
516
+ var markControlAsPristine = function (_a, action) {
517
+ var form = _a.form;
518
+ var controlRef = action.payload;
519
+ var descendants = getDescendantControls(controlRef, form);
520
+ var result = Object.entries(form).reduce(function (acc, _a) {
521
+ var _b;
522
+ var key = _a[0], control = _a[1];
523
+ var isDescendant = descendants.includes(control);
524
+ return __assign(__assign({}, acc), (_b = {}, _b[key] = isDescendant
525
+ ? __assign(__assign({}, control), { pristineValue: control.value, dirty: false }) : control, _b));
526
+ }, {});
527
+ if (controlRef.length) {
528
+ result = updateAncestorPristineValues(result, {
529
+ type: UPDATE_ANCESTOR_PRISTINE_VALUES,
530
+ payload: controlRef
531
+ });
532
+ }
533
+ return { form: updateDirty(result), action: action };
534
+ };
535
+
536
+ var markControlAsTouched = function (_a, action) {
537
+ var form = _a.form;
538
+ var _b = action.payload, controlRef = _b.controlRef, markAll = _b.markAll;
539
+ var controls = markAll
540
+ ? getControlBranch(controlRef, form)
541
+ : getAncestorControls(controlRef, form);
542
+ return {
543
+ form: Object.entries(form).reduce(function (acc, _a) {
544
+ var _b;
545
+ var key = _a[0], control = _a[1];
546
+ return (__assign(__assign({}, acc), (_b = {}, _b[key] = __assign(__assign({}, control), { touched: controls.includes(control) ? true : control.touched }), _b)));
547
+ }, {}),
548
+ action: action
549
+ };
550
+ };
551
+
552
+ var markControlAsUntouched = function (_a, action) {
553
+ var _b;
554
+ var form = _a.form;
555
+ var controlRef = action.payload;
556
+ var descendants = getDescendantControls(controlRef, form);
557
+ var result = Object.entries(form).reduce(function (acc, _a) {
558
+ var _b;
559
+ var key = _a[0], control = _a[1];
560
+ return (__assign(__assign({}, acc), (_b = {}, _b[key] = __assign(__assign({}, control), { touched: descendants.includes(control) ? false : control.touched }), _b)));
561
+ }, {});
562
+ // Update ancestors
563
+ var currentRef = controlRef;
564
+ var key;
565
+ while (currentRef.length > 0) {
566
+ currentRef = currentRef.slice(0, -1);
567
+ key = getFormKey(currentRef);
568
+ result = __assign(__assign({}, result), (_b = {}, _b[key] = __assign(__assign({}, result[key]), { touched: getDescendantControls(currentRef, result, true).some(function (control) { return control.touched; }) }), _b));
569
+ }
570
+ return { form: result, action: action };
571
+ };
572
+
573
+ var resetControl = function (_a, action) {
574
+ var form = _a.form;
575
+ var controlRef = action.payload;
576
+ var controlToReset = getControl(controlRef, form);
577
+ var descendants = getDescendantControls(controlRef, form);
578
+ // Remove all descendants
579
+ var descendantsRemoved = Object.entries(form).reduce(function (acc, _a) {
580
+ var _b;
581
+ var key = _a[0], control = _a[1];
582
+ if (descendants.includes(control))
583
+ return acc;
584
+ return __assign(__assign({}, acc), (_b = {}, _b[key] = control, _b));
585
+ }, {});
586
+ var restoredControls = buildState(controlToReset.config, descendantsRemoved, controlToReset.controlRef);
587
+ return {
588
+ form: updateDirty(syncValidate(updateAncestorValues(restoredControls, {
589
+ type: UPDATE_ANCESTOR_VALUES,
590
+ payload: controlRef
591
+ }))),
592
+ action: action
593
+ };
594
+ };
595
+
596
+ var asyncValidation = function (form, _a) {
597
+ var controlRef = _a.payload.controlRef;
598
+ var ancestors = getAncestorControls(controlRef, form);
599
+ var result = Object.entries(form).reduce(function (acc, _a) {
600
+ var _b, _c;
601
+ var key = _a[0], control = _a[1];
602
+ if (ancestors.includes(control)) {
603
+ var isChangedControl = getFormKey(control.controlRef) === getFormKey(controlRef);
604
+ return __assign(__assign({}, acc), (_b = {}, _b[key] = __assign(__assign({}, control), { pending: true, asyncValidateInProgress: isChangedControl
605
+ ? __assign({}, control.config.asyncValidators.reduce(function (acc, _, index) {
606
+ var _a;
607
+ return (__assign(__assign({}, acc), (_a = {}, _a[index] = true, _a)));
608
+ }, {})) : control.asyncValidateInProgress }), _b));
609
+ }
610
+ return __assign(__assign({}, acc), (_c = {}, _c[key] = control, _c));
611
+ }, {});
612
+ return result;
613
+ };
614
+
615
+ var hasErrors = function (errors) {
616
+ return Object.values(errors).some(function (hasError) { return hasError; });
617
+ };
618
+ var mergeErrors = function (form) {
619
+ var errorsMerged = Object.entries(form).reduce(function (acc, _a) {
620
+ var _b;
621
+ var key = _a[0], control = _a[1];
622
+ var errors = __assign(__assign({}, control.validatorErrors), control.asyncValidatorErrors);
623
+ return __assign(__assign({}, acc), (_b = {}, _b[key] = __assign(__assign({}, control), { errors: errors, valid: !hasErrors(errors) }), _b));
624
+ }, {});
625
+ var childrenErrorsChecked = Object.entries(errorsMerged).reduce(function (acc, _a) {
626
+ var _b;
627
+ var key = _a[0], control = _a[1];
628
+ return __assign(__assign({}, acc), (_b = {}, _b[key] = __assign(__assign({}, control), { valid: control.valid &&
629
+ getDescendantControls(control.controlRef, errorsMerged).every(function (control) { return control.valid; }) }), _b));
630
+ }, {});
631
+ return childrenErrorsChecked;
632
+ };
633
+
634
+ var isControlValidating = function (control) {
635
+ if (!control.asyncValidateInProgress)
636
+ return false;
637
+ return Object.values(control.asyncValidateInProgress).some(function (pending) { return pending; });
638
+ };
639
+ var isControlAsyncValid = function (control) {
640
+ return !Object.values(control.asyncValidatorErrors).some(function (error) { return error; });
641
+ };
642
+ var getControlByKey = function (key, form) {
643
+ return Object.values(form).find(function (control) { return control.key === key; });
644
+ };
645
+ var asyncValidationResponseSuccess = function (form, _a) {
646
+ var _b, _c;
647
+ var _d = _a.payload, key = _d.key, validatorIndex = _d.validatorIndex, errors = _d.errors;
648
+ var control = getControlByKey(key, form);
649
+ if (!control) {
650
+ return form;
651
+ }
652
+ 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));
653
+ var ancestors = getAncestorControls(control.controlRef, controlUpdated);
654
+ var ancestorsUpdated = Object.entries(controlUpdated).reduce(function (acc, _a) {
655
+ var _b, _c;
656
+ var key = _a[0], control = _a[1];
657
+ if (ancestors.includes(control)) {
658
+ var descendants = getDescendantControls(control.controlRef, controlUpdated);
659
+ return __assign(__assign({}, acc), (_b = {}, _b[key] = __assign(__assign({}, control), { pending: descendants.some(function (control) { return isControlValidating(control); }), asyncValidatorsValid: descendants.every(function (control) { return isControlAsyncValid(control); }) }), _b));
660
+ }
661
+ return __assign(__assign({}, acc), (_c = {}, _c[key] = control, _c));
662
+ }, {});
663
+ return mergeErrors(ancestorsUpdated);
664
+ };
665
+
666
+ var DEFAULT_HUB2_FIELDS = {
667
+ asyncValidatorsValid: true,
668
+ asyncValidatorErrors: {},
669
+ asyncValidateInProgress: {},
670
+ pending: false,
671
+ valid: null,
672
+ errors: null
673
+ };
674
+ var formChange = function (state, _a) {
675
+ if (state === void 0) { state = null; }
676
+ var _b = _a.payload, form = _b.form, action = _b.action;
677
+ return mergeErrors(Object.entries(form).reduce(function (acc, _a) {
678
+ var _b;
679
+ var dictKey = _a[0], baseControl = _a[1];
680
+ var existingControl = (action === null || action === void 0 ? void 0 : action.type) === 'removeControl'
681
+ ? state && Object.values(state).find(function (control) { return baseControl.key === control.key; })
682
+ : state && state[dictKey];
683
+ return __assign(__assign({}, acc), (_b = {}, _b[dictKey] = __assign(__assign({}, (existingControl
684
+ ? existingControl
685
+ : structuredClone(DEFAULT_HUB2_FIELDS))), baseControl), _b));
686
+ }, {}));
687
+ };
688
+
689
+ var control = function (config) {
690
+ if (Array.isArray(config)) {
691
+ return config.reduce(function (acc, item, index) {
692
+ var _a;
693
+ var indexMap = {
694
+ 0: 'initialValue',
695
+ 1: 'validators',
696
+ 2: 'asyncValidators'
697
+ };
698
+ return __assign(__assign({}, acc), (_a = {}, _a[indexMap[index]] = index < 1 ? item : [].concat(item || []), _a));
699
+ }, {});
700
+ }
701
+ return config;
702
+ };
703
+ var array = function (config) { return config; };
704
+ var group = function (config) { return config; };
705
+ var build = function (config) {
706
+ var initialState = buildFormState(config);
707
+ var rxHub1 = core.RxBuilder({
708
+ initialState: initialState,
709
+ reducers: {
710
+ updateValues: updateValues,
711
+ removeControl: removeControl,
712
+ addControl: addControl,
713
+ markControlAsPristine: markControlAsPristine,
714
+ markControlAsTouched: markControlAsTouched,
715
+ markControlAsUntouched: markControlAsUntouched,
716
+ resetControl: resetControl
717
+ }
718
+ });
719
+ var rxHub2 = core.RxBuilder({
720
+ sources: [buildHub2Source(rxHub1)],
721
+ initialState: null,
722
+ reducers: {
723
+ formChange: formChange,
724
+ asyncValidation: {
725
+ reducer: asyncValidation,
726
+ effects: function (control) { return ({
727
+ key: control.key,
728
+ effects: getScopedEffectsForControl(control)
729
+ }); }
730
+ },
731
+ asyncValidationResponseSuccess: asyncValidationResponseSuccess
732
+ }
733
+ });
734
+ return {
735
+ state$: rxHub2.state$.pipe(operators.filter(function (form) { return form !== null; })),
736
+ actions: rxHub1.actions
737
+ };
738
+ };
739
+ var RxForm = {
740
+ group: group,
741
+ array: array,
742
+ control: control,
743
+ build: build
744
+ };
745
+
746
+ var getArrayItems = function (controlRef, form) {
747
+ if (!Array.isArray(getControl(controlRef, form).config.controls)) {
748
+ throw "".concat(controlRef.join('.'), " is not a Form Array control");
749
+ }
750
+ return Object.values(form)
751
+ .filter(function (control) {
752
+ var isItem = controlRef.every(function (key, index) { return key === control.controlRef[index]; }) &&
753
+ control.controlRef.length === controlRef.length + 1 &&
754
+ typeof control.controlRef[controlRef.length] === 'number';
755
+ return isItem;
756
+ })
757
+ .sort(function (a, b) { return +a.controlRef.slice(-1) - +b.controlRef.slice(-1); });
758
+ };
759
+
760
+ exports.ControlModels = Controls;
761
+ exports.RxForm = RxForm;
762
+ exports.Validators = index;
763
+ exports.getArrayItems = getArrayItems;