Skip to content

ProForm

基于 antdv-next Form 的高级表单封装,通过 Schema 配置驱动,内置所有常用输入组件,支持响应式属性、Grid 布局、嵌套字段、自定义组件等。

API

useForm

ts
import { useForm } from '@qin-ui/antdv-next-pro';

// 初始化数据 + 字段配置
const form = useForm<DataType>(initData, initFields);

参数

参数类型说明
initDataDeepPartial<D>表单初始数据
initFieldsField<D>[]表单字段初始配置
rootboolean是否为根 form 实例(用于嵌套 form 场景)

返回值

属性类型说明
formRefRef<FormInstance | undefined>antdv-next FormInstance 引用,可调用 validateresetFields 等方法
formDataReactive<D>响应式表单数据对象
fieldsRef<Field<D>[]>响应式字段配置数组
getFormData(path?)(path?) => any读取字段值,不传 path 返回全部数据
setFormData(path, value)-设置字段值,value 支持函数形式 (prev) => next
setField(path, patch)-动态更新指定字段的配置项
resetFormData()-将表单数据重置为初始值

ProForm Props

属性类型默认值说明
formForm<D>-useForm 返回的实例
gridboolean | GridPropsfalse是否启用 Grid 网格布局,传入 GridProps 可定制 gutter、align 等
其余FormProps-透传至 antdv-next Form(如 layoutlabelColcolon 等)

ProForm Slots

插槽名说明
default表单尾部内容(通常放提交 / 重置按钮)
[path]按字段 path 命名的具名插槽,接管该字段的渲染。插槽 props 包含 modelValueonUpdate:modelValue

字段配置(Field)

所有字段共享 Base 公共属性,并叠加所选组件自身的 Props。

公共属性(Base)

属性类型说明
pathPath<D>字段标识,对应 formData 中的键路径(支持 'a.b.c' 嵌套)
namePath<D>别名,优先于 path 作为 FormItem name(用于与 formData 解耦)
labelstring | Component字段标签,支持传入 Vue 组件
component见内置组件表所用组件名或 Vue 组件实例
hiddenMaybeRef<boolean>是否隐藏,支持 ref / computed
rulesRuleObject[]校验规则(antdv-next FormItem rules)
spannumberGrid 列宽(仅 grid 模式生效,1–24)
xs/sm/md/lg/xl/xxlnumber | { span, offset }响应式列宽(同 antdv-next Col)
fieldsFields<D>嵌套子字段,设置后当前字段表现为分组容器
gridboolean | GridProps子字段的 Grid 配置(仅 fields 不为空时有效)
slotsPartial<ComponentSlots<FormItem>>FormItem 插槽(labelextrahelptooltip
formItemStyleMaybeRef<CSSProperties>FormItem 样式
formItemClassMaybeRef<string>FormItem 类名
formItemContainerRenderComponentTypeFormItem 外层包裹组件(函数式或 Vue 组件)
formItemDataAttrsRecord<string, string>附加到 FormItem DOM 节点的属性(如 data-*aria-*
componentStyleMaybeRef<CSSProperties>组件本身的样式
componentClassMaybeRef<string>组件本身的类名
componentContainerRenderComponentType组件外层包裹组件
componentDataAttrsRecord<string, string>附加到组件 DOM 节点的属性
valueFormatterValueFormatter值转换函数,在 v-model 读写时执行。支持函数 (val, oldVal) => newVal 或对象 { get, set }
modelPropstringv-model 绑定属性名,默认 'value'

响应式支持:除 componentfieldsslotsmodelPropformItemContainercomponentContainervalueFormatter 外,所有属性均支持 RefComputedRef

内置组件(component 取值)

组件额外 Props
inputInput 文本框maxlengthallowClearprefixsuffix...
textareaTextArea 文本域autoSizeshowCount...
input-passwordInputPassword 密码框visibilityToggle...
input-otpInputOTP 一次性密码length...
input-searchInputSearch 搜索框enterButtononSearch...
input-numberInputNumber 数字输入minmaxprecisionstep...
selectSelect 下拉选择optionsmodeallowClear...
auto-completeAutoComplete 自动完成optionsfilterOption...
cascaderCascader 级联选择optionsfieldNames...
date-pickerDatePicker 日期选择pickerformatdisabledDate...
range-pickerRangePicker 日期范围placeholder: [string, string]...
time-pickerTimePicker 时间选择formatminuteStep...
time-range-pickerTimeRangePicker 时间范围同上
checkbox-groupCheckboxGroup 复选框组options
radio-groupRadioGroup 单选框组optionsoptionType...
switchSwitch 开关checkedChildrenunCheckedChildren...
sliderSlider 滑块minmaxsteprange...
tree-selectTreeSelect 树形选择treeDatamultiple...
transferTransfer 穿梭框dataSourcetitles...
custom完全自定义需同时传入 Vue 组件 component: markRaw(MyComp)

高级:TypeScript 类型推导与覆盖

为了在使用 ProForm 的 fields 时能够获得自定义组件的类型推导,或强制覆盖内置组件(如 input)的属性提示,你可以利用 TypeScript 的声明合并来扩展全局的 ComponentMap

在你的业务项目(如 env.d.tscomponents.d.ts)中补充如下内容:

typescript
import 'antdv-next-pro';
// 映入你的自定义组件或覆盖组件
import type MyCustomUpload from '@/components/MyCustomUpload.vue';
import type MySuperInput from '@/components/MySuperInput.vue';

declare module 'antdv-next-pro' {
  // 扩展 ComponentMap
  interface ComponentMap {
    // 1. 新增一个全新的组件 name(如 'custom-upload'),会自动带有 MyCustomUpload 的 Props 提示
    'custom-upload': typeof MyCustomUpload;

    // 2. 蓄意覆盖原生内置组件的类型(比如彻底替换内置 'input' 的类型提示)
    input: typeof MySuperInput;
  }
}

配置完成后:

  1. component 字段会自动提示 'custom-upload'
  2. 当你在 fields 数组中设置 component: 'custom-upload' 时,会自动出现 MyCustomUpload 组件所接受的各项 Props
  3. 当你设置 component: 'input' 时,弹出的将是 MySuperInputProps(原版 AntD Input 的提示会被熔断覆盖)。