@tmagic/form 1.8.0-beta.16 → 1.8.0-beta.18

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.
@@ -2,7 +2,6 @@
2
2
  .m-fields-group-list-item.tmagic-design-card--flat:last-child {
3
3
  border: 0;
4
4
  }
5
-
6
5
  .el-button--text {
7
6
  padding: 0;
8
7
  margin-bottom: 7px;
@@ -32,9 +31,19 @@
32
31
  }
33
32
 
34
33
  .m-fields-group-list-footer {
35
- display: flex;
36
34
  justify-content: space-between;
37
35
  margin-top: 10px;
38
36
  margin-bottom: 16px;
39
37
  }
40
38
  }
39
+
40
+ /** 最外层的groupList需要每个item需要增加空白区域界限时,增加outer-gorup_list可以实现 */
41
+ .m-container-group-list {
42
+ &.outer-gorup_list {
43
+ > .m-fields-group-list {
44
+ > .m-fields-group-list-item {
45
+ margin-bottom: 10px;
46
+ }
47
+ }
48
+ }
49
+ }
@@ -92,4 +92,15 @@
92
92
  text-align: left;
93
93
  }
94
94
  }
95
+
96
+ .m-fields-group-list {
97
+ .el-table__empty-block {
98
+ .el-table__empty-text {
99
+ width: 100%;
100
+ background-color: rgba(0, 0, 0, 0.03);
101
+ margin-top: 8px;
102
+ border-radius: 4px;
103
+ }
104
+ }
105
+ }
95
106
  }
package/src/utils/form.ts CHANGED
@@ -60,13 +60,15 @@ export const adaptFormValidator = (validator: AsyncValidatorFn): AsyncValidatorF
60
60
  const value = arg1;
61
61
  return new Promise((resolve) => {
62
62
  let settled = false;
63
- const callback = (error?: Error | string) => {
63
+ const callback = (error?: Error | string | (Error | string)[]) => {
64
64
  if (settled) return;
65
65
  settled = true;
66
- if (error) {
66
+ // async-validator 约定 callback 也可接收错误数组,TDesign 只能展示一条,取首条
67
+ const first = Array.isArray(error) ? error[0] : error;
68
+ if (first) {
67
69
  resolve({
68
70
  result: false,
69
- message: typeof error === 'string' ? error : error.message,
71
+ message: typeof first === 'string' ? first : first.message,
70
72
  });
71
73
  } else {
72
74
  resolve(true);
@@ -74,8 +76,9 @@ export const adaptFormValidator = (validator: AsyncValidatorFn): AsyncValidatorF
74
76
  };
75
77
 
76
78
  try {
79
+ // 异步 validator 会先返回 undefined,稍后再调 callback,这里不能当成 thenable 取值
77
80
  const result = validator(undefined, value, callback);
78
- if (result !== null && typeof (result as PromiseLike<unknown>).then === 'function') {
81
+ if (result && typeof (result as PromiseLike<unknown>).then === 'function') {
79
82
  Promise.resolve(result).then(
80
83
  () => {
81
84
  if (!settled) callback();
@@ -38,8 +38,15 @@ export interface TypeMatchValidateContext {
38
38
  // #endregion TypeMatchValidateContext
39
39
 
40
40
  // #region TypeMatchValidator
41
- /** 自定义 type 校验器:返回错误文案;通过则返回 undefined */
42
- export type TypeMatchValidator = (value: any, context: TypeMatchValidateContext) => string | undefined;
41
+ /**
42
+ * 自定义 type 校验器:返回错误文案;通过则返回 undefined
43
+ *
44
+ * 支持返回 Promise,用于需要异步确认取值是否合法的场景(如请求接口校验 id 是否存在)。
45
+ */
46
+ export type TypeMatchValidator = (
47
+ value: any,
48
+ context: TypeMatchValidateContext,
49
+ ) => string | undefined | Promise<string | undefined>;
43
50
  // #endregion TypeMatchValidator
44
51
 
45
52
  const typeMatchRuleRegistry = new Map<string, TypeMatchValidator>();
@@ -692,12 +699,17 @@ const validateBuiltinTypeMatch = (
692
699
  return undefined;
693
700
  };
694
701
 
702
+ /**
703
+ * 校验取值与字段 type 是否匹配;通过返回 undefined,否则返回错误文案。
704
+ *
705
+ * 命中的自定义规则是异步校验器时返回 Promise,内置规则始终同步返回。
706
+ */
695
707
  export const validateTypeMatch = (
696
708
  value: any,
697
709
  mForm: FormState | undefined,
698
710
  props: any,
699
711
  message?: string,
700
- ): string | undefined => {
712
+ ): string | undefined | Promise<string | undefined> => {
701
713
  if (isEmptyValue(value) || isEmptyArray(value)) {
702
714
  return undefined;
703
715
  }
@@ -724,43 +736,135 @@ export const validateTypeMatch = (
724
736
  return validateBuiltinTypeMatch(value, fieldType, mForm, props, message);
725
737
  };
726
738
 
739
+ const toError = (error: any): Error => (error instanceof Error ? error : new Error(`${error}`));
740
+
727
741
  export const createTypeMatchValidator = (mForm: FormState | undefined, props: any, rule: Rule) => {
728
742
  const originalValidator = typeof rule.validator === 'function' ? rule.validator : undefined;
729
743
 
744
+ /**
745
+ * 每次校验取一个自增序号,只有最新一轮的结论会被采用。
746
+ *
747
+ * 旧值的在途校验不能用自己的结论结算:它可能晚于新校验结束,用过期结论覆盖新结论
748
+ * (表单取最后结束的那次结果);也不能在新校验开始时无条件按通过结算,否则
749
+ * `form.validate()` 会对一个还没校验过的值返回成功。因此所有尚未结算的调用都登记在
750
+ * pendingSettlers 中,等最新一轮出结论后用同一个结论一起结算。
751
+ *
752
+ * 不比较取值本身:配了 names 或取值为对象/数组时拿到的是同一个引用,就地修改后比较不出变化。
753
+ */
754
+ let generation = 0;
755
+ let pendingSettlers: ((args: any[]) => void)[] = [];
756
+
730
757
  return (asyncValidatorRule: any, value: any, callback: Function, source: any, options: any) => {
731
758
  const actualValue = props.config?.names ? props.model : value;
732
- try {
733
- const error = validateTypeMatch(actualValue, mForm, props, rule.message);
734
759
 
735
- if (error) {
736
- callback(new Error(error));
760
+ generation += 1;
761
+ const currentGeneration = generation;
762
+ /** 已有更新的一轮校验开始,本轮结论作废 */
763
+ const isStale = () => currentGeneration !== generation;
764
+
765
+ pendingSettlers.push((args: any[]) => callback(...args));
766
+
767
+ /**
768
+ * 结算本轮与被本轮取代的所有在途校验。
769
+ *
770
+ * async-validator 的 callback 不幂等:重复调用会让它的内部计数提前满足、错误信息重复;
771
+ * 原始 validator 写成 async 时很容易既调 callback 又返回 Promise,故所有回调都收敛到这里,
772
+ * 由「取出即出队」保证每个调用的 callback 只会被调用一次。
773
+ */
774
+ const conclude = (...args: any[]) => {
775
+ if (isStale()) {
737
776
  return;
738
777
  }
739
- } catch (error) {
740
- console.error(error);
741
- }
742
-
743
- if (originalValidator) {
744
- return originalValidator(
745
- {
746
- rule: asyncValidatorRule,
747
- value: actualValue,
748
- callback,
749
- source,
750
- options,
751
- },
752
- {
753
- values: mForm?.initValues || {},
754
- model: props.model,
755
- parent: mForm?.parentValues || {},
756
- formValue: mForm?.values || props.model,
757
- prop: props.prop,
758
- config: props.config,
759
- },
760
- mForm,
761
- );
778
+
779
+ const settlers = pendingSettlers;
780
+ pendingSettlers = [];
781
+ for (const settle of settlers) {
782
+ settle(args);
783
+ }
784
+ };
785
+
786
+ /**
787
+ * 执行原始 validator 并把结果统一转成 callback
788
+ *
789
+ * async-validator 只解析同步返回给它的返回值(true / false / Error / 错误数组 / Promise,
790
+ * 抛错转成错误信息),异步路径已脱离它的调用栈,这些约定会失效导致校验永不结束,故复刻一份。
791
+ */
792
+ const settleWithOriginalValidator = () => {
793
+ // 本轮结论已作废,不必再跑一遍原始 validator 触发副作用
794
+ if (isStale()) {
795
+ return;
796
+ }
797
+
798
+ if (!originalValidator) {
799
+ conclude();
800
+ return;
801
+ }
802
+
803
+ let result: any;
804
+ try {
805
+ result = originalValidator(
806
+ { rule: asyncValidatorRule, value: actualValue, callback: conclude, source, options },
807
+ {
808
+ values: mForm?.initValues || {},
809
+ model: props.model,
810
+ parent: mForm?.parentValues || {},
811
+ formValue: mForm?.values || props.model,
812
+ prop: props.prop,
813
+ config: props.config,
814
+ },
815
+ mForm,
816
+ );
817
+ } catch (err) {
818
+ conclude(toError(err));
819
+ return;
820
+ }
821
+
822
+ if (isPromise(result)) {
823
+ result.then(
824
+ () => conclude(),
825
+ (err: any) => conclude(toError(err)),
826
+ );
827
+ } else if (result === true) {
828
+ conclude();
829
+ } else if (result === false) {
830
+ const field = asyncValidatorRule?.fullField || asyncValidatorRule?.field || props.prop;
831
+ conclude(new Error(rule.message || `${field} fails`));
832
+ } else if (result instanceof Error || Array.isArray(result)) {
833
+ conclude(result);
834
+ }
835
+ // 其余返回值(undefined / void)按约定由 validator 自行调用 callback
836
+ };
837
+
838
+ // 校验器自身失败(如接口异常)不应阻塞用户,记录后按通过处理
839
+ const skipFailedTypeMatch = (err: any) => {
840
+ console.error(err);
841
+ settleWithOriginalValidator();
842
+ };
843
+
844
+ let error: string | undefined | Promise<string | undefined>;
845
+ try {
846
+ error = validateTypeMatch(actualValue, mForm, props, rule.message);
847
+ } catch (err) {
848
+ skipFailedTypeMatch(err);
849
+ return;
850
+ }
851
+
852
+ if (isPromise(error)) {
853
+ error.then((asyncMessage) => {
854
+ if (asyncMessage) {
855
+ conclude(new Error(asyncMessage));
856
+ return;
857
+ }
858
+ settleWithOriginalValidator();
859
+ }, skipFailedTypeMatch);
860
+ return;
861
+ }
862
+
863
+ if (error) {
864
+ conclude(new Error(error));
865
+ return;
762
866
  }
763
867
 
764
- callback();
868
+ settleWithOriginalValidator();
765
869
  };
766
870
  };
package/types/index.d.ts CHANGED
@@ -128,6 +128,11 @@ interface SubmitFormOptions {
128
128
  */
129
129
  debug?: boolean;
130
130
  typeMatchValid?: boolean;
131
+ /**
132
+ * 外部中断信号。abort 时会立即以 `signal.reason` reject 并卸载临时表单实例、移除容器。
133
+ * 主要用于 `debug` 模式(无超时兜底)下取消一个被放弃的表单弹层,避免其无限驻留在页面上。
134
+ */
135
+ signal?: AbortSignal;
131
136
  }
132
137
  /**
133
138
  * 开启 `returnChangeNodes` 时 submitForm 的返回结果
@@ -209,6 +214,11 @@ interface ValidateFormOptions {
209
214
  */
210
215
  debug?: boolean;
211
216
  typeMatchValid?: boolean;
217
+ /**
218
+ * 外部中断信号。abort 时会立即以 `signal.reason` reject 并卸载临时表单实例、移除容器。
219
+ * 主要用于 `debug` 模式(无超时兜底)下取消一个被放弃的表单弹层,避免其无限驻留在页面上。
220
+ */
221
+ signal?: AbortSignal;
212
222
  }
213
223
  /**
214
224
  * 返回一份去除了 tab 标签页 lazy 的配置副本。
@@ -2196,8 +2206,12 @@ interface TypeMatchValidateContext {
2196
2206
  props: any;
2197
2207
  message?: string;
2198
2208
  }
2199
- /** 自定义 type 校验器:返回错误文案;通过则返回 undefined */
2200
- type TypeMatchValidator = (value: any, context: TypeMatchValidateContext) => string | undefined;
2209
+ /**
2210
+ * 自定义 type 校验器:返回错误文案;通过则返回 undefined
2211
+ *
2212
+ * 支持返回 Promise,用于需要异步确认取值是否合法的场景(如请求接口校验 id 是否存在)。
2213
+ */
2214
+ type TypeMatchValidator = (value: any, context: TypeMatchValidateContext) => string | undefined | Promise<string | undefined>;
2201
2215
  /** 注册或覆盖某个字段 type 的 typeMatch 校验规则 */
2202
2216
  declare const registerTypeMatchRule: (type: string, validator: TypeMatchValidator) => void;
2203
2217
  /** 批量注册 typeMatch 校验规则 */
@@ -2208,7 +2222,12 @@ declare const getTypeMatchRule: (type: string) => TypeMatchValidator | undefined
2208
2222
  declare const deleteTypeMatchRule: (type: string) => boolean;
2209
2223
  /** 清空所有自定义 typeMatch 校验规则 */
2210
2224
  declare const clearTypeMatchRules: () => void;
2211
- declare const validateTypeMatch: (value: any, mForm: schema_d_exports.FormState | undefined, props: any, message?: string) => string | undefined;
2225
+ /**
2226
+ * 校验取值与字段 type 是否匹配;通过返回 undefined,否则返回错误文案。
2227
+ *
2228
+ * 命中的自定义规则是异步校验器时返回 Promise,内置规则始终同步返回。
2229
+ */
2230
+ declare const validateTypeMatch: (value: any, mForm: schema_d_exports.FormState | undefined, props: any, message?: string) => string | undefined | Promise<string | undefined>;
2212
2231
  //#endregion
2213
2232
  //#region temp/packages/form/src/plugin.d.ts
2214
2233
  interface FormInstallOptions {