@piying/view-core 2.6.1 → 2.6.3

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.
@@ -134,6 +134,7 @@ const InitPendingValue = {
134
134
  value: undefined,
135
135
  };
136
136
  class AbstractControl {
137
+ skipValuePath;
137
138
  pendingStatus = signal(InitPendingValue);
138
139
  emptyValue$$ = computed(() => clone(this.config$().emptyValue));
139
140
  /** 父级取值时,当前子级是否包含在内 */
@@ -147,8 +148,6 @@ class AbstractControl {
147
148
  const result = this.#schemaCheck$$();
148
149
  return result.success ? result.output : value;
149
150
  });
150
- /** 已激活的子级,用于校验获得返回值之类 */
151
- activatedChildren$$;
152
151
  /** 通用的子级,用于查询之类 */
153
152
  children$$;
154
153
  /** disabled */
@@ -189,6 +188,15 @@ class AbstractControl {
189
188
  /** validator */
190
189
  #validators$$ = computed(() => this.config$().validators ?? []);
191
190
  #asyncValidators$$ = computed(() => this.config$().asyncValidators ?? []);
191
+ #undefinedable$$ = computed(() => this.config$().undefinedable);
192
+ #nullable$$ = computed(() => this.config$().nullable);
193
+ #isOptionalEmpty = computed(() => {
194
+ if (this.required$$()) {
195
+ return false;
196
+ }
197
+ return ((this.#undefinedable$$() && this.originValue$$() === undefined) ||
198
+ (this.#nullable$$() && this.originValue$$() === null));
199
+ });
192
200
  resetIndex$ = signal(0);
193
201
  syncError$ = linkedSignal(computed(() => {
194
202
  const disabled = this.disabled$$();
@@ -197,23 +205,24 @@ class AbstractControl {
197
205
  }
198
206
  // 请求同级
199
207
  this.resetIndex$();
200
- this.originValue$$();
201
- const childrenResult = this.reduceChildren([], (child, value, key) => {
202
- if ((!child.selfDisabled$$() ||
203
- (child.selfDisabled$$() &&
204
- child.config$().disabledValue === 'reserve')) &&
205
- child.errors) {
206
- value.push({
207
- kind: 'descendant',
208
- key: key,
209
- metadata: child.errors,
210
- field: child,
211
- });
208
+ if (!this.#isOptionalEmpty()) {
209
+ const childrenResult = this.reduceChildren([], (child, value, key) => {
210
+ if ((!child.selfDisabled$$() ||
211
+ (child.selfDisabled$$() &&
212
+ child.config$().disabledValue === 'reserve')) &&
213
+ child.errors) {
214
+ value.push({
215
+ kind: 'descendant',
216
+ key: key,
217
+ metadata: child.errors,
218
+ field: child,
219
+ });
220
+ }
221
+ return value;
222
+ });
223
+ if (childrenResult.length) {
224
+ return childrenResult;
212
225
  }
213
- return value;
214
- });
215
- if (childrenResult.length) {
216
- return childrenResult;
217
226
  }
218
227
  const result = this.#validators$$().flatMap((item) => {
219
228
  const result = untracked(() => item(this));
@@ -327,6 +336,40 @@ class AbstractControl {
327
336
  get parent() {
328
337
  return this._parent;
329
338
  }
339
+ #getPath(parentPath, skip) {
340
+ if (!this.parent) {
341
+ return [];
342
+ }
343
+ if (skip()) {
344
+ return parentPath();
345
+ }
346
+ const parentChildren = this.parent.children$$();
347
+ let index;
348
+ if (Array.isArray(parentChildren)) {
349
+ index = parentChildren.findIndex((item) => item === this);
350
+ if (index === -1) {
351
+ throw new Error('child index not found');
352
+ }
353
+ }
354
+ else {
355
+ for (const key in parentChildren) {
356
+ if (parentChildren[key] === this) {
357
+ index = key;
358
+ break;
359
+ }
360
+ }
361
+ if (!index) {
362
+ throw new Error('child index not found');
363
+ }
364
+ }
365
+ return [...parentPath(), index];
366
+ }
367
+ get valuePath() {
368
+ return this.#getPath(() => this.parent.valuePath, () => !!this.parent.skipValuePath);
369
+ }
370
+ get formPath() {
371
+ return this.#getPath(() => this.parent.formPath, () => false);
372
+ }
330
373
  get value() {
331
374
  return this.value$$();
332
375
  }
@@ -428,27 +471,20 @@ class AbstractControl {
428
471
  return null;
429
472
  }
430
473
  setControl(name, control) { }
474
+ *activatedChildrenIterable() { }
431
475
  /** 校验和获得值用 */
432
476
  reduceChildren(initialValue, fn, shortCircuit) {
433
- const childrenMap = (this.activatedChildren$$ ?? this.children$$)?.();
434
- if (!childrenMap) {
435
- return initialValue;
436
- }
437
- let value = initialValue;
438
- const list = Object.entries(childrenMap);
439
- const isArray = Array.isArray(childrenMap);
440
- for (let index = 0; index < list.length; index++) {
441
- if (!list[index][1]) {
477
+ let result = initialValue;
478
+ for (const [key, child] of this.activatedChildrenIterable()) {
479
+ if (!child) {
442
480
  continue;
443
481
  }
444
- const key = isArray ? index : list[index][0];
445
- const item = list[index][1];
446
- if (shortCircuit?.(value)) {
482
+ if (shortCircuit?.(result)) {
447
483
  break;
448
484
  }
449
- value = fn(item, value, key);
485
+ result = fn(child, result, key);
450
486
  }
451
- return value;
487
+ return result;
452
488
  }
453
489
  #valueChange;
454
490
  get valueChanges() {
@@ -464,15 +500,18 @@ class AbstractControl {
464
500
  if (this.disabled$$()) {
465
501
  return VALID;
466
502
  }
467
- const childStatus = this.reduceChildren(VALID, (child, value) => {
468
- if (value === INVALID || child.status$$() === INVALID) {
469
- return INVALID;
470
- }
471
- else if (value === PENDING || child.status$$() === PENDING) {
472
- return PENDING;
473
- }
474
- return VALID;
475
- }, (v) => v === INVALID || v === PENDING);
503
+ let childStatus = VALID;
504
+ if (!this.#isOptionalEmpty()) {
505
+ childStatus = this.reduceChildren(VALID, (child, value) => {
506
+ if (value === INVALID || child.status$$() === INVALID) {
507
+ return INVALID;
508
+ }
509
+ else if (value === PENDING || child.status$$() === PENDING) {
510
+ return PENDING;
511
+ }
512
+ return VALID;
513
+ }, (v) => v === INVALID || v === PENDING);
514
+ }
476
515
  if (childStatus === VALID) {
477
516
  if (this.rawError$$()) {
478
517
  if (this.rawError$$() !== PENDING) {
@@ -622,6 +661,12 @@ class FieldGroup extends FieldGroupbase {
622
661
  return this.#controls$$();
623
662
  }
624
663
  children$$ = computed(() => this.#controls$$());
664
+ *activatedChildrenIterable() {
665
+ const children = this.children$$();
666
+ for (const key in children) {
667
+ yield [key, children[key]];
668
+ }
669
+ }
625
670
  removeRestControl(key) {
626
671
  if (!this.resetControls$()[key]) {
627
672
  return;
@@ -793,6 +838,12 @@ class FieldArray extends FieldGroupbase {
793
838
  get controls() {
794
839
  return this.children$$();
795
840
  }
841
+ *activatedChildrenIterable() {
842
+ const children = this.children$$();
843
+ for (let index = 0; index < children.length; index++) {
844
+ yield [index, children[index]];
845
+ }
846
+ }
796
847
  removeRestControl(key) {
797
848
  if (!this.resetControls$()[key]) {
798
849
  return;
@@ -845,9 +896,11 @@ class FieldArray extends FieldGroupbase {
845
896
 
846
897
  // 切换索引后,理论上应该触发下值变更,否则不知道值是什么
847
898
  class FieldLogicGroup extends FieldArray {
899
+ skipValuePath = true;
848
900
  activateIndex$ = signal(0);
849
901
  type = signal('and');
850
- activateControls$ = signal(undefined);
902
+ /** 过滤激活控件 */
903
+ filterActivateControl$ = signal(undefined);
851
904
  #childUpdate() {
852
905
  const returnResult = this.getValue(false);
853
906
  return this.transformToModel(returnResult, this);
@@ -861,8 +914,9 @@ class FieldLogicGroup extends FieldArray {
861
914
  });
862
915
  #getActivateControls() {
863
916
  let list;
864
- if (this.activateControls$()) {
865
- list = this.activateControls$();
917
+ const fn = this.filterActivateControl$();
918
+ if (fn) {
919
+ list = this.children$$().filter(fn);
866
920
  }
867
921
  else if (this.type() === 'and') {
868
922
  list = this.fixedControls$();
@@ -876,6 +930,26 @@ class FieldLogicGroup extends FieldArray {
876
930
  return list;
877
931
  }
878
932
  activatedChildren$$ = computed(() => this.#getActivateControls());
933
+ activatedChildrenIterable = computed(() => {
934
+ const filterFn = this.filterActivateControl$();
935
+ const type = this.type();
936
+ if (filterFn) {
937
+ const children = this.children$$();
938
+ return children
939
+ .map((element, index) => [index, element])
940
+ .filter(([index, item]) => !!filterFn(item, index, children));
941
+ }
942
+ else if (type === 'and') {
943
+ return this.fixedControls$().map((control, index) => [index, control]);
944
+ }
945
+ else if (type === 'or') {
946
+ const index = this.activateIndex$();
947
+ return [
948
+ [index, this.fixedControls$()[index]],
949
+ ];
950
+ }
951
+ return [];
952
+ });
879
953
  getValue(rawData) {
880
954
  const controls = rawData
881
955
  ? this.activatedChildren$$()
@@ -2505,7 +2579,7 @@ function AnyDefault(input, schemahandle) {
2505
2579
  : input;
2506
2580
  }
2507
2581
  const checkOverride = {
2508
- logicGroup: (schemahandle) => v.pipe(AnyDefault(v.pipe(AnyDefine, v.check(Boolean)), schemahandle)),
2582
+ logicGroup: (schemahandle) => v.pipe(AnyDefault(AnyDefine, schemahandle)),
2509
2583
  array: (schemahandle) => {
2510
2584
  const source = schemahandle.coreSchema;
2511
2585
  const length = source &&
@@ -2682,6 +2756,8 @@ class CoreSchemaHandle extends BaseSchemaHandle {
2682
2756
  beforeSchemaType(schema) {
2683
2757
  super.beforeSchemaType(schema);
2684
2758
  this.formConfig.required = !this.undefinedable && !this.nullable;
2759
+ this.formConfig.undefinedable = this.undefinedable;
2760
+ this.formConfig.nullable = this.nullable;
2685
2761
  }
2686
2762
  voidSchema(schema) {
2687
2763
  this.nonFieldControl = true;