@klippa/ngx-enhancy-forms 11.5.2 → 11.6.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.
@@ -3,7 +3,7 @@ import { Directive, Input, Component, SkipSelf, Optional, InjectionToken, Host,
3
3
  import * as i1 from '@angular/common';
4
4
  import { CommonModule } from '@angular/common';
5
5
  import * as i2 from '@angular/forms';
6
- import { UntypedFormArray, UntypedFormGroup, UntypedFormControl, NG_VALUE_ACCESSOR, FormsModule } from '@angular/forms';
6
+ import { UntypedFormArray, UntypedFormGroup, UntypedFormControl, FormControl, FormArray, FormGroup, NG_VALUE_ACCESSOR, FormsModule } from '@angular/forms';
7
7
  import { isString } from 'lodash-es';
8
8
  import { isArray } from 'lodash';
9
9
  import * as i4 from '@ng-select/ng-select';
@@ -86,6 +86,44 @@ function splitArrayByCondition(value, condition) {
86
86
  }, [[]]);
87
87
  }
88
88
 
89
+ function mergeArray(arrA, arrB) {
90
+ const arr = new Array(Math.max(arrA.length, arrB.length));
91
+ for (let i = 0; i < arr.length; i++) {
92
+ if (typeof arrA[i] === 'object' && typeof arrB[i] === 'object') {
93
+ arr[i] = deepMerge(arrA[i], arrB[i]);
94
+ }
95
+ else {
96
+ arr[i] = arrB[i] ?? arrA[i];
97
+ }
98
+ }
99
+ return arr;
100
+ }
101
+ function deepMerge(objA, objB) {
102
+ if (Array.isArray(objA) || Array.isArray(objB)) {
103
+ if (Array.isArray(objA) && Array.isArray(objB)) {
104
+ return mergeArray(objA, objB);
105
+ }
106
+ // if a and b differ in type, prefer b.
107
+ return objB ?? objA;
108
+ }
109
+ const merged = {};
110
+ const keys = new Set([
111
+ ...Object.keys(objA),
112
+ ...Object.keys(objB),
113
+ ]);
114
+ keys.forEach((key) => {
115
+ const a = objA[key];
116
+ const b = objB[key];
117
+ if (typeof a === 'object' && typeof b === 'object') {
118
+ merged[key] = deepMerge(a, b);
119
+ return;
120
+ }
121
+ // if a and b differ in type, prefer b.
122
+ merged[key] = (b ?? a);
123
+ });
124
+ return merged;
125
+ }
126
+
89
127
  const invalidFieldsSymbol = Symbol('Not all fields are valid');
90
128
  class SubFormDirective {
91
129
  }
@@ -227,10 +265,13 @@ class FormComponent {
227
265
  });
228
266
  allControls.forEach(e => this.disableInactiveFormControl(e));
229
267
  allControls.forEach(e => e.updateValueAndValidity());
230
- const values = this.formGroup.value;
268
+ const formGroupValue = this.formGroup.value;
269
+ const renderedAndEnabledValues = this.getRenderedFieldValuesFormGroup(this.formGroup, true);
270
+ const renderedButDisabledValues = this.getRenderedFieldValuesFormGroup(this.formGroup, false);
231
271
  if (this.formGroup.valid) {
232
272
  this.setDisabledStatesForAllControls(originalDisabledStates);
233
- return Promise.resolve(values);
273
+ const renderedValues = deepMerge(renderedAndEnabledValues, renderedButDisabledValues);
274
+ return Promise.resolve([formGroupValue, renderedValues]);
234
275
  }
235
276
  else {
236
277
  this.activeControls.find((e) => !e.formControl.valid)?.formElement?.scrollTo();
@@ -238,6 +279,39 @@ class FormComponent {
238
279
  return Promise.reject(invalidFieldsSymbol);
239
280
  }
240
281
  }
282
+ getRenderedFieldValuesFormGroup(formGroup, enabled, valueObject = {}) {
283
+ Object.entries(formGroup.controls).forEach(([name, control]) => {
284
+ if (control instanceof FormControl && control.enabled === enabled && this.activeControls.some(e => e.formControl === control)) {
285
+ valueObject[name] = control.value;
286
+ }
287
+ else if (control instanceof FormArray) {
288
+ valueObject[name] = [];
289
+ this.getRenderedFieldValuesFormArray(control, enabled, valueObject[name]);
290
+ }
291
+ else if (control instanceof FormGroup) {
292
+ valueObject[name] = {};
293
+ this.getRenderedFieldValuesFormGroup(control, enabled, valueObject[name]);
294
+ }
295
+ });
296
+ return valueObject;
297
+ }
298
+ getRenderedFieldValuesFormArray(formArray, enabled, valueArray) {
299
+ formArray.controls.forEach((control) => {
300
+ if (control instanceof FormControl && control.enabled === enabled && this.activeControls.some(e => e.formControl === control)) {
301
+ valueArray.push(control.value);
302
+ }
303
+ else if (control instanceof FormArray) {
304
+ const newArray = [];
305
+ valueArray.push(newArray);
306
+ this.getRenderedFieldValuesFormArray(control, enabled, newArray);
307
+ }
308
+ else if (control instanceof FormGroup) {
309
+ const newObject = {};
310
+ valueArray.push(newObject);
311
+ this.getRenderedFieldValuesFormGroup(control, enabled, newObject);
312
+ }
313
+ });
314
+ }
241
315
  setDisabledStatesForAllControls(originalDisabledStates) {
242
316
  originalDisabledStates.forEach((e) => {
243
317
  if (e.disabled) {
@@ -996,9 +1070,9 @@ class FormSubmitButtonComponent {
996
1070
  submitForm() {
997
1071
  this.parentForm
998
1072
  .trySubmit()
999
- .then((value) => {
1073
+ .then(([renderedAndEnabledValues, renderedValues]) => {
1000
1074
  this.isLoading = true;
1001
- const submitCallbackResult = this.submitCallback(value);
1075
+ const submitCallbackResult = this.submitCallback(renderedAndEnabledValues, renderedValues);
1002
1076
  if (isNullOrUndefined(submitCallbackResult)) {
1003
1077
  throw new Error('No promise is returned in your submit function.');
1004
1078
  }