@reactables/forms 1.0.1 → 1.0.2-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.
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { Observable } from 'rxjs';
|
|
2
2
|
import { Action } from '@reactables/core';
|
|
3
3
|
import { BaseFormState } from '../Models/Controls';
|
|
4
|
-
export declare const buildHub2Source: <T>(hub1State$: Observable<BaseFormState<T
|
|
4
|
+
export declare const buildHub2Source: <T>(hub1State$: Observable<BaseFormState<T>>, initialBaseState: BaseFormState<T>) => Observable<Action<T>>;
|
|
@@ -2,4 +2,4 @@ import { Action } from '@reactables/core';
|
|
|
2
2
|
import { BaseFormState } from '../../Models/Controls';
|
|
3
3
|
import { UpdateValuesPayload } from '../../Models/Payloads';
|
|
4
4
|
import { RxFormProviders } from '../../RxForm/RxForm';
|
|
5
|
-
export declare const updateValues: <T>({ form, _changedControls,
|
|
5
|
+
export declare const updateValues: <T>({ form, _changedControls, _removedControls }: BaseFormState<T>, action: Action<UpdateValuesPayload<unknown>>, providers: RxFormProviders, mergeChanges?: boolean) => BaseFormState<T>;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Form, FormControl, BaseFormState } from '../../Models/Controls';
|
|
2
|
-
export declare const mergeControls: <T>(state: Form<T>, { form, _changedControls,
|
|
2
|
+
export declare const mergeControls: <T>(state: Form<T>, { form, _changedControls, _removedControls }: BaseFormState<unknown>) => {
|
|
3
3
|
[x: string]: FormControl<unknown>;
|
|
4
4
|
root?: FormControl<unknown>;
|
|
5
5
|
};
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { ValidatorAsyncFn } from '../Models/Validators';
|
|
2
2
|
export declare const uniqueEmail: ValidatorAsyncFn;
|
|
3
|
+
export declare const noError: ValidatorAsyncFn;
|
|
4
|
+
export declare const noError2: ValidatorAsyncFn;
|
|
3
5
|
export declare const blacklistedEmail: ValidatorAsyncFn;
|
|
4
6
|
export declare const arrayLengthError: ValidatorAsyncFn;
|
|
5
7
|
export declare const uniqueFirstAndLastName: ValidatorAsyncFn;
|
package/dist/index.js
CHANGED
|
@@ -237,13 +237,16 @@ var getAsyncValidationActions = function (formControls) {
|
|
|
237
237
|
}, []);
|
|
238
238
|
};
|
|
239
239
|
|
|
240
|
-
var buildHub2Source = function (hub1State
|
|
240
|
+
var buildHub2Source = function (hub1State$, initialBaseState) {
|
|
241
241
|
var hub1StateMapped$ = hub1State$.pipe(operators.map(function (payload) { return ({ type: 'formChange', payload: payload }); }));
|
|
242
|
-
var
|
|
243
|
-
|
|
244
|
-
var
|
|
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) : [];
|
|
245
248
|
var asyncValidationActions = getAsyncValidationActions(controlsToCheck);
|
|
246
|
-
return rxjs.of.apply(void 0, __spreadArray([
|
|
249
|
+
return rxjs.of.apply(void 0, __spreadArray([currAction], asyncValidationActions, false));
|
|
247
250
|
}));
|
|
248
251
|
return sourceForHub2$;
|
|
249
252
|
};
|
|
@@ -404,7 +407,7 @@ var updateDescendantValues = function (form, _a, providers) {
|
|
|
404
407
|
// Use AddControlPayload/RemoveControl action reducers to add/remove control
|
|
405
408
|
var updateValues = function (_a, action, providers, mergeChanges) {
|
|
406
409
|
var _b, _c;
|
|
407
|
-
var form = _a.form, _d = _a._changedControls, _changedControls = _d === void 0 ? {} : _d, _e = _a.
|
|
410
|
+
var form = _a.form, _d = _a._changedControls, _changedControls = _d === void 0 ? {} : _d, _e = _a._removedControls, _removedControls = _e === void 0 ? {} : _e;
|
|
408
411
|
if (mergeChanges === void 0) { mergeChanges = false; }
|
|
409
412
|
var normalizers = providers.normalizers;
|
|
410
413
|
var _f = action.payload, controlRef = _f.controlRef, value = _f.value;
|
|
@@ -454,7 +457,7 @@ var updateValues = function (_a, action, providers, mergeChanges) {
|
|
|
454
457
|
var _a;
|
|
455
458
|
return (__assign(__assign({}, acc), (_a = {}, _a[control.key] = control, _a)));
|
|
456
459
|
}, {});
|
|
457
|
-
var mergedResult = __assign(__assign({}, result), { _changedControls: __assign(__assign(__assign({}, (mergeChanges ? _changedControls : undefined)), changedAncestorControls), result._changedControls),
|
|
460
|
+
var mergedResult = __assign(__assign({}, result), { _changedControls: __assign(__assign(__assign({}, (mergeChanges ? _changedControls : undefined)), changedAncestorControls), result._changedControls), _removedControls: mergeChanges ? _removedControls : undefined });
|
|
458
461
|
return mergedResult;
|
|
459
462
|
};
|
|
460
463
|
|
|
@@ -564,7 +567,7 @@ var removeControl = function (state, action, providers, mergeChanges) {
|
|
|
564
567
|
}
|
|
565
568
|
}, {});
|
|
566
569
|
}
|
|
567
|
-
var
|
|
570
|
+
var _removedControls = __assign(__assign({}, (mergeChanges ? state._removedControls || {} : undefined)), (_a = {}, _a[controlToRemove.key] = controlToRemove, _a));
|
|
568
571
|
descendants
|
|
569
572
|
.map(function (_a) {
|
|
570
573
|
var key = _a.key;
|
|
@@ -576,7 +579,7 @@ var removeControl = function (state, action, providers, mergeChanges) {
|
|
|
576
579
|
return {
|
|
577
580
|
form: result,
|
|
578
581
|
_changedControls: _changedControls,
|
|
579
|
-
|
|
582
|
+
_removedControls: _removedControls
|
|
580
583
|
};
|
|
581
584
|
};
|
|
582
585
|
|
|
@@ -634,7 +637,7 @@ var addControl = function (state, action, providers, mergeChanges) {
|
|
|
634
637
|
return {
|
|
635
638
|
form: ancestorsUpdated,
|
|
636
639
|
_changedControls: __assign(__assign({}, (mergeChanges ? state._changedControls || {} : undefined)), _changedControls),
|
|
637
|
-
|
|
640
|
+
_removedControls: mergeChanges ? state._removedControls || {} : undefined
|
|
638
641
|
};
|
|
639
642
|
};
|
|
640
643
|
|
|
@@ -666,7 +669,7 @@ var pushControl = function (state, action, providers, mergeChanges) {
|
|
|
666
669
|
return {
|
|
667
670
|
form: ancestorsUpdated,
|
|
668
671
|
_changedControls: __assign(__assign({}, (mergeChanges ? state._changedControls || {} : undefined)), _changedControls),
|
|
669
|
-
|
|
672
|
+
_removedControls: mergeChanges ? state._removedControls || {} : undefined
|
|
670
673
|
};
|
|
671
674
|
};
|
|
672
675
|
|
|
@@ -729,7 +732,7 @@ var markControlAsPristine = function (state, action, mergeChanges) {
|
|
|
729
732
|
return {
|
|
730
733
|
form: result,
|
|
731
734
|
_changedControls: __assign(__assign({}, (mergeChanges ? state._changedControls || {} : undefined)), _changedControls),
|
|
732
|
-
|
|
735
|
+
_removedControls: mergeChanges ? state._removedControls || {} : undefined
|
|
733
736
|
};
|
|
734
737
|
};
|
|
735
738
|
|
|
@@ -753,7 +756,7 @@ var markControlAsTouched = function (state, action, mergeChanges) {
|
|
|
753
756
|
return {
|
|
754
757
|
form: __assign(__assign({}, form), controls),
|
|
755
758
|
_changedControls: __assign(__assign({}, (mergeChanges ? state._changedControls || {} : undefined)), _changedControls),
|
|
756
|
-
|
|
759
|
+
_removedControls: mergeChanges ? state._removedControls || {} : undefined
|
|
757
760
|
};
|
|
758
761
|
};
|
|
759
762
|
|
|
@@ -783,7 +786,7 @@ var markControlAsUntouched = function (state, action, mergeChanges) {
|
|
|
783
786
|
return {
|
|
784
787
|
form: result,
|
|
785
788
|
_changedControls: __assign(__assign({}, (mergeChanges ? state._changedControls || {} : undefined)), _changedControls),
|
|
786
|
-
|
|
789
|
+
_removedControls: mergeChanges ? state._removedControls || {} : undefined
|
|
787
790
|
};
|
|
788
791
|
};
|
|
789
792
|
|
|
@@ -814,7 +817,7 @@ var resetControl = function (state, action, providers, mergeChanges) {
|
|
|
814
817
|
var _a;
|
|
815
818
|
return (__assign(__assign({}, acc), (_a = {}, _a[control.key] = control, _a)));
|
|
816
819
|
}, {}));
|
|
817
|
-
var
|
|
820
|
+
var _removedControls = __assign(__assign({}, (mergeChanges ? state._removedControls || {} : undefined)), (_a = {}, _a[controlToReset.key] = controlToReset, _a));
|
|
818
821
|
// If control is removed, we can delete it from the _changedControls check
|
|
819
822
|
descendants
|
|
820
823
|
.map(function (_a) {
|
|
@@ -827,7 +830,7 @@ var resetControl = function (state, action, providers, mergeChanges) {
|
|
|
827
830
|
return {
|
|
828
831
|
form: result,
|
|
829
832
|
_changedControls: _changedControls,
|
|
830
|
-
|
|
833
|
+
_removedControls: _removedControls
|
|
831
834
|
};
|
|
832
835
|
};
|
|
833
836
|
|
|
@@ -836,7 +839,7 @@ var asyncValidation = function (form, _a) {
|
|
|
836
839
|
var updatedSelfAndAncestors = getAncestorControls(controlRef, form).reduce(function (acc, control) {
|
|
837
840
|
var _a;
|
|
838
841
|
var isChangedControl = getFormKey(control.controlRef) === getFormKey(controlRef);
|
|
839
|
-
return __assign(__assign({}, acc), (_a = {}, _a[getFormKey(control.controlRef)] = __assign(__assign({}, control), { pending: true, asyncValidateInProgress: isChangedControl
|
|
842
|
+
return __assign(__assign({}, acc), (_a = {}, _a[getFormKey(control.controlRef)] = __assign(__assign({}, control), { pending: true, valid: false, asyncValidateInProgress: isChangedControl
|
|
840
843
|
? __assign({}, control.config.asyncValidators.reduce(function (acc, _, index) {
|
|
841
844
|
var _a;
|
|
842
845
|
return (__assign(__assign({}, acc), (_a = {}, _a[index] = true, _a)));
|
|
@@ -886,7 +889,7 @@ var mergeBranchErrors = function (form, controlRef) {
|
|
|
886
889
|
return valid;
|
|
887
890
|
});
|
|
888
891
|
}
|
|
889
|
-
return __assign(__assign({}, acc), (_b = {}, _b[key] = __assign(__assign({}, control), { errors: errors, valid: selfValid && childrenValid, childrenValid: childrenValid }), _b));
|
|
892
|
+
return __assign(__assign({}, acc), (_b = {}, _b[key] = __assign(__assign({}, control), { errors: errors, valid: selfValid && childrenValid && !control.pending, childrenValid: childrenValid }), _b));
|
|
890
893
|
}, {});
|
|
891
894
|
var errorsMergedOrderRestored = reverseObjectKeys(errorsMerged);
|
|
892
895
|
return __assign(__assign({}, form), errorsMergedOrderRestored);
|
|
@@ -914,7 +917,8 @@ var asyncValidationResponseSuccess = function (form, _a) {
|
|
|
914
917
|
var key = _a[0], control = _a[1];
|
|
915
918
|
if (ancestors.includes(control)) {
|
|
916
919
|
var descendants = getDescendantControls(control.controlRef, controlUpdated);
|
|
917
|
-
|
|
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));
|
|
918
922
|
}
|
|
919
923
|
return __assign(__assign({}, acc), (_c = {}, _c[key] = control, _c));
|
|
920
924
|
}, {});
|
|
@@ -943,7 +947,7 @@ var mergeErrors = function (form) {
|
|
|
943
947
|
// If control is a FormGroup
|
|
944
948
|
childrenValid = Object.keys(control.value).every(function (childKey) { return acc[getFormKey(control.controlRef.concat(childKey))].valid; });
|
|
945
949
|
}
|
|
946
|
-
return __assign(__assign({}, acc), (_b = {}, _b[key] = __assign(__assign({}, control), { errors: errors, valid: selfValid && childrenValid, childrenValid: childrenValid }), _b));
|
|
950
|
+
return __assign(__assign({}, acc), (_b = {}, _b[key] = __assign(__assign({}, control), { errors: errors, valid: selfValid && childrenValid && !control.pending, childrenValid: childrenValid }), _b));
|
|
947
951
|
}, {});
|
|
948
952
|
var restoredOrder = reverseObjectKeys(errorsMerged);
|
|
949
953
|
return restoredOrder;
|
|
@@ -981,25 +985,25 @@ var mergeRemoveControl = function (state, form, controlRef) {
|
|
|
981
985
|
return valid;
|
|
982
986
|
});
|
|
983
987
|
}
|
|
984
|
-
return __assign(__assign({}, acc), (_a = {}, _a[key] = __assign(__assign(__assign({}, existingControl), baseControl), { errors: errors, valid: selfValid && childrenValid, childrenValid: childrenValid }), _a));
|
|
988
|
+
return __assign(__assign({}, acc), (_a = {}, _a[key] = __assign(__assign(__assign({}, existingControl), baseControl), { errors: errors, valid: selfValid && childrenValid && !existingControl.pending, childrenValid: childrenValid }), _a));
|
|
985
989
|
}, {});
|
|
986
990
|
var updatedControlBranchOrderRestored = reverseObjectKeys(updatedControlBranch);
|
|
987
991
|
var descendants = existingBranch.filter(function (control) { return control.controlRef.length > parentRef.length; });
|
|
988
|
-
var
|
|
992
|
+
var _removedControls = __assign({}, state);
|
|
989
993
|
descendants.forEach(function (control) {
|
|
990
|
-
delete
|
|
994
|
+
delete _removedControls[getFormKey(control.controlRef)];
|
|
991
995
|
});
|
|
992
|
-
delete
|
|
993
|
-
return __assign(__assign({},
|
|
996
|
+
delete _removedControls[getFormKey(controlRef)];
|
|
997
|
+
return __assign(__assign({}, _removedControls), updatedControlBranchOrderRestored);
|
|
994
998
|
};
|
|
995
999
|
|
|
996
1000
|
var hasErrors = function (errors) {
|
|
997
1001
|
return Object.values(errors).some(function (hasError) { return hasError; });
|
|
998
1002
|
};
|
|
999
1003
|
var mergeControls = function (state, _a) {
|
|
1000
|
-
var form = _a.form, _b = _a._changedControls, _changedControls = _b === void 0 ? {} : _b,
|
|
1001
|
-
var controlsRemoved =
|
|
1002
|
-
? Object.values(
|
|
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) {
|
|
1003
1007
|
var controlRef = _a.controlRef;
|
|
1004
1008
|
return mergeRemoveControl(acc, form, controlRef);
|
|
1005
1009
|
}, state)
|
|
@@ -1032,7 +1036,7 @@ var mergeControls = function (state, _a) {
|
|
|
1032
1036
|
return valid;
|
|
1033
1037
|
});
|
|
1034
1038
|
}
|
|
1035
|
-
return __assign(__assign({}, acc), (_a = {}, _a[formKey] = __assign(__assign({}, newControl), { errors: errors, valid: selfValid && childrenValid, childrenValid: childrenValid }), _a));
|
|
1039
|
+
return __assign(__assign({}, acc), (_a = {}, _a[formKey] = __assign(__assign({}, newControl), { errors: errors, valid: selfValid && childrenValid && !newControl.pending, childrenValid: childrenValid }), _a));
|
|
1036
1040
|
}, {});
|
|
1037
1041
|
var orderRestored = reverseObjectKeys(updatedBranch);
|
|
1038
1042
|
var result = __assign(__assign({}, controlsRemoved), orderRestored);
|
|
@@ -1160,7 +1164,7 @@ var createReactable = function (initialBaseState, options, initialFormState) {
|
|
|
1160
1164
|
return resetControl(state, action, providers, mergeChanges);
|
|
1161
1165
|
}, markControlAsPristine: markControlAsPristine, markControlAsTouched: markControlAsTouched, markControlAsUntouched: markControlAsUntouched }, customReducers) }, otherOptions)), hub1State$ = _d[0], hub1Actions = _d[1];
|
|
1162
1166
|
var state$ = core.RxBuilder({
|
|
1163
|
-
sources: [buildHub2Source(hub1State
|
|
1167
|
+
sources: [buildHub2Source(hub1State$, initialBaseState).pipe(operators.skip(initialFormState ? 1 : 0))],
|
|
1164
1168
|
initialState: initialFormState || null,
|
|
1165
1169
|
name: "Stage 2 ".concat(name ? name : 'rxForm'),
|
|
1166
1170
|
debug: debug,
|
package/package.json
CHANGED
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
"author": "David Lai",
|
|
15
15
|
"license": "ISC",
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@reactables/core": "^1.0.1",
|
|
17
|
+
"@reactables/core": "^1.0.2-beta.1",
|
|
18
18
|
"lodash.isequal": "^4.5.0"
|
|
19
19
|
},
|
|
20
20
|
"peerDependencies": {
|
|
@@ -23,5 +23,5 @@
|
|
|
23
23
|
"devDependencies": {
|
|
24
24
|
"lodash.clonedeep": "^4.5.0"
|
|
25
25
|
},
|
|
26
|
-
"version": "1.0.1"
|
|
26
|
+
"version": "1.0.2-beta.1"
|
|
27
27
|
}
|