@ldquocc/dynamic-form 0.0.10 → 0.0.12

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.
@@ -966,15 +966,22 @@ const buildForm = (fields, mode, model) => {
966
966
  continue;
967
967
  const fieldKey = field?.key;
968
968
  // field.value = field.allowSetValue ? (model?.[fieldKey] || field.value) : null;
969
- if (field.allowSetValue) {
970
- field.value = field.getDefaultValue();
971
- field.modelValue = null;
972
- if (model?.[fieldKey]) {
973
- field.value = field.toFormValue(model[fieldKey]);
974
- field.modelValue = model[fieldKey];
975
- }
976
- field.initialValue = field.value;
969
+ // 1. Luôn luôn reset về default trước để xoá cache cũ của instance field
970
+ field.value = field.getDefaultValue();
971
+ field.modelValue = null;
972
+ // 2. Kiểm tra quyền set value theo FormMode hiện tại
973
+ const isAllowedSet = Array.isArray(field.allowSetValue)
974
+ ? field.allowSetValue.includes(mode)
975
+ : Boolean(field.allowSetValue);
976
+ // 3. Chỉ nạp từ model nếu mode cho phép và model có key đó
977
+ if (isAllowedSet &&
978
+ model &&
979
+ fieldKey in model &&
980
+ model[fieldKey] !== undefined) {
981
+ field.value = field.toFormValue(model[fieldKey]);
982
+ field.modelValue = model[fieldKey];
977
983
  }
984
+ field.initialValue = field.value;
978
985
  // console.log("e", field.value );
979
986
  // field.value = field.allowSetValue ? field.toFormValue(model?.[fieldKey] | field.getDefaultValue()) : field.getDefaultValue();
980
987
  const control = field.toFormControl();
@@ -2257,6 +2264,8 @@ class BaseValueAccessor {
2257
2264
  this.onTouched = () => { };
2258
2265
  this.isDisabled = false;
2259
2266
  this.disabledSignal = signal(false); // Đang có select dùng isDisableSignal nên sau này đồng bộ lại.
2267
+ // Khai báo signal lưu dữ liệu gốc (root data / model value)
2268
+ this.rootDataSignal = signal(null);
2260
2269
  this.dFormService = inject(DynamicFormService);
2261
2270
  this.formDataService = inject(FormDataService);
2262
2271
  this.fieldService = inject(FieldService);
@@ -2291,6 +2300,9 @@ class BaseValueAccessor {
2291
2300
  writeValue(value) {
2292
2301
  this.value = value;
2293
2302
  this.valueSignal.set(value);
2303
+ // Chụp lại dữ liệu gốc từ modelValue (nếu có) hoặc fallback về value:
2304
+ const rootData = this.fieldInput()?.modelValue ?? value;
2305
+ this.rootDataSignal.set(rootData);
2294
2306
  this.writeValueToView(value); // 👈 component con override nếu cần.
2295
2307
  }
2296
2308
  registerOnChange(fn) {
@@ -2969,8 +2981,8 @@ class MultiSelectComponent extends BaseSelectComponent {
2969
2981
  this.viewDisplayValue = computed(() => {
2970
2982
  if (!this.isView())
2971
2983
  return [];
2972
- console.log("viewDisplayValue", this.field().modelValue || []);
2973
- return this.getDisplayValue(this.field().modelValue || []);
2984
+ const value = this.rootDataSignal();
2985
+ return this.getDisplayValue(value);
2974
2986
  });
2975
2987
  }
2976
2988
  onApiOptionsLoaded() {
@@ -3117,12 +3129,18 @@ class SingleSelectComponent extends BaseSelectComponent {
3117
3129
  }
3118
3130
  return base;
3119
3131
  });
3132
+ this.log = computed(() => {
3133
+ console.log("valueSignalvalueSignalvalueSignalvalueSignal", this.fieldKey(), this.valueSignal());
3134
+ return this.valueSignal();
3135
+ });
3120
3136
  // Giá trị dùng hiển thị ở chế độ View.
3121
3137
  this.viewDisplayValue = computed(() => {
3122
3138
  if (!this.isView())
3123
3139
  return "";
3124
- return this.getDisplayValue(this.field().modelValue || this.valueSignal());
3140
+ const value = this.rootDataSignal();
3141
+ return this.getDisplayValue(value);
3125
3142
  });
3143
+ this.displayValue = "";
3126
3144
  }
3127
3145
  onApiOptionsLoaded() {
3128
3146
  const field = this.field();
@@ -3137,6 +3155,12 @@ class SingleSelectComponent extends BaseSelectComponent {
3137
3155
  }
3138
3156
  // Chạy khi value form được gán bởi các hàm reset, patchValue, setValue.
3139
3157
  writeValueToView(value) {
3158
+ // if (this.isView()) {
3159
+ // this.displayValue = this.getDisplayValue(
3160
+ // this.field().modelValue as string | Record<string, unknown>,
3161
+ // );
3162
+ // return;
3163
+ // }
3140
3164
  if (!this.isAddOrEdit())
3141
3165
  return;
3142
3166
  // Bắn giá trị mới vào BehaviorSubject của FieldService.
@@ -3191,6 +3215,7 @@ class SingleSelectComponent extends BaseSelectComponent {
3191
3215
  }
3192
3216
  // Làm phẳng giá trị để hiện thị ở chế độ View.
3193
3217
  getDisplayValue(value) {
3218
+ console.log("getdisplay", value);
3194
3219
  if (!value)
3195
3220
  return NO_INFO_TEXT;
3196
3221
  if (typeof value === "object") {