@piying/view-angular-core 1.2.1 → 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,10 +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
  }
489
+ getInitValue(value) {
490
+ return value ?? this.config$().defaultValue;
491
+ }
492
+ /** @internal */
493
+ updateInitValue(value) { }
481
494
  find(name) {
482
495
  return null;
483
496
  }
@@ -548,90 +561,152 @@ class AbstractControl {
548
561
  }
549
562
  }
550
563
 
551
- 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 {
552
633
  #deletionMode$$ = computed(() => this.config$().deletionMode ?? 'shrink');
553
634
  value$$ = computed(() => {
554
- const list = [];
555
- for (const control of this.controls$()) {
635
+ let list = [];
636
+ this._reduceChildren(list, (acc, control, name) => {
556
637
  if (control && control.shouldInclude$$()) {
557
638
  list.push(control.value$$());
558
639
  }
559
640
  else {
560
641
  if (this.#deletionMode$$() === 'shrink') {
561
- continue;
642
+ return list;
562
643
  }
563
644
  else if (this.#deletionMode$$() === 'mark') {
564
645
  list.push(undefined);
565
646
  }
566
647
  }
567
- }
648
+ return list;
649
+ });
650
+ list = [...list, ...(this.resetValue$() ?? [])];
568
651
  const returnResult = list.length === 0 ? this.emptyValue$$() : list;
569
652
  return (this.config$().transfomer?.toModel?.(returnResult, this) ?? returnResult);
570
653
  });
571
- children$$ = computed(() => this.controls$());
572
- controls$ = signal([]);
654
+ children$$ = computed(() => [
655
+ ...this.fixedControls$(),
656
+ ...this.resetControls$(),
657
+ ]);
658
+ fixedControls$ = signal([]);
659
+ resetControls$ = signal([]);
573
660
  get controls() {
574
- return this.controls$();
575
- }
576
- removeAt(index) {
577
- const adjustedIndex = this._adjustIndex(index);
578
- if (this.controls$()[adjustedIndex]) {
579
- this.controls$.update((list) => {
580
- list = list.slice();
581
- list.splice(adjustedIndex, 1);
582
- return list;
583
- });
661
+ return this.children$$();
662
+ }
663
+ removeRestControl(key) {
664
+ if (!this.resetControls$()[key]) {
665
+ return;
584
666
  }
667
+ this.resetControls$.update((controls) => {
668
+ controls = controls.slice();
669
+ controls.splice(key, 1);
670
+ return controls;
671
+ });
585
672
  }
586
- setControl(index, control) {
587
- const adjustedIndex = this._adjustIndex(index);
588
- 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) => {
589
677
  list = list.slice();
590
- list[adjustedIndex] = control;
678
+ list[key] = control;
591
679
  return list;
592
680
  });
593
681
  control.setParent(this);
594
682
  }
595
683
  get length() {
596
- return this.controls$().length;
597
- }
598
- reset(value = []) {
599
- this._forEachChild((control, index) => {
600
- control.reset(value[index]);
601
- });
684
+ return this.controls.length;
602
685
  }
603
686
  getRawValue() {
604
- return this.controls$().map((control) => control.getRawValue());
687
+ return this._reduceChildren([], (acc, control, key) => {
688
+ acc[key] = control.getRawValue();
689
+ return acc;
690
+ });
605
691
  }
606
692
  clear() {
607
- if (this.controls$().length < 1)
693
+ if (this.resetControls$().length < 1)
608
694
  return;
609
- this.controls$.update(() => []);
610
- }
611
- _adjustIndex(index) {
612
- return index < 0 ? Math.max(index + this.length, 0) : index;
695
+ this.beforeUpdateList.forEach((fn) => fn([], false));
613
696
  }
614
697
  /** @internal */
615
698
  _forEachChild(cb) {
616
- this.controls$().forEach((control, index) => {
699
+ this.children$$().forEach((control, index) => {
617
700
  if (control) {
618
701
  cb(control, index);
619
702
  }
620
703
  });
621
704
  }
622
- find(name) {
623
- return this.controls$()[this._adjustIndex(name)];
705
+ find(key) {
706
+ return this.children$$()[key];
624
707
  }
625
- beforeUpdateList = [];
626
- updateValue(value = []) {
627
- if (deepEqual(value, this.value$$())) {
628
- return;
629
- }
630
- const viewValue = this.config$().transfomer?.toView?.(value, this) ?? value;
631
- this.beforeUpdateList.forEach((item) => item(viewValue));
632
- this.controls$().forEach((control, i) => {
633
- control.updateValue(viewValue[i]);
634
- });
708
+ getResetValue(value = []) {
709
+ return value.slice(this.fixedControls$().length);
635
710
  }
636
711
  }
637
712
 
@@ -642,7 +717,6 @@ const InitPendingValue = {
642
717
  };
643
718
  class FieldControl extends AbstractControl {
644
719
  pendingStatus = signal(InitPendingValue);
645
- children$$;
646
720
  #viewIndex = 0;
647
721
  /** 视图变化时model值不变也要更新view */
648
722
  viewIndex$ = signal(0);
@@ -668,8 +742,6 @@ class FieldControl extends AbstractControl {
668
742
  this.markAsPristine();
669
743
  this.markAsUntouched();
670
744
  }
671
- /** @internal */
672
- _forEachChild(cb) { }
673
745
  #initInput = true;
674
746
  #viewSubject$$ = computed(() => {
675
747
  const subject = new Subject();
@@ -736,13 +808,6 @@ class FieldControl extends AbstractControl {
736
808
  this.modelValue$.set(value);
737
809
  this.value$$.set(value);
738
810
  }
739
- initConfig(config) {
740
- super.initConfig(config);
741
- if ('defaultValue' in this.config$()) {
742
- this.modelValue$.set(this.config$().defaultValue);
743
- this.value$$.set(this.config$().defaultValue);
744
- }
745
- }
746
811
  emitSubmit() {
747
812
  const pendingStatus = this.pendingStatus();
748
813
  if (pendingStatus.touched) {
@@ -753,6 +818,11 @@ class FieldControl extends AbstractControl {
753
818
  }
754
819
  this.pendingStatus.set(InitPendingValue);
755
820
  }
821
+ updateInitValue(value) {
822
+ const initValue = this.getInitValue(value);
823
+ this.modelValue$.set(initValue);
824
+ this.value$$.set(initValue);
825
+ }
756
826
  }
757
827
 
758
828
  // 切换索引后,理论上应该触发下值变更,否则不知道值是什么
@@ -765,16 +835,16 @@ class FieldLogicGroup extends FieldArray {
765
835
  const returnResult = this.getValue(false);
766
836
  return (this.config$().transfomer?.toModel?.(returnResult, this) ?? returnResult);
767
837
  });
768
- getActivateControls() {
838
+ #getActivateControls() {
769
839
  let list;
770
840
  if (this.activateControl$()) {
771
841
  list = this.activateControl$();
772
842
  }
773
843
  else if (this.type() === 'and') {
774
- list = this.controls$();
844
+ list = this.fixedControls$();
775
845
  }
776
846
  else if (this.type() === 'or') {
777
- list = [this.controls$()[this.activateIndex$()]];
847
+ list = [this.fixedControls$()[this.activateIndex$()]];
778
848
  }
779
849
  else {
780
850
  throw new Error('');
@@ -783,8 +853,8 @@ class FieldLogicGroup extends FieldArray {
783
853
  }
784
854
  getValue(rawData) {
785
855
  const controls = rawData
786
- ? this.getActivateControls()
787
- : this.getActivateControls().filter((control) => control.shouldInclude$$());
856
+ ? this.#getActivateControls()
857
+ : this.#getActivateControls().filter((control) => control.shouldInclude$$());
788
858
  const control = controls[0];
789
859
  if (controls.length === 0) {
790
860
  return this.emptyValue$$();
@@ -795,6 +865,13 @@ class FieldLogicGroup extends FieldArray {
795
865
  const result = controls.reduce((obj, control) => ({ ...obj, ...control.value$$() }), {});
796
866
  return Object.keys(result).length ? result : this.emptyValue$$();
797
867
  }
868
+ reset(value) {
869
+ const initValue = this.getInitValue(value);
870
+ const viewValue = this.config$().transfomer?.toView?.(initValue, this) ?? initValue;
871
+ this.fixedControls$().forEach((control, i) => {
872
+ control.reset(viewValue);
873
+ });
874
+ }
798
875
  getRawValue() {
799
876
  return this.getValue(true);
800
877
  }
@@ -803,10 +880,18 @@ class FieldLogicGroup extends FieldArray {
803
880
  return;
804
881
  }
805
882
  const viewValue = this.config$().transfomer?.toView?.(value, this) ?? value;
806
- this.controls$().forEach((control, i) => {
883
+ this.fixedControls$().forEach((control, i) => {
807
884
  control.updateValue(viewValue);
808
885
  });
809
886
  }
887
+ /** @internal */
888
+ updateInitValue(value) {
889
+ const initValue = this.getInitValue(value);
890
+ const viewValue = this.config$().transfomer?.toView?.(initValue, this) ?? initValue;
891
+ this.fixedControls$().forEach((control, i) => {
892
+ control.updateInitValue(viewValue);
893
+ });
894
+ }
810
895
  }
811
896
 
812
897
  function isFieldGroup(input) {
@@ -822,115 +907,83 @@ function isFieldLogicGroup(input) {
822
907
  return input instanceof FieldLogicGroup;
823
908
  }
824
909
 
825
- class FieldGroup extends AbstractControl {
910
+ class FieldGroup extends FieldGroupbase {
826
911
  value$$ = computed(() => {
827
- const acc = {};
828
- const value = this._reduceChildren(acc, (acc, control, name) => {
912
+ const result = this._reduceChildren({ ...this.#looseValue$$() }, (acc, control, name) => {
829
913
  if (control.shouldInclude$$()) {
830
914
  acc[name] = control.value;
831
915
  }
832
916
  return acc;
833
917
  });
834
- const result = { ...value, ...this.looseValue$$() };
835
918
  const returnResult = Object.keys(result).length
836
919
  ? result
837
920
  : this.emptyValue$$();
838
921
  return (this.config$().transfomer?.toModel?.(returnResult, this) ?? returnResult);
839
922
  });
840
- children$$ = computed(() => Object.values(this.controls$()));
841
- controls$ = signal({});
923
+ #controls$$ = computed(() => ({
924
+ ...this.fixedControls$(),
925
+ ...this.resetControls$(),
926
+ }));
927
+ fixedControls$ = signal({});
928
+ resetControls$ = signal({});
842
929
  get controls() {
843
- return this.controls$();
930
+ return this.#controls$$();
844
931
  }
845
- registerControl(name, control) {
846
- if (this.controls$()[name])
847
- return this.controls$()[name];
848
- this.controls$.update((controls) => ({ ...controls, [name]: control }));
849
- control.setParent(this);
850
- return control;
851
- }
852
- removeControl(name) {
853
- if (this.controls$()[name]) {
854
- this.controls$.update((controls) => {
855
- controls = { ...controls };
856
- delete controls[name];
857
- return controls;
858
- });
932
+ children$$ = computed(() => Object.values(this.#controls$$()));
933
+ removeRestControl(key) {
934
+ if (!this.resetControls$()[key]) {
935
+ return;
859
936
  }
860
- }
861
- setControl(name, control) {
862
- this.controls$.update((controls) => {
937
+ this.resetControls$.update((controls) => {
863
938
  controls = { ...controls };
864
- delete controls[name];
939
+ delete controls[key];
865
940
  return controls;
866
941
  });
867
- if (control)
868
- this.registerControl(name, control);
869
942
  }
870
- reset(value = {}) {
871
- this._forEachChild((control, name) => {
872
- control.reset(value ? value[name] : undefined);
873
- });
943
+ setControl(key, control) {
944
+ const controls$ = this.inited ? this.resetControls$ : this.fixedControls$;
945
+ controls$.update((controls) => ({ ...controls, [key]: control }));
946
+ control.setParent(this);
874
947
  }
875
948
  getRawValue() {
876
- return this._reduceChildren({}, (acc, control, name) => {
877
- acc[name] = control.getRawValue();
949
+ return this._reduceChildren({}, (acc, control, key) => {
950
+ acc[key] = control.getRawValue();
878
951
  return acc;
879
952
  });
880
953
  }
954
+ clear() {
955
+ if (Object.keys(this.resetControls$()).length < 1)
956
+ return;
957
+ this.beforeUpdateList.forEach((fn) => fn({}, false));
958
+ }
881
959
  /** @internal */
882
960
  _forEachChild(cb) {
883
- Object.keys(this.controls$()).forEach((key) => {
884
- const control = this.controls$()[key];
885
- control && cb(control, key);
961
+ const controls = this.#controls$$();
962
+ Object.keys(controls).forEach((key) => {
963
+ cb(controls[key], key);
886
964
  });
887
965
  }
888
- /** @internal */
889
- _reduceChildren(initValue, fn) {
890
- let res = initValue;
891
- this._forEachChild((control, name) => {
892
- res = fn(res, control, name);
893
- });
894
- return res;
966
+ find(key) {
967
+ return this.#controls$$()[key];
895
968
  }
896
- find(name) {
897
- 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
+ : {};
898
979
  }
899
- looseValue$$ = computed(() => {
900
- const resetValue = this.#inputValue$();
980
+ #looseValue$$ = computed(() => {
981
+ const resetValue = this.resetValue$();
901
982
  if (!resetValue || isFieldLogicGroup(this.parent)) {
902
- return {};
903
- }
904
- const controls = this.controls$();
905
- const looseValue = {};
906
- for (const key in resetValue) {
907
- if (!(key in controls)) {
908
- looseValue[key] = resetValue[key];
909
- }
983
+ return undefined;
910
984
  }
911
- return looseValue;
985
+ return resetValue;
912
986
  });
913
- /**
914
- * loose object
915
- * todo 动态添加
916
- * */
917
- #inputValue$ = signal({});
918
- #setInputValue(obj) {
919
- this.#inputValue$.set(obj);
920
- }
921
- updateValue(value) {
922
- if (deepEqual(value, this.value$$())) {
923
- return;
924
- }
925
- if (!deepEqual(value, this.#inputValue$())) {
926
- this.#setInputValue(value);
927
- }
928
- const viewValue = this.config$().transfomer?.toView?.(value, this) ?? value;
929
- for (const key in this.controls$()) {
930
- const control = this.controls$()[key];
931
- control.updateValue(viewValue?.[key]);
932
- }
933
- }
934
987
  }
935
988
 
936
989
  function isGroup(schema) {
@@ -1009,8 +1062,8 @@ class ParentMap extends Map {
1009
1062
 
1010
1063
  function* groupGenerator(list) {
1011
1064
  for (const item of list) {
1012
- if (!item.keyPath?.length && item.fieldGroup?.().length) {
1013
- yield* groupGenerator(item.fieldGroup());
1065
+ if (!item.keyPath?.length && item.fixedChildren?.().length) {
1066
+ yield* groupGenerator(item.fixedChildren());
1014
1067
  }
1015
1068
  else if (item.keyPath?.length) {
1016
1069
  yield item;
@@ -1032,14 +1085,12 @@ function fieldQuery(keyPath, field, aliasMap, root) {
1032
1085
  const queryField = aliasMap.get(firstPath.slice(1));
1033
1086
  list = [{ field: queryField, level: 1 }];
1034
1087
  }
1035
- else if (field.fieldGroup) {
1036
- list = groupGenerator(field.fieldGroup())
1037
- .filter((field) => field.keyPath && arrayStartsWith(keyPath, field.keyPath))
1038
- .map((field) => ({ field: field, level: field.keyPath?.length }));
1039
- }
1040
- else if (field.fieldArray) {
1041
- list = field
1042
- .fieldArray()
1088
+ else if (field.fixedChildren || field.restChildren) {
1089
+ const children = [
1090
+ ...(field.fixedChildren?.() ?? []),
1091
+ ...(field.restChildren?.() ?? []),
1092
+ ];
1093
+ list = groupGenerator(children)
1043
1094
  .filter((field) => field.keyPath && arrayStartsWith(keyPath, field.keyPath))
1044
1095
  .map((field) => ({ field: field, level: field.keyPath?.length }));
1045
1096
  }
@@ -1058,6 +1109,7 @@ function fieldQuery(keyPath, field, aliasMap, root) {
1058
1109
  return undefined;
1059
1110
  }
1060
1111
 
1112
+ var _a;
1061
1113
  class FormBuilder {
1062
1114
  #scopeMap = inject(PI_FORM_BUILDER_ALIAS_MAP, { optional: true }) ??
1063
1115
  new ParentMap();
@@ -1067,13 +1119,14 @@ class FormBuilder {
1067
1119
  #globalConfig = inject(PI_VIEW_CONFIG_TOKEN);
1068
1120
  #allFieldInitHookList = [];
1069
1121
  buildRoot(item) {
1070
- this.#buildControl({
1122
+ const field = this.#buildControl({
1071
1123
  type: 'root',
1072
1124
  field: { fullPath: [] },
1073
1125
  form: undefined,
1074
1126
  resolvedField$: item.resolvedField$,
1075
1127
  append: () => { },
1076
1128
  }, item.field, 0);
1129
+ field.form.control?.updateInitValue(undefined);
1077
1130
  this.allFieldInitHookCall();
1078
1131
  }
1079
1132
  allFieldInitHookCall() {
@@ -1082,12 +1135,20 @@ class FormBuilder {
1082
1135
  list.forEach((fn) => fn());
1083
1136
  }
1084
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
+ }
1085
1142
  if (item.type === 'group') {
1086
1143
  this.#buildGroup(item);
1087
1144
  }
1088
1145
  else {
1089
1146
  this.#buildArray(item);
1090
1147
  }
1148
+ item.field.children = computed(() => [
1149
+ ...item.field.fixedChildren(),
1150
+ ...(item.field.restChildren?.() ?? []),
1151
+ ]);
1091
1152
  }
1092
1153
  afterResolveConfig(rawConfig, config) {
1093
1154
  return;
@@ -1144,7 +1205,7 @@ class FormBuilder {
1144
1205
  let control;
1145
1206
  let keyPath = field.key;
1146
1207
  if (isFieldLogicGroup(parent.form)) {
1147
- keyPath ??= parent.form.controls$().length;
1208
+ keyPath ??= parent.form.fixedControls$().length;
1148
1209
  }
1149
1210
  else if (isFieldArray(parent.form)) {
1150
1211
  keyPath ??= index;
@@ -1194,7 +1255,10 @@ class FormBuilder {
1194
1255
  this.#moveViewField(field.movePath, resolvedConfig);
1195
1256
  }
1196
1257
  else {
1197
- parent.append(resolvedConfig);
1258
+ if ((parent.type === 'group' || parent.type === 'array') &&
1259
+ !parent.skipAppend) {
1260
+ parent.append(resolvedConfig);
1261
+ }
1198
1262
  }
1199
1263
  if (field.alias) {
1200
1264
  this.#scopeMap.set(field.alias, resolvedConfig);
@@ -1204,28 +1268,28 @@ class FormBuilder {
1204
1268
  parent.resolvedField$.set(resolvedConfig);
1205
1269
  }
1206
1270
  // 递归进行解析
1207
- if (isGroup(field) ||
1208
- field.isLogicAnd ||
1209
- field.isLogicOr ||
1210
- field.isTuple) {
1211
- resolvedConfig.fieldGroup = signal(new SortedArray((a, b) => a.priority - b.priority));
1271
+ if (isGroup(field) || field.isLogicAnd || field.isLogicOr) {
1212
1272
  this.#buildField({
1213
1273
  type: 'group',
1274
+ templateField: field.arrayChild,
1214
1275
  fields: field.children,
1215
1276
  field: resolvedConfig,
1216
1277
  form: (control || parent.form),
1217
1278
  append: (field) => {
1218
- resolvedConfig.fieldGroup().push(field);
1279
+ resolvedConfig.fixedChildren().push(field);
1219
1280
  },
1220
1281
  });
1221
1282
  }
1222
- else if (isArray(field)) {
1283
+ else if (isArray(field) || field.isTuple) {
1223
1284
  this.#buildField({
1224
1285
  type: 'array',
1225
1286
  templateField: field.arrayChild,
1287
+ fields: field.children,
1226
1288
  field: resolvedConfig,
1227
1289
  form: control,
1228
- append: (field) => { },
1290
+ append: (field) => {
1291
+ resolvedConfig.fixedChildren().push(field);
1292
+ },
1229
1293
  });
1230
1294
  }
1231
1295
  if (resolvedConfig.hooks?.allFieldsResolved) {
@@ -1235,16 +1299,136 @@ class FormBuilder {
1235
1299
  }
1236
1300
  return resolvedConfig;
1237
1301
  }
1238
- #buildGroup(groupItem) {
1239
- for (let index = 0; index < groupItem.fields.length; index++) {
1240
- const field = groupItem.fields[index];
1241
- 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
+ };
1242
1360
  }
1243
- /** 虚拟group不存在hooks */
1244
- const field = groupItem.field;
1245
- field.hooks?.afterChildrenInit?.(field);
1246
1361
  }
1247
- 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,
1248
1432
  // 单独一项
1249
1433
  field, index) {
1250
1434
  const Builder = this.constructor;
@@ -1263,68 +1447,11 @@ class FormBuilder {
1263
1447
  result.injector?.destroy();
1264
1448
  });
1265
1449
  const instance = injector.get(Builder);
1266
- const result = instance.#buildControl(parent, field, index);
1450
+ const result = instance.#buildControl({ ...parent, skipAppend: true }, field, index);
1267
1451
  this.#allFieldInitHookList.push(() => instance.allFieldInitHookCall());
1268
1452
  result.injector = injector.get(EnvironmentInjector);
1269
1453
  return result;
1270
1454
  }
1271
- #buildArray(arrayItem) {
1272
- const { templateField, form } = arrayItem;
1273
- arrayItem.field.action = {
1274
- // 因为数组需要有动态添加的能力,所以才加上,group不需要
1275
- set: (value, index) => {
1276
- untracked(() => {
1277
- index = (typeof index === 'number'
1278
- ? index
1279
- : (arrayItem.field.fieldArray?.().length ?? 0));
1280
- const result = this.createArrayItem(arrayItem, templateField, index);
1281
- const list = [...arrayItem.field.fieldArray()];
1282
- list[index] = result;
1283
- arrayItem.field.fieldArray.set(list);
1284
- this.allFieldInitHookCall();
1285
- result.form.control.updateValue(value);
1286
- });
1287
- },
1288
- remove: (index) => {
1289
- untracked(() => {
1290
- const list = [...arrayItem.field.fieldArray()];
1291
- const [deletedItem] = list.splice(index, 1);
1292
- arrayItem.field.fieldArray.set(list);
1293
- form.removeAt(index);
1294
- if (deletedItem) {
1295
- deletedItem.injector.destroy();
1296
- deletedItem.injector = undefined;
1297
- }
1298
- });
1299
- },
1300
- };
1301
- arrayItem.field.fieldArray = signal([]);
1302
- arrayItem.field.hooks?.afterChildrenInit?.(arrayItem.field);
1303
- form.beforeUpdateList.push((input) => {
1304
- const controlLength = form.controls$().length;
1305
- if (controlLength < input.length) {
1306
- const list = arrayItem.field.fieldArray().slice();
1307
- for (let index = controlLength; index < input.length; index++) {
1308
- const result = this.createArrayItem(arrayItem, arrayItem.templateField, index);
1309
- list[index] = result;
1310
- }
1311
- arrayItem.field.fieldArray.set(list);
1312
- }
1313
- else if (input.length < controlLength) {
1314
- const list = arrayItem.field.fieldArray().slice();
1315
- for (let index = arrayItem.field.fieldArray().length - 1; index >= input.length; index--) {
1316
- const [deletedItem] = list.splice(index, 1);
1317
- form.removeAt(index);
1318
- if (deletedItem) {
1319
- deletedItem.injector?.destroy();
1320
- deletedItem.injector = undefined;
1321
- }
1322
- }
1323
- arrayItem.field.fieldArray.set(list);
1324
- }
1325
- this.allFieldInitHookCall();
1326
- });
1327
- }
1328
1455
  #resolveComponent(type) {
1329
1456
  let define;
1330
1457
  let defaultConfig;
@@ -1393,7 +1520,7 @@ class FormBuilder {
1393
1520
  const newKeyPath = inputField.fullPath.slice(parent.fullPath.length);
1394
1521
  inputField.keyPath = newKeyPath;
1395
1522
  inputField.parent = parent;
1396
- parent.fieldGroup().push(inputField);
1523
+ parent.fixedChildren().push(inputField);
1397
1524
  }
1398
1525
  #resolveWrappers(wrappers) {
1399
1526
  const result = (wrappers ?? []).map((wrapper) => {
@@ -1433,6 +1560,7 @@ class FormBuilder {
1433
1560
  return signal(result);
1434
1561
  }
1435
1562
  }
1563
+ _a = FormBuilder;
1436
1564
 
1437
1565
  function layout(value) {
1438
1566
  return {
@@ -1928,10 +2056,11 @@ class CoreSchemaHandle extends BaseSchemaHandle {
1928
2056
  }
1929
2057
  else {
1930
2058
  this.isArray = true;
1931
- const sh = new this.globalConfig.handle(this.globalConfig, this, schema);
2059
+ const sh = new this.globalConfig.handle(this.globalConfig, this, schema.item);
1932
2060
  sh.parent = this;
1933
2061
  this.arrayChild = sh;
1934
2062
  convertSchema(schema.item, sh);
2063
+ this.formConfig.groupMode = 'reset';
1935
2064
  }
1936
2065
  }
1937
2066
  defaultSchema(schema) {
@@ -1940,6 +2069,41 @@ class CoreSchemaHandle extends BaseSchemaHandle {
1940
2069
  tupleDefault(schema) {
1941
2070
  super.tupleDefault(schema);
1942
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';
1943
2107
  }
1944
2108
  enumSchema(schema) {
1945
2109
  this.props ??= {};
@@ -2053,5 +2217,5 @@ const NFCSchema = v.optional(v.void());
2053
2217
  * Generated bundle index. Do not edit.
2054
2218
  */
2055
2219
 
2056
- 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 };
2057
2221
  //# sourceMappingURL=piying-view-angular-core.mjs.map