@piying/view-angular-core 2.6.2 → 2.6.4

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
  /** 父级取值时,当前子级是否包含在内 */
@@ -187,6 +188,15 @@ class AbstractControl {
187
188
  /** validator */
188
189
  #validators$$ = computed(() => this.config$().validators ?? []);
189
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
+ });
190
200
  resetIndex$ = signal(0);
191
201
  syncError$ = linkedSignal(computed(() => {
192
202
  const disabled = this.disabled$$();
@@ -195,23 +205,24 @@ class AbstractControl {
195
205
  }
196
206
  // 请求同级
197
207
  this.resetIndex$();
198
- this.originValue$$();
199
- const childrenResult = this.reduceChildren([], (child, value, key) => {
200
- if ((!child.selfDisabled$$() ||
201
- (child.selfDisabled$$() &&
202
- child.config$().disabledValue === 'reserve')) &&
203
- child.errors) {
204
- value.push({
205
- kind: 'descendant',
206
- key: key,
207
- metadata: child.errors,
208
- field: child,
209
- });
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;
210
225
  }
211
- return value;
212
- });
213
- if (childrenResult.length) {
214
- return childrenResult;
215
226
  }
216
227
  const result = this.#validators$$().flatMap((item) => {
217
228
  const result = untracked(() => item(this));
@@ -325,6 +336,40 @@ class AbstractControl {
325
336
  get parent() {
326
337
  return this._parent;
327
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
+ }
328
373
  get value() {
329
374
  return this.value$$();
330
375
  }
@@ -426,11 +471,11 @@ class AbstractControl {
426
471
  return null;
427
472
  }
428
473
  setControl(name, control) { }
429
- *activatedChildrenIterable() { }
474
+ *activatedChildren() { }
430
475
  /** 校验和获得值用 */
431
476
  reduceChildren(initialValue, fn, shortCircuit) {
432
477
  let result = initialValue;
433
- for (const [key, child] of this.activatedChildrenIterable()) {
478
+ for (const [key, child] of this.activatedChildren()) {
434
479
  if (!child) {
435
480
  continue;
436
481
  }
@@ -455,15 +500,18 @@ class AbstractControl {
455
500
  if (this.disabled$$()) {
456
501
  return VALID;
457
502
  }
458
- const childStatus = this.reduceChildren(VALID, (child, value) => {
459
- if (value === INVALID || child.status$$() === INVALID) {
460
- return INVALID;
461
- }
462
- else if (value === PENDING || child.status$$() === PENDING) {
463
- return PENDING;
464
- }
465
- return VALID;
466
- }, (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
+ }
467
515
  if (childStatus === VALID) {
468
516
  if (this.rawError$$()) {
469
517
  if (this.rawError$$() !== PENDING) {
@@ -613,7 +661,7 @@ class FieldGroup extends FieldGroupbase {
613
661
  return this.#controls$$();
614
662
  }
615
663
  children$$ = computed(() => this.#controls$$());
616
- *activatedChildrenIterable() {
664
+ *activatedChildren() {
617
665
  const children = this.children$$();
618
666
  for (const key in children) {
619
667
  yield [key, children[key]];
@@ -790,7 +838,7 @@ class FieldArray extends FieldGroupbase {
790
838
  get controls() {
791
839
  return this.children$$();
792
840
  }
793
- *activatedChildrenIterable() {
841
+ *activatedChildren() {
794
842
  const children = this.children$$();
795
843
  for (let index = 0; index < children.length; index++) {
796
844
  yield [index, children[index]];
@@ -848,6 +896,7 @@ class FieldArray extends FieldGroupbase {
848
896
 
849
897
  // 切换索引后,理论上应该触发下值变更,否则不知道值是什么
850
898
  class FieldLogicGroup extends FieldArray {
899
+ skipValuePath = true;
851
900
  activateIndex$ = signal(0);
852
901
  type = signal('and');
853
902
  /** 过滤激活控件 */
@@ -863,25 +912,7 @@ class FieldLogicGroup extends FieldArray {
863
912
  }
864
913
  return this.#childUpdate();
865
914
  });
866
- #getActivateControls() {
867
- let list;
868
- const fn = this.filterActivateControl$();
869
- if (fn) {
870
- list = this.children$$().filter(fn);
871
- }
872
- else if (this.type() === 'and') {
873
- list = this.fixedControls$();
874
- }
875
- else if (this.type() === 'or') {
876
- list = [this.fixedControls$()[this.activateIndex$()]];
877
- }
878
- else {
879
- throw new Error('');
880
- }
881
- return list;
882
- }
883
- activatedChildren$$ = computed(() => this.#getActivateControls());
884
- activatedChildrenIterable = computed(() => {
915
+ activatedChildren = computed(() => {
885
916
  const filterFn = this.filterActivateControl$();
886
917
  const type = this.type();
887
918
  if (filterFn) {
@@ -903,8 +934,10 @@ class FieldLogicGroup extends FieldArray {
903
934
  });
904
935
  getValue(rawData) {
905
936
  const controls = rawData
906
- ? this.activatedChildren$$()
907
- : this.activatedChildren$$().filter((control) => control.shouldInclude$$());
937
+ ? this.activatedChildren().map(([index, control]) => control)
938
+ : this.activatedChildren()
939
+ .map(([index, control]) => control)
940
+ .filter((control) => control.shouldInclude$$());
908
941
  const control = controls[0];
909
942
  if (controls.length === 0) {
910
943
  return this.emptyValue$$();
@@ -2530,7 +2563,7 @@ function AnyDefault(input, schemahandle) {
2530
2563
  : input;
2531
2564
  }
2532
2565
  const checkOverride = {
2533
- logicGroup: (schemahandle) => v.pipe(AnyDefault(v.pipe(AnyDefine, v.check(Boolean)), schemahandle)),
2566
+ logicGroup: (schemahandle) => v.pipe(AnyDefault(AnyDefine, schemahandle)),
2534
2567
  array: (schemahandle) => {
2535
2568
  const source = schemahandle.coreSchema;
2536
2569
  const length = source &&
@@ -2707,6 +2740,8 @@ class CoreSchemaHandle extends BaseSchemaHandle {
2707
2740
  beforeSchemaType(schema) {
2708
2741
  super.beforeSchemaType(schema);
2709
2742
  this.formConfig.required = !this.undefinedable && !this.nullable;
2743
+ this.formConfig.undefinedable = this.undefinedable;
2744
+ this.formConfig.nullable = this.nullable;
2710
2745
  }
2711
2746
  voidSchema(schema) {
2712
2747
  this.nonFieldControl = true;