@reactables/forms 0.4.4-alpha.0 → 0.4.5-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.
- package/dist/Reducers/Hub1/index.d.ts +0 -2
- package/dist/Reducers/Hub2/mergeRemoveControl.d.ts +3 -0
- package/dist/Reducers/Hub2/mergeTouchUpdated.d.ts +6 -0
- package/dist/index.js +104 -92
- package/package.json +2 -2
- package/dist/Reducers/Hub1/updateDirty.d.ts +0 -2
- package/dist/Reducers/Hub1/updateDirty.test.d.ts +0 -1
|
@@ -4,6 +4,4 @@ export { markControlAsUntouched } from './markControlAsUntouched';
|
|
|
4
4
|
export { removeControl } from './removeControl';
|
|
5
5
|
export { resetControl } from './resetControl';
|
|
6
6
|
export { syncValidate } from './syncValidate';
|
|
7
|
-
export { updateDirty } from './updateDirty';
|
|
8
7
|
export { updateValues } from './updateValues';
|
|
9
|
-
export { buildHub1Reducer } from './buildHub1Reducer';
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { Form, BaseForm, FormControl } from '../../Models/Controls';
|
|
2
|
+
import { ControlRef } from '../../Models/ControlRef';
|
|
3
|
+
export declare const mergeTouchUpdated: <T>(state: Form<T>, form: BaseForm<T>, controlRef: ControlRef) => {
|
|
4
|
+
[x: string]: FormControl<unknown>;
|
|
5
|
+
root?: FormControl<T>;
|
|
6
|
+
};
|
package/dist/index.js
CHANGED
|
@@ -346,7 +346,8 @@ var isChildRef = function (controlRef, parentRef) {
|
|
|
346
346
|
var UPDATE_DESCENDANT_VALUES = 'UPDATE_DESCENDANT_VALUES';
|
|
347
347
|
var updateDescendants = function (form, _a) {
|
|
348
348
|
var _b = _a.payload, controlRef = _b.controlRef, value = _b.value;
|
|
349
|
-
var
|
|
349
|
+
var descendants = getDescendantControls(controlRef, form, true).map(function (control) { return [getFormKey(control.controlRef), control]; });
|
|
350
|
+
var result = descendants.reduce(function (acc, _a) {
|
|
350
351
|
var _b;
|
|
351
352
|
var key = _a[0], control = _a[1];
|
|
352
353
|
if (isChildRef(control.controlRef, controlRef)) {
|
|
@@ -435,14 +436,16 @@ var removeControl = function (_a, action) {
|
|
|
435
436
|
return { form: form, action: action };
|
|
436
437
|
var parentRef = controlRef.slice(0, -1);
|
|
437
438
|
var parentIsFormArray = Array.isArray(getControl(parentRef, form).config.controls);
|
|
439
|
+
var descendantkeys = getDescendantControls(controlRef, form).map(function (_a) {
|
|
440
|
+
var controlRef = _a.controlRef;
|
|
441
|
+
return getFormKey(controlRef);
|
|
442
|
+
});
|
|
443
|
+
var controlsRemoved = __assign({}, form);
|
|
444
|
+
descendantkeys.forEach(function (key) {
|
|
445
|
+
delete controlsRemoved[key];
|
|
446
|
+
});
|
|
438
447
|
// Remove control and all descendants
|
|
439
|
-
var controlRemoved = Object.entries(
|
|
440
|
-
.filter(function (_a) {
|
|
441
|
-
_a[0]; var control = _a[1];
|
|
442
|
-
return !(control.controlRef.length > parentRef.length &&
|
|
443
|
-
controlRef.every(function (key, index) { return key === control.controlRef[index]; }));
|
|
444
|
-
})
|
|
445
|
-
.reduce(function (acc, _a) {
|
|
448
|
+
var controlRemoved = Object.entries(controlsRemoved).reduce(function (acc, _a) {
|
|
446
449
|
var _b, _c;
|
|
447
450
|
var key = _a[0], control = _a[1];
|
|
448
451
|
// May need to reindex array items of removed control
|
|
@@ -551,7 +554,7 @@ var updateAncestorPristineValues = function (form, _a) {
|
|
|
551
554
|
return __assign(__assign({}, acc), (_b = {}, _b[controlRef.at(-1)] = pristineValue, _b));
|
|
552
555
|
}, {});
|
|
553
556
|
}
|
|
554
|
-
var newParentControl = __assign(__assign({}, form[parentKey]), { pristineValue: newValue });
|
|
557
|
+
var newParentControl = __assign(__assign({}, form[parentKey]), { pristineValue: newValue, dirty: isEqual__default["default"](form[parentKey].value, newValue) });
|
|
555
558
|
return updateAncestorPristineValues(__assign(__assign({}, form), (_b = {}, _b[parentKey] = newParentControl, _b)), {
|
|
556
559
|
type: UPDATE_ANCESTOR_PRISTINE_VALUES,
|
|
557
560
|
payload: parentRef
|
|
@@ -560,46 +563,32 @@ var updateAncestorPristineValues = function (form, _a) {
|
|
|
560
563
|
return form;
|
|
561
564
|
};
|
|
562
565
|
|
|
563
|
-
var updateDirty = function (form) {
|
|
564
|
-
return Object.entries(form).reduce(function (acc, _a) {
|
|
565
|
-
var _b;
|
|
566
|
-
var key = _a[0], control = _a[1];
|
|
567
|
-
return __assign(__assign({}, acc), (_b = {}, _b[key] = __assign(__assign({}, control), { dirty: !isEqual__default["default"](control.value, control.pristineValue) }), _b));
|
|
568
|
-
}, {});
|
|
569
|
-
};
|
|
570
|
-
|
|
571
566
|
var markControlAsPristine = function (_a, action) {
|
|
572
567
|
var form = _a.form;
|
|
573
568
|
var controlRef = action.payload;
|
|
574
|
-
var descendants = getDescendantControls(controlRef, form)
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
var key = _a[0], control = _a[1];
|
|
578
|
-
var isDescendant = descendants.includes(control);
|
|
579
|
-
return __assign(__assign({}, acc), (_b = {}, _b[key] = isDescendant
|
|
580
|
-
? __assign(__assign({}, control), { pristineValue: control.value, dirty: false }) : control, _b));
|
|
569
|
+
var descendants = getDescendantControls(controlRef, form).reduce(function (acc, control) {
|
|
570
|
+
var _a;
|
|
571
|
+
return (__assign(__assign({}, acc), (_a = {}, _a[getFormKey(control.controlRef)] = __assign(__assign({}, control), { dirty: false, pristineValue: control.value }), _a)));
|
|
581
572
|
}, {});
|
|
573
|
+
var result = __assign(__assign({}, form), descendants);
|
|
582
574
|
if (controlRef.length) {
|
|
583
575
|
result = updateAncestorPristineValues(result, {
|
|
584
576
|
type: UPDATE_ANCESTOR_PRISTINE_VALUES,
|
|
585
577
|
payload: controlRef
|
|
586
578
|
});
|
|
587
579
|
}
|
|
588
|
-
return { form:
|
|
580
|
+
return { form: result, action: action };
|
|
589
581
|
};
|
|
590
582
|
|
|
591
583
|
var markControlAsTouched = function (_a, action) {
|
|
592
584
|
var form = _a.form;
|
|
593
585
|
var _b = action.payload, controlRef = _b.controlRef, markAll = _b.markAll;
|
|
594
|
-
var controls = markAll
|
|
595
|
-
|
|
596
|
-
|
|
586
|
+
var controls = (markAll ? getControlBranch(controlRef, form) : getAncestorControls(controlRef, form)).reduce(function (acc, control) {
|
|
587
|
+
var _a;
|
|
588
|
+
return (__assign(__assign({}, acc), (_a = {}, _a[getFormKey(control.controlRef)] = __assign(__assign({}, control), { touched: true }), _a)));
|
|
589
|
+
}, {});
|
|
597
590
|
return {
|
|
598
|
-
form:
|
|
599
|
-
var _b;
|
|
600
|
-
var key = _a[0], control = _a[1];
|
|
601
|
-
return (__assign(__assign({}, acc), (_b = {}, _b[key] = __assign(__assign({}, control), { touched: controls.includes(control) ? true : control.touched }), _b)));
|
|
602
|
-
}, {}),
|
|
591
|
+
form: __assign(__assign({}, form), controls),
|
|
603
592
|
action: action
|
|
604
593
|
};
|
|
605
594
|
};
|
|
@@ -608,12 +597,11 @@ var markControlAsUntouched = function (_a, action) {
|
|
|
608
597
|
var _b;
|
|
609
598
|
var form = _a.form;
|
|
610
599
|
var controlRef = action.payload;
|
|
611
|
-
var
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
var key = _a[0], control = _a[1];
|
|
615
|
-
return (__assign(__assign({}, acc), (_b = {}, _b[key] = __assign(__assign({}, control), { touched: descendants.includes(control) ? false : control.touched }), _b)));
|
|
600
|
+
var result = getDescendantControls(controlRef, form).reduce(function (acc, control) {
|
|
601
|
+
var _a;
|
|
602
|
+
return (__assign(__assign({}, acc), (_a = {}, _a[getFormKey(control.controlRef)] = __assign(__assign({}, control), { touched: false }), _a)));
|
|
616
603
|
}, {});
|
|
604
|
+
result = __assign(__assign({}, form), result);
|
|
617
605
|
// Update ancestors
|
|
618
606
|
var currentRef = controlRef;
|
|
619
607
|
var key;
|
|
@@ -651,30 +639,27 @@ var resetControl = function (_a, action) {
|
|
|
651
639
|
|
|
652
640
|
var asyncValidation = function (form, _a) {
|
|
653
641
|
var controlRef = _a.payload.controlRef;
|
|
654
|
-
var
|
|
655
|
-
|
|
656
|
-
var
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
var _a;
|
|
663
|
-
return (__assign(__assign({}, acc), (_a = {}, _a[index] = true, _a)));
|
|
664
|
-
}, {})) : control.asyncValidateInProgress }), _b));
|
|
665
|
-
}
|
|
666
|
-
return __assign(__assign({}, acc), (_c = {}, _c[key] = control, _c));
|
|
642
|
+
var updatedSelfAndAncestors = getAncestorControls(controlRef, form).reduce(function (acc, control) {
|
|
643
|
+
var _a;
|
|
644
|
+
var isChangedControl = getFormKey(control.controlRef) === getFormKey(controlRef);
|
|
645
|
+
return __assign(__assign({}, acc), (_a = {}, _a[getFormKey(control.controlRef)] = __assign(__assign({}, control), { pending: true, asyncValidateInProgress: isChangedControl
|
|
646
|
+
? __assign({}, control.config.asyncValidators.reduce(function (acc, _, index) {
|
|
647
|
+
var _a;
|
|
648
|
+
return (__assign(__assign({}, acc), (_a = {}, _a[index] = true, _a)));
|
|
649
|
+
}, {})) : control.asyncValidateInProgress }), _a));
|
|
667
650
|
}, {});
|
|
668
|
-
return
|
|
651
|
+
return __assign(__assign({}, form), updatedSelfAndAncestors);
|
|
669
652
|
};
|
|
670
653
|
|
|
671
654
|
var hasErrors$1 = function (errors) {
|
|
672
655
|
return Object.values(errors).some(function (hasError) { return hasError; });
|
|
673
656
|
};
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
var
|
|
677
|
-
|
|
657
|
+
var mergeBranchErrors = function (form, controlRef) {
|
|
658
|
+
var controlBranch = getControlBranch(controlRef, form).reduce(function (acc, ctrl) {
|
|
659
|
+
var _a;
|
|
660
|
+
return __assign(__assign({}, acc), (_a = {}, _a[getFormKey(ctrl.controlRef)] = ctrl, _a));
|
|
661
|
+
}, {});
|
|
662
|
+
var errorsMerged = Object.entries(controlBranch)
|
|
678
663
|
.reverse()
|
|
679
664
|
.reduce(function (acc, _a) {
|
|
680
665
|
var _b;
|
|
@@ -684,21 +669,23 @@ var mergeErrors = function (form) {
|
|
|
684
669
|
var childrenValid = true;
|
|
685
670
|
if (Array.isArray(control.config.controls)) {
|
|
686
671
|
// If control is a FormArray
|
|
687
|
-
childrenValid = control.value.every(function (item, index) {
|
|
672
|
+
childrenValid = control.value.every(function (item, index) {
|
|
673
|
+
var formKey = getFormKey(control.controlRef.concat(index));
|
|
674
|
+
var valid = acc[formKey] === undefined ? form[formKey].valid : acc[formKey].valid;
|
|
675
|
+
return valid;
|
|
676
|
+
});
|
|
688
677
|
}
|
|
689
678
|
else if (control.config.controls) {
|
|
690
679
|
// If control is a FormGroup
|
|
691
|
-
childrenValid = Object.keys(control.value).every(function (childKey) {
|
|
680
|
+
childrenValid = Object.keys(control.value).every(function (childKey) {
|
|
681
|
+
var formKey = getFormKey(control.controlRef.concat(childKey));
|
|
682
|
+
var valid = acc[formKey] === undefined ? form[formKey].valid : acc[formKey].valid;
|
|
683
|
+
return valid;
|
|
684
|
+
});
|
|
692
685
|
}
|
|
693
686
|
return __assign(__assign({}, acc), (_b = {}, _b[key] = __assign(__assign({}, control), { errors: errors, valid: selfValid && childrenValid }), _b));
|
|
694
687
|
}, {});
|
|
695
|
-
|
|
696
|
-
.reverse()
|
|
697
|
-
.reduce(function (acc, key) {
|
|
698
|
-
acc[key] = errorsMerged[key];
|
|
699
|
-
return acc;
|
|
700
|
-
}, {});
|
|
701
|
-
return restoredOrder;
|
|
688
|
+
return __assign(__assign({}, form), errorsMerged);
|
|
702
689
|
};
|
|
703
690
|
|
|
704
691
|
var isControlValidating = function (control) {
|
|
@@ -727,18 +714,16 @@ var asyncValidationResponseSuccess = function (form, _a) {
|
|
|
727
714
|
}
|
|
728
715
|
return __assign(__assign({}, acc), (_c = {}, _c[key] = control, _c));
|
|
729
716
|
}, {});
|
|
730
|
-
return
|
|
717
|
+
return mergeBranchErrors(ancestorsUpdated, control.controlRef);
|
|
731
718
|
};
|
|
732
719
|
|
|
733
720
|
var hasErrors = function (errors) {
|
|
734
721
|
return Object.values(errors).some(function (hasError) { return hasError; });
|
|
735
722
|
};
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
}, {});
|
|
741
|
-
var errorsMerged = Object.entries(controlBranch)
|
|
723
|
+
// TODO: update merge errors to not do the entire form but just a subset of it
|
|
724
|
+
// TODO: separation of concern to update validity of control and ancestors
|
|
725
|
+
var mergeErrors = function (form) {
|
|
726
|
+
var errorsMerged = Object.entries(form)
|
|
742
727
|
.reverse()
|
|
743
728
|
.reduce(function (acc, _a) {
|
|
744
729
|
var _b;
|
|
@@ -748,23 +733,21 @@ var mergeBranchErrors = function (form, controlRef) {
|
|
|
748
733
|
var childrenValid = true;
|
|
749
734
|
if (Array.isArray(control.config.controls)) {
|
|
750
735
|
// If control is a FormArray
|
|
751
|
-
childrenValid = control.value.every(function (item, index) {
|
|
752
|
-
var formKey = getFormKey(control.controlRef.concat(index));
|
|
753
|
-
var valid = acc[formKey] === undefined ? form[formKey].valid : acc[formKey].valid;
|
|
754
|
-
return valid;
|
|
755
|
-
});
|
|
736
|
+
childrenValid = control.value.every(function (item, index) { return acc[getFormKey(control.controlRef.concat(index))].valid; });
|
|
756
737
|
}
|
|
757
738
|
else if (control.config.controls) {
|
|
758
739
|
// If control is a FormGroup
|
|
759
|
-
childrenValid = Object.keys(control.value).every(function (childKey) {
|
|
760
|
-
var formKey = getFormKey(control.controlRef.concat(childKey));
|
|
761
|
-
var valid = acc[formKey] === undefined ? form[formKey].valid : acc[formKey].valid;
|
|
762
|
-
return valid;
|
|
763
|
-
});
|
|
740
|
+
childrenValid = Object.keys(control.value).every(function (childKey) { return acc[getFormKey(control.controlRef.concat(childKey))].valid; });
|
|
764
741
|
}
|
|
765
742
|
return __assign(__assign({}, acc), (_b = {}, _b[key] = __assign(__assign({}, control), { errors: errors, valid: selfValid && childrenValid }), _b));
|
|
766
743
|
}, {});
|
|
767
|
-
|
|
744
|
+
var restoredOrder = Object.keys(errorsMerged)
|
|
745
|
+
.reverse()
|
|
746
|
+
.reduce(function (acc, key) {
|
|
747
|
+
acc[key] = errorsMerged[key];
|
|
748
|
+
return acc;
|
|
749
|
+
}, {});
|
|
750
|
+
return restoredOrder;
|
|
768
751
|
};
|
|
769
752
|
|
|
770
753
|
var mergeValueUpdated = function (state, form, controlRef) {
|
|
@@ -776,6 +759,31 @@ var mergeValueUpdated = function (state, form, controlRef) {
|
|
|
776
759
|
return mergeBranchErrors(__assign(__assign({}, state), controlBranch), controlRef);
|
|
777
760
|
};
|
|
778
761
|
|
|
762
|
+
var mergeRemoveControl = function (state, form, controlRef) {
|
|
763
|
+
var parentRef = controlRef.slice(0, -1);
|
|
764
|
+
var controlBranch = getControlBranch(parentRef, form).reduce(function (acc, baseControl) {
|
|
765
|
+
var _a;
|
|
766
|
+
var key = getFormKey(baseControl.controlRef);
|
|
767
|
+
var existingControl = getControlBranch(parentRef, state).find(function (control) { return baseControl.key === control.key; });
|
|
768
|
+
return __assign(__assign({}, acc), (_a = {}, _a[key] = __assign(__assign({}, existingControl), baseControl), _a));
|
|
769
|
+
}, {});
|
|
770
|
+
var removedControls = __assign({}, state);
|
|
771
|
+
var descendants = getDescendantControls(parentRef, state);
|
|
772
|
+
descendants.forEach(function (control) {
|
|
773
|
+
delete removedControls[getFormKey(control.controlRef)];
|
|
774
|
+
});
|
|
775
|
+
return mergeBranchErrors(__assign(__assign({}, removedControls), controlBranch), parentRef);
|
|
776
|
+
};
|
|
777
|
+
|
|
778
|
+
var mergeTouchUpdated = function (state, form, controlRef) {
|
|
779
|
+
var controlBranch = getControlBranch(controlRef, form).reduce(function (acc, control) {
|
|
780
|
+
var _a;
|
|
781
|
+
var key = getFormKey(control.controlRef);
|
|
782
|
+
return __assign(__assign({}, acc), (_a = {}, _a[key] = __assign(__assign({}, state[key]), control), _a));
|
|
783
|
+
}, {});
|
|
784
|
+
return __assign(__assign({}, state), controlBranch);
|
|
785
|
+
};
|
|
786
|
+
|
|
779
787
|
var DEFAULT_HUB2_FIELDS = {
|
|
780
788
|
asyncValidatorErrors: {},
|
|
781
789
|
asyncValidateInProgress: {},
|
|
@@ -795,7 +803,16 @@ var formChange = function (state, _a) {
|
|
|
795
803
|
}
|
|
796
804
|
switch (action === null || action === void 0 ? void 0 : action.type) {
|
|
797
805
|
case 'updateValues':
|
|
806
|
+
case 'addControl':
|
|
798
807
|
return mergeValueUpdated(state, form, action.payload.controlRef);
|
|
808
|
+
case 'resetControl':
|
|
809
|
+
case 'removeControl':
|
|
810
|
+
return mergeRemoveControl(state, form, action.payload);
|
|
811
|
+
case 'markControlAsPristine':
|
|
812
|
+
case 'markControlAsUntouched':
|
|
813
|
+
return mergeTouchUpdated(state, form, action.payload);
|
|
814
|
+
case 'markControlAsTouched':
|
|
815
|
+
return mergeTouchUpdated(state, form, action.payload.controlRef);
|
|
799
816
|
default:
|
|
800
817
|
return mergeErrors(Object.entries(form).reduce(function (acc, _a) {
|
|
801
818
|
var _b;
|
|
@@ -865,17 +882,12 @@ var RxForm = {
|
|
|
865
882
|
};
|
|
866
883
|
|
|
867
884
|
var getArrayItems = function (controlRef, form) {
|
|
868
|
-
|
|
885
|
+
var control = getControl(controlRef, form);
|
|
886
|
+
if (!Array.isArray(control.config.controls)) {
|
|
869
887
|
throw "".concat(controlRef.join('.'), " is not a Form Array control");
|
|
870
888
|
}
|
|
871
|
-
return
|
|
872
|
-
|
|
873
|
-
var isItem = controlRef.every(function (key, index) { return key === control.controlRef[index]; }) &&
|
|
874
|
-
control.controlRef.length === controlRef.length + 1 &&
|
|
875
|
-
typeof control.controlRef[controlRef.length] === 'number';
|
|
876
|
-
return isItem;
|
|
877
|
-
})
|
|
878
|
-
.sort(function (a, b) { return +a.controlRef.slice(-1) - +b.controlRef.slice(-1); });
|
|
889
|
+
var result = control.value.map(function (_, index) { return form[getFormKey(controlRef.concat(index))]; });
|
|
890
|
+
return result;
|
|
879
891
|
};
|
|
880
892
|
|
|
881
893
|
exports.ControlModels = Controls;
|
package/package.json
CHANGED
|
@@ -14,12 +14,12 @@
|
|
|
14
14
|
"author": "David Lai",
|
|
15
15
|
"license": "ISC",
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@reactables/core": "^0.4.
|
|
17
|
+
"@reactables/core": "^0.4.5-alpha.0",
|
|
18
18
|
"lodash.clonedeep": "^4.5.0",
|
|
19
19
|
"lodash.isequal": "^4.5.0"
|
|
20
20
|
},
|
|
21
21
|
"peerDependencies": {
|
|
22
22
|
"rxjs": "^6.0.0 || ^7.0.0"
|
|
23
23
|
},
|
|
24
|
-
"version": "0.4.
|
|
24
|
+
"version": "0.4.5-alpha.0"
|
|
25
25
|
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|