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>

属性事件插槽简介 ​

基础用法 ​

主题(theme 默认值:default) ​

切换主题查看表单及其 schema 控件的联动样式,单项 attrs.theme 可覆盖表单主题。基础用法:通过顶部选项切换主题。theme 可选值 default / chenghua / shijingshan / sybz / gulou,默认值 default。

标签主副文本 subLabel(默认值为空) ​

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函数 ​

事件 ​

s-form 的根节点是 Element Plus 的 el-form(原生 <form>),组件内部已默认阻止原生 submit 的页面跳转,避免表单内输入框回车触发整页刷新(例如在 s-dialog 中新建数据时弹窗被刷新关闭)。内部只调用 event.preventDefault(),不阻止事件冒泡,通过 @submit 传入的监听仍会正常触发。

通常用法 ​

远程返回的表单数据 ​

自定义指令 ​

提交前 trim(默认值 true) ​

标签提示 tooltip(默认值为空) ​

属性 ​

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

fieldList 内部属性 ​

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

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

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-
subLabellabel 下方的副文本,支持函数动态返回string / Function-
prop值对应路径,支持 a.b、a.0.b、a[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内容插槽名;未设置时使用 prop,type="title" 时也作为标题插槽名string-
labelSlotNamelabel 插槽名,未设置时使用 ${prop}-labelstring-
labelRender自定义标签渲染,可与 tooltip 同时使用,优先于标签插槽Function-
tooltip标签右侧提示图标的悬浮内容,支持函数动态返回,可与 labelRender 或标签插槽同时使用string / Function-
tooltipAttrs提示图标及 Tooltip 属性,支持 dangerouslyUseHTMLStringobject / Function-
copy是否显示 tooltip 复制图标;传字符串时复制指定文本boolean / stringtrue
type项类型;设置为 title 时渲染分组标题,attrs 会透传给 s-titlestring-
trim提交/校验前是否对当前字段去除前后空格,可覆盖表单级配置boolean跟随 s-form.trim

表单上下文 ​

attrs、formItemAttrs、rules、isShow、render 和插槽都会拿到同一份上下文:{ 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 | --> |

思云博智私有前端组件库