Jelajahi Sumber

fix: 函数式 componentProps 按已注册 component 的 Props 校验返回值

dullathanol 2 bulan lalu
induk
melakukan
5ed39c03a4

+ 17 - 8
apps/web-antd/src/adapter/form-schema.ts

@@ -32,19 +32,28 @@ import type {
 
 import type { ComponentType } from './component';
 
+type ComponentPropsFnArgs = Parameters<
+  Extract<
+    NonNullable<CoreFormSchema<ComponentType>['componentProps']>,
+    (...args: any) => any
+  >
+>;
+
 /**
- * 对象形式:使用适配器里为各 `component` 声明的 Props 类型 `P`;
- * 与 `Record<string, any>` 相交是为了满足核心库 `MaybeComponentProps` 的索引签名。
- * 函数形式:通过联合 `CoreFormSchema['componentProps']`,与表单核心对动态 `componentProps` 的约定保持一致。
+ * 使用适配器里为各 `component` 声明的 Props 类型 `P`;
+ * 与 `Record<string, any>` 相交以兼容核心库 `MaybeComponentProps` 的索引签名。
  */
-type SchemaComponentProps<P> =
-  | CoreFormSchema<ComponentType>['componentProps']
+type ComponentProps<P> =
+  | ((
+      value: ComponentPropsFnArgs[0],
+      actions: ComponentPropsFnArgs[1],
+    ) => P & Record<string, any>)
   | (P & Record<string, any>);
 
 /**
  * 与 {@link ComponentType} 中注册的组件名一一对应,便于 Schema 上 `component` + `componentProps` 联动提示
  */
-interface FormSchemaComponentPropsMap {
+interface ComponentPropsMap {
   ApiCascader: ApiComponentSharedProps & CascaderProps;
   ApiSelect: ApiComponentSharedProps & SelectProps;
   ApiTreeSelect: ApiComponentSharedProps & TreeSelectProps;
@@ -79,12 +88,12 @@ type BaseSchema = Omit<
   'component' | 'componentProps'
 >;
 
-type RegisteredName = keyof FormSchemaComponentPropsMap;
+type RegisteredName = keyof ComponentPropsMap;
 
 type DiscriminatedFormSchema = {
   [K in RegisteredName]: BaseSchema & {
     component: K;
-    componentProps?: SchemaComponentProps<FormSchemaComponentPropsMap[K]>;
+    componentProps?: ComponentProps<ComponentPropsMap[K]>;
   };
 }[RegisteredName];
 

+ 17 - 8
apps/web-antdv-next/src/adapter/form-schema.ts

@@ -32,19 +32,28 @@ import type {
 
 import type { ComponentType } from './component';
 
+type ComponentPropsFnArgs = Parameters<
+  Extract<
+    NonNullable<CoreFormSchema<ComponentType>['componentProps']>,
+    (...args: any) => any
+  >
+>;
+
 /**
- * 对象形式:使用适配器里为各 `component` 声明的 Props 类型 `P`;
- * 与 `Record<string, any>` 相交是为了满足核心库 `MaybeComponentProps` 的索引签名。
- * 函数形式:通过联合 `CoreFormSchema['componentProps']`,与表单核心对动态 `componentProps` 的约定保持一致。
+ * 使用适配器里为各 `component` 声明的 Props 类型 `P`;
+ * 与 `Record<string, any>` 相交以兼容核心库 `MaybeComponentProps` 的索引签名。
  */
-type SchemaComponentProps<P> =
-  | CoreFormSchema<ComponentType>['componentProps']
+type ComponentProps<P> =
+  | ((
+      value: ComponentPropsFnArgs[0],
+      actions: ComponentPropsFnArgs[1],
+    ) => P & Record<string, any>)
   | (P & Record<string, any>);
 
 /**
  * 与 {@link ComponentType} 中注册的组件名一一对应,便于 Schema 上 `component` + `componentProps` 联动提示
  */
-interface FormSchemaComponentPropsMap {
+interface ComponentPropsMap {
   ApiCascader: ApiComponentSharedProps & CascaderProps;
   ApiSelect: ApiComponentSharedProps & SelectProps;
   ApiTreeSelect: ApiComponentSharedProps & TreeSelectProps;
@@ -79,12 +88,12 @@ type BaseSchema = Omit<
   'component' | 'componentProps'
 >;
 
-type RegisteredName = keyof FormSchemaComponentPropsMap;
+type RegisteredName = keyof ComponentPropsMap;
 
 type DiscriminatedFormSchema = {
   [K in RegisteredName]: BaseSchema & {
     component: K;
-    componentProps?: SchemaComponentProps<FormSchemaComponentPropsMap[K]>;
+    componentProps?: ComponentProps<ComponentPropsMap[K]>;
   };
 }[RegisteredName];
 

+ 17 - 8
apps/web-ele/src/adapter/form-schema.ts

@@ -27,19 +27,28 @@ import type { ComponentType } from './component';
 type ElTreeSelectSchemaProps = InstanceType<typeof ElTreeSelect>['$props'];
 type ElTimePickerSchemaProps = InstanceType<typeof ElTimePicker>['$props'];
 
+type ComponentPropsFnArgs = Parameters<
+  Extract<
+    NonNullable<CoreFormSchema<ComponentType>['componentProps']>,
+    (...args: any) => any
+  >
+>;
+
 /**
- * 对象形式:使用适配器里为各 `component` 声明的 Props 类型 `P`;
- * 与 `Record<string, any>` 相交是为了满足核心库 `MaybeComponentProps` 的索引签名。
- * 函数形式:通过联合 `CoreFormSchema['componentProps']`,与表单核心对动态 `componentProps` 的约定保持一致。
+ * 使用适配器里为各 `component` 声明的 Props 类型 `P`;
+ * 与 `Record<string, any>` 相交以兼容核心库 `MaybeComponentProps` 的索引签名。
  */
-type SchemaComponentProps<P> =
-  | CoreFormSchema<ComponentType>['componentProps']
+type ComponentProps<P> =
+  | ((
+      value: ComponentPropsFnArgs[0],
+      actions: ComponentPropsFnArgs[1],
+    ) => P & Record<string, any>)
   | (P & Record<string, any>);
 
 /**
  * 与 {@link ComponentType} 中注册的组件名一一对应,便于 Schema 上 `component` + `componentProps` 联动提示
  */
-interface FormSchemaComponentPropsMap {
+interface ComponentPropsMap {
   ApiSelect: ApiComponentSharedProps & SelectV2Props;
   ApiTreeSelect: ApiComponentSharedProps & ElTreeSelectSchemaProps;
   Checkbox: CheckboxProps;
@@ -63,12 +72,12 @@ type BaseSchema = Omit<
   'component' | 'componentProps'
 >;
 
-type RegisteredName = keyof FormSchemaComponentPropsMap;
+type RegisteredName = keyof ComponentPropsMap;
 
 type DiscriminatedFormSchema = {
   [K in RegisteredName]: BaseSchema & {
     component: K;
-    componentProps?: SchemaComponentProps<FormSchemaComponentPropsMap[K]>;
+    componentProps?: ComponentProps<ComponentPropsMap[K]>;
   };
 }[RegisteredName];
 

+ 17 - 8
apps/web-naive/src/adapter/form-schema.ts

@@ -24,19 +24,28 @@ import type {
 
 import type { ComponentType } from './component';
 
+type ComponentPropsFnArgs = Parameters<
+  Extract<
+    NonNullable<CoreFormSchema<ComponentType>['componentProps']>,
+    (...args: any) => any
+  >
+>;
+
 /**
- * 对象形式:使用适配器里为各 `component` 声明的 Props 类型 `P`;
- * 与 `Record<string, any>` 相交是为了满足核心库 `MaybeComponentProps` 的索引签名。
- * 函数形式:通过联合 `CoreFormSchema['componentProps']`,与表单核心对动态 `componentProps` 的约定保持一致。
+ * 使用适配器里为各 `component` 声明的 Props 类型 `P`;
+ * 与 `Record<string, any>` 相交以兼容核心库 `MaybeComponentProps` 的索引签名。
  */
-type SchemaComponentProps<P> =
-  | CoreFormSchema<ComponentType>['componentProps']
+type ComponentProps<P> =
+  | ((
+      value: ComponentPropsFnArgs[0],
+      actions: ComponentPropsFnArgs[1],
+    ) => P & Record<string, any>)
   | (P & Record<string, any>);
 
 /**
  * 与 {@link ComponentType} 中注册的组件名一一对应,便于 Schema 上 `component` + `componentProps` 联动提示
  */
-interface FormSchemaComponentPropsMap {
+interface ComponentPropsMap {
   ApiSelect: ApiComponentSharedProps & SelectProps;
   ApiTreeSelect: ApiComponentSharedProps & TreeSelectProps;
   Checkbox: CheckboxProps;
@@ -60,12 +69,12 @@ type BaseSchema = Omit<
   'component' | 'componentProps'
 >;
 
-type RegisteredName = keyof FormSchemaComponentPropsMap;
+type RegisteredName = keyof ComponentPropsMap;
 
 type DiscriminatedFormSchema = {
   [K in RegisteredName]: BaseSchema & {
     component: K;
-    componentProps?: SchemaComponentProps<FormSchemaComponentPropsMap[K]>;
+    componentProps?: ComponentProps<ComponentPropsMap[K]>;
   };
 }[RegisteredName];
 

+ 17 - 8
apps/web-tdesign/src/adapter/form-schema.ts

@@ -30,19 +30,28 @@ import type {
 
 import type { ComponentType } from './component';
 
+type ComponentPropsFnArgs = Parameters<
+  Extract<
+    NonNullable<CoreFormSchema<ComponentType>['componentProps']>,
+    (...args: any) => any
+  >
+>;
+
 /**
- * 对象形式:使用适配器里为各 `component` 声明的 Props 类型 `P`;
- * 与 `Record<string, any>` 相交是为了满足核心库 `MaybeComponentProps` 的索引签名。
- * 函数形式:通过联合 `CoreFormSchema['componentProps']`,与表单核心对动态 `componentProps` 的约定保持一致。
+ * 使用适配器里为各 `component` 声明的 Props 类型 `P`;
+ * 与 `Record<string, any>` 相交以兼容核心库 `MaybeComponentProps` 的索引签名。
  */
-type SchemaComponentProps<P> =
-  | CoreFormSchema<ComponentType>['componentProps']
+type ComponentProps<P> =
+  | ((
+      value: ComponentPropsFnArgs[0],
+      actions: ComponentPropsFnArgs[1],
+    ) => P & Record<string, any>)
   | (P & Record<string, any>);
 
 /**
  * 与 {@link ComponentType} 中注册的组件名一一对应,便于 Schema 上 `component` + `componentProps` 联动提示
  */
-interface FormSchemaComponentPropsMap {
+interface ComponentPropsMap {
   ApiSelect: ApiComponentSharedProps & SelectProps;
   ApiTreeSelect: ApiComponentSharedProps & TreeSelectProps;
   AutoComplete: AutoCompleteProps;
@@ -73,12 +82,12 @@ type BaseSchema = Omit<
   'component' | 'componentProps'
 >;
 
-type RegisteredName = keyof FormSchemaComponentPropsMap;
+type RegisteredName = keyof ComponentPropsMap;
 
 type DiscriminatedFormSchema = {
   [K in RegisteredName]: BaseSchema & {
     component: K;
-    componentProps?: SchemaComponentProps<FormSchemaComponentPropsMap[K]>;
+    componentProps?: ComponentProps<ComponentPropsMap[K]>;
   };
 }[RegisteredName];
 

+ 17 - 8
playground/src/adapter/form-schema.ts

@@ -32,19 +32,28 @@ import type {
 
 import type { ComponentType } from './component';
 
+type ComponentPropsFnArgs = Parameters<
+  Extract<
+    NonNullable<CoreFormSchema<ComponentType>['componentProps']>,
+    (...args: any) => any
+  >
+>;
+
 /**
- * 对象形式:使用适配器里为各 `component` 声明的 Props 类型 `P`;
- * 与 `Record<string, any>` 相交是为了满足核心库 `MaybeComponentProps` 的索引签名。
- * 函数形式:通过联合 `CoreFormSchema['componentProps']`,与表单核心对动态 `componentProps` 的约定保持一致。
+ * 使用适配器里为各 `component` 声明的 Props 类型 `P`;
+ * 与 `Record<string, any>` 相交以兼容核心库 `MaybeComponentProps` 的索引签名。
  */
-type SchemaComponentProps<P> =
-  | CoreFormSchema<ComponentType>['componentProps']
+type ComponentProps<P> =
+  | ((
+      value: ComponentPropsFnArgs[0],
+      actions: ComponentPropsFnArgs[1],
+    ) => P & Record<string, any>)
   | (P & Record<string, any>);
 
 /**
  * 与 {@link ComponentType} 中注册的组件名一一对应,便于 Schema 上 `component` + `componentProps` 联动提示
  */
-interface FormSchemaComponentPropsMap {
+interface ComponentPropsMap {
   ApiCascader: ApiComponentSharedProps & CascaderProps;
   ApiSelect: ApiComponentSharedProps & SelectProps;
   ApiTreeSelect: ApiComponentSharedProps & TreeSelectProps;
@@ -79,12 +88,12 @@ type BaseSchema = Omit<
   'component' | 'componentProps'
 >;
 
-type RegisteredName = keyof FormSchemaComponentPropsMap;
+type RegisteredName = keyof ComponentPropsMap;
 
 type DiscriminatedFormSchema = {
   [K in RegisteredName]: BaseSchema & {
     component: K;
-    componentProps?: SchemaComponentProps<FormSchemaComponentPropsMap[K]>;
+    componentProps?: ComponentProps<ComponentPropsMap[K]>;
   };
 }[RegisteredName];