@piying/view-angular-core 1.2.2 → 1.3.1

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.
@@ -258,6 +258,9 @@ class AbstractControl {
258
258
  (!this.selfDisabled$$() ||
259
259
  (this.selfDisabled$$() && this.config$().disabledValue === 'reserve')));
260
260
  injector;
261
+ /** model的value */
262
+ value$$;
263
+ children$$;
261
264
  /** disabled */
262
265
  selfDisabled$$ = computed(() => this.config$?.().disabled ?? false);
263
266
  /** `self` || `parent` */
@@ -454,6 +457,7 @@ class AbstractControl {
454
457
  markAsPristine() {
455
458
  this.selfDirty$.set(false);
456
459
  }
460
+ reset(value) { }
457
461
  getRawValue() {
458
462
  return this.value;
459
463
  }
@@ -474,13 +478,19 @@ class AbstractControl {
474
478
  }
475
479
  return x;
476
480
  }
481
+ /** @internal */
482
+ _forEachChild(cb) { }
483
+ updateValue(value) { }
477
484
  config$ = signal({});
485
+ /** @internal */
478
486
  initConfig(config) {
479
487
  this.config$ = config;
480
488
  }
481
489
  getInitValue(value) {
482
490
  return value ?? this.config$().defaultValue;
483
491
  }
492
+ /** @internal */
493
+ updateInitValue(value) { }
484
494
  find(name) {
485
495
  return null;
486
496
  }
@@ -551,101 +561,152 @@ class AbstractControl {
551
561
  }
552
562
  }
553
563
 
554
- class FieldArray extends AbstractControl {
564
+ var UpdateType;
565
+ (function (UpdateType) {
566
+ UpdateType[UpdateType["init"] = 0] = "init";
567
+ UpdateType[UpdateType["update"] = 1] = "update";
568
+ UpdateType[UpdateType["reset"] = 2] = "reset";
569
+ })(UpdateType || (UpdateType = {}));
570
+
571
+ class FieldGroupbase extends AbstractControl {
572
+ /** @internal */
573
+ getResetValue(value = []) { }
574
+ /** @internal */
575
+ beforeUpdateList = [];
576
+ resetValue$ = signal(undefined);
577
+ /** @internal */
578
+ _updateValue(value, type) {
579
+ const viewValue = this.config$().transfomer?.toView?.(value, this) ?? value;
580
+ if (type === UpdateType.init) {
581
+ this.initedValue = viewValue;
582
+ }
583
+ if (this.config$().groupMode === 'reset') {
584
+ const restValue = this.getResetValue(viewValue);
585
+ this.beforeUpdateList.forEach((fn) => fn(restValue, type !== UpdateType.init));
586
+ }
587
+ else if (this.config$().groupMode === 'loose') {
588
+ const resetValue = this.getResetValue(viewValue);
589
+ this.resetValue$.set(resetValue);
590
+ }
591
+ this._forEachChild((control, key) => {
592
+ if (type === UpdateType.init) {
593
+ control.updateInitValue(viewValue?.[key]);
594
+ }
595
+ else if (type === UpdateType.update) {
596
+ control.updateValue(viewValue?.[key]);
597
+ }
598
+ else {
599
+ control.reset(viewValue ? viewValue[key] : undefined);
600
+ }
601
+ });
602
+ }
603
+ updateValue(value) {
604
+ if (deepEqual(value, this.value$$())) {
605
+ return;
606
+ }
607
+ this._updateValue(value, UpdateType.update);
608
+ }
609
+ inited = false;
610
+ /** @internal */
611
+ initedValue;
612
+ /** @internal */
613
+ updateInitValue(value) {
614
+ this.inited = true;
615
+ const initValue = this.getInitValue(value);
616
+ this._updateValue(initValue, UpdateType.init);
617
+ }
618
+ reset(value) {
619
+ const initValue = this.getInitValue(value);
620
+ this._updateValue(initValue, UpdateType.reset);
621
+ }
622
+ /** @internal */
623
+ _reduceChildren(initValue, fn) {
624
+ let res = initValue;
625
+ this._forEachChild((control, name) => {
626
+ res = fn(res, control, name);
627
+ });
628
+ return res;
629
+ }
630
+ }
631
+
632
+ class FieldArray extends FieldGroupbase {
555
633
  #deletionMode$$ = computed(() => this.config$().deletionMode ?? 'shrink');
556
634
  value$$ = computed(() => {
557
- const list = [];
558
- for (const control of this.controls$()) {
635
+ let list = [];
636
+ this._reduceChildren(list, (acc, control, name) => {
559
637
  if (control && control.shouldInclude$$()) {
560
638
  list.push(control.value$$());
561
639
  }
562
640
  else {
563
641
  if (this.#deletionMode$$() === 'shrink') {
564
- continue;
642
+ return list;
565
643
  }
566
644
  else if (this.#deletionMode$$() === 'mark') {
567
645
  list.push(undefined);
568
646
  }
569
647
  }
570
- }
648
+ return list;
649
+ });
650
+ list = [...list, ...(this.resetValue$() ?? [])];
571
651
  const returnResult = list.length === 0 ? this.emptyValue$$() : list;
572
652
  return (this.config$().transfomer?.toModel?.(returnResult, this) ?? returnResult);
573
653
  });
574
- children$$ = computed(() => this.controls$());
575
- controls$ = signal([]);
654
+ children$$ = computed(() => [
655
+ ...this.fixedControls$(),
656
+ ...this.resetControls$(),
657
+ ]);
658
+ fixedControls$ = signal([]);
659
+ resetControls$ = signal([]);
576
660
  get controls() {
577
- return this.controls$();
578
- }
579
- removeAt(index) {
580
- const adjustedIndex = this._adjustIndex(index);
581
- if (this.controls$()[adjustedIndex]) {
582
- this.controls$.update((list) => {
583
- list = list.slice();
584
- list.splice(adjustedIndex, 1);
585
- return list;
586
- });
661
+ return this.children$$();
662
+ }
663
+ removeRestControl(key) {
664
+ if (!this.resetControls$()[key]) {
665
+ return;
587
666
  }
667
+ this.resetControls$.update((controls) => {
668
+ controls = controls.slice();
669
+ controls.splice(key, 1);
670
+ return controls;
671
+ });
588
672
  }
589
- setControl(index, control) {
590
- const adjustedIndex = this._adjustIndex(index);
591
- this.controls$.update((list) => {
673
+ setControl(key, control) {
674
+ const controls$ = this.inited ? this.resetControls$ : this.fixedControls$;
675
+ key = this.inited ? key - this.fixedControls$().length : key;
676
+ controls$.update((list) => {
592
677
  list = list.slice();
593
- list[adjustedIndex] = control;
678
+ list[key] = control;
594
679
  return list;
595
680
  });
596
681
  control.setParent(this);
597
682
  }
598
683
  get length() {
599
- return this.controls$().length;
600
- }
601
- reset(value) {
602
- let initValue = this.getInitValue(value);
603
- const viewValue = this.config$().transfomer?.toView?.(initValue, this) ?? initValue;
604
- this.beforeUpdateList.forEach((item) => item(viewValue));
605
- this._forEachChild((control, index) => {
606
- control.reset(initValue[index]);
607
- });
684
+ return this.controls.length;
608
685
  }
609
686
  getRawValue() {
610
- return this.controls$().map((control) => control.getRawValue());
687
+ return this._reduceChildren([], (acc, control, key) => {
688
+ acc[key] = control.getRawValue();
689
+ return acc;
690
+ });
611
691
  }
612
692
  clear() {
613
- if (this.controls$().length < 1)
693
+ if (this.resetControls$().length < 1)
614
694
  return;
615
- this.controls$.update(() => []);
616
- }
617
- _adjustIndex(index) {
618
- return index < 0 ? Math.max(index + this.length, 0) : index;
695
+ this.beforeUpdateList.forEach((fn) => fn([], false));
619
696
  }
620
697
  /** @internal */
621
698
  _forEachChild(cb) {
622
- this.controls$().forEach((control, index) => {
699
+ this.children$$().forEach((control, index) => {
623
700
  if (control) {
624
701
  cb(control, index);
625
702
  }
626
703
  });
627
704
  }
628
- find(name) {
629
- return this.controls$()[this._adjustIndex(name)];
705
+ find(key) {
706
+ return this.children$$()[key];
630
707
  }
631
- beforeUpdateList = [];
632
- updateValue(value = []) {
633
- if (deepEqual(value, this.value$$())) {
634
- return;
635
- }
636
- const viewValue = this.config$().transfomer?.toView?.(value, this) ?? value;
637
- this.beforeUpdateList.forEach((item) => item(viewValue));
638
- this.controls$().forEach((control, i) => {
639
- control.updateValue(viewValue[i]);
640
- });
641
- }
642
- updateInitValue(value) {
643
- let initValue = this.getInitValue(value);
644
- const viewValue = this.config$().transfomer?.toView?.(initValue, this) ?? initValue;
645
- this.beforeUpdateList.forEach((item) => item(viewValue));
646
- this.controls$().forEach((control, i) => {
647
- control.updateInitValue(viewValue?.[i]);
648
- });
708
+ getResetValue(value = []) {
709
+ return value.slice(this.fixedControls$().length);
649
710
  }
650
711
  }
651
712
 
@@ -656,7 +717,6 @@ const InitPendingValue = {
656
717
  };
657
718
  class FieldControl extends AbstractControl {
658
719
  pendingStatus = signal(InitPendingValue);
659
- children$$;
660
720
  #viewIndex = 0;
661
721
  /** 视图变化时model值不变也要更新view */
662
722
  viewIndex$ = signal(0);
@@ -682,8 +742,6 @@ class FieldControl extends AbstractControl {
682
742
  this.markAsPristine();
683
743
  this.markAsUntouched();
684
744
  }
685
- /** @internal */
686
- _forEachChild(cb) { }
687
745
  #initInput = true;
688
746
  #viewSubject$$ = computed(() => {
689
747
  const subject = new Subject();
@@ -761,7 +819,7 @@ class FieldControl extends AbstractControl {
761
819
  this.pendingStatus.set(InitPendingValue);
762
820
  }
763
821
  updateInitValue(value) {
764
- let initValue = this.getInitValue(value);
822
+ const initValue = this.getInitValue(value);
765
823
  this.modelValue$.set(initValue);
766
824
  this.value$$.set(initValue);
767
825
  }
@@ -777,16 +835,16 @@ class FieldLogicGroup extends FieldArray {
777
835
  const returnResult = this.getValue(false);
778
836
  return (this.config$().transfomer?.toModel?.(returnResult, this) ?? returnResult);
779
837
  });
780
- getActivateControls() {
838
+ #getActivateControls() {
781
839
  let list;
782
840
  if (this.activateControl$()) {
783
841
  list = this.activateControl$();
784
842
  }
785
843
  else if (this.type() === 'and') {
786
- list = this.controls$();
844
+ list = this.fixedControls$();
787
845
  }
788
846
  else if (this.type() === 'or') {
789
- list = [this.controls$()[this.activateIndex$()]];
847
+ list = [this.fixedControls$()[this.activateIndex$()]];
790
848
  }
791
849
  else {
792
850
  throw new Error('');
@@ -795,8 +853,8 @@ class FieldLogicGroup extends FieldArray {
795
853
  }
796
854
  getValue(rawData) {
797
855
  const controls = rawData
798
- ? this.getActivateControls()
799
- : this.getActivateControls().filter((control) => control.shouldInclude$$());
856
+ ? this.#getActivateControls()
857
+ : this.#getActivateControls().filter((control) => control.shouldInclude$$());
800
858
  const control = controls[0];
801
859
  if (controls.length === 0) {
802
860
  return this.emptyValue$$();
@@ -808,9 +866,9 @@ class FieldLogicGroup extends FieldArray {
808
866
  return Object.keys(result).length ? result : this.emptyValue$$();
809
867
  }
810
868
  reset(value) {
811
- let initValue = this.getInitValue(value);
869
+ const initValue = this.getInitValue(value);
812
870
  const viewValue = this.config$().transfomer?.toView?.(initValue, this) ?? initValue;
813
- this.controls$().forEach((control, i) => {
871
+ this.fixedControls$().forEach((control, i) => {
814
872
  control.reset(viewValue);
815
873
  });
816
874
  }
@@ -822,14 +880,15 @@ class FieldLogicGroup extends FieldArray {
822
880
  return;
823
881
  }
824
882
  const viewValue = this.config$().transfomer?.toView?.(value, this) ?? value;
825
- this.controls$().forEach((control, i) => {
883
+ this.fixedControls$().forEach((control, i) => {
826
884
  control.updateValue(viewValue);
827
885
  });
828
886
  }
887
+ /** @internal */
829
888
  updateInitValue(value) {
830
- let initValue = this.getInitValue(value);
889
+ const initValue = this.getInitValue(value);
831
890
  const viewValue = this.config$().transfomer?.toView?.(initValue, this) ?? initValue;
832
- this.controls$().forEach((control, i) => {
891
+ this.fixedControls$().forEach((control, i) => {
833
892
  control.updateInitValue(viewValue);
834
893
  });
835
894
  }
@@ -848,125 +907,83 @@ function isFieldLogicGroup(input) {
848
907
  return input instanceof FieldLogicGroup;
849
908
  }
850
909
 
851
- class FieldGroup extends AbstractControl {
910
+ class FieldGroup extends FieldGroupbase {
852
911
  value$$ = computed(() => {
853
- const acc = {};
854
- const value = this._reduceChildren(acc, (acc, control, name) => {
912
+ const result = this._reduceChildren({ ...this.#looseValue$$() }, (acc, control, name) => {
855
913
  if (control.shouldInclude$$()) {
856
914
  acc[name] = control.value;
857
915
  }
858
916
  return acc;
859
917
  });
860
- const result = { ...value, ...this.looseValue$$() };
861
918
  const returnResult = Object.keys(result).length
862
919
  ? result
863
920
  : this.emptyValue$$();
864
921
  return (this.config$().transfomer?.toModel?.(returnResult, this) ?? returnResult);
865
922
  });
866
- children$$ = computed(() => Object.values(this.controls$()));
867
- controls$ = signal({});
923
+ #controls$$ = computed(() => ({
924
+ ...this.fixedControls$(),
925
+ ...this.resetControls$(),
926
+ }));
927
+ fixedControls$ = signal({});
928
+ resetControls$ = signal({});
868
929
  get controls() {
869
- return this.controls$();
930
+ return this.#controls$$();
870
931
  }
871
- registerControl(name, control) {
872
- if (this.controls$()[name])
873
- return this.controls$()[name];
874
- this.controls$.update((controls) => ({ ...controls, [name]: control }));
875
- control.setParent(this);
876
- return control;
877
- }
878
- removeControl(name) {
879
- if (this.controls$()[name]) {
880
- this.controls$.update((controls) => {
881
- controls = { ...controls };
882
- delete controls[name];
883
- return controls;
884
- });
932
+ children$$ = computed(() => Object.values(this.#controls$$()));
933
+ removeRestControl(key) {
934
+ if (!this.resetControls$()[key]) {
935
+ return;
885
936
  }
886
- }
887
- setControl(name, control) {
888
- this.controls$.update((controls) => {
937
+ this.resetControls$.update((controls) => {
889
938
  controls = { ...controls };
890
- delete controls[name];
939
+ delete controls[key];
891
940
  return controls;
892
941
  });
893
- if (control)
894
- this.registerControl(name, control);
895
942
  }
896
- reset(value) {
897
- let initValue = this.getInitValue(value);
898
- const viewValue = this.config$().transfomer?.toView?.(initValue, this) ?? initValue;
899
- this._forEachChild((control, name) => {
900
- control.reset(viewValue ? viewValue[name] : undefined);
901
- });
943
+ setControl(key, control) {
944
+ const controls$ = this.inited ? this.resetControls$ : this.fixedControls$;
945
+ controls$.update((controls) => ({ ...controls, [key]: control }));
946
+ control.setParent(this);
902
947
  }
903
948
  getRawValue() {
904
- return this._reduceChildren({}, (acc, control, name) => {
905
- acc[name] = control.getRawValue();
949
+ return this._reduceChildren({}, (acc, control, key) => {
950
+ acc[key] = control.getRawValue();
906
951
  return acc;
907
952
  });
908
953
  }
954
+ clear() {
955
+ if (Object.keys(this.resetControls$()).length < 1)
956
+ return;
957
+ this.beforeUpdateList.forEach((fn) => fn({}, false));
958
+ }
909
959
  /** @internal */
910
960
  _forEachChild(cb) {
911
- Object.keys(this.controls$()).forEach((key) => {
912
- const control = this.controls$()[key];
913
- control && cb(control, key);
961
+ const controls = this.#controls$$();
962
+ Object.keys(controls).forEach((key) => {
963
+ cb(controls[key], key);
914
964
  });
915
965
  }
916
- /** @internal */
917
- _reduceChildren(initValue, fn) {
918
- let res = initValue;
919
- this._forEachChild((control, name) => {
920
- res = fn(res, control, name);
921
- });
922
- return res;
966
+ find(key) {
967
+ return this.#controls$$()[key];
923
968
  }
924
- find(name) {
925
- return this.controls$()[name];
969
+ getResetValue(inputValue) {
970
+ const controls = this.fixedControls$();
971
+ return inputValue
972
+ ? Object.keys(inputValue).reduce((obj, item) => {
973
+ if (!(item in controls)) {
974
+ obj[item] = inputValue[item];
975
+ }
976
+ return obj;
977
+ }, {})
978
+ : {};
926
979
  }
927
- looseValue$$ = computed(() => {
928
- const resetValue = this.#inputValue$();
980
+ #looseValue$$ = computed(() => {
981
+ const resetValue = this.resetValue$();
929
982
  if (!resetValue || isFieldLogicGroup(this.parent)) {
930
- return {};
931
- }
932
- const controls = this.controls$();
933
- const looseValue = {};
934
- for (const key in resetValue) {
935
- if (!(key in controls)) {
936
- looseValue[key] = resetValue[key];
937
- }
983
+ return undefined;
938
984
  }
939
- return looseValue;
985
+ return resetValue;
940
986
  });
941
- /**
942
- * loose object
943
- * todo 动态添加
944
- * */
945
- #inputValue$ = signal({});
946
- #setInputValue(obj) {
947
- this.#inputValue$.set(obj);
948
- }
949
- updateValue(value) {
950
- if (deepEqual(value, this.value$$())) {
951
- return;
952
- }
953
- if (!deepEqual(value, this.#inputValue$())) {
954
- this.#setInputValue(value);
955
- }
956
- const viewValue = this.config$().transfomer?.toView?.(value, this) ?? value;
957
- for (const key in this.controls$()) {
958
- const control = this.controls$()[key];
959
- control.updateValue(viewValue?.[key]);
960
- }
961
- }
962
- updateInitValue(value) {
963
- let initValue = this.getInitValue(value);
964
- const viewValue = this.config$().transfomer?.toView?.(initValue, this) ?? initValue;
965
- for (const key in this.controls$()) {
966
- const control = this.controls$()[key];
967
- control.updateInitValue(viewValue?.[key]);
968
- }
969
- }
970
987
  }
971
988
 
972
989
  function isGroup(schema) {
@@ -1045,8 +1062,8 @@ class ParentMap extends Map {
1045
1062
 
1046
1063
  function* groupGenerator(list) {
1047
1064
  for (const item of list) {
1048
- if (!item.keyPath?.length && item.fieldGroup?.().length) {
1049
- yield* groupGenerator(item.fieldGroup());
1065
+ if (!item.keyPath?.length && item.fixedChildren?.().length) {
1066
+ yield* groupGenerator(item.fixedChildren());
1050
1067
  }
1051
1068
  else if (item.keyPath?.length) {
1052
1069
  yield item;
@@ -1068,14 +1085,12 @@ function fieldQuery(keyPath, field, aliasMap, root) {
1068
1085
  const queryField = aliasMap.get(firstPath.slice(1));
1069
1086
  list = [{ field: queryField, level: 1 }];
1070
1087
  }
1071
- else if (field.fieldGroup) {
1072
- list = groupGenerator(field.fieldGroup())
1073
- .filter((field) => field.keyPath && arrayStartsWith(keyPath, field.keyPath))
1074
- .map((field) => ({ field: field, level: field.keyPath?.length }));
1075
- }
1076
- else if (field.fieldArray) {
1077
- list = field
1078
- .fieldArray()
1088
+ else if (field.fixedChildren || field.restChildren) {
1089
+ const children = [
1090
+ ...(field.fixedChildren?.() ?? []),
1091
+ ...(field.restChildren?.() ?? []),
1092
+ ];
1093
+ list = groupGenerator(children)
1079
1094
  .filter((field) => field.keyPath && arrayStartsWith(keyPath, field.keyPath))
1080
1095
  .map((field) => ({ field: field, level: field.keyPath?.length }));
1081
1096
  }
@@ -1094,6 +1109,7 @@ function fieldQuery(keyPath, field, aliasMap, root) {
1094
1109
  return undefined;
1095
1110
  }
1096
1111
 
1112
+ var _a;
1097
1113
  class FormBuilder {
1098
1114
  #scopeMap = inject(PI_FORM_BUILDER_ALIAS_MAP, { optional: true }) ??
1099
1115
  new ParentMap();
@@ -1103,7 +1119,7 @@ class FormBuilder {
1103
1119
  #globalConfig = inject(PI_VIEW_CONFIG_TOKEN);
1104
1120
  #allFieldInitHookList = [];
1105
1121
  buildRoot(item) {
1106
- let field = this.#buildControl({
1122
+ const field = this.#buildControl({
1107
1123
  type: 'root',
1108
1124
  field: { fullPath: [] },
1109
1125
  form: undefined,
@@ -1119,12 +1135,20 @@ class FormBuilder {
1119
1135
  list.forEach((fn) => fn());
1120
1136
  }
1121
1137
  #buildField(item) {
1138
+ item.field.fixedChildren = signal(new SortedArray((a, b) => a.priority - b.priority));
1139
+ for (let index = 0; index < item.fields.length; index++) {
1140
+ this.#buildControl(item, item.fields[index], index);
1141
+ }
1122
1142
  if (item.type === 'group') {
1123
1143
  this.#buildGroup(item);
1124
1144
  }
1125
1145
  else {
1126
1146
  this.#buildArray(item);
1127
1147
  }
1148
+ item.field.children = computed(() => [
1149
+ ...item.field.fixedChildren(),
1150
+ ...(item.field.restChildren?.() ?? []),
1151
+ ]);
1128
1152
  }
1129
1153
  afterResolveConfig(rawConfig, config) {
1130
1154
  return;
@@ -1181,7 +1205,7 @@ class FormBuilder {
1181
1205
  let control;
1182
1206
  let keyPath = field.key;
1183
1207
  if (isFieldLogicGroup(parent.form)) {
1184
- keyPath ??= parent.form.controls$().length;
1208
+ keyPath ??= parent.form.fixedControls$().length;
1185
1209
  }
1186
1210
  else if (isFieldArray(parent.form)) {
1187
1211
  keyPath ??= index;
@@ -1231,7 +1255,10 @@ class FormBuilder {
1231
1255
  this.#moveViewField(field.movePath, resolvedConfig);
1232
1256
  }
1233
1257
  else {
1234
- parent.append(resolvedConfig);
1258
+ if ((parent.type === 'group' || parent.type === 'array') &&
1259
+ !parent.skipAppend) {
1260
+ parent.append(resolvedConfig);
1261
+ }
1235
1262
  }
1236
1263
  if (field.alias) {
1237
1264
  this.#scopeMap.set(field.alias, resolvedConfig);
@@ -1241,28 +1268,28 @@ class FormBuilder {
1241
1268
  parent.resolvedField$.set(resolvedConfig);
1242
1269
  }
1243
1270
  // 递归进行解析
1244
- if (isGroup(field) ||
1245
- field.isLogicAnd ||
1246
- field.isLogicOr ||
1247
- field.isTuple) {
1248
- resolvedConfig.fieldGroup = signal(new SortedArray((a, b) => a.priority - b.priority));
1271
+ if (isGroup(field) || field.isLogicAnd || field.isLogicOr) {
1249
1272
  this.#buildField({
1250
1273
  type: 'group',
1274
+ templateField: field.arrayChild,
1251
1275
  fields: field.children,
1252
1276
  field: resolvedConfig,
1253
1277
  form: (control || parent.form),
1254
1278
  append: (field) => {
1255
- resolvedConfig.fieldGroup().push(field);
1279
+ resolvedConfig.fixedChildren().push(field);
1256
1280
  },
1257
1281
  });
1258
1282
  }
1259
- else if (isArray(field)) {
1283
+ else if (isArray(field) || field.isTuple) {
1260
1284
  this.#buildField({
1261
1285
  type: 'array',
1262
1286
  templateField: field.arrayChild,
1287
+ fields: field.children,
1263
1288
  field: resolvedConfig,
1264
1289
  form: control,
1265
- append: (field) => { },
1290
+ append: (field) => {
1291
+ resolvedConfig.fixedChildren().push(field);
1292
+ },
1266
1293
  });
1267
1294
  }
1268
1295
  if (resolvedConfig.hooks?.allFieldsResolved) {
@@ -1272,16 +1299,136 @@ class FormBuilder {
1272
1299
  }
1273
1300
  return resolvedConfig;
1274
1301
  }
1275
- #buildGroup(groupItem) {
1276
- for (let index = 0; index < groupItem.fields.length; index++) {
1277
- const field = groupItem.fields[index];
1278
- this.#buildControl(groupItem, field, index);
1302
+ #buildGroup(buildItem) {
1303
+ const { templateField, form, field } = buildItem;
1304
+ if (templateField && field.form.control) {
1305
+ field.restChildren = signal([]);
1306
+ const updateItem = (key, initValue) => {
1307
+ const result = this.#createObjectRestItem({ ...buildItem, skipAppend: true }, {
1308
+ ...templateField,
1309
+ key,
1310
+ });
1311
+ field.restChildren.update((list) => [...list, result]);
1312
+ if (initValue) {
1313
+ result.form.control?.updateInitValue(form.initedValue?.[key]);
1314
+ }
1315
+ return result;
1316
+ };
1317
+ function removeItem(key) {
1318
+ field.restChildren.update((list) => {
1319
+ const index = list.findIndex((item) => item.keyPath.slice(-1)[0] === key);
1320
+ list = [...list];
1321
+ list.splice(index, 1);
1322
+ return list;
1323
+ });
1324
+ form.removeRestControl(key);
1325
+ }
1326
+ form.beforeUpdateList.push((restValue = {}, initUpdate) => {
1327
+ const restControl = form.resetControls$();
1328
+ for (const key in restControl) {
1329
+ if (key in restValue) {
1330
+ continue;
1331
+ }
1332
+ removeItem(key);
1333
+ }
1334
+ let isUpdateItem = false;
1335
+ for (const key in restValue) {
1336
+ if (key in restControl) {
1337
+ continue;
1338
+ }
1339
+ isUpdateItem = true;
1340
+ updateItem(key, initUpdate);
1341
+ }
1342
+ if (isUpdateItem) {
1343
+ this.allFieldInitHookCall();
1344
+ }
1345
+ });
1346
+ field.action = {
1347
+ set: (value, key) => {
1348
+ untracked(() => {
1349
+ const result = updateItem(key, true);
1350
+ this.allFieldInitHookCall();
1351
+ result.form.control.updateValue(value);
1352
+ });
1353
+ },
1354
+ remove: (key) => {
1355
+ untracked(() => {
1356
+ removeItem(key);
1357
+ });
1358
+ },
1359
+ };
1279
1360
  }
1280
- /** 虚拟group不存在hooks */
1281
- const field = groupItem.field;
1282
- field.hooks?.afterChildrenInit?.(field);
1283
1361
  }
1284
- createArrayItem(parent,
1362
+ #createObjectRestItem(parent,
1363
+ // 单独一项
1364
+ field) {
1365
+ const result = this.#buildControl(parent, field, 0);
1366
+ this.#allFieldInitHookList.push(() => this.allFieldInitHookCall());
1367
+ return result;
1368
+ }
1369
+ #buildArray(buildItem) {
1370
+ const { templateField, form, field } = buildItem;
1371
+ if (templateField && field.form.control) {
1372
+ const fixedLength = field.fixedChildren?.().length ?? 0;
1373
+ field.restChildren = signal([]);
1374
+ const updateItem = (list, index, initValue) => {
1375
+ const result = this.#createArrayItem(buildItem, templateField, fixedLength + index);
1376
+ list[index] = result;
1377
+ if (initValue) {
1378
+ result.form.control?.updateInitValue(form.initedValue?.[fixedLength + index]);
1379
+ }
1380
+ return result;
1381
+ };
1382
+ function removeItem(list, index) {
1383
+ const [deletedItem] = list.splice(index, 1);
1384
+ form.removeRestControl(index);
1385
+ if (deletedItem) {
1386
+ deletedItem.injector.destroy();
1387
+ deletedItem.injector = undefined;
1388
+ }
1389
+ }
1390
+ form.beforeUpdateList.push((resetValue = [], initUpdate) => {
1391
+ const controlLength = form.resetControls$().length;
1392
+ if (resetValue.length < controlLength) {
1393
+ const list = [...field.restChildren()];
1394
+ for (let index = list.length - 1; index >= resetValue.length; index--) {
1395
+ removeItem(list, index);
1396
+ }
1397
+ field.restChildren.set(list);
1398
+ }
1399
+ else if (controlLength < resetValue.length) {
1400
+ const list = [...field.restChildren()];
1401
+ for (let index = controlLength; index < resetValue.length; index++) {
1402
+ updateItem(list, index, initUpdate);
1403
+ }
1404
+ field.restChildren.set(list);
1405
+ this.allFieldInitHookCall();
1406
+ }
1407
+ });
1408
+ field.action = {
1409
+ set: (value, index) => {
1410
+ untracked(() => {
1411
+ index = (typeof index === 'number'
1412
+ ? index
1413
+ : (field.restChildren?.().length ?? 0));
1414
+ const list = [...field.restChildren()];
1415
+ const result = updateItem(list, index, true);
1416
+ field.restChildren.set(list);
1417
+ this.allFieldInitHookCall();
1418
+ result.form.control.updateValue(value);
1419
+ });
1420
+ },
1421
+ remove: (index) => {
1422
+ untracked(() => {
1423
+ const list = [...field.restChildren()];
1424
+ removeItem(list, index);
1425
+ field.restChildren.set(list);
1426
+ });
1427
+ },
1428
+ };
1429
+ }
1430
+ }
1431
+ #createArrayItem(parent,
1285
1432
  // 单独一项
1286
1433
  field, index) {
1287
1434
  const Builder = this.constructor;
@@ -1300,69 +1447,11 @@ class FormBuilder {
1300
1447
  result.injector?.destroy();
1301
1448
  });
1302
1449
  const instance = injector.get(Builder);
1303
- const result = instance.#buildControl(parent, field, index);
1450
+ const result = instance.#buildControl({ ...parent, skipAppend: true }, field, index);
1304
1451
  this.#allFieldInitHookList.push(() => instance.allFieldInitHookCall());
1305
1452
  result.injector = injector.get(EnvironmentInjector);
1306
1453
  return result;
1307
1454
  }
1308
- #buildArray(arrayItem) {
1309
- const { templateField, form } = arrayItem;
1310
- arrayItem.field.action = {
1311
- // 因为数组需要有动态添加的能力,所以才加上,group不需要
1312
- set: (value, index) => {
1313
- untracked(() => {
1314
- index = (typeof index === 'number'
1315
- ? index
1316
- : (arrayItem.field.fieldArray?.().length ?? 0));
1317
- const result = this.createArrayItem(arrayItem, templateField, index);
1318
- const list = [...arrayItem.field.fieldArray()];
1319
- list[index] = result;
1320
- arrayItem.field.fieldArray.set(list);
1321
- result.form.control?.updateInitValue(arrayItem.field.form.control?.config$().defaultValue?.[index]);
1322
- this.allFieldInitHookCall();
1323
- result.form.control.updateValue(value);
1324
- });
1325
- },
1326
- remove: (index) => {
1327
- untracked(() => {
1328
- const list = [...arrayItem.field.fieldArray()];
1329
- const [deletedItem] = list.splice(index, 1);
1330
- arrayItem.field.fieldArray.set(list);
1331
- form.removeAt(index);
1332
- if (deletedItem) {
1333
- deletedItem.injector.destroy();
1334
- deletedItem.injector = undefined;
1335
- }
1336
- });
1337
- },
1338
- };
1339
- arrayItem.field.fieldArray = signal([]);
1340
- arrayItem.field.hooks?.afterChildrenInit?.(arrayItem.field);
1341
- form.beforeUpdateList.push((input = []) => {
1342
- const controlLength = form.controls$().length;
1343
- if (controlLength < input.length) {
1344
- const list = arrayItem.field.fieldArray().slice();
1345
- for (let index = controlLength; index < input.length; index++) {
1346
- const result = this.createArrayItem(arrayItem, arrayItem.templateField, index);
1347
- list[index] = result;
1348
- }
1349
- arrayItem.field.fieldArray.set(list);
1350
- }
1351
- else if (input.length < controlLength) {
1352
- const list = arrayItem.field.fieldArray().slice();
1353
- for (let index = arrayItem.field.fieldArray().length - 1; index >= input.length; index--) {
1354
- const [deletedItem] = list.splice(index, 1);
1355
- form.removeAt(index);
1356
- if (deletedItem) {
1357
- deletedItem.injector?.destroy();
1358
- deletedItem.injector = undefined;
1359
- }
1360
- }
1361
- arrayItem.field.fieldArray.set(list);
1362
- }
1363
- this.allFieldInitHookCall();
1364
- });
1365
- }
1366
1455
  #resolveComponent(type) {
1367
1456
  let define;
1368
1457
  let defaultConfig;
@@ -1431,7 +1520,7 @@ class FormBuilder {
1431
1520
  const newKeyPath = inputField.fullPath.slice(parent.fullPath.length);
1432
1521
  inputField.keyPath = newKeyPath;
1433
1522
  inputField.parent = parent;
1434
- parent.fieldGroup().push(inputField);
1523
+ parent.fixedChildren().push(inputField);
1435
1524
  }
1436
1525
  #resolveWrappers(wrappers) {
1437
1526
  const result = (wrappers ?? []).map((wrapper) => {
@@ -1471,6 +1560,7 @@ class FormBuilder {
1471
1560
  return signal(result);
1472
1561
  }
1473
1562
  }
1563
+ _a = FormBuilder;
1474
1564
 
1475
1565
  function layout(value) {
1476
1566
  return {
@@ -1966,10 +2056,11 @@ class CoreSchemaHandle extends BaseSchemaHandle {
1966
2056
  }
1967
2057
  else {
1968
2058
  this.isArray = true;
1969
- const sh = new this.globalConfig.handle(this.globalConfig, this, schema);
2059
+ const sh = new this.globalConfig.handle(this.globalConfig, this, schema.item);
1970
2060
  sh.parent = this;
1971
2061
  this.arrayChild = sh;
1972
2062
  convertSchema(schema.item, sh);
2063
+ this.formConfig.groupMode = 'reset';
1973
2064
  }
1974
2065
  }
1975
2066
  defaultSchema(schema) {
@@ -1978,6 +2069,41 @@ class CoreSchemaHandle extends BaseSchemaHandle {
1978
2069
  tupleDefault(schema) {
1979
2070
  super.tupleDefault(schema);
1980
2071
  this.isTuple = true;
2072
+ if (schema.type === 'tuple') {
2073
+ this.formConfig.groupMode = 'default';
2074
+ }
2075
+ else if (schema.type === 'loose_tuple') {
2076
+ this.formConfig.groupMode = 'loose';
2077
+ }
2078
+ else if (schema.type === 'strict_tuple') {
2079
+ this.formConfig.groupMode = 'strict';
2080
+ }
2081
+ }
2082
+ objectDefault(schema) {
2083
+ super.objectDefault(schema);
2084
+ if (schema.type === 'object') {
2085
+ this.formConfig.groupMode = 'default';
2086
+ }
2087
+ else if (schema.type === 'loose_object') {
2088
+ this.formConfig.groupMode = 'loose';
2089
+ }
2090
+ else if (schema.type === 'strict_object') {
2091
+ this.formConfig.groupMode = 'strict';
2092
+ }
2093
+ }
2094
+ recordSchema(key, value) {
2095
+ super.recordSchema(key, value);
2096
+ this.isGroup = true;
2097
+ // equal {[name:string]:v.InferOutput< typeof value>}
2098
+ this.restSchema(value);
2099
+ }
2100
+ restSchema(schema) {
2101
+ super.restSchema(schema);
2102
+ const sh = new this.globalConfig.handle(this.globalConfig, this, schema);
2103
+ sh.parent = this;
2104
+ this.arrayChild = sh;
2105
+ convertSchema(schema, sh);
2106
+ this.formConfig.groupMode = 'reset';
1981
2107
  }
1982
2108
  enumSchema(schema) {
1983
2109
  this.props ??= {};
@@ -2091,5 +2217,5 @@ const NFCSchema = v.optional(v.void());
2091
2217
  * Generated bundle index. Do not edit.
2092
2218
  */
2093
2219
 
2094
- export { AbstractControl, CoreSchemaHandle, FieldArray, FieldControl, FieldGroup, FieldLogicGroup, FormBuilder, INVALID, NFCSchema, PENDING, PI_CONTEXT_TOKEN, PI_FORM_BUILDER_ALIAS_MAP, PI_FORM_BUILDER_OPTIONS_TOKEN, PI_VIEW_CONFIG_TOKEN, SortedArray, VALID, arrayStartsWith, asyncInputMerge, clone, componentClass, controlStatusList, convert, createViewControlLink, disableWhen, effectListen, fieldControlStatusClass, formConfig, hideWhen, isArray, isFieldArray, isFieldControl, isFieldGroup, isFieldLogicGroup, isGroup, layout, mergeHooks, mergeHooksFn, mergeOutputFn, mergeOutputs, nonFieldControl, outputChange, outputChangeFn, patchAsyncAttributes, patchAsyncFn, patchAsyncInputs, patchAsyncProps, patchAttributes, patchHooks, patchInputs, patchOutputs, patchProps, patchWrappers, rawConfig, removeAttributes, removeHooks, removeInputs, removeOutputs, removeProps, removeWrappers, renderConfig, setAlias, setAttributes, setComponent, setHooks, setInputs, setOutputs, setProps, setWrappers, toArray, toObservable, topClass, unWrapSignal$1 as unWrapSignal, valueChange, valueChangeFn };
2220
+ export { AbstractControl, CoreSchemaHandle, FieldArray, FieldControl, FieldGroup, FieldLogicGroup, FormBuilder, INVALID, NFCSchema, PENDING, PI_CONTEXT_TOKEN, PI_FORM_BUILDER_ALIAS_MAP, PI_FORM_BUILDER_OPTIONS_TOKEN, PI_VIEW_CONFIG_TOKEN, SortedArray, UpdateType, VALID, arrayStartsWith, asyncInputMerge, clone, componentClass, controlStatusList, convert, createViewControlLink, disableWhen, effectListen, fieldControlStatusClass, formConfig, hideWhen, isArray, isFieldArray, isFieldControl, isFieldGroup, isFieldLogicGroup, isGroup, layout, mergeHooks, mergeHooksFn, mergeOutputFn, mergeOutputs, nonFieldControl, outputChange, outputChangeFn, patchAsyncAttributes, patchAsyncFn, patchAsyncInputs, patchAsyncProps, patchAttributes, patchHooks, patchInputs, patchOutputs, patchProps, patchWrappers, rawConfig, removeAttributes, removeHooks, removeInputs, removeOutputs, removeProps, removeWrappers, renderConfig, setAlias, setAttributes, setComponent, setHooks, setInputs, setOutputs, setProps, setWrappers, toArray, toObservable, topClass, unWrapSignal$1 as unWrapSignal, valueChange, valueChangeFn };
2095
2221
  //# sourceMappingURL=piying-view-angular-core.mjs.map