@piying/view-angular-core 1.2.2 → 1.3.2

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
  }
@@ -492,6 +502,9 @@ class AbstractControl {
492
502
  }
493
503
  let value = initialValue;
494
504
  for (const child of childrenMap) {
505
+ if (!child) {
506
+ continue;
507
+ }
495
508
  if (shortCircuit?.(value)) {
496
509
  break;
497
510
  }
@@ -545,107 +558,167 @@ class AbstractControl {
545
558
  emitSubmit() {
546
559
  if (this.children$$) {
547
560
  this.children$$().forEach((child) => {
561
+ if (!child) {
562
+ return;
563
+ }
548
564
  child.emitSubmit();
549
565
  });
550
566
  }
551
567
  }
568
+ isUnChanged() {
569
+ return this.pristine && this.untouched;
570
+ }
571
+ }
572
+
573
+ var UpdateType;
574
+ (function (UpdateType) {
575
+ UpdateType[UpdateType["init"] = 0] = "init";
576
+ UpdateType[UpdateType["update"] = 1] = "update";
577
+ UpdateType[UpdateType["reset"] = 2] = "reset";
578
+ })(UpdateType || (UpdateType = {}));
579
+
580
+ class FieldGroupbase extends AbstractControl {
581
+ /** @internal */
582
+ getResetValue(value = []) { }
583
+ /** @internal */
584
+ beforeUpdateList = [];
585
+ resetValue$ = signal(undefined);
586
+ /** @internal */
587
+ _updateValue(value, type) {
588
+ const viewValue = this.config$().transfomer?.toView?.(value, this) ?? value;
589
+ if (type === UpdateType.init) {
590
+ this.initedValue = viewValue;
591
+ }
592
+ if (this.config$().groupMode === 'reset') {
593
+ const restValue = this.getResetValue(viewValue);
594
+ this.beforeUpdateList.forEach((fn) => fn(restValue, type !== UpdateType.init));
595
+ }
596
+ else if (this.config$().groupMode === 'loose') {
597
+ const resetValue = this.getResetValue(viewValue);
598
+ this.resetValue$.set(resetValue);
599
+ }
600
+ this._forEachChild((control, key) => {
601
+ if (type === UpdateType.init) {
602
+ control.updateInitValue(viewValue?.[key]);
603
+ }
604
+ else if (type === UpdateType.update) {
605
+ control.updateValue(viewValue?.[key]);
606
+ }
607
+ else {
608
+ control.reset(viewValue ? viewValue[key] : undefined);
609
+ }
610
+ });
611
+ }
612
+ updateValue(value) {
613
+ if (deepEqual(value, this.value$$())) {
614
+ return;
615
+ }
616
+ if (this.isUnChanged()) {
617
+ value ??= this.getInitValue(value);
618
+ }
619
+ this._updateValue(value, UpdateType.update);
620
+ }
621
+ inited = false;
622
+ /** @internal */
623
+ initedValue;
624
+ /** @internal */
625
+ updateInitValue(value) {
626
+ this.inited = true;
627
+ const initValue = this.getInitValue(value);
628
+ this._updateValue(initValue, UpdateType.init);
629
+ }
630
+ reset(value) {
631
+ const initValue = this.getInitValue(value);
632
+ this._updateValue(initValue, UpdateType.reset);
633
+ }
634
+ /** @internal */
635
+ _reduceChildren(initValue, fn) {
636
+ let res = initValue;
637
+ this._forEachChild((control, name) => {
638
+ res = fn(res, control, name);
639
+ });
640
+ return res;
641
+ }
552
642
  }
553
643
 
554
- class FieldArray extends AbstractControl {
644
+ class FieldArray extends FieldGroupbase {
555
645
  #deletionMode$$ = computed(() => this.config$().deletionMode ?? 'shrink');
556
646
  value$$ = computed(() => {
557
- const list = [];
558
- for (const control of this.controls$()) {
647
+ let list = [];
648
+ this._reduceChildren(list, (acc, control, name) => {
559
649
  if (control && control.shouldInclude$$()) {
560
650
  list.push(control.value$$());
561
651
  }
562
652
  else {
563
653
  if (this.#deletionMode$$() === 'shrink') {
564
- continue;
654
+ return list;
565
655
  }
566
656
  else if (this.#deletionMode$$() === 'mark') {
567
657
  list.push(undefined);
568
658
  }
569
659
  }
570
- }
660
+ return list;
661
+ });
662
+ list = [...list, ...(this.resetValue$() ?? [])];
571
663
  const returnResult = list.length === 0 ? this.emptyValue$$() : list;
572
664
  return (this.config$().transfomer?.toModel?.(returnResult, this) ?? returnResult);
573
665
  });
574
- children$$ = computed(() => this.controls$());
575
- controls$ = signal([]);
666
+ children$$ = computed(() => [
667
+ ...this.fixedControls$(),
668
+ ...this.resetControls$(),
669
+ ]);
670
+ fixedControls$ = signal([]);
671
+ resetControls$ = signal([]);
576
672
  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
- });
673
+ return this.children$$();
674
+ }
675
+ removeRestControl(key) {
676
+ if (!this.resetControls$()[key]) {
677
+ return;
587
678
  }
679
+ this.resetControls$.update((controls) => {
680
+ controls = controls.slice();
681
+ controls.splice(key, 1);
682
+ return controls;
683
+ });
588
684
  }
589
- setControl(index, control) {
590
- const adjustedIndex = this._adjustIndex(index);
591
- this.controls$.update((list) => {
685
+ setControl(key, control) {
686
+ const controls$ = this.inited ? this.resetControls$ : this.fixedControls$;
687
+ key = this.inited ? key - this.fixedControls$().length : key;
688
+ controls$.update((list) => {
592
689
  list = list.slice();
593
- list[adjustedIndex] = control;
690
+ list[key] = control;
594
691
  return list;
595
692
  });
596
693
  control.setParent(this);
597
694
  }
598
695
  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
- });
696
+ return this.controls.length;
608
697
  }
609
698
  getRawValue() {
610
- return this.controls$().map((control) => control.getRawValue());
699
+ return this._reduceChildren([], (acc, control, key) => {
700
+ acc[key] = control.getRawValue();
701
+ return acc;
702
+ });
611
703
  }
612
704
  clear() {
613
- if (this.controls$().length < 1)
705
+ if (this.resetControls$().length < 1)
614
706
  return;
615
- this.controls$.update(() => []);
616
- }
617
- _adjustIndex(index) {
618
- return index < 0 ? Math.max(index + this.length, 0) : index;
707
+ this.beforeUpdateList.forEach((fn) => fn([], false));
619
708
  }
620
709
  /** @internal */
621
710
  _forEachChild(cb) {
622
- this.controls$().forEach((control, index) => {
711
+ this.children$$().forEach((control, index) => {
623
712
  if (control) {
624
713
  cb(control, index);
625
714
  }
626
715
  });
627
716
  }
628
- find(name) {
629
- return this.controls$()[this._adjustIndex(name)];
630
- }
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
- });
717
+ find(key) {
718
+ return this.children$$()[key];
641
719
  }
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
- });
720
+ getResetValue(value = []) {
721
+ return value.slice(this.fixedControls$().length);
649
722
  }
650
723
  }
651
724
 
@@ -656,7 +729,6 @@ const InitPendingValue = {
656
729
  };
657
730
  class FieldControl extends AbstractControl {
658
731
  pendingStatus = signal(InitPendingValue);
659
- children$$;
660
732
  #viewIndex = 0;
661
733
  /** 视图变化时model值不变也要更新view */
662
734
  viewIndex$ = signal(0);
@@ -678,13 +750,10 @@ class FieldControl extends AbstractControl {
678
750
  /** modelValue + viewValue => modelValue */
679
751
  value$$ = signal(undefined);
680
752
  reset(formState = this.config$().defaultValue) {
681
- this.updateValue(formState, true);
682
753
  this.markAsPristine();
683
754
  this.markAsUntouched();
755
+ this.updateValue(formState);
684
756
  }
685
- /** @internal */
686
- _forEachChild(cb) { }
687
- #initInput = true;
688
757
  #viewSubject$$ = computed(() => {
689
758
  const subject = new Subject();
690
759
  this.injector.get(DestroyRef).onDestroy(() => {
@@ -729,26 +798,21 @@ class FieldControl extends AbstractControl {
729
798
  }
730
799
  }
731
800
  updateValue(value, force) {
732
- if (force) {
801
+ if (this.isUnChanged() || force) {
802
+ value ??= this.getInitValue(value);
803
+ this.modelValue$.set(value);
804
+ this.value$$.set(value);
733
805
  this.#viewIndex++;
734
806
  this.viewIndex$.set(this.#viewIndex);
735
807
  this.resetIndex$.update((a) => a + 1);
736
- this.#initInput = true;
737
- }
738
- if (this.#initInput) {
739
- this.#initInput = false;
740
- if ((value !== undefined && !deepEqual(value, this.value$$())) || force) {
741
- this.modelValue$.set(value);
742
- this.value$$.set(value);
743
- }
744
808
  return;
745
809
  }
746
810
  if (deepEqual(value, this.value$$())) {
747
811
  return;
748
812
  }
749
- this.viewIndex$.set(this.#viewIndex);
750
813
  this.modelValue$.set(value);
751
814
  this.value$$.set(value);
815
+ this.viewIndex$.set(this.#viewIndex);
752
816
  }
753
817
  emitSubmit() {
754
818
  const pendingStatus = this.pendingStatus();
@@ -761,7 +825,7 @@ class FieldControl extends AbstractControl {
761
825
  this.pendingStatus.set(InitPendingValue);
762
826
  }
763
827
  updateInitValue(value) {
764
- let initValue = this.getInitValue(value);
828
+ const initValue = this.getInitValue(value);
765
829
  this.modelValue$.set(initValue);
766
830
  this.value$$.set(initValue);
767
831
  }
@@ -777,16 +841,16 @@ class FieldLogicGroup extends FieldArray {
777
841
  const returnResult = this.getValue(false);
778
842
  return (this.config$().transfomer?.toModel?.(returnResult, this) ?? returnResult);
779
843
  });
780
- getActivateControls() {
844
+ #getActivateControls() {
781
845
  let list;
782
846
  if (this.activateControl$()) {
783
847
  list = this.activateControl$();
784
848
  }
785
849
  else if (this.type() === 'and') {
786
- list = this.controls$();
850
+ list = this.fixedControls$();
787
851
  }
788
852
  else if (this.type() === 'or') {
789
- list = [this.controls$()[this.activateIndex$()]];
853
+ list = [this.fixedControls$()[this.activateIndex$()]];
790
854
  }
791
855
  else {
792
856
  throw new Error('');
@@ -795,8 +859,8 @@ class FieldLogicGroup extends FieldArray {
795
859
  }
796
860
  getValue(rawData) {
797
861
  const controls = rawData
798
- ? this.getActivateControls()
799
- : this.getActivateControls().filter((control) => control.shouldInclude$$());
862
+ ? this.#getActivateControls()
863
+ : this.#getActivateControls().filter((control) => control.shouldInclude$$());
800
864
  const control = controls[0];
801
865
  if (controls.length === 0) {
802
866
  return this.emptyValue$$();
@@ -808,9 +872,9 @@ class FieldLogicGroup extends FieldArray {
808
872
  return Object.keys(result).length ? result : this.emptyValue$$();
809
873
  }
810
874
  reset(value) {
811
- let initValue = this.getInitValue(value);
875
+ const initValue = this.getInitValue(value);
812
876
  const viewValue = this.config$().transfomer?.toView?.(initValue, this) ?? initValue;
813
- this.controls$().forEach((control, i) => {
877
+ this.fixedControls$().forEach((control, i) => {
814
878
  control.reset(viewValue);
815
879
  });
816
880
  }
@@ -821,15 +885,19 @@ class FieldLogicGroup extends FieldArray {
821
885
  if (deepEqual(value, this.value$$())) {
822
886
  return;
823
887
  }
888
+ if (this.isUnChanged()) {
889
+ value ??= this.getInitValue(value);
890
+ }
824
891
  const viewValue = this.config$().transfomer?.toView?.(value, this) ?? value;
825
- this.controls$().forEach((control, i) => {
892
+ this.fixedControls$().forEach((control, i) => {
826
893
  control.updateValue(viewValue);
827
894
  });
828
895
  }
896
+ /** @internal */
829
897
  updateInitValue(value) {
830
- let initValue = this.getInitValue(value);
898
+ const initValue = this.getInitValue(value);
831
899
  const viewValue = this.config$().transfomer?.toView?.(initValue, this) ?? initValue;
832
- this.controls$().forEach((control, i) => {
900
+ this.fixedControls$().forEach((control, i) => {
833
901
  control.updateInitValue(viewValue);
834
902
  });
835
903
  }
@@ -848,125 +916,83 @@ function isFieldLogicGroup(input) {
848
916
  return input instanceof FieldLogicGroup;
849
917
  }
850
918
 
851
- class FieldGroup extends AbstractControl {
919
+ class FieldGroup extends FieldGroupbase {
852
920
  value$$ = computed(() => {
853
- const acc = {};
854
- const value = this._reduceChildren(acc, (acc, control, name) => {
921
+ const result = this._reduceChildren({ ...this.#looseValue$$() }, (acc, control, name) => {
855
922
  if (control.shouldInclude$$()) {
856
923
  acc[name] = control.value;
857
924
  }
858
925
  return acc;
859
926
  });
860
- const result = { ...value, ...this.looseValue$$() };
861
927
  const returnResult = Object.keys(result).length
862
928
  ? result
863
929
  : this.emptyValue$$();
864
930
  return (this.config$().transfomer?.toModel?.(returnResult, this) ?? returnResult);
865
931
  });
866
- children$$ = computed(() => Object.values(this.controls$()));
867
- controls$ = signal({});
932
+ #controls$$ = computed(() => ({
933
+ ...this.fixedControls$(),
934
+ ...this.resetControls$(),
935
+ }));
936
+ fixedControls$ = signal({});
937
+ resetControls$ = signal({});
868
938
  get controls() {
869
- return this.controls$();
939
+ return this.#controls$$();
870
940
  }
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
- });
941
+ children$$ = computed(() => Object.values(this.#controls$$()));
942
+ removeRestControl(key) {
943
+ if (!this.resetControls$()[key]) {
944
+ return;
885
945
  }
886
- }
887
- setControl(name, control) {
888
- this.controls$.update((controls) => {
946
+ this.resetControls$.update((controls) => {
889
947
  controls = { ...controls };
890
- delete controls[name];
948
+ delete controls[key];
891
949
  return controls;
892
950
  });
893
- if (control)
894
- this.registerControl(name, control);
895
951
  }
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
- });
952
+ setControl(key, control) {
953
+ const controls$ = this.inited ? this.resetControls$ : this.fixedControls$;
954
+ controls$.update((controls) => ({ ...controls, [key]: control }));
955
+ control.setParent(this);
902
956
  }
903
957
  getRawValue() {
904
- return this._reduceChildren({}, (acc, control, name) => {
905
- acc[name] = control.getRawValue();
958
+ return this._reduceChildren({}, (acc, control, key) => {
959
+ acc[key] = control.getRawValue();
906
960
  return acc;
907
961
  });
908
962
  }
963
+ clear() {
964
+ if (Object.keys(this.resetControls$()).length < 1)
965
+ return;
966
+ this.beforeUpdateList.forEach((fn) => fn({}, false));
967
+ }
909
968
  /** @internal */
910
969
  _forEachChild(cb) {
911
- Object.keys(this.controls$()).forEach((key) => {
912
- const control = this.controls$()[key];
913
- control && cb(control, key);
970
+ const controls = this.#controls$$();
971
+ Object.keys(controls).forEach((key) => {
972
+ cb(controls[key], key);
914
973
  });
915
974
  }
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;
975
+ find(key) {
976
+ return this.#controls$$()[key];
923
977
  }
924
- find(name) {
925
- return this.controls$()[name];
978
+ getResetValue(inputValue) {
979
+ const controls = this.fixedControls$();
980
+ return inputValue
981
+ ? Object.keys(inputValue).reduce((obj, item) => {
982
+ if (!(item in controls)) {
983
+ obj[item] = inputValue[item];
984
+ }
985
+ return obj;
986
+ }, {})
987
+ : {};
926
988
  }
927
- looseValue$$ = computed(() => {
928
- const resetValue = this.#inputValue$();
989
+ #looseValue$$ = computed(() => {
990
+ const resetValue = this.resetValue$();
929
991
  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
- }
992
+ return undefined;
938
993
  }
939
- return looseValue;
994
+ return resetValue;
940
995
  });
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
996
  }
971
997
 
972
998
  function isGroup(schema) {
@@ -1045,8 +1071,8 @@ class ParentMap extends Map {
1045
1071
 
1046
1072
  function* groupGenerator(list) {
1047
1073
  for (const item of list) {
1048
- if (!item.keyPath?.length && item.fieldGroup?.().length) {
1049
- yield* groupGenerator(item.fieldGroup());
1074
+ if (!item.keyPath?.length && item.fixedChildren?.().length) {
1075
+ yield* groupGenerator(item.fixedChildren());
1050
1076
  }
1051
1077
  else if (item.keyPath?.length) {
1052
1078
  yield item;
@@ -1068,14 +1094,12 @@ function fieldQuery(keyPath, field, aliasMap, root) {
1068
1094
  const queryField = aliasMap.get(firstPath.slice(1));
1069
1095
  list = [{ field: queryField, level: 1 }];
1070
1096
  }
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()
1097
+ else if (field.fixedChildren || field.restChildren) {
1098
+ const children = [
1099
+ ...(field.fixedChildren?.() ?? []),
1100
+ ...(field.restChildren?.() ?? []),
1101
+ ];
1102
+ list = groupGenerator(children)
1079
1103
  .filter((field) => field.keyPath && arrayStartsWith(keyPath, field.keyPath))
1080
1104
  .map((field) => ({ field: field, level: field.keyPath?.length }));
1081
1105
  }
@@ -1094,6 +1118,7 @@ function fieldQuery(keyPath, field, aliasMap, root) {
1094
1118
  return undefined;
1095
1119
  }
1096
1120
 
1121
+ var _a;
1097
1122
  class FormBuilder {
1098
1123
  #scopeMap = inject(PI_FORM_BUILDER_ALIAS_MAP, { optional: true }) ??
1099
1124
  new ParentMap();
@@ -1103,7 +1128,7 @@ class FormBuilder {
1103
1128
  #globalConfig = inject(PI_VIEW_CONFIG_TOKEN);
1104
1129
  #allFieldInitHookList = [];
1105
1130
  buildRoot(item) {
1106
- let field = this.#buildControl({
1131
+ const field = this.#buildControl({
1107
1132
  type: 'root',
1108
1133
  field: { fullPath: [] },
1109
1134
  form: undefined,
@@ -1119,12 +1144,20 @@ class FormBuilder {
1119
1144
  list.forEach((fn) => fn());
1120
1145
  }
1121
1146
  #buildField(item) {
1147
+ item.field.fixedChildren = signal(new SortedArray((a, b) => a.priority - b.priority));
1148
+ for (let index = 0; index < item.fields.length; index++) {
1149
+ this.#buildControl(item, item.fields[index], index);
1150
+ }
1122
1151
  if (item.type === 'group') {
1123
1152
  this.#buildGroup(item);
1124
1153
  }
1125
1154
  else {
1126
1155
  this.#buildArray(item);
1127
1156
  }
1157
+ item.field.children = computed(() => [
1158
+ ...item.field.fixedChildren(),
1159
+ ...(item.field.restChildren?.() ?? []),
1160
+ ]);
1128
1161
  }
1129
1162
  afterResolveConfig(rawConfig, config) {
1130
1163
  return;
@@ -1181,7 +1214,7 @@ class FormBuilder {
1181
1214
  let control;
1182
1215
  let keyPath = field.key;
1183
1216
  if (isFieldLogicGroup(parent.form)) {
1184
- keyPath ??= parent.form.controls$().length;
1217
+ keyPath ??= parent.form.fixedControls$().length;
1185
1218
  }
1186
1219
  else if (isFieldArray(parent.form)) {
1187
1220
  keyPath ??= index;
@@ -1231,7 +1264,10 @@ class FormBuilder {
1231
1264
  this.#moveViewField(field.movePath, resolvedConfig);
1232
1265
  }
1233
1266
  else {
1234
- parent.append(resolvedConfig);
1267
+ if ((parent.type === 'group' || parent.type === 'array') &&
1268
+ !parent.skipAppend) {
1269
+ parent.append(resolvedConfig);
1270
+ }
1235
1271
  }
1236
1272
  if (field.alias) {
1237
1273
  this.#scopeMap.set(field.alias, resolvedConfig);
@@ -1241,28 +1277,28 @@ class FormBuilder {
1241
1277
  parent.resolvedField$.set(resolvedConfig);
1242
1278
  }
1243
1279
  // 递归进行解析
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));
1280
+ if (isGroup(field) || field.isLogicAnd || field.isLogicOr) {
1249
1281
  this.#buildField({
1250
1282
  type: 'group',
1283
+ templateField: field.arrayChild,
1251
1284
  fields: field.children,
1252
1285
  field: resolvedConfig,
1253
1286
  form: (control || parent.form),
1254
1287
  append: (field) => {
1255
- resolvedConfig.fieldGroup().push(field);
1288
+ resolvedConfig.fixedChildren().push(field);
1256
1289
  },
1257
1290
  });
1258
1291
  }
1259
- else if (isArray(field)) {
1292
+ else if (isArray(field) || field.isTuple) {
1260
1293
  this.#buildField({
1261
1294
  type: 'array',
1262
1295
  templateField: field.arrayChild,
1296
+ fields: field.children,
1263
1297
  field: resolvedConfig,
1264
1298
  form: control,
1265
- append: (field) => { },
1299
+ append: (field) => {
1300
+ resolvedConfig.fixedChildren().push(field);
1301
+ },
1266
1302
  });
1267
1303
  }
1268
1304
  if (resolvedConfig.hooks?.allFieldsResolved) {
@@ -1272,16 +1308,136 @@ class FormBuilder {
1272
1308
  }
1273
1309
  return resolvedConfig;
1274
1310
  }
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);
1311
+ #buildGroup(buildItem) {
1312
+ const { templateField, form, field } = buildItem;
1313
+ if (templateField && field.form.control) {
1314
+ field.restChildren = signal([]);
1315
+ const updateItem = (key, initValue) => {
1316
+ const result = this.#createObjectRestItem({ ...buildItem, skipAppend: true }, {
1317
+ ...templateField,
1318
+ key,
1319
+ });
1320
+ field.restChildren.update((list) => [...list, result]);
1321
+ if (initValue) {
1322
+ result.form.control?.updateInitValue(form.initedValue?.[key]);
1323
+ }
1324
+ return result;
1325
+ };
1326
+ function removeItem(key) {
1327
+ field.restChildren.update((list) => {
1328
+ const index = list.findIndex((item) => item.keyPath.slice(-1)[0] === key);
1329
+ list = [...list];
1330
+ list.splice(index, 1);
1331
+ return list;
1332
+ });
1333
+ form.removeRestControl(key);
1334
+ }
1335
+ form.beforeUpdateList.push((restValue = {}, initUpdate) => {
1336
+ const restControl = form.resetControls$();
1337
+ for (const key in restControl) {
1338
+ if (key in restValue) {
1339
+ continue;
1340
+ }
1341
+ removeItem(key);
1342
+ }
1343
+ let isUpdateItem = false;
1344
+ for (const key in restValue) {
1345
+ if (key in restControl) {
1346
+ continue;
1347
+ }
1348
+ isUpdateItem = true;
1349
+ updateItem(key, initUpdate);
1350
+ }
1351
+ if (isUpdateItem) {
1352
+ this.allFieldInitHookCall();
1353
+ }
1354
+ });
1355
+ field.action = {
1356
+ set: (value, key) => {
1357
+ untracked(() => {
1358
+ const result = updateItem(key, true);
1359
+ this.allFieldInitHookCall();
1360
+ result.form.control.updateValue(value);
1361
+ });
1362
+ },
1363
+ remove: (key) => {
1364
+ untracked(() => {
1365
+ removeItem(key);
1366
+ });
1367
+ },
1368
+ };
1369
+ }
1370
+ }
1371
+ #createObjectRestItem(parent,
1372
+ // 单独一项
1373
+ field) {
1374
+ const result = this.#buildControl(parent, field, 0);
1375
+ this.#allFieldInitHookList.push(() => this.allFieldInitHookCall());
1376
+ return result;
1377
+ }
1378
+ #buildArray(buildItem) {
1379
+ const { templateField, form, field } = buildItem;
1380
+ if (templateField && field.form.control) {
1381
+ const fixedLength = field.fixedChildren?.().length ?? 0;
1382
+ field.restChildren = signal([]);
1383
+ const updateItem = (list, index, initValue) => {
1384
+ const result = this.#createArrayItem(buildItem, templateField, fixedLength + index);
1385
+ list[index] = result;
1386
+ if (initValue) {
1387
+ result.form.control?.updateInitValue(form.initedValue?.[fixedLength + index]);
1388
+ }
1389
+ return result;
1390
+ };
1391
+ function removeItem(list, index) {
1392
+ const [deletedItem] = list.splice(index, 1);
1393
+ form.removeRestControl(index);
1394
+ if (deletedItem) {
1395
+ deletedItem.injector.destroy();
1396
+ deletedItem.injector = undefined;
1397
+ }
1398
+ }
1399
+ form.beforeUpdateList.push((resetValue = [], initUpdate) => {
1400
+ const controlLength = form.resetControls$().length;
1401
+ if (resetValue.length < controlLength) {
1402
+ const list = [...field.restChildren()];
1403
+ for (let index = list.length - 1; index >= resetValue.length; index--) {
1404
+ removeItem(list, index);
1405
+ }
1406
+ field.restChildren.set(list);
1407
+ }
1408
+ else if (controlLength < resetValue.length) {
1409
+ const list = [...field.restChildren()];
1410
+ for (let index = controlLength; index < resetValue.length; index++) {
1411
+ updateItem(list, index, initUpdate);
1412
+ }
1413
+ field.restChildren.set(list);
1414
+ this.allFieldInitHookCall();
1415
+ }
1416
+ });
1417
+ field.action = {
1418
+ set: (value, index) => {
1419
+ untracked(() => {
1420
+ index = (typeof index === 'number'
1421
+ ? index
1422
+ : (field.restChildren?.().length ?? 0));
1423
+ const list = [...field.restChildren()];
1424
+ const result = updateItem(list, index, true);
1425
+ field.restChildren.set(list);
1426
+ this.allFieldInitHookCall();
1427
+ result.form.control.updateValue(value);
1428
+ });
1429
+ },
1430
+ remove: (index) => {
1431
+ untracked(() => {
1432
+ const list = [...field.restChildren()];
1433
+ removeItem(list, index);
1434
+ field.restChildren.set(list);
1435
+ });
1436
+ },
1437
+ };
1279
1438
  }
1280
- /** 虚拟group不存在hooks */
1281
- const field = groupItem.field;
1282
- field.hooks?.afterChildrenInit?.(field);
1283
1439
  }
1284
- createArrayItem(parent,
1440
+ #createArrayItem(parent,
1285
1441
  // 单独一项
1286
1442
  field, index) {
1287
1443
  const Builder = this.constructor;
@@ -1300,69 +1456,11 @@ class FormBuilder {
1300
1456
  result.injector?.destroy();
1301
1457
  });
1302
1458
  const instance = injector.get(Builder);
1303
- const result = instance.#buildControl(parent, field, index);
1459
+ const result = instance.#buildControl({ ...parent, skipAppend: true }, field, index);
1304
1460
  this.#allFieldInitHookList.push(() => instance.allFieldInitHookCall());
1305
1461
  result.injector = injector.get(EnvironmentInjector);
1306
1462
  return result;
1307
1463
  }
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
1464
  #resolveComponent(type) {
1367
1465
  let define;
1368
1466
  let defaultConfig;
@@ -1431,7 +1529,7 @@ class FormBuilder {
1431
1529
  const newKeyPath = inputField.fullPath.slice(parent.fullPath.length);
1432
1530
  inputField.keyPath = newKeyPath;
1433
1531
  inputField.parent = parent;
1434
- parent.fieldGroup().push(inputField);
1532
+ parent.fixedChildren().push(inputField);
1435
1533
  }
1436
1534
  #resolveWrappers(wrappers) {
1437
1535
  const result = (wrappers ?? []).map((wrapper) => {
@@ -1471,6 +1569,7 @@ class FormBuilder {
1471
1569
  return signal(result);
1472
1570
  }
1473
1571
  }
1572
+ _a = FormBuilder;
1474
1573
 
1475
1574
  function layout(value) {
1476
1575
  return {
@@ -1966,10 +2065,11 @@ class CoreSchemaHandle extends BaseSchemaHandle {
1966
2065
  }
1967
2066
  else {
1968
2067
  this.isArray = true;
1969
- const sh = new this.globalConfig.handle(this.globalConfig, this, schema);
2068
+ const sh = new this.globalConfig.handle(this.globalConfig, this, schema.item);
1970
2069
  sh.parent = this;
1971
2070
  this.arrayChild = sh;
1972
2071
  convertSchema(schema.item, sh);
2072
+ this.formConfig.groupMode = 'reset';
1973
2073
  }
1974
2074
  }
1975
2075
  defaultSchema(schema) {
@@ -1978,6 +2078,41 @@ class CoreSchemaHandle extends BaseSchemaHandle {
1978
2078
  tupleDefault(schema) {
1979
2079
  super.tupleDefault(schema);
1980
2080
  this.isTuple = true;
2081
+ if (schema.type === 'tuple') {
2082
+ this.formConfig.groupMode = 'default';
2083
+ }
2084
+ else if (schema.type === 'loose_tuple') {
2085
+ this.formConfig.groupMode = 'loose';
2086
+ }
2087
+ else if (schema.type === 'strict_tuple') {
2088
+ this.formConfig.groupMode = 'strict';
2089
+ }
2090
+ }
2091
+ objectDefault(schema) {
2092
+ super.objectDefault(schema);
2093
+ if (schema.type === 'object') {
2094
+ this.formConfig.groupMode = 'default';
2095
+ }
2096
+ else if (schema.type === 'loose_object') {
2097
+ this.formConfig.groupMode = 'loose';
2098
+ }
2099
+ else if (schema.type === 'strict_object') {
2100
+ this.formConfig.groupMode = 'strict';
2101
+ }
2102
+ }
2103
+ recordSchema(key, value) {
2104
+ super.recordSchema(key, value);
2105
+ this.isGroup = true;
2106
+ // equal {[name:string]:v.InferOutput< typeof value>}
2107
+ this.restSchema(value);
2108
+ }
2109
+ restSchema(schema) {
2110
+ super.restSchema(schema);
2111
+ const sh = new this.globalConfig.handle(this.globalConfig, this, schema);
2112
+ sh.parent = this;
2113
+ this.arrayChild = sh;
2114
+ convertSchema(schema, sh);
2115
+ this.formConfig.groupMode = 'reset';
1981
2116
  }
1982
2117
  enumSchema(schema) {
1983
2118
  this.props ??= {};
@@ -2091,5 +2226,5 @@ const NFCSchema = v.optional(v.void());
2091
2226
  * Generated bundle index. Do not edit.
2092
2227
  */
2093
2228
 
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 };
2229
+ 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
2230
  //# sourceMappingURL=piying-view-angular-core.mjs.map