2025-10-22 14:43:42 +08:00
|
|
|
|
import type { ColProps } from "antd/es/col";
|
|
|
|
|
|
import type { FormItemProps, Rule } from "antd/es/form";
|
2025-12-02 17:43:11 +08:00
|
|
|
|
import type { FormListFieldData } from "antd/es/form/FormList";
|
|
|
|
|
|
import type { Gutter } from "antd/es/grid/row";
|
2026-04-14 17:56:20 +08:00
|
|
|
|
import type { Store } from "rc-field-form/lib/interface";
|
2025-10-22 14:43:42 +08:00
|
|
|
|
import type { FC, ReactNode } from "react";
|
2025-12-18 16:29:53 +08:00
|
|
|
|
import type { FORM_ITEM_RENDER_TYPE_MAP } from "../../enum/formItemRender";
|
2025-10-22 14:43:42 +08:00
|
|
|
|
|
2026-03-06 10:30:42 +08:00
|
|
|
|
/** 表单项 name */
|
|
|
|
|
|
export type FormFieldName = string | number | (string | number)[];
|
|
|
|
|
|
|
2025-10-22 14:43:42 +08:00
|
|
|
|
/**
|
2026-03-06 10:30:42 +08:00
|
|
|
|
* 选择项数据类型
|
2025-10-22 14:43:42 +08:00
|
|
|
|
*/
|
|
|
|
|
|
export interface OptionItem {
|
|
|
|
|
|
/** ID字段 */
|
2025-11-04 16:20:00 +08:00
|
|
|
|
bianma?: any;
|
2025-10-22 14:43:42 +08:00
|
|
|
|
/** 名称字段 */
|
2026-01-22 17:20:11 +08:00
|
|
|
|
name?: string | ReactNode;
|
2026-01-19 14:43:16 +08:00
|
|
|
|
/** 是否禁用 */
|
|
|
|
|
|
disabled?: boolean;
|
2025-10-22 14:43:42 +08:00
|
|
|
|
[key: string]: any;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 字段键配置
|
|
|
|
|
|
*/
|
2026-03-06 10:30:42 +08:00
|
|
|
|
export interface ItemsFieldConfig {
|
2025-12-24 15:45:07 +08:00
|
|
|
|
/** 值字段的键名,默认 'bianma' */
|
2025-10-22 14:43:42 +08:00
|
|
|
|
valueKey?: string;
|
2025-12-24 15:45:07 +08:00
|
|
|
|
/** 标签字段的键名,默认 'name' */
|
2025-12-16 17:17:22 +08:00
|
|
|
|
labelKey?: string | ((item: Record<string, any>) => ReactNode);
|
2025-10-22 14:43:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-20 13:50:40 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* Form.List 操作项
|
|
|
|
|
|
*/
|
|
|
|
|
|
export interface FormListOperations {
|
|
|
|
|
|
/** 当前表单项的数据字段信息 */
|
|
|
|
|
|
field: FormListFieldData;
|
|
|
|
|
|
/** 当前项在列表中的索引位置 */
|
2025-12-24 14:27:07 +08:00
|
|
|
|
fieldIndex: number;
|
2025-12-20 13:50:40 +08:00
|
|
|
|
/** 新增方法 */
|
2026-04-14 17:56:20 +08:00
|
|
|
|
add: (defaultValue?: Store, insertIndex?: number) => void;
|
2025-12-20 13:50:40 +08:00
|
|
|
|
/** 删除方法 */
|
|
|
|
|
|
remove: (index: number | number[]) => void;
|
|
|
|
|
|
/** 移动方法 */
|
|
|
|
|
|
move: (from: number, to: number) => void;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-02 17:43:11 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* Form.List 独有的属性
|
|
|
|
|
|
*/
|
|
|
|
|
|
export interface FormListUniqueProps {
|
|
|
|
|
|
/** 是否显示新增按钮,默认 true */
|
|
|
|
|
|
showAddButton?: boolean;
|
|
|
|
|
|
/** 是否显示删除按钮,默认 true */
|
|
|
|
|
|
showRemoveButton?: boolean;
|
|
|
|
|
|
/** 新增按钮的文本,默认 '添加' */
|
|
|
|
|
|
addButtonText?: string;
|
|
|
|
|
|
/** 删除按钮的文本,默认 '删除' */
|
|
|
|
|
|
removeButtonText?: string;
|
|
|
|
|
|
/** 表单配置项 */
|
2025-12-24 14:27:07 +08:00
|
|
|
|
options: (field: FormListFieldData, fieldIndex: number, operations: FormListOperations) => FormOption[];
|
2025-12-02 17:43:11 +08:00
|
|
|
|
/** 点击新增按钮时的默认值 */
|
2026-04-14 17:56:20 +08:00
|
|
|
|
addDefaultValue?: Store;
|
2025-12-02 17:43:11 +08:00
|
|
|
|
/** 点击新增按钮时插入的索引位置 */
|
|
|
|
|
|
addInsertIndex?: number;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-14 17:56:20 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 条件类型辅助工具
|
|
|
|
|
|
*/
|
|
|
|
|
|
type WhenTrue<Condition extends boolean, T> = Condition extends true ? never : T;
|
|
|
|
|
|
|
2026-04-14 17:20:05 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 表单配置项属性类型辅助工具
|
|
|
|
|
|
*/
|
2026-04-14 17:56:20 +08:00
|
|
|
|
type FormOptionProperty<O extends boolean, C extends boolean, T> = WhenTrue<O, WhenTrue<C, T>>;
|
2026-04-14 17:20:05 +08:00
|
|
|
|
|
2025-10-22 14:43:42 +08:00
|
|
|
|
/**
|
2026-03-06 10:30:42 +08:00
|
|
|
|
* 表单配置项公共字段
|
2025-10-22 14:43:42 +08:00
|
|
|
|
*/
|
2026-04-14 17:20:05 +08:00
|
|
|
|
export interface FormOptionBase<O extends boolean = false, C extends boolean = false> {
|
2025-11-05 14:42:39 +08:00
|
|
|
|
/** React 需要的 key,如果传递了唯一的 name,则不需要 */
|
|
|
|
|
|
key?: string;
|
2025-10-22 14:43:42 +08:00
|
|
|
|
/** 表单项字段名 */
|
2026-03-06 10:30:42 +08:00
|
|
|
|
name?: FormFieldName;
|
2025-10-22 14:43:42 +08:00
|
|
|
|
/** 表单项标签 */
|
|
|
|
|
|
label?: ReactNode;
|
2025-12-12 15:53:38 +08:00
|
|
|
|
/** 占据栅格列数,默认 12 */
|
2026-04-14 17:56:20 +08:00
|
|
|
|
span?: WhenTrue<O, number | string>;
|
2025-10-22 14:43:42 +08:00
|
|
|
|
/** 是否必填,默认 true,支持函数动态计算 */
|
2026-04-14 17:56:20 +08:00
|
|
|
|
required?: FormOptionProperty<O, C, boolean | ((formValues: Store) => boolean)>;
|
2025-10-22 14:43:42 +08:00
|
|
|
|
/** 验证规则 */
|
2026-04-14 17:20:05 +08:00
|
|
|
|
rules?: FormOptionProperty<O, C, Rule | Rule[]>;
|
2025-12-15 15:26:56 +08:00
|
|
|
|
/** 是否使用字符验证限制 */
|
2026-04-14 17:20:05 +08:00
|
|
|
|
useConstraints?: FormOptionProperty<O, C, boolean>;
|
2025-10-22 14:43:42 +08:00
|
|
|
|
/** 占位符文本,默认会根据传入的 render 类型自动判断(请选择、请输入)和 label 组合 */
|
2026-04-14 17:20:05 +08:00
|
|
|
|
placeholder?: FormOptionProperty<O, C, ReactNode>;
|
2025-10-22 14:43:42 +08:00
|
|
|
|
/** 提示信息,传入将在 label 右侧生成图标展示 tooltip */
|
2026-04-14 17:20:05 +08:00
|
|
|
|
tip?: FormOptionProperty<O, C, ReactNode>;
|
2025-10-22 14:43:42 +08:00
|
|
|
|
/** 是否隐藏,默认 false,支持函数动态计算 */
|
2026-04-14 17:56:20 +08:00
|
|
|
|
hidden?: WhenTrue<O, boolean | ((formValues: Store) => boolean)>;
|
2025-11-11 09:10:08 +08:00
|
|
|
|
/** 是否自定义渲染,完全交给外部控制渲染,默认 false */
|
2026-04-14 17:20:05 +08:00
|
|
|
|
customizeRender?: C;
|
2025-10-22 14:43:42 +08:00
|
|
|
|
/** 传递给 Form.Item 的属性,支持函数动态计算 */
|
2026-04-14 17:56:20 +08:00
|
|
|
|
formItemProps?: FormOptionProperty<O, C, FormItemProps | ((formValues: Store) => FormItemProps)>;
|
2025-10-28 12:30:57 +08:00
|
|
|
|
/** label 栅格配置,默认直接使用外层的 labelCol,如果 span 等于 24,是外层的 labelCol.span 一半 */
|
2026-04-14 17:20:05 +08:00
|
|
|
|
labelCol?: FormOptionProperty<O, C, ColProps>;
|
2025-12-24 15:45:07 +08:00
|
|
|
|
/** wrapper 栅格配置,默认 24 - labelCol.span */
|
2026-04-14 17:20:05 +08:00
|
|
|
|
wrapperCol?: FormOptionProperty<O, C, ColProps>;
|
2025-10-22 14:43:42 +08:00
|
|
|
|
/** 是否应该更新(用于表单联动) */
|
2026-04-14 17:56:20 +08:00
|
|
|
|
shouldUpdate?: FormOptionProperty<O, C, boolean | ((prevValues: Store, nextValues: Store, info: { source?: string }) => boolean)>;
|
2025-10-22 14:43:42 +08:00
|
|
|
|
/** 依赖字段(用于表单联动) */
|
2026-04-14 17:20:05 +08:00
|
|
|
|
dependencies?: FormOptionProperty<O, C, FormFieldName[]>;
|
2025-11-10 16:39:38 +08:00
|
|
|
|
/** 是否仅用于保存标签,不渲染到页面上,只在表单中保存数据,默认 false */
|
2026-04-14 17:20:05 +08:00
|
|
|
|
onlyForLabel?: O;
|
2025-10-22 14:43:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-06 10:30:42 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 按 render 类型区分的表单项
|
|
|
|
|
|
*/
|
2026-04-14 17:56:20 +08:00
|
|
|
|
export type FormOptionByRender<R extends keyof FORM_ITEM_RENDER_TYPE_MAP, O extends boolean = false, C extends boolean = false> = FormOptionBase<O, C> & {
|
2026-03-06 10:30:42 +08:00
|
|
|
|
/** 渲染类型(写字面量时 componentProps 会按该类型推导) */
|
2026-04-14 17:56:20 +08:00
|
|
|
|
render: R;
|
2026-03-06 10:30:42 +08:00
|
|
|
|
/** 传递给表单控件的属性,类型由 render 决定 */
|
2026-04-14 17:56:20 +08:00
|
|
|
|
componentProps?: FormOptionProperty<O, C, FORM_ITEM_RENDER_TYPE_MAP[R] | ((formValues: Store) => FORM_ITEM_RENDER_TYPE_MAP[R])>;
|
2026-04-14 17:20:05 +08:00
|
|
|
|
/** 选项数据(用于 select、radio、checkbox) */
|
2026-04-14 17:56:20 +08:00
|
|
|
|
items?: FormOptionProperty<O, C, R extends "select" | "radio" | "checkbox" ? OptionItem[] : never>;
|
2026-04-14 17:20:05 +08:00
|
|
|
|
/** 字段键配置 */
|
2026-04-14 17:56:20 +08:00
|
|
|
|
itemsField?: FormOptionProperty<O, C, R extends "select" | "radio" | "checkbox" ? ItemsFieldConfig : never>;
|
2026-04-14 17:20:05 +08:00
|
|
|
|
/** checkbox 的栅格数量,如果不传入不使用栅格,传入才使用 */
|
2026-04-14 17:56:20 +08:00
|
|
|
|
checkboxCol?: FormOptionProperty<O, C, R extends "checkbox" ? number : never>;
|
2026-04-14 17:20:05 +08:00
|
|
|
|
/** Form.List 独有的属性 */
|
2026-04-14 17:56:20 +08:00
|
|
|
|
formListUniqueProps?: FormOptionProperty<O, C, R extends "formList" ? FormListUniqueProps | ((formValues: Store) => FormListUniqueProps) : never>;
|
2026-03-06 10:30:42 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 不写 render 或 render 为 input 时的表单项(默认按输入框)
|
|
|
|
|
|
*/
|
2026-04-14 17:20:05 +08:00
|
|
|
|
export type FormOptionDefault<O extends boolean = false, C extends boolean = false> = FormOptionBase<O, C> & {
|
|
|
|
|
|
/** 渲染类型,默认 input */
|
2026-03-06 10:30:42 +08:00
|
|
|
|
render?: "input" | undefined;
|
|
|
|
|
|
/** 传递给 Input 的属性 */
|
2026-04-14 17:56:20 +08:00
|
|
|
|
componentProps?: FORM_ITEM_RENDER_TYPE_MAP["input"] | ((formValues: Store) => FORM_ITEM_RENDER_TYPE_MAP["input"]);
|
2026-04-14 17:20:05 +08:00
|
|
|
|
/** 选项数据(用于 select、radio、checkbox),input 时不需要 */
|
|
|
|
|
|
items?: never;
|
|
|
|
|
|
/** 字段键配置,input 时不需要 */
|
|
|
|
|
|
itemsField?: never;
|
|
|
|
|
|
/** checkbox 的栅格数量,input 时不需要 */
|
|
|
|
|
|
checkboxCol?: never;
|
|
|
|
|
|
/** Form.List 独有的属性,input 时不需要 */
|
|
|
|
|
|
formListUniqueProps?: never;
|
2026-03-06 10:30:42 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 自定义渲染时的表单项(render 为 ReactNode 时使用)
|
|
|
|
|
|
*/
|
2026-04-14 17:20:05 +08:00
|
|
|
|
export type FormOptionCustomRender<O extends boolean = false, C extends boolean = false> = FormOptionBase<O, C> & {
|
|
|
|
|
|
/** 渲染类型,默认 ReactNode */
|
|
|
|
|
|
render: ReactNode;
|
|
|
|
|
|
/** 传递给表单控件的属性,自定义渲染时不需要 */
|
2026-04-14 17:56:20 +08:00
|
|
|
|
componentProps?: never;
|
2026-04-14 17:20:05 +08:00
|
|
|
|
/** 选项数据(用于 select、radio、checkbox),自定义渲染时不需要 */
|
|
|
|
|
|
items?: never;
|
|
|
|
|
|
/** 字段键配置,自定义渲染时不需要 */
|
|
|
|
|
|
itemsField?: never;
|
|
|
|
|
|
/** checkbox 的栅格数量,自定义渲染时不需要 */
|
|
|
|
|
|
checkboxCol?: never;
|
|
|
|
|
|
/** Form.List 独有的属性,自定义渲染时不需要 */
|
|
|
|
|
|
formListUniqueProps?: never;
|
2026-03-06 10:30:42 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 表单配置项
|
|
|
|
|
|
*/
|
|
|
|
|
|
export type FormOption
|
2026-04-15 17:58:01 +08:00
|
|
|
|
= { [K in keyof FORM_ITEM_RENDER_TYPE_MAP]: FormOptionByRender<K, false, false> }[keyof FORM_ITEM_RENDER_TYPE_MAP]
|
|
|
|
|
|
;
|
2026-03-06 10:30:42 +08:00
|
|
|
|
|
2025-10-22 14:43:42 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* FormItemsRenderer 组件属性
|
|
|
|
|
|
*/
|
|
|
|
|
|
export interface FormItemsRendererProps {
|
|
|
|
|
|
/** 表单配置项数组 */
|
|
|
|
|
|
options: FormOption[];
|
|
|
|
|
|
/** 默认栅格占据列数,默认 12 */
|
|
|
|
|
|
span?: number;
|
|
|
|
|
|
/** 是否折叠(仅显示前3项),默认 false */
|
|
|
|
|
|
collapse?: boolean;
|
|
|
|
|
|
/** 自动生成必填规则,默认 true */
|
|
|
|
|
|
useAutoGenerateRequired?: boolean;
|
2025-10-29 09:38:36 +08:00
|
|
|
|
/** 初始值,用于在表单未初始化时提供默认值 */
|
2026-04-14 17:56:20 +08:00
|
|
|
|
initialValues?: Store;
|
2025-12-02 17:43:11 +08:00
|
|
|
|
/** 栅格间距,继承自 FormBuilder */
|
|
|
|
|
|
gutter?: Gutter | [Gutter, Gutter];
|
|
|
|
|
|
/** label 栅格配置,继承自 FormBuilder */
|
|
|
|
|
|
labelCol?: ColProps;
|
2025-10-22 14:43:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 表单项渲染器组件
|
|
|
|
|
|
*/
|
|
|
|
|
|
declare const FormItemsRenderer: FC<FormItemsRendererProps>;
|
|
|
|
|
|
|
|
|
|
|
|
export default FormItemsRenderer;
|