@piying/view-angular-core 1.5.8 → 1.5.10

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.
@@ -623,7 +623,9 @@ class FieldGroupbase extends AbstractControl {
623
623
  const restValue = this.getResetValue(viewValue);
624
624
  this.beforeUpdateList.forEach((fn) => fn(restValue, type !== UpdateType.init));
625
625
  }
626
- else if (this.config$().groupMode === 'loose') {
626
+ else if (this.config$().groupMode === 'loose' ||
627
+ this.config$().groupMode === 'strict') {
628
+ // loose下为了输出值,strict下为了验证
627
629
  const resetValue = this.getResetValue(viewValue);
628
630
  this.resetValue$.set(resetValue);
629
631
  }
@@ -671,84 +673,75 @@ class FieldGroupbase extends AbstractControl {
671
673
  }
672
674
  }
673
675
 
674
- class FieldArray extends FieldGroupbase {
675
- #deletionMode$$ = computed(() => this.config$().deletionMode ?? 'shrink');
676
+ class FieldGroup extends FieldGroupbase {
676
677
  value$$ = computed(() => {
677
- let list = [];
678
- this._reduceChildren(list, (acc, control, name) => {
679
- if (control && control.shouldInclude$$()) {
680
- list.push(control.value$$());
681
- }
682
- else {
683
- if (this.#deletionMode$$() === 'shrink') {
684
- return list;
685
- }
686
- else if (this.#deletionMode$$() === 'mark') {
687
- list.push(undefined);
688
- }
678
+ const result = this._reduceChildren({ ...this.resetValue$() }, (acc, control, name) => {
679
+ if (control.shouldInclude$$()) {
680
+ acc[name] = control.value;
689
681
  }
690
- return list;
682
+ return acc;
691
683
  });
692
- list = [...list, ...(this.resetValue$() ?? [])];
693
- const returnResult = list.length === 0 ? this.emptyValue$$() : list;
684
+ const returnResult = Object.keys(result).length
685
+ ? result
686
+ : this.emptyValue$$();
694
687
  return (this.config$().transfomer?.toModel?.(returnResult, this) ?? returnResult);
695
688
  });
696
- children$$ = computed(() => [
689
+ #controls$$ = computed(() => ({
697
690
  ...this.fixedControls$(),
698
691
  ...this.resetControls$(),
699
- ]);
700
- fixedControls$ = signal([]);
701
- resetControls$ = signal([]);
692
+ }));
693
+ fixedControls$ = signal({});
694
+ resetControls$ = signal({});
702
695
  get controls() {
703
- return this.children$$();
696
+ return this.#controls$$();
704
697
  }
698
+ children$$ = computed(() => Object.values(this.#controls$$()));
705
699
  removeRestControl(key) {
706
700
  if (!this.resetControls$()[key]) {
707
701
  return;
708
702
  }
709
703
  this.resetControls$.update((controls) => {
710
- controls = controls.slice();
711
- controls.splice(key, 1);
704
+ controls = { ...controls };
705
+ delete controls[key];
712
706
  return controls;
713
707
  });
714
708
  }
715
709
  setControl(key, control) {
716
710
  const controls$ = this.inited ? this.resetControls$ : this.fixedControls$;
717
- key = this.inited ? key - this.fixedControls$().length : key;
718
- controls$.update((list) => {
719
- list = list.slice();
720
- list[key] = control;
721
- return list;
722
- });
711
+ controls$.update((controls) => ({ ...controls, [key]: control }));
723
712
  control.setParent(this);
724
713
  }
725
- get length() {
726
- return this.controls.length;
727
- }
728
714
  getRawValue() {
729
- return this._reduceChildren([], (acc, control, key) => {
715
+ return this._reduceChildren({}, (acc, control, key) => {
730
716
  acc[key] = control.getRawValue();
731
717
  return acc;
732
718
  });
733
719
  }
734
720
  clear() {
735
- if (this.resetControls$().length < 1)
721
+ if (Object.keys(this.resetControls$()).length < 1)
736
722
  return;
737
- this.beforeUpdateList.forEach((fn) => fn([], false));
723
+ this.beforeUpdateList.forEach((fn) => fn({}, false));
738
724
  }
739
725
  /** @internal */
740
726
  _forEachChild(cb) {
741
- this.children$$().forEach((control, index) => {
742
- if (control) {
743
- cb(control, index);
744
- }
727
+ const controls = this.#controls$$();
728
+ Object.keys(controls).forEach((key) => {
729
+ cb(controls[key], key);
745
730
  });
746
731
  }
747
732
  find(key) {
748
- return this.children$$()[key];
733
+ return this.#controls$$()[key];
749
734
  }
750
- getResetValue(value = []) {
751
- return value.slice(this.fixedControls$().length);
735
+ getResetValue(inputValue) {
736
+ const controls = this.fixedControls$();
737
+ return inputValue
738
+ ? Object.keys(inputValue).reduce((obj, key) => {
739
+ if (!(key in controls)) {
740
+ obj[key] = inputValue[key];
741
+ }
742
+ return obj;
743
+ }, {})
744
+ : {};
752
745
  }
753
746
  }
754
747
 
@@ -861,20 +854,100 @@ class FieldControl extends AbstractControl {
861
854
  }
862
855
  }
863
856
 
857
+ class FieldArray extends FieldGroupbase {
858
+ #deletionMode$$ = computed(() => this.config$().deletionMode ?? 'shrink');
859
+ value$$ = computed(() => {
860
+ let list = [];
861
+ this._reduceChildren(list, (acc, control, name) => {
862
+ if (control && control.shouldInclude$$()) {
863
+ list.push(control.value$$());
864
+ }
865
+ else {
866
+ if (this.#deletionMode$$() === 'shrink') {
867
+ return list;
868
+ }
869
+ else if (this.#deletionMode$$() === 'mark') {
870
+ list.push(undefined);
871
+ }
872
+ }
873
+ return list;
874
+ });
875
+ list = [...list, ...(this.resetValue$() ?? [])];
876
+ const returnResult = list.length === 0 ? this.emptyValue$$() : list;
877
+ return (this.config$().transfomer?.toModel?.(returnResult, this) ?? returnResult);
878
+ });
879
+ children$$ = computed(() => [
880
+ ...this.fixedControls$(),
881
+ ...this.resetControls$(),
882
+ ]);
883
+ fixedControls$ = signal([]);
884
+ resetControls$ = signal([]);
885
+ get controls() {
886
+ return this.children$$();
887
+ }
888
+ removeRestControl(key) {
889
+ if (!this.resetControls$()[key]) {
890
+ return;
891
+ }
892
+ this.resetControls$.update((controls) => {
893
+ controls = controls.slice();
894
+ controls.splice(key, 1);
895
+ return controls;
896
+ });
897
+ }
898
+ setControl(key, control) {
899
+ const controls$ = this.inited ? this.resetControls$ : this.fixedControls$;
900
+ key = this.inited ? key - this.fixedControls$().length : key;
901
+ controls$.update((list) => {
902
+ list = list.slice();
903
+ list[key] = control;
904
+ return list;
905
+ });
906
+ control.setParent(this);
907
+ }
908
+ get length() {
909
+ return this.controls.length;
910
+ }
911
+ getRawValue() {
912
+ return this._reduceChildren([], (acc, control, key) => {
913
+ acc[key] = control.getRawValue();
914
+ return acc;
915
+ });
916
+ }
917
+ clear() {
918
+ if (this.resetControls$().length < 1)
919
+ return;
920
+ this.beforeUpdateList.forEach((fn) => fn([], false));
921
+ }
922
+ /** @internal */
923
+ _forEachChild(cb) {
924
+ this.children$$().forEach((control, index) => {
925
+ if (control) {
926
+ cb(control, index);
927
+ }
928
+ });
929
+ }
930
+ find(key) {
931
+ return this.children$$()[key];
932
+ }
933
+ getResetValue(value = []) {
934
+ return value.slice(this.fixedControls$().length);
935
+ }
936
+ }
937
+
864
938
  // 切换索引后,理论上应该触发下值变更,否则不知道值是什么
865
939
  class FieldLogicGroup extends FieldArray {
866
- /** 待定参数 */
867
940
  activateIndex$ = signal(0);
868
941
  type = signal('and');
869
- activateControl$ = signal(undefined);
942
+ activateControls$ = signal(undefined);
870
943
  value$$ = computed(() => {
871
944
  const returnResult = this.getValue(false);
872
945
  return (this.config$().transfomer?.toModel?.(returnResult, this) ?? returnResult);
873
946
  });
874
947
  #getActivateControls() {
875
948
  let list;
876
- if (this.activateControl$()) {
877
- list = this.activateControl$();
949
+ if (this.activateControls$()) {
950
+ list = this.activateControls$();
878
951
  }
879
952
  else if (this.type() === 'and') {
880
953
  list = this.fixedControls$();
@@ -903,10 +976,7 @@ class FieldLogicGroup extends FieldArray {
903
976
  }
904
977
  reset(value) {
905
978
  const initValue = this.getInitValue(value);
906
- const viewValue = this.config$().transfomer?.toView?.(initValue, this) ?? initValue;
907
- this.fixedControls$().forEach((control, i) => {
908
- control.reset(viewValue);
909
- });
979
+ this.#updateValue(initValue, UpdateType.reset);
910
980
  }
911
981
  getRawValue() {
912
982
  return this.getValue(true);
@@ -918,17 +988,33 @@ class FieldLogicGroup extends FieldArray {
918
988
  if (this.isUnChanged()) {
919
989
  value ??= this.getInitValue(value);
920
990
  }
921
- const viewValue = this.config$().transfomer?.toView?.(value, this) ?? value;
922
- this.fixedControls$().forEach((control, i) => {
923
- control.updateValue(viewValue);
924
- });
991
+ this.#updateValue(value, UpdateType.update);
925
992
  }
926
993
  /** @internal */
927
994
  updateInitValue(value) {
928
995
  const initValue = this.getInitValue(value);
929
- const viewValue = this.config$().transfomer?.toView?.(initValue, this) ?? initValue;
996
+ this.#updateValue(initValue, UpdateType.init);
997
+ }
998
+ #updateValue(value, type) {
999
+ const viewValue = this.config$().transfomer?.toView?.(value, this) ?? value;
1000
+ let isUpdateActivate = false;
930
1001
  this.fixedControls$().forEach((control, i) => {
931
- control.updateInitValue(viewValue);
1002
+ if (type === UpdateType.init) {
1003
+ control.updateInitValue(viewValue);
1004
+ }
1005
+ else if (type === UpdateType.update) {
1006
+ control.updateValue(viewValue);
1007
+ }
1008
+ else {
1009
+ control.reset(viewValue);
1010
+ }
1011
+ if (!this.config$().disableOrUpdateActivate &&
1012
+ !isUpdateActivate &&
1013
+ control.valid &&
1014
+ this.type() === 'or') {
1015
+ this.activateIndex$.set(i);
1016
+ isUpdateActivate = true;
1017
+ }
932
1018
  });
933
1019
  }
934
1020
  }
@@ -946,85 +1032,6 @@ function isFieldLogicGroup(input) {
946
1032
  return input instanceof FieldLogicGroup;
947
1033
  }
948
1034
 
949
- class FieldGroup extends FieldGroupbase {
950
- value$$ = computed(() => {
951
- const result = this._reduceChildren({ ...this.#looseValue$$() }, (acc, control, name) => {
952
- if (control.shouldInclude$$()) {
953
- acc[name] = control.value;
954
- }
955
- return acc;
956
- });
957
- const returnResult = Object.keys(result).length
958
- ? result
959
- : this.emptyValue$$();
960
- return (this.config$().transfomer?.toModel?.(returnResult, this) ?? returnResult);
961
- });
962
- #controls$$ = computed(() => ({
963
- ...this.fixedControls$(),
964
- ...this.resetControls$(),
965
- }));
966
- fixedControls$ = signal({});
967
- resetControls$ = signal({});
968
- get controls() {
969
- return this.#controls$$();
970
- }
971
- children$$ = computed(() => Object.values(this.#controls$$()));
972
- removeRestControl(key) {
973
- if (!this.resetControls$()[key]) {
974
- return;
975
- }
976
- this.resetControls$.update((controls) => {
977
- controls = { ...controls };
978
- delete controls[key];
979
- return controls;
980
- });
981
- }
982
- setControl(key, control) {
983
- const controls$ = this.inited ? this.resetControls$ : this.fixedControls$;
984
- controls$.update((controls) => ({ ...controls, [key]: control }));
985
- control.setParent(this);
986
- }
987
- getRawValue() {
988
- return this._reduceChildren({}, (acc, control, key) => {
989
- acc[key] = control.getRawValue();
990
- return acc;
991
- });
992
- }
993
- clear() {
994
- if (Object.keys(this.resetControls$()).length < 1)
995
- return;
996
- this.beforeUpdateList.forEach((fn) => fn({}, false));
997
- }
998
- /** @internal */
999
- _forEachChild(cb) {
1000
- const controls = this.#controls$$();
1001
- Object.keys(controls).forEach((key) => {
1002
- cb(controls[key], key);
1003
- });
1004
- }
1005
- find(key) {
1006
- return this.#controls$$()[key];
1007
- }
1008
- getResetValue(inputValue) {
1009
- const controls = this.fixedControls$();
1010
- return inputValue
1011
- ? Object.keys(inputValue).reduce((obj, item) => {
1012
- if (!(item in controls)) {
1013
- obj[item] = inputValue[item];
1014
- }
1015
- return obj;
1016
- }, {})
1017
- : {};
1018
- }
1019
- #looseValue$$ = computed(() => {
1020
- const resetValue = this.resetValue$();
1021
- if (!resetValue || isFieldLogicGroup(this.parent)) {
1022
- return undefined;
1023
- }
1024
- return resetValue;
1025
- });
1026
- }
1027
-
1028
1035
  function isGroup(schema) {
1029
1036
  return schema.isGroup;
1030
1037
  }