@reactables/forms 0.7.0-alpha.0 → 0.7.0-alpha.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/Helpers/addAsyncValidationEffects.d.ts +2 -1
- package/dist/Helpers/buildFormState.d.ts +3 -2
- package/dist/Helpers/reverseObjectKeys.d.ts +2 -0
- package/dist/Models/Configs.d.ts +3 -4
- package/dist/Models/Validators.d.ts +2 -2
- package/dist/Reducers/Hub1/addControl.d.ts +2 -1
- package/dist/Reducers/Hub1/getErrors.d.ts +2 -1
- package/dist/Reducers/Hub1/pushControl.d.ts +2 -1
- package/dist/Reducers/Hub1/removeControl.d.ts +2 -1
- package/dist/Reducers/Hub1/resetControl.d.ts +2 -1
- package/dist/Reducers/Hub1/updateAncestorValues.d.ts +2 -1
- package/dist/Reducers/Hub1/updateAncestorValuesAddControl.d.ts +2 -1
- package/dist/Reducers/Hub1/updateAncestorValuesRemoveControl.d.ts +2 -1
- package/dist/Reducers/Hub1/updateValues.d.ts +2 -1
- package/dist/Reducers/Hub2/mergeControls.d.ts +1 -22
- package/dist/Reducers/Hub2/mergeRemoveControl.d.ts +1 -1
- package/dist/RxForm/RxForm.d.ts +15 -1
- package/dist/RxForm/Tests/load.test.d.ts +1 -0
- package/dist/RxForm/index.d.ts +1 -1
- package/dist/Testing/Models/initialState.d.ts +667 -0
- package/dist/Testing/Validators.d.ts +10 -0
- package/dist/Testing/asyncConfig.d.ts +0 -8
- package/dist/Testing/config.d.ts +0 -8
- package/dist/Validators/Validators.d.ts +0 -1
- package/dist/index.js +144 -73
- package/package.json +2 -2
- package/dist/Helpers/FormBuilder.d.ts +0 -10
package/dist/index.js
CHANGED
|
@@ -39,18 +39,18 @@ var required = function (value) {
|
|
|
39
39
|
};
|
|
40
40
|
var email = function (value) {
|
|
41
41
|
return value && !/^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/g.test(value) ? { email: true } : { email: false };
|
|
42
|
-
};
|
|
43
|
-
var phoneNumber = function (value) {
|
|
44
|
-
return value && !/^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$/.test(value)
|
|
45
|
-
? { phoneNumber: true }
|
|
46
|
-
: { phoneNumber: false };
|
|
47
42
|
};
|
|
48
43
|
|
|
44
|
+
var Validators = /*#__PURE__*/Object.freeze({
|
|
45
|
+
__proto__: null,
|
|
46
|
+
required: required,
|
|
47
|
+
email: email
|
|
48
|
+
});
|
|
49
|
+
|
|
49
50
|
var index = /*#__PURE__*/Object.freeze({
|
|
50
51
|
__proto__: null,
|
|
51
52
|
required: required,
|
|
52
|
-
email: email
|
|
53
|
-
phoneNumber: phoneNumber
|
|
53
|
+
email: email
|
|
54
54
|
});
|
|
55
55
|
|
|
56
56
|
/******************************************************************************
|
|
@@ -144,12 +144,18 @@ var generateKey = function (length) {
|
|
|
144
144
|
return result;
|
|
145
145
|
};
|
|
146
146
|
|
|
147
|
-
var getErrors = function (control, value) {
|
|
148
|
-
var
|
|
149
|
-
|
|
147
|
+
var getErrors = function (control, value, _a) {
|
|
148
|
+
var _b;
|
|
149
|
+
var validators = _a.validators;
|
|
150
|
+
return ((_b = control.config.validators) === null || _b === void 0 ? void 0 : _b.reduce(function (acc, validator) {
|
|
151
|
+
if (!validators[validator]) {
|
|
152
|
+
throw "You have not provided a validator for \"".concat(validator, "\"");
|
|
153
|
+
}
|
|
154
|
+
return __assign(__assign({}, acc), validators[validator](value));
|
|
155
|
+
}, {})) || {};
|
|
150
156
|
};
|
|
151
157
|
|
|
152
|
-
var buildState = function (config, form, controlRef) {
|
|
158
|
+
var buildState = function (config, form, controlRef, providers) {
|
|
153
159
|
var _a;
|
|
154
160
|
if (form === void 0) { form = { root: null }; }
|
|
155
161
|
if (controlRef === void 0) { controlRef = []; }
|
|
@@ -164,40 +170,45 @@ var buildState = function (config, form, controlRef) {
|
|
|
164
170
|
config: config,
|
|
165
171
|
key: generateKey(5)
|
|
166
172
|
};
|
|
167
|
-
var newForm = __assign(__assign({}, form), (_a = {}, _a[getFormKey(controlRef)] = __assign(__assign({}, control), { validatorErrors: getErrors(control, value) }), _a));
|
|
173
|
+
var newForm = __assign(__assign({}, form), (_a = {}, _a[getFormKey(controlRef)] = __assign(__assign({}, control), { validatorErrors: getErrors(control, value, providers) }), _a));
|
|
168
174
|
var controls = config.controls;
|
|
169
175
|
// Adding controls for Form Group
|
|
170
176
|
if (controls && !(controls instanceof Array)) {
|
|
171
177
|
newForm = Object.entries(config.controls).reduce(function (acc, _a) {
|
|
172
178
|
var key = _a[0], controlConfig = _a[1];
|
|
173
|
-
return buildState(controlConfig, acc, controlRef.concat(key));
|
|
179
|
+
return buildState(controlConfig, acc, controlRef.concat(key), providers);
|
|
174
180
|
}, newForm);
|
|
175
181
|
}
|
|
176
182
|
else if (controls && controls instanceof Array) {
|
|
177
183
|
// Adding controls for Form Array
|
|
178
|
-
newForm = config.controls.reduce(function (acc, controlConfig, index) {
|
|
184
|
+
newForm = config.controls.reduce(function (acc, controlConfig, index) {
|
|
185
|
+
return buildState(controlConfig, acc, controlRef.concat(index), providers);
|
|
186
|
+
}, newForm);
|
|
179
187
|
}
|
|
180
188
|
return newForm;
|
|
181
189
|
};
|
|
182
|
-
var buildFormState = function (config, form, controlRef) {
|
|
190
|
+
var buildFormState = function (config, form, controlRef, providers) {
|
|
183
191
|
if (form === void 0) { form = { root: null }; }
|
|
184
192
|
if (controlRef === void 0) { controlRef = []; }
|
|
185
193
|
return {
|
|
186
|
-
form: buildState(config, form, controlRef)
|
|
194
|
+
form: buildState(config, form, controlRef, providers)
|
|
187
195
|
};
|
|
188
196
|
};
|
|
189
197
|
|
|
190
|
-
var getScopedEffectsForControl = function (formControl) {
|
|
198
|
+
var getScopedEffectsForControl = function (formControl, providers) {
|
|
191
199
|
var config = formControl.config, key = formControl.key;
|
|
192
200
|
var asyncValidators = config.asyncValidators;
|
|
193
201
|
var scopedEffects = [];
|
|
194
202
|
if (asyncValidators && asyncValidators.length) {
|
|
195
|
-
scopedEffects = asyncValidators.reduce(function (acc,
|
|
203
|
+
scopedEffects = asyncValidators.reduce(function (acc, asyncValidator, validatorIndex) {
|
|
196
204
|
var effect = function (actions$) {
|
|
205
|
+
if (!providers.asyncValidators[asyncValidator]) {
|
|
206
|
+
throw "You have not provided an asyncValidator for \"".concat(asyncValidator, "\"");
|
|
207
|
+
}
|
|
197
208
|
return actions$.pipe(operators.map(function (_a) {
|
|
198
209
|
var control = _a.payload;
|
|
199
210
|
return control;
|
|
200
|
-
}),
|
|
211
|
+
}), providers.asyncValidators[asyncValidator], operators.map(function (errors) { return ({
|
|
201
212
|
type: 'asyncValidationResponseSuccess',
|
|
202
213
|
payload: {
|
|
203
214
|
key: key,
|
|
@@ -213,8 +224,8 @@ var getScopedEffectsForControl = function (formControl) {
|
|
|
213
224
|
};
|
|
214
225
|
var getAsyncValidationActions = function (formControls) {
|
|
215
226
|
return formControls.reduce(function (acc, control) {
|
|
216
|
-
var
|
|
217
|
-
if (!
|
|
227
|
+
var _a;
|
|
228
|
+
if (!((_a = control.config.asyncValidators) === null || _a === void 0 ? void 0 : _a.length))
|
|
218
229
|
return acc;
|
|
219
230
|
var action = { type: 'asyncValidation', payload: control };
|
|
220
231
|
return acc.concat(action);
|
|
@@ -237,7 +248,7 @@ var getControl = function (controlRef, form) {
|
|
|
237
248
|
};
|
|
238
249
|
|
|
239
250
|
var UPDATE_ANCESTOR_VALUES = 'UPDATE_ANCESTOR_VALUES';
|
|
240
|
-
var updateAncestorValues = function (form, _a) {
|
|
251
|
+
var updateAncestorValues = function (form, _a, providers) {
|
|
241
252
|
var _b, _c;
|
|
242
253
|
var _d = _a.payload, controlRef = _d.controlRef, value = _d.value;
|
|
243
254
|
if (controlRef.length) {
|
|
@@ -256,11 +267,11 @@ var updateAncestorValues = function (form, _a) {
|
|
|
256
267
|
// If parent is a Form Group
|
|
257
268
|
newValue = __assign(__assign({}, parentControl.value), (_b = {}, _b[childKey_1] = value, _b));
|
|
258
269
|
}
|
|
259
|
-
var newParentControl = __assign(__assign({}, parentControl), { validatorErrors: getErrors(parentControl, newValue), value: newValue, dirty: !isEqual__default["default"](newValue, parentControl.pristineValue) });
|
|
270
|
+
var newParentControl = __assign(__assign({}, parentControl), { validatorErrors: getErrors(parentControl, newValue, providers), value: newValue, dirty: !isEqual__default["default"](newValue, parentControl.pristineValue) });
|
|
260
271
|
return updateAncestorValues(__assign(__assign({}, form), (_c = {}, _c[parentFormKey] = newParentControl, _c)), {
|
|
261
272
|
type: UPDATE_ANCESTOR_VALUES,
|
|
262
273
|
payload: { controlRef: parentRef, value: newValue }
|
|
263
|
-
});
|
|
274
|
+
}, providers);
|
|
264
275
|
}
|
|
265
276
|
return form;
|
|
266
277
|
};
|
|
@@ -321,7 +332,7 @@ var getAncestorControls = function (controlRef, form, excludeSelf) {
|
|
|
321
332
|
};
|
|
322
333
|
|
|
323
334
|
var UPDATE_DESCENDANT_VALUES = 'UPDATE_DESCENDANT_VALUES';
|
|
324
|
-
var updateDescendants = function (state, _a) {
|
|
335
|
+
var updateDescendants = function (state, _a, providers) {
|
|
325
336
|
var _b = _a.payload, controlRef = _b.controlRef, value = _b.value;
|
|
326
337
|
var descendants = getDescendantControls(controlRef, state.form, true).map(function (control) { return [getFormKey(control.controlRef), control]; });
|
|
327
338
|
var result = descendants.reduce(function (acc, _a) {
|
|
@@ -329,7 +340,7 @@ var updateDescendants = function (state, _a) {
|
|
|
329
340
|
var key = _a[0], control = _a[1];
|
|
330
341
|
if (isChildRef(control.controlRef, controlRef)) {
|
|
331
342
|
var childValue = value[control.controlRef.at(-1)];
|
|
332
|
-
var validatorErrors = getErrors(control, value);
|
|
343
|
+
var validatorErrors = getErrors(control, value, providers);
|
|
333
344
|
var newControl = __assign(__assign({}, control), { value: childValue, validatorErrors: validatorErrors, dirty: !isEqual__default["default"](childValue, control.pristineValue) });
|
|
334
345
|
acc = {
|
|
335
346
|
form: __assign(__assign({}, acc.form), (_b = {}, _b[key] = newControl, _b)),
|
|
@@ -340,7 +351,7 @@ var updateDescendants = function (state, _a) {
|
|
|
340
351
|
acc = updateDescendants(acc, {
|
|
341
352
|
type: UPDATE_DESCENDANT_VALUES,
|
|
342
353
|
payload: { controlRef: control.controlRef, value: childValue }
|
|
343
|
-
});
|
|
354
|
+
}, providers);
|
|
344
355
|
}
|
|
345
356
|
}
|
|
346
357
|
return acc;
|
|
@@ -352,19 +363,25 @@ var updateDescendants = function (state, _a) {
|
|
|
352
363
|
};
|
|
353
364
|
// Will only update child controls that are present.
|
|
354
365
|
// Use AddControlPayload/RemoveControl action reducers to add/remove control
|
|
355
|
-
var updateValues = function (_a, action, mergeChanges) {
|
|
366
|
+
var updateValues = function (_a, action, providers, mergeChanges) {
|
|
356
367
|
var _b, _c;
|
|
357
368
|
var form = _a.form, _d = _a.changedControls, changedControls = _d === void 0 ? {} : _d, _e = _a.removedControls, removedControls = _e === void 0 ? {} : _e;
|
|
358
369
|
if (mergeChanges === void 0) { mergeChanges = false; }
|
|
370
|
+
var normalizers = providers.normalizers;
|
|
359
371
|
var _f = action.payload, controlRef = _f.controlRef, value = _f.value;
|
|
360
372
|
// Update its own value
|
|
361
373
|
var ctrlKey = getFormKey(controlRef);
|
|
362
374
|
var newValue = value;
|
|
363
375
|
var config = form[ctrlKey].config;
|
|
364
376
|
if (config.normalizers) {
|
|
365
|
-
newValue = config.normalizers.reduce(function (acc, normalizer) {
|
|
377
|
+
newValue = config.normalizers.reduce(function (acc, normalizer) {
|
|
378
|
+
if (!normalizers[normalizer]) {
|
|
379
|
+
throw "You have not provided a normalizer for \"".concat(normalizer, "\"");
|
|
380
|
+
}
|
|
381
|
+
return normalizers[normalizer](acc);
|
|
382
|
+
}, value);
|
|
366
383
|
}
|
|
367
|
-
var validatorErrors = getErrors(form[ctrlKey], newValue);
|
|
384
|
+
var validatorErrors = getErrors(form[ctrlKey], newValue, providers);
|
|
368
385
|
var newControl = __assign(__assign({}, form[ctrlKey]), { validatorErrors: validatorErrors, dirty: !isEqual__default["default"](value, form[ctrlKey].pristineValue), value: newValue });
|
|
369
386
|
var result = {
|
|
370
387
|
form: __assign(__assign({}, form), (_b = {}, _b[ctrlKey] = newControl, _b)),
|
|
@@ -379,14 +396,14 @@ var updateValues = function (_a, action, mergeChanges) {
|
|
|
379
396
|
controlRef: controlRef,
|
|
380
397
|
value: newValue
|
|
381
398
|
}
|
|
382
|
-
});
|
|
399
|
+
}, providers);
|
|
383
400
|
}
|
|
384
401
|
// Update its Ancestors
|
|
385
402
|
if (controlRef.length) {
|
|
386
403
|
result = __assign(__assign({}, result), { form: updateAncestorValues(result.form, {
|
|
387
404
|
type: UPDATE_ANCESTOR_VALUES,
|
|
388
405
|
payload: { controlRef: controlRef, value: newValue }
|
|
389
|
-
}) });
|
|
406
|
+
}, providers) });
|
|
390
407
|
}
|
|
391
408
|
var changedAncestorControls = getAncestorControls(controlRef, result.form).reduce(function (acc, control) {
|
|
392
409
|
var _a;
|
|
@@ -397,7 +414,7 @@ var updateValues = function (_a, action, mergeChanges) {
|
|
|
397
414
|
};
|
|
398
415
|
|
|
399
416
|
var UPDATE_ANCESTOR_VALUES_REMOVE_CONTROL = 'UPDATE_ANCESTOR_VALUES_REMOVE_CONTROL';
|
|
400
|
-
var updateAncestorValuesRemoveControl = function (form, _a) {
|
|
417
|
+
var updateAncestorValuesRemoveControl = function (form, _a, providers) {
|
|
401
418
|
var _b;
|
|
402
419
|
var controlRef = _a.payload;
|
|
403
420
|
if (controlRef.length) {
|
|
@@ -415,16 +432,16 @@ var updateAncestorValuesRemoveControl = function (form, _a) {
|
|
|
415
432
|
newValue = __assign({}, parentControl.value);
|
|
416
433
|
delete newValue[childKey_1];
|
|
417
434
|
}
|
|
418
|
-
var newParentControl = __assign(__assign({}, parentControl), { value: newValue, validatorErrors: getErrors(parentControl, newValue), dirty: !isEqual__default["default"](newValue, parentControl.pristineValue) });
|
|
435
|
+
var newParentControl = __assign(__assign({}, parentControl), { value: newValue, validatorErrors: getErrors(parentControl, newValue, providers), dirty: !isEqual__default["default"](newValue, parentControl.pristineValue) });
|
|
419
436
|
return updateAncestorValues(__assign(__assign({}, form), (_b = {}, _b[parentFormKey] = newParentControl, _b)), {
|
|
420
437
|
type: UPDATE_ANCESTOR_VALUES,
|
|
421
438
|
payload: { controlRef: parentRef, value: newValue }
|
|
422
|
-
});
|
|
439
|
+
}, providers);
|
|
423
440
|
}
|
|
424
441
|
return form;
|
|
425
442
|
};
|
|
426
443
|
|
|
427
|
-
var removeControl = function (state, action, mergeChanges) {
|
|
444
|
+
var removeControl = function (state, action, providers, mergeChanges) {
|
|
428
445
|
var _a;
|
|
429
446
|
if (mergeChanges === void 0) { mergeChanges = false; }
|
|
430
447
|
var form = state.form;
|
|
@@ -473,7 +490,7 @@ var removeControl = function (state, action, mergeChanges) {
|
|
|
473
490
|
var result = updateAncestorValuesRemoveControl(controlRemoved, {
|
|
474
491
|
type: UPDATE_ANCESTOR_VALUES_REMOVE_CONTROL,
|
|
475
492
|
payload: controlRef
|
|
476
|
-
});
|
|
493
|
+
}, providers);
|
|
477
494
|
var changedControls = __assign(__assign({}, (mergeChanges ? state.changedControls || {} : undefined)), getAncestorControls(controlRef.slice(0, -1), result).reduce(function (acc, control) {
|
|
478
495
|
var _a;
|
|
479
496
|
return (__assign(__assign({}, acc), (_a = {}, _a[control.key] = control, _a)));
|
|
@@ -518,7 +535,7 @@ var removeControl = function (state, action, mergeChanges) {
|
|
|
518
535
|
};
|
|
519
536
|
|
|
520
537
|
var UPDATE_ANCESTOR_VALUES_ADD_CONTROL = 'UPDATE_ANCESTOR_VALUES_ADD_CONTROL';
|
|
521
|
-
var updateAncestorValuesAddControl = function (form, _a) {
|
|
538
|
+
var updateAncestorValuesAddControl = function (form, _a, providers) {
|
|
522
539
|
var _b, _c;
|
|
523
540
|
var _d = _a.payload, controlRef = _d.controlRef, value = _d.value;
|
|
524
541
|
if (controlRef.length) {
|
|
@@ -535,11 +552,11 @@ var updateAncestorValuesAddControl = function (form, _a) {
|
|
|
535
552
|
// If parent is a Form Group
|
|
536
553
|
newValue = __assign(__assign({}, form[parentFormKey].value), (_b = {}, _b[childKey] = value, _b));
|
|
537
554
|
}
|
|
538
|
-
var newParentControl = __assign(__assign({}, parentControl), { value: newValue, validatorErrors: getErrors(parentControl, newValue), dirty: !isEqual__default["default"](newValue, parentControl.pristineValue) });
|
|
555
|
+
var newParentControl = __assign(__assign({}, parentControl), { value: newValue, validatorErrors: getErrors(parentControl, newValue, providers), dirty: !isEqual__default["default"](newValue, parentControl.pristineValue) });
|
|
539
556
|
return updateAncestorValues(__assign(__assign({}, form), (_c = {}, _c[parentFormKey] = newParentControl, _c)), {
|
|
540
557
|
type: UPDATE_ANCESTOR_VALUES,
|
|
541
558
|
payload: { controlRef: parentRef, value: newValue }
|
|
542
|
-
});
|
|
559
|
+
}, providers);
|
|
543
560
|
}
|
|
544
561
|
return form;
|
|
545
562
|
};
|
|
@@ -550,19 +567,19 @@ var getControlBranch = function (controlRef, form) {
|
|
|
550
567
|
return ancestors.concat(childControls).sort(function (a, b) { return a.controlRef.length - b.controlRef.length; });
|
|
551
568
|
};
|
|
552
569
|
|
|
553
|
-
var addControl = function (state, action, mergeChanges) {
|
|
570
|
+
var addControl = function (state, action, providers, mergeChanges) {
|
|
554
571
|
if (mergeChanges === void 0) { mergeChanges = false; }
|
|
555
572
|
var _a = action.payload, config = _a.config, controlRef = _a.controlRef;
|
|
556
573
|
// If controlRef does not exist we are adding control to a Form Group
|
|
557
574
|
if (!getControl(controlRef.slice(0, -1), state.form)) {
|
|
558
575
|
throw 'You are attempting to add a control to a non-existent form group';
|
|
559
576
|
}
|
|
560
|
-
var newForm = buildState(config, state.form, controlRef);
|
|
577
|
+
var newForm = buildState(config, state.form, controlRef, providers);
|
|
561
578
|
var newValue = getControl(controlRef, newForm).value;
|
|
562
579
|
var ancestorsUpdated = updateAncestorValuesAddControl(newForm, {
|
|
563
580
|
type: UPDATE_ANCESTOR_VALUES_ADD_CONTROL,
|
|
564
581
|
payload: { controlRef: controlRef, value: newValue }
|
|
565
|
-
});
|
|
582
|
+
}, providers);
|
|
566
583
|
var changedControls = getControlBranch(controlRef, ancestorsUpdated).reduce(function (acc, control) {
|
|
567
584
|
var _a;
|
|
568
585
|
return (__assign(__assign({}, acc), (_a = {}, _a[control.key] = control, _a)));
|
|
@@ -574,7 +591,7 @@ var addControl = function (state, action, mergeChanges) {
|
|
|
574
591
|
};
|
|
575
592
|
};
|
|
576
593
|
|
|
577
|
-
var pushControl = function (state, action, mergeChanges) {
|
|
594
|
+
var pushControl = function (state, action, providers, mergeChanges) {
|
|
578
595
|
if (mergeChanges === void 0) { mergeChanges = false; }
|
|
579
596
|
var newControlRef;
|
|
580
597
|
var _a = action.payload, config = _a.config, controlRef = _a.controlRef;
|
|
@@ -588,12 +605,12 @@ var pushControl = function (state, action, mergeChanges) {
|
|
|
588
605
|
else {
|
|
589
606
|
throw 'You are attempting to push to a control that is not a Form Array';
|
|
590
607
|
}
|
|
591
|
-
var newForm = buildState(config, state.form, newControlRef);
|
|
608
|
+
var newForm = buildState(config, state.form, newControlRef, providers);
|
|
592
609
|
var newValue = getControl(newControlRef, newForm).value;
|
|
593
610
|
var ancestorsUpdated = updateAncestorValuesAddControl(newForm, {
|
|
594
611
|
type: UPDATE_ANCESTOR_VALUES_ADD_CONTROL,
|
|
595
612
|
payload: { controlRef: newControlRef, value: newValue }
|
|
596
|
-
});
|
|
613
|
+
}, providers);
|
|
597
614
|
var changedControls = getControlBranch(newControlRef, ancestorsUpdated).reduce(function (acc, control) {
|
|
598
615
|
var _a;
|
|
599
616
|
return (__assign(__assign({}, acc), (_a = {}, _a[control.key] = control, _a)));
|
|
@@ -719,7 +736,7 @@ var markControlAsUntouched = function (state, action, mergeChanges) {
|
|
|
719
736
|
};
|
|
720
737
|
};
|
|
721
738
|
|
|
722
|
-
var resetControl = function (state, action, mergeChanges) {
|
|
739
|
+
var resetControl = function (state, action, providers, mergeChanges) {
|
|
723
740
|
var _a;
|
|
724
741
|
if (mergeChanges === void 0) { mergeChanges = false; }
|
|
725
742
|
var form = state.form;
|
|
@@ -735,12 +752,12 @@ var resetControl = function (state, action, mergeChanges) {
|
|
|
735
752
|
delete descendantsRemoved[key];
|
|
736
753
|
});
|
|
737
754
|
// Remove all descendants
|
|
738
|
-
var restoredControls = buildState(controlToReset.config, descendantsRemoved, controlToReset.controlRef);
|
|
755
|
+
var restoredControls = buildState(controlToReset.config, descendantsRemoved, controlToReset.controlRef, providers);
|
|
739
756
|
var restoredControlValue = getControl(controlRef, restoredControls).value;
|
|
740
757
|
var result = updateAncestorValues(restoredControls, {
|
|
741
758
|
type: UPDATE_ANCESTOR_VALUES,
|
|
742
759
|
payload: { controlRef: controlRef, value: restoredControlValue }
|
|
743
|
-
});
|
|
760
|
+
}, providers);
|
|
744
761
|
var changedControls = __assign(__assign({}, (mergeChanges ? state.changedControls || {} : undefined)), getControlBranch(controlRef, result).reduce(function (acc, control) {
|
|
745
762
|
var _a;
|
|
746
763
|
return (__assign(__assign({}, acc), (_a = {}, _a[control.key] = control, _a)));
|
|
@@ -776,6 +793,15 @@ var asyncValidation = function (form, _a) {
|
|
|
776
793
|
return __assign(__assign({}, form), updatedSelfAndAncestors);
|
|
777
794
|
};
|
|
778
795
|
|
|
796
|
+
var reverseObjectKeys = function (form) {
|
|
797
|
+
return Object.keys(form)
|
|
798
|
+
.reverse()
|
|
799
|
+
.reduce(function (acc, key) {
|
|
800
|
+
acc[key] = form[key];
|
|
801
|
+
return acc;
|
|
802
|
+
}, {});
|
|
803
|
+
};
|
|
804
|
+
|
|
779
805
|
var hasErrors$3 = function (errors) {
|
|
780
806
|
return Object.values(errors).some(function (hasError) { return hasError; });
|
|
781
807
|
};
|
|
@@ -810,7 +836,8 @@ var mergeBranchErrors = function (form, controlRef) {
|
|
|
810
836
|
}
|
|
811
837
|
return __assign(__assign({}, acc), (_b = {}, _b[key] = __assign(__assign({}, control), { errors: errors, valid: selfValid && childrenValid, childrenValid: childrenValid }), _b));
|
|
812
838
|
}, {});
|
|
813
|
-
|
|
839
|
+
var errorsMergedOrderRestored = reverseObjectKeys(errorsMerged);
|
|
840
|
+
return __assign(__assign({}, form), errorsMergedOrderRestored);
|
|
814
841
|
};
|
|
815
842
|
|
|
816
843
|
var isControlValidating = function (control) {
|
|
@@ -866,12 +893,7 @@ var mergeErrors = function (form) {
|
|
|
866
893
|
}
|
|
867
894
|
return __assign(__assign({}, acc), (_b = {}, _b[key] = __assign(__assign({}, control), { errors: errors, valid: selfValid && childrenValid, childrenValid: childrenValid }), _b));
|
|
868
895
|
}, {});
|
|
869
|
-
var restoredOrder =
|
|
870
|
-
.reverse()
|
|
871
|
-
.reduce(function (acc, key) {
|
|
872
|
-
acc[key] = errorsMerged[key];
|
|
873
|
-
return acc;
|
|
874
|
-
}, {});
|
|
896
|
+
var restoredOrder = reverseObjectKeys(errorsMerged);
|
|
875
897
|
return restoredOrder;
|
|
876
898
|
};
|
|
877
899
|
|
|
@@ -909,13 +931,14 @@ var mergeRemoveControl = function (state, form, controlRef) {
|
|
|
909
931
|
}
|
|
910
932
|
return __assign(__assign({}, acc), (_a = {}, _a[key] = __assign(__assign(__assign({}, existingControl), baseControl), { errors: errors, valid: selfValid && childrenValid, childrenValid: childrenValid }), _a));
|
|
911
933
|
}, {});
|
|
934
|
+
var updatedControlBranchOrderRestored = reverseObjectKeys(updatedControlBranch);
|
|
912
935
|
var descendants = existingBranch.filter(function (control) { return control.controlRef.length > parentRef.length; });
|
|
913
936
|
var removedControls = __assign({}, state);
|
|
914
937
|
descendants.forEach(function (control) {
|
|
915
938
|
delete removedControls[getFormKey(control.controlRef)];
|
|
916
939
|
});
|
|
917
940
|
delete removedControls[getFormKey(controlRef)];
|
|
918
|
-
return __assign(__assign({}, removedControls),
|
|
941
|
+
return __assign(__assign({}, removedControls), updatedControlBranchOrderRestored);
|
|
919
942
|
};
|
|
920
943
|
|
|
921
944
|
var hasErrors = function (errors) {
|
|
@@ -959,7 +982,8 @@ var mergeControls = function (state, _a) {
|
|
|
959
982
|
}
|
|
960
983
|
return __assign(__assign({}, acc), (_a = {}, _a[formKey] = __assign(__assign({}, newControl), { errors: errors, valid: selfValid && childrenValid, childrenValid: childrenValid }), _a));
|
|
961
984
|
}, {});
|
|
962
|
-
var
|
|
985
|
+
var orderRestored = reverseObjectKeys(updatedBranch);
|
|
986
|
+
var result = __assign(__assign({}, controlsRemoved), orderRestored);
|
|
963
987
|
return result;
|
|
964
988
|
};
|
|
965
989
|
|
|
@@ -993,18 +1017,21 @@ var control = function (config) {
|
|
|
993
1017
|
};
|
|
994
1018
|
var array = function (config) { return config; };
|
|
995
1019
|
var group = function (config) { return config; };
|
|
996
|
-
var reducerTools = {
|
|
1020
|
+
var reducerTools = function (providers) { return ({
|
|
997
1021
|
updateValues: function (state, payload) {
|
|
998
|
-
return updateValues(state, { payload: payload }, true);
|
|
1022
|
+
return updateValues(state, { payload: payload }, providers, true);
|
|
999
1023
|
},
|
|
1000
1024
|
removeControl: function (state, payload) {
|
|
1001
|
-
return removeControl(state, { payload: payload }, true);
|
|
1025
|
+
return removeControl(state, { payload: payload }, providers, true);
|
|
1002
1026
|
},
|
|
1003
1027
|
pushControl: function (state, payload) {
|
|
1004
|
-
return pushControl(state, { payload: payload }, true);
|
|
1028
|
+
return pushControl(state, { payload: payload }, providers, true);
|
|
1005
1029
|
},
|
|
1006
1030
|
addControl: function (state, payload) {
|
|
1007
|
-
return addControl(state, { payload: payload }, true);
|
|
1031
|
+
return addControl(state, { payload: payload }, providers, true);
|
|
1032
|
+
},
|
|
1033
|
+
resetControl: function (state, payload) {
|
|
1034
|
+
return resetControl(state, { payload: payload }, providers, true);
|
|
1008
1035
|
},
|
|
1009
1036
|
markControlAsPristine: function (state, payload) {
|
|
1010
1037
|
return markControlAsPristine(state, { payload: payload }, true);
|
|
@@ -1014,14 +1041,47 @@ var reducerTools = {
|
|
|
1014
1041
|
},
|
|
1015
1042
|
markControlAsUntouched: function (state, payload) {
|
|
1016
1043
|
return markControlAsUntouched(state, { payload: payload }, true);
|
|
1017
|
-
},
|
|
1018
|
-
resetControl: function (state, payload) {
|
|
1019
|
-
return resetControl(state, { payload: payload }, true);
|
|
1020
1044
|
}
|
|
1021
|
-
};
|
|
1045
|
+
}); };
|
|
1022
1046
|
var build = function (config, options) {
|
|
1047
|
+
var _a, _b, _c;
|
|
1048
|
+
if (options === void 0) { options = {}; }
|
|
1049
|
+
var providers = {
|
|
1050
|
+
normalizers: __assign({}, (_a = options.providers) === null || _a === void 0 ? void 0 : _a.normalizers),
|
|
1051
|
+
validators: __assign(__assign({}, Validators), (_b = options.providers) === null || _b === void 0 ? void 0 : _b.validators),
|
|
1052
|
+
asyncValidators: __assign({}, (_c = options.providers) === null || _c === void 0 ? void 0 : _c.asyncValidators)
|
|
1053
|
+
};
|
|
1054
|
+
var initialState = buildFormState(config, undefined, undefined, providers);
|
|
1055
|
+
return createReactable(initialState, options);
|
|
1056
|
+
};
|
|
1057
|
+
var load = function (state, options) {
|
|
1023
1058
|
if (options === void 0) { options = {}; }
|
|
1024
|
-
var
|
|
1059
|
+
var baseFormState = {
|
|
1060
|
+
form: Object.entries(state).reduce(function (acc, _a) {
|
|
1061
|
+
var _b;
|
|
1062
|
+
var key = _a[0], control = _a[1];
|
|
1063
|
+
return __assign(__assign({}, acc), (_b = {}, _b[key] = Object.entries(control)
|
|
1064
|
+
.filter(function (_a) {
|
|
1065
|
+
var key = _a[0];
|
|
1066
|
+
return !Object.keys(DEFAULT_HUB2_FIELDS).includes(key);
|
|
1067
|
+
})
|
|
1068
|
+
.reduce(function (acc, _a) {
|
|
1069
|
+
var _b;
|
|
1070
|
+
var key = _a[0], value = _a[1];
|
|
1071
|
+
return (__assign(__assign({}, acc), (_b = {}, _b[key] = value, _b)));
|
|
1072
|
+
}, {}), _b));
|
|
1073
|
+
}, {})
|
|
1074
|
+
};
|
|
1075
|
+
return createReactable(baseFormState, options);
|
|
1076
|
+
};
|
|
1077
|
+
var createReactable = function (initialBaseState, options, initialFormState) {
|
|
1078
|
+
var _a, _b, _c;
|
|
1079
|
+
if (options === void 0) { options = {}; }
|
|
1080
|
+
var providers = {
|
|
1081
|
+
normalizers: __assign({}, (_a = options.providers) === null || _a === void 0 ? void 0 : _a.normalizers),
|
|
1082
|
+
validators: __assign(__assign({}, Validators), (_b = options.providers) === null || _b === void 0 ? void 0 : _b.validators),
|
|
1083
|
+
asyncValidators: __assign({}, (_c = options.providers) === null || _c === void 0 ? void 0 : _c.asyncValidators)
|
|
1084
|
+
};
|
|
1025
1085
|
var reducers = options.reducers, otherOptions = __rest(options, ["reducers"]);
|
|
1026
1086
|
var customReducers = Object.entries(reducers || {}).reduce(function (acc, _a) {
|
|
1027
1087
|
var _b;
|
|
@@ -1031,22 +1091,32 @@ var build = function (config, options) {
|
|
|
1031
1091
|
return __assign(__assign({}, acc), (_b = {}, _b[key] = {
|
|
1032
1092
|
reducer: function (_a, action) {
|
|
1033
1093
|
var form = _a.form;
|
|
1034
|
-
return _reducer(reducerTools, { form: form }, action);
|
|
1094
|
+
return _reducer(reducerTools(providers), { form: form }, action);
|
|
1035
1095
|
},
|
|
1036
1096
|
effects: effects
|
|
1037
1097
|
}, _b));
|
|
1038
1098
|
}, {});
|
|
1039
|
-
var
|
|
1099
|
+
var _d = core.RxBuilder(__assign({ initialState: initialBaseState, reducers: __assign({ updateValues: function (state, action, mergeChanges) {
|
|
1100
|
+
return updateValues(state, action, providers, mergeChanges);
|
|
1101
|
+
}, removeControl: function (state, action, mergeChanges) {
|
|
1102
|
+
return removeControl(state, action, providers, mergeChanges);
|
|
1103
|
+
}, addControl: function (state, action, mergeChanges) {
|
|
1104
|
+
return addControl(state, action, providers, mergeChanges);
|
|
1105
|
+
}, pushControl: function (state, action, mergeChanges) {
|
|
1106
|
+
return pushControl(state, action, providers, mergeChanges);
|
|
1107
|
+
}, resetControl: function (state, action, mergeChanges) {
|
|
1108
|
+
return resetControl(state, action, providers, mergeChanges);
|
|
1109
|
+
}, markControlAsPristine: markControlAsPristine, markControlAsTouched: markControlAsTouched, markControlAsUntouched: markControlAsUntouched }, customReducers) }, otherOptions)), hub1State$ = _d[0], hub1Actions = _d[1];
|
|
1040
1110
|
var state$ = core.RxBuilder({
|
|
1041
|
-
sources: [buildHub2Source(hub1State$)],
|
|
1042
|
-
initialState: null,
|
|
1111
|
+
sources: [buildHub2Source(hub1State$).pipe(operators.skip(initialFormState ? 1 : 0))],
|
|
1112
|
+
initialState: initialFormState || null,
|
|
1043
1113
|
reducers: {
|
|
1044
1114
|
formChange: formChange,
|
|
1045
1115
|
asyncValidation: {
|
|
1046
1116
|
reducer: asyncValidation,
|
|
1047
1117
|
effects: function (control) { return ({
|
|
1048
1118
|
key: control.key,
|
|
1049
|
-
effects: getScopedEffectsForControl(control)
|
|
1119
|
+
effects: getScopedEffectsForControl(control, providers)
|
|
1050
1120
|
}); }
|
|
1051
1121
|
},
|
|
1052
1122
|
asyncValidationResponseSuccess: asyncValidationResponseSuccess
|
|
@@ -1074,3 +1144,4 @@ exports.build = build;
|
|
|
1074
1144
|
exports.control = control;
|
|
1075
1145
|
exports.getArrayItems = getArrayItems;
|
|
1076
1146
|
exports.group = group;
|
|
1147
|
+
exports.load = load;
|
package/package.json
CHANGED
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
"author": "David Lai",
|
|
15
15
|
"license": "ISC",
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@reactables/core": "^0.7.0-alpha.
|
|
17
|
+
"@reactables/core": "^0.7.0-alpha.2",
|
|
18
18
|
"lodash.isequal": "^4.5.0"
|
|
19
19
|
},
|
|
20
20
|
"peerDependencies": {
|
|
@@ -23,5 +23,5 @@
|
|
|
23
23
|
"devDependencies": {
|
|
24
24
|
"lodash.clonedeep": "^4.5.0"
|
|
25
25
|
},
|
|
26
|
-
"version": "0.7.0-alpha.
|
|
26
|
+
"version": "0.7.0-alpha.2"
|
|
27
27
|
}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { FormControlConfig, FormArrayConfig, FormGroupConfig, AbstractControlConfig } from '../Models/Configs';
|
|
2
|
-
import { ValidatorFn, ValidatorAsyncFn } from '../Models/Validators';
|
|
3
|
-
type FbControl<T> = [T, (ValidatorFn | ValidatorFn[])?, (ValidatorAsyncFn | ValidatorAsyncFn[])?];
|
|
4
|
-
export declare const FormBuilder: {
|
|
5
|
-
control: <T>(config: FormControlConfig<T> | FbControl<T>) => FormControlConfig<T>;
|
|
6
|
-
array: (config: FormArrayConfig) => FormArrayConfig;
|
|
7
|
-
group: (config: FormGroupConfig) => FormGroupConfig;
|
|
8
|
-
build: (config: AbstractControlConfig, hub?: any) => any;
|
|
9
|
-
};
|
|
10
|
-
export {};
|