@stemy/ngx-dynamic-form 19.9.36 → 19.9.38

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,7 +1,7 @@
1
1
  import * as i0 from '@angular/core';
2
2
  import { InjectionToken, inject, Inject, Injectable, untracked, input, Renderer2, ElementRef, computed, signal, effect, HostBinding, Directive, Input, Optional, Pipe, Type, Component, Injector, output, ChangeDetectionStrategy, ViewEncapsulation, viewChild, makeEnvironmentProviders, NgModule } from '@angular/core';
3
3
  import * as i2 from '@stemy/ngx-utils';
4
- import { cachedFactory, ReflectUtils, ObjectUtils, convertToDateFormat, LANGUAGE_SERVICE, convertToDate, ForbiddenZone, SetUtils, ArrayUtils, API_SERVICE, StringUtils, AsyncMethodBase, EventsService, NgxUtilsModule } from '@stemy/ngx-utils';
4
+ import { cachedFactory, ReflectUtils, ObjectUtils, convertToDateFormat, LANGUAGE_SERVICE, convertToDate, ForbiddenZone, SetUtils, ArrayUtils, API_SERVICE, StringUtils, Enum, AsyncMethodBase, EventsService, NgxUtilsModule } from '@stemy/ngx-utils';
5
5
  import { of, merge, Observable, firstValueFrom, BehaviorSubject, combineLatestWith, switchMap, distinctUntilChanged, first, Subject, map, filter } from 'rxjs';
6
6
  import { debounceTime } from 'rxjs/operators';
7
7
  import * as i1 from '@angular/forms';
@@ -68,19 +68,37 @@ function FormInput(data) {
68
68
  break;
69
69
  }
70
70
  data.type = data.type || inputType;
71
- defineFormControl(target, key, (fb, path, options) => fb.createFormInput(key, data, path, options));
71
+ defineFormControl(target, key, (fb, path, options) => {
72
+ const instance = getInstance(target);
73
+ return fb.createFormInput(key, {
74
+ ...data,
75
+ defaultValue: instance[key] ?? null
76
+ }, path, options);
77
+ });
72
78
  };
73
79
  }
74
80
  function FormSelect(data) {
75
81
  data = data || {};
76
82
  return (target, key) => {
77
- defineFormControl(target, key, (fb, path, options) => fb.createFormSelect(key, data, path, options));
83
+ defineFormControl(target, key, (fb, path, options) => {
84
+ const instance = getInstance(target);
85
+ return fb.createFormSelect(key, {
86
+ ...data,
87
+ defaultValue: instance[key] ?? null
88
+ }, path, options);
89
+ });
78
90
  };
79
91
  }
80
92
  function FormDate(data) {
81
93
  data = data || {};
82
94
  return (target, key) => {
83
- defineFormControl(target, key, (fb, path, options) => fb.createFormDate(key, data, path, options));
95
+ defineFormControl(target, key, (fb, path, options) => {
96
+ const instance = getInstance(target);
97
+ return fb.createFormDate(key, {
98
+ ...data,
99
+ defaultValue: instance[key] ?? null
100
+ }, path, options);
101
+ });
84
102
  };
85
103
  }
86
104
  function FormUpload(data) {
@@ -92,7 +110,13 @@ function FormUpload(data) {
92
110
  function FormStatic(data) {
93
111
  data = data || {};
94
112
  return (target, key) => {
95
- defineFormControl(target, key, (fb, path, options) => fb.createFormStatic(key, data, path, options));
113
+ defineFormControl(target, key, (fb, path, options) => {
114
+ const instance = getInstance(target);
115
+ return fb.createFormStatic(key, {
116
+ ...data,
117
+ defaultValue: instance[key] ?? null
118
+ }, path, options);
119
+ });
96
120
  };
97
121
  }
98
122
  function FormGroup(data) {
@@ -100,7 +124,11 @@ function FormGroup(data) {
100
124
  return (target, key) => {
101
125
  defineFormControl(target, key, (fb, parent, options) => {
102
126
  const targetType = ReflectUtils.getOwnMetadata("design:type", target, key);
103
- return fb.resolveFormGroup(key, targetType, data, parent, options);
127
+ const instance = getInstance(target);
128
+ return fb.resolveFormGroup(key, targetType, {
129
+ ...data,
130
+ defaultValue: instance[key] ?? {}
131
+ }, parent, options);
104
132
  });
105
133
  };
106
134
  }
@@ -108,7 +136,11 @@ function FormArray(itemType, data) {
108
136
  data = data || {};
109
137
  return (target, key) => {
110
138
  defineFormControl(target, key, (fb, parent, options) => {
111
- return fb.resolveFormArray(key, itemType, data, parent, options);
139
+ const instance = getInstance(target);
140
+ return fb.resolveFormArray(key, itemType, {
141
+ ...data,
142
+ defaultValue: instance[key] ?? []
143
+ }, parent, options);
112
144
  });
113
145
  };
114
146
  }
@@ -122,6 +154,19 @@ function FormFieldSet(data) {
122
154
  defineFormFieldSet(target, data);
123
155
  };
124
156
  }
157
+ function getInstance(target) {
158
+ let instance = ReflectUtils.getMetadata("dynamicFormInstance", target);
159
+ if (!instance) {
160
+ try {
161
+ instance = new target.constructor();
162
+ }
163
+ catch (e) {
164
+ instance = {};
165
+ }
166
+ ReflectUtils.defineMetadata("dynamicFormInstance", target, instance);
167
+ }
168
+ return instance;
169
+ }
125
170
 
126
171
  function replaceSpecialChars(str, to = "-") {
127
172
  return `${str}`.replace(/[&\/\\#, +()$~%.@'":*?<>{}]/g, to);
@@ -1091,7 +1136,7 @@ class DynamicFormBuilderService {
1091
1136
  path: target => {
1092
1137
  const tp = target.parent;
1093
1138
  const prefix = tp?.path || "";
1094
- return [prefix, String(target.key ?? "")].filter(ObjectUtils.isStringWithValue).join("-");
1139
+ return [prefix, String(target.key ?? "")].filter(ObjectUtils.isStringWithValue).join(".");
1095
1140
  },
1096
1141
  testId: target => {
1097
1142
  const tp = target.parent;
@@ -1688,7 +1733,8 @@ class DynamicFormService {
1688
1733
  Object.assign(result, group);
1689
1734
  continue;
1690
1735
  }
1691
- result[key] = control.value;
1736
+ const value = control.value;
1737
+ result[key] = value instanceof Enum ? value.value : value;
1692
1738
  }
1693
1739
  return result;
1694
1740
  }