Skip to content
On this page

form表单组件

https://element-plus.org/zh-CN/component/form.html

Hidden Title

基础用法:<s-form :model="model" :fieldList="fieldList" :rules="rules" ref="gFormRef"></s-form>

属性事件插槽简介

基础用法

成华主题

石景山主题

gap 左右间隙,默认值 16px

高级 schema 用法

分组标题 type="title"

表单校验规则

动态显隐 isShow

多个属性

添加/删除表单项

每行展示多少项

插槽

插槽名说明插槽参数
prop / slotName内容插槽;useSlot: true 时启用,未设置 slotName 时使用当前 propSFormContext
${prop}-label / labelSlotNamelabel 插槽;未设置 labelSlotName 时使用 ${prop}-labelSFormContext
slotName分组标题插槽;type="title"useSlot: true 时启用SFormContext

render函数

事件

通常用法

远程返回的表单数据

自定义指令

属性

属性名说明类型默认值
fieldList表单 schema 配置array / object-
model表单数据对象object-
theme主题,传递给 s-* 控件和分组标题default / chenghua / shijingshandefault
column默认每行展示几项1 / 2 / 3 / 4 / 5 / 61
gap多列表单项左右间距,仅 column > 1 时生效string / number16px
align表单项垂直对齐center / top / flex-endtop
showFooter是否显示调试按钮boolean开发环境为 true
footershowFooter 的别名,设置后优先级更高boolean-
autoSetDefaultValue是否自动写入字段默认值booleantrue
componentDefaults所有控件的默认透传属性object{ clearable: true, filterable: true, width: '100%' }

fieldList 内部属性

fieldLists-form 的表单 schema 配置。每一项只保留表单编排需要的字段;控件属性统一放到 attrs,表单项属性统一放到 formItemAttrs。例如 type="title" 时,s-titletitlesubTitlethemetype 等属性都写到 attrs 里。

TypeScript 项目中建议从 sybz-components/types 引入表单项类型,让动态函数里的 modelvaluesetFieldValue 等上下文有明确提示:

ts
import type { SFormFieldItem } from 'sybz-components/types'

const fieldList: SFormFieldItem[] = [
  { label: '账号', prop: 'account', required: true },
  { label: '角色', prop: 'role', comp: 's-select', attrs: { options: roleOptions } },
]
import type { SFormFieldItem } from 'sybz-components/types'

const fieldList: SFormFieldItem[] = [
  { label: '账号', prop: 'account', required: true },
  { label: '角色', prop: 'role', comp: 's-select', attrs: { options: roleOptions } },
]
属性名说明类型默认值
label左侧显示的 label 值string-
prop值对应路径,支持 a.ba.0.ba[0].bstring-
comp要渲染的组件名称或组件对象string / Components-input
attrs透传给表单控件;type="title" 时透传给 s-title,标题文案也写在这里object / Function-
formItemAttrs透传给 el-form-itemobject / Function-
rules当前项校验规则,函数参数为表单上下文object / array / Function-
required快速生成必填规则;传字符串时作为错误提示boolean / string-
defaultValue默认值,model 对应路径不存在时写入any-
render自定义内容渲染,参数为表单上下文Function-
isShow是否展示当前项,函数参数为表单上下文boolean / Functiontrue
useSlot使用插槽渲染booleanfalse
slotName内容插槽名;未设置时使用 proptype="title" 时也作为标题插槽名string-
labelSlotNamelabel 插槽名,未设置时使用 ${prop}-labelstring-
type项类型;设置为 title 时渲染分组标题,attrs 会透传给 s-titlestring-

表单上下文

attrsformItemAttrsrulesisShowrender 和插槽都会拿到同一份上下文:{ option, model, value, prop, index, formRef, getValue, setValue, setFieldValue }。其中 option 是当前 fieldList 配置项,model 是完整表单数据。

Form Exposes

| 名称 | 说明 | 类型 | | :----------------: | ------------------------------------ | --------------------------------------------------------- | --- | | validate | 校验 form 表单,成功后返回当前 model | Function(isResetFieldsOrParams = false, otherParams = {}) | | validateField | 校验指定字段 | Function | | resetFields | 重置 form 表单 | Function | | clearValidate | 清除校验 | Function(props?: string / string[]) | | scrollToField | 滚动到指定字段 | Function(prop: string) | | submit | 调用 validate | Function | | getModel | 获取当前 model | Function | | getValue | 获取指定路径的值 | Function(prop: string) | | setValue | 设置指定路径的值 | Function(prop: string, value: any) | | getFields | 获取全部字段配置 | Function | | getField | 获取指定字段配置 | Function(prop: string) | | getVisibleFields | 获取当前可见字段配置 | Function | | formRef / sFormRef | 获取内部 el-form 实例 | Ref | --> |

思云博智私有前端组件库