@reactables/forms 0.4.5-alpha.0 → 0.4.6-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.
@@ -39,4 +39,5 @@ export interface Form<T> {
39
39
  root?: FormControl<T>;
40
40
  [key: string]: FormControl<unknown>;
41
41
  }
42
+ export declare const DEFAULT_HUB2_FIELDS: Hub2Fields;
42
43
  export {};
@@ -1,4 +1,3 @@
1
1
  import { Reducer } from '@reactables/core';
2
- import { Form, Hub2Fields } from '../../Models/Controls';
3
- export declare const DEFAULT_HUB2_FIELDS: Hub2Fields;
2
+ import { Form } from '../../Models/Controls';
4
3
  export declare const formChange: Reducer<Form<unknown>>;
@@ -1,3 +1,6 @@
1
- import { Form, BaseForm } from '../../Models/Controls';
1
+ import { Form, BaseForm, FormControl } from '../../Models/Controls';
2
2
  import { ControlRef } from '../../Models/ControlRef';
3
- export declare const mergeRemoveControl: <T>(state: Form<T>, form: BaseForm<T>, controlRef: ControlRef) => Form<T>;
3
+ export declare const mergeRemoveControl: <T>(state: Form<T>, form: BaseForm<T>, controlRef: ControlRef) => {
4
+ [x: string]: FormControl<unknown>;
5
+ root?: FormControl<T>;
6
+ };
@@ -1,3 +1,6 @@
1
- import { Form, BaseForm } from '../../Models/Controls';
1
+ import { Form, BaseForm, FormControl } from '../../Models/Controls';
2
2
  import { ControlRef } from '../../Models/ControlRef';
3
- export declare const mergeValueUpdated: <T>(state: Form<T>, form: BaseForm<T>, controlRef: ControlRef) => Form<T>;
3
+ export declare const mergeValueUpdated: <T>(state: Form<T>, form: BaseForm<T>, controlRef: ControlRef) => {
4
+ [x: string]: FormControl<unknown>;
5
+ root?: FormControl<T>;
6
+ };
package/dist/index.js CHANGED
@@ -11,8 +11,17 @@ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'defau
11
11
 
12
12
  var isEqual__default = /*#__PURE__*/_interopDefaultLegacy(isEqual);
13
13
 
14
+ var DEFAULT_HUB2_FIELDS = {
15
+ asyncValidatorErrors: {},
16
+ asyncValidateInProgress: {},
17
+ pending: false,
18
+ valid: null,
19
+ errors: null
20
+ };
21
+
14
22
  var Controls = /*#__PURE__*/Object.freeze({
15
- __proto__: null
23
+ __proto__: null,
24
+ DEFAULT_HUB2_FIELDS: DEFAULT_HUB2_FIELDS
16
25
  });
17
26
 
18
27
  var required = function (value) {
@@ -651,7 +660,7 @@ var asyncValidation = function (form, _a) {
651
660
  return __assign(__assign({}, form), updatedSelfAndAncestors);
652
661
  };
653
662
 
654
- var hasErrors$1 = function (errors) {
663
+ var hasErrors$3 = function (errors) {
655
664
  return Object.values(errors).some(function (hasError) { return hasError; });
656
665
  };
657
666
  var mergeBranchErrors = function (form, controlRef) {
@@ -665,7 +674,7 @@ var mergeBranchErrors = function (form, controlRef) {
665
674
  var _b;
666
675
  var key = _a[0], control = _a[1];
667
676
  var errors = __assign(__assign({}, control.validatorErrors), control.asyncValidatorErrors);
668
- var selfValid = !hasErrors$1(errors);
677
+ var selfValid = !hasErrors$3(errors);
669
678
  var childrenValid = true;
670
679
  if (Array.isArray(control.config.controls)) {
671
680
  // If control is a FormArray
@@ -717,7 +726,7 @@ var asyncValidationResponseSuccess = function (form, _a) {
717
726
  return mergeBranchErrors(ancestorsUpdated, control.controlRef);
718
727
  };
719
728
 
720
- var hasErrors = function (errors) {
729
+ var hasErrors$2 = function (errors) {
721
730
  return Object.values(errors).some(function (hasError) { return hasError; });
722
731
  };
723
732
  // TODO: update merge errors to not do the entire form but just a subset of it
@@ -729,7 +738,7 @@ var mergeErrors = function (form) {
729
738
  var _b;
730
739
  var key = _a[0], control = _a[1];
731
740
  var errors = __assign(__assign({}, control.validatorErrors), control.asyncValidatorErrors);
732
- var selfValid = !hasErrors(errors);
741
+ var selfValid = !hasErrors$2(errors);
733
742
  var childrenValid = true;
734
743
  if (Array.isArray(control.config.controls)) {
735
744
  // If control is a FormArray
@@ -750,29 +759,80 @@ var mergeErrors = function (form) {
750
759
  return restoredOrder;
751
760
  };
752
761
 
762
+ var hasErrors$1 = function (errors) {
763
+ return Object.values(errors).some(function (hasError) { return hasError; });
764
+ };
753
765
  var mergeValueUpdated = function (state, form, controlRef) {
754
- var controlBranch = getControlBranch(controlRef, form).reduce(function (acc, control) {
766
+ var controlBranch = getControlBranch(controlRef, form)
767
+ .reverse()
768
+ .reduce(function (acc, control) {
755
769
  var _a;
756
770
  var key = getFormKey(control.controlRef);
757
- return __assign(__assign({}, acc), (_a = {}, _a[key] = __assign(__assign({}, state[key]), control), _a));
771
+ var existingControl = state[key] || structuredClone(DEFAULT_HUB2_FIELDS);
772
+ var errors = __assign(__assign({}, control.validatorErrors), existingControl.asyncValidatorErrors);
773
+ var selfValid = !hasErrors$1(errors);
774
+ var childrenValid = true;
775
+ if (Array.isArray(control.config.controls)) {
776
+ // If control is a FormArray
777
+ childrenValid = control.value.every(function (item, index) {
778
+ var formKey = getFormKey(control.controlRef.concat(index));
779
+ var valid = acc[formKey] === undefined ? state[formKey].valid : acc[formKey].valid;
780
+ return valid;
781
+ });
782
+ }
783
+ else if (control.config.controls) {
784
+ // If control is a FormGroup
785
+ childrenValid = Object.keys(control.value).every(function (childKey) {
786
+ var formKey = getFormKey(control.controlRef.concat(childKey));
787
+ var valid = acc[formKey] === undefined ? state[formKey].valid : acc[formKey].valid;
788
+ return valid;
789
+ });
790
+ }
791
+ return __assign(__assign({}, acc), (_a = {}, _a[key] = __assign(__assign(__assign({}, existingControl), control), { errors: errors, valid: selfValid && childrenValid }), _a));
758
792
  }, {});
759
- return mergeBranchErrors(__assign(__assign({}, state), controlBranch), controlRef);
793
+ return __assign(__assign({}, state), controlBranch);
760
794
  };
761
795
 
796
+ var hasErrors = function (errors) {
797
+ return Object.values(errors).some(function (hasError) { return hasError; });
798
+ };
762
799
  var mergeRemoveControl = function (state, form, controlRef) {
763
800
  var parentRef = controlRef.slice(0, -1);
764
- var controlBranch = getControlBranch(parentRef, form).reduce(function (acc, baseControl) {
801
+ var existingBranch = getControlBranch(parentRef, state);
802
+ var descendants = existingBranch.filter(function (control) { return control.controlRef.length < parentRef.length; });
803
+ var controlBranch = getControlBranch(parentRef, form)
804
+ .reverse()
805
+ .reduce(function (acc, baseControl) {
765
806
  var _a;
766
807
  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));
808
+ var existingControl = existingBranch.find(function (control) { return baseControl.key === control.key; }) ||
809
+ structuredClone(DEFAULT_HUB2_FIELDS);
810
+ var errors = __assign(__assign({}, baseControl.validatorErrors), existingControl.asyncValidatorErrors);
811
+ var selfValid = !hasErrors(errors);
812
+ var childrenValid = true;
813
+ if (Array.isArray(baseControl.config.controls)) {
814
+ // If control is a FormArray
815
+ childrenValid = baseControl.value.every(function (item, index) {
816
+ var formKey = getFormKey(baseControl.controlRef.concat(index));
817
+ var valid = acc[formKey] === undefined ? state[formKey].valid : acc[formKey].valid;
818
+ return valid;
819
+ });
820
+ }
821
+ else if (baseControl.config.controls) {
822
+ // If control is a FormGroup
823
+ childrenValid = Object.keys(baseControl.value).every(function (childKey) {
824
+ var formKey = getFormKey(baseControl.controlRef.concat(childKey));
825
+ var valid = acc[formKey] === undefined ? state[formKey].valid : acc[formKey].valid;
826
+ return valid;
827
+ });
828
+ }
829
+ return __assign(__assign({}, acc), (_a = {}, _a[key] = __assign(__assign(__assign({}, existingControl), baseControl), { errors: errors, valid: selfValid && childrenValid }), _a));
769
830
  }, {});
770
831
  var removedControls = __assign({}, state);
771
- var descendants = getDescendantControls(parentRef, state);
772
832
  descendants.forEach(function (control) {
773
833
  delete removedControls[getFormKey(control.controlRef)];
774
834
  });
775
- return mergeBranchErrors(__assign(__assign({}, removedControls), controlBranch), parentRef);
835
+ return __assign(__assign({}, removedControls), controlBranch);
776
836
  };
777
837
 
778
838
  var mergeTouchUpdated = function (state, form, controlRef) {
@@ -784,13 +844,6 @@ var mergeTouchUpdated = function (state, form, controlRef) {
784
844
  return __assign(__assign({}, state), controlBranch);
785
845
  };
786
846
 
787
- var DEFAULT_HUB2_FIELDS = {
788
- asyncValidatorErrors: {},
789
- asyncValidateInProgress: {},
790
- pending: false,
791
- valid: null,
792
- errors: null
793
- };
794
847
  var formChange = function (state, _a) {
795
848
  if (state === void 0) { state = null; }
796
849
  var _b = _a.payload, form = _b.form, action = _b.action;
@@ -802,8 +855,8 @@ var formChange = function (state, _a) {
802
855
  }, {}));
803
856
  }
804
857
  switch (action === null || action === void 0 ? void 0 : action.type) {
805
- case 'updateValues':
806
858
  case 'addControl':
859
+ case 'updateValues':
807
860
  return mergeValueUpdated(state, form, action.payload.controlRef);
808
861
  case 'resetControl':
809
862
  case 'removeControl':
@@ -817,9 +870,7 @@ var formChange = function (state, _a) {
817
870
  return mergeErrors(Object.entries(form).reduce(function (acc, _a) {
818
871
  var _b;
819
872
  var dictKey = _a[0], baseControl = _a[1];
820
- var existingControl = (action === null || action === void 0 ? void 0 : action.type) === 'removeControl'
821
- ? state && Object.values(state).find(function (control) { return baseControl.key === control.key; })
822
- : state && state[dictKey];
873
+ var existingControl = state && state[dictKey];
823
874
  return __assign(__assign({}, acc), (_b = {}, _b[dictKey] = __assign(__assign({}, (existingControl
824
875
  ? existingControl
825
876
  : structuredClone(DEFAULT_HUB2_FIELDS))), baseControl), _b));
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.5-alpha.0",
17
+ "@reactables/core": "^0.4.6-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.5-alpha.0"
24
+ "version": "0.4.6-alpha.0"
25
25
  }