FormItem
全新的 FormItem 组件,相比于 Element 的 FormItem,它支持的功能更多,同时它的定位是纯样式组件,不管理表单状态,所以也会更轻量,更方便定制
该组件经过重构,请注意更新后的文档。移除了全部 Antd 内置的而 Element-Plus 没有开发的功能。本组件库主要是 Element-Plus 的 Formily 封装, 不应考虑将设计风格与 Antd 对齐。但是在布局配置项上保留了原本Formily的配置方式。
- 移除了
inset配置项 - 移除了
bordered配置项 - 移除了天然会被Vue继承的属性,如
class、style - 移除了其他Antd中实现的,并非Formily实现的功能
注意
由于该组件的样式与交互基本沿用了 Element-Plus 的 FormItem,因此移除了原来浅层封装的ElFormItem组件。
注意
由于现在formily的decorator的使用不支持插槽,form-item组件中原有通过插槽支持的功能现在只能通过传入VNode实现。
常用属性案例
vue
<script setup lang="ts">
import { createForm } from '@formily/core'
import { createSchemaField, FormProvider } from '@formily/vue'
import {
Cascader,
DatePicker,
FormItem,
FormLayout,
Input,
InputNumber,
Select,
TimePicker,
} from '@silver-formily/element-plus'
import { h } from 'vue'
function Title(props, { slots }) {
return h('p', props, slots.default?.())
}
const options = [
{
value: 'guide',
label: 'Guide',
children: [
{
value: 'disciplines',
label: 'Disciplines',
children: [
{
value: 'consistency',
label: 'Consistency',
},
{
value: 'feedback',
label: 'Feedback',
},
{
value: 'efficiency',
label: 'Efficiency',
},
{
value: 'controllability',
label: 'Controllability',
},
],
},
],
},
{
value: 'component',
label: 'Component',
children: [
{
value: 'basic',
label: 'Basic',
children: [
{
value: 'layout',
label: 'Layout',
},
{
value: 'color',
label: 'Color',
},
{
value: 'typography',
label: 'Typography',
},
],
},
{
value: 'form',
label: 'Form',
children: [
{
value: 'radio',
label: 'Radio',
},
],
},
{
value: 'data',
label: 'Data',
children: [
{
value: 'table',
label: 'Table',
},
{
value: 'tag',
label: 'Tag',
},
{
value: 'progress',
label: 'Progress',
},
],
},
{
value: 'notice',
label: 'Notice',
children: [
{
value: 'alert',
label: 'Alert',
},
],
},
{
value: 'navigation',
label: 'Navigation',
children: [
{
value: 'menu',
label: 'Menu',
},
{
value: 'tabs',
label: 'Tabs',
},
{
value: 'breadcrumb',
label: 'Breadcrumb',
},
{
value: 'dropdown',
label: 'Dropdown',
},
{
value: 'steps',
label: 'Steps',
},
],
},
],
},
]
const { SchemaField, SchemaVoidField, SchemaStringField } = createSchemaField({
components: {
Title,
FormItem,
InputNumber,
Input,
Cascader,
Select,
DatePicker,
FormLayout,
TimePicker,
},
})
const form = createForm()
</script>
<template>
<FormProvider :form="form">
<SchemaField>
<SchemaVoidField x-component="Title" x-content="label为空时的展示: " />
<SchemaStringField
x-decorator="FormItem"
x-component="Input"
:x-decorator-props="{
labelWidth: 300,
}"
/>
<SchemaStringField
title=""
x-decorator="FormItem"
x-component="Input"
:x-decorator-props="{
labelWidth: 300,
}"
/>
<SchemaVoidField x-component="Title" x-content="冒号: " />
<SchemaStringField
title="默认"
x-decorator="FormItem"
x-component="Input"
/>
<SchemaStringField
title="无冒号(colon=false)"
x-decorator="FormItem"
x-component="Input"
:x-decorator-props="{
colon: false,
}"
/>
<SchemaVoidField x-component="Title" x-content="固定宽度设置: " />
<SchemaStringField
title="固定label宽度(labelWidth)"
x-decorator="FormItem"
x-component="Input"
:x-decorator-props="{
labelWidth: 300,
}"
/>
<SchemaStringField
title="固定label宽度(labelWidth)溢出溢出溢出溢出溢出溢出溢出溢出溢出溢出溢出溢出溢出溢出溢出溢出溢出溢出溢出溢出溢出溢出溢出溢出溢出溢出溢出溢出"
description="描述描述"
x-decorator="FormItem"
x-component="Input"
:x-decorator-props="{
labelWidth: 300,
tooltip: '提示提示',
}"
/>
<SchemaStringField
title="固定label宽度(labelWidth)换行换行换行换行换行换行换行换行换行换行换行换行换行换行换行换行换行换行换行换行换行换行换行换行换行换行"
description="描述描述"
x-decorator="FormItem"
x-component="Input"
:x-decorator-props="{
labelWidth: 300,
labelWrap: true,
tooltip: '提示提示',
}"
/>
<SchemaStringField
title="固定内容宽度(wrapperWidth)"
x-decorator="FormItem"
x-component="Input"
:x-decorator-props="{
labelWidth: 300,
wrapperWidth: 300,
}"
/>
<SchemaVoidField x-component="Title" x-content="对齐方式设置:" />
<SchemaStringField
title="label左对齐(labelAlign=left)"
x-decorator="FormItem"
x-component="Input"
:x-decorator-props="{
labelWidth: 300,
labelAlign: 'left',
}"
/>
<SchemaStringField
title="label右对齐(labelAlign=right默认)"
x-decorator="FormItem"
x-component="Input"
:x-decorator-props="{
labelWidth: 300,
labelAlign: 'right',
}"
/>
<SchemaStringField
title="内容左对齐(wrapperAlign=left默认)"
x-decorator="FormItem"
x-component="Input"
:x-decorator-props="{
labelWidth: 300,
wrapperWidth: 240,
wrapperAlign: 'left',
}"
/>
<SchemaStringField
title="内容右对齐(wrapperAlign=right)"
x-decorator="FormItem"
x-component="Input"
:x-decorator-props="{
labelWidth: 300,
wrapperWidth: 240,
wrapperAlign: 'right',
}"
/>
<SchemaStringField
title="tooltip"
x-decorator="FormItem"
x-component="Input"
:x-decorator-props="{
tooltip: 'tooltip',
}"
/>
<SchemaStringField
title="tooltip"
x-decorator="FormItem"
x-component="Input"
:x-decorator-props="{
tooltip: 'tooltip',
tooltipLayout: 'text',
}"
/>
<SchemaVoidField x-component="Title" x-content="是否撑满: " />
<SchemaStringField
title="默认不撑满(fullness=false)"
x-decorator="FormItem"
x-component="Cascader"
:enum="options"
:x-decorator-props="{
fullness: false,
}"
/>
<SchemaStringField
title="撑满(fullness=true)"
x-decorator="FormItem"
x-component="Cascader"
:enum="options"
:x-decorator-props="{
fullness: true,
}"
/>
<SchemaStringField
title="InputNumber撑满(fullness=true)"
x-decorator="FormItem"
x-component="InputNumber"
:x-decorator-props="{
fullness: true,
}"
/>
<SchemaStringField
title="InputNumber不撑满(fullness=false)"
x-decorator="FormItem"
:x-decorator-props="{
addonBefore: 'addonBefore',
addonAfter: 'addonAfter',
}"
x-component="InputNumber"
/>
<SchemaVoidField x-component="Title" x-content="辅助信息: " />
<SchemaStringField
title="必填星号"
x-decorator="FormItem"
x-component="Input"
:x-decorator-props="{
asterisk: true,
labelCol: 6,
wrapperCol: 10,
}"
/>
<SchemaStringField
title="前缀"
x-decorator="FormItem"
x-component="Input"
:x-decorator-props="{
addonBefore: 'addonBefore',
labelCol: 6,
wrapperCol: 10,
}"
/>
<SchemaStringField
title="后缀"
x-decorator="FormItem"
x-component="Input"
:x-decorator-props="{
addonAfter: 'addonAfter',
labelCol: 6,
wrapperCol: 10,
}"
/>
<SchemaStringField
title="帮助信息feedbackText"
x-decorator="FormItem"
x-component="Input"
:x-decorator-props="{
feedbackText: 'feedbackText',
labelCol: 6,
wrapperCol: 10,
}"
/>
<SchemaStringField
title="额外信息extra"
x-decorator="FormItem"
x-component="Input"
:x-decorator-props="{
feedbackText: 'feedbackText',
extra: 'extra',
labelCol: 6,
wrapperCol: 10,
}"
/>
<SchemaVoidField x-component="Title" x-content="表单状态: " />
<SchemaVoidField x-component="FormLayout">
<SchemaStringField
title="错误状态(feedbackStatus=error)"
x-decorator="FormItem"
x-component="Input"
description="description"
:x-decorator-props="{
feedbackStatus: 'error',
}"
/>
<SchemaStringField
title="警告状态(feedbackStatus=warning)"
x-decorator="FormItem"
x-component="Input"
description="description"
:x-decorator-props="{
feedbackStatus: 'warning',
}"
/>
<SchemaStringField
title="成功状态(feedbackStatus=success)"
x-decorator="FormItem"
x-component="Input"
description="description"
:x-decorator-props="{
feedbackStatus: 'success',
}"
/>
<SchemaStringField
title="加载状态(feedbackStatus=pending)"
x-decorator="FormItem"
x-component="Input"
description="description"
:x-decorator-props="{
feedbackStatus: 'pending',
}"
/>
</SchemaVoidField>
<SchemaVoidField x-component="Title" x-content="反馈信息的布局: " />
<SchemaStringField
title="紧凑模式required"
x-decorator="FormItem"
x-component="Input"
:required="true"
:x-decorator-props="{
feedbackLayout: 'terse',
}"
/>
<SchemaStringField
title="紧凑模式有feedback(feedbackLayout=terse)"
x-decorator="FormItem"
x-component="Input"
:x-decorator-props="{
feedbackStatus: 'error',
feedbackText: 'error message',
feedbackLayout: 'terse',
}"
/>
<SchemaStringField
title="紧凑模式无feedback(feedbackLayout=terse)"
x-decorator="FormItem"
x-component="Input"
:x-decorator-props="{
feedbackLayout: 'terse',
}"
/>
<SchemaStringField
title="松散模式(feedbackLayout=loose)"
x-decorator="FormItem"
x-component="Input"
:x-decorator-props="{
feedbackStatus: 'error',
feedbackText: 'error message',
feedbackLayout: 'loose',
}"
/>
<SchemaStringField
title="弹出模式(feedbackLayout=popover)"
x-decorator="FormItem"
x-component="Input"
:x-decorator-props="{
feedbackStatus: 'warning',
feedbackText: 'warning message',
feedbackLayout: 'popover',
}"
/>
<SchemaStringField
title="弹出模式(feedbackLayout=popover)"
x-decorator="FormItem"
x-component="Input"
:x-decorator-props="{
feedbackStatus: 'error',
feedbackText: 'error message',
feedbackLayout: 'popover',
}"
/>
<SchemaStringField
title="弹出模式(feedbackLayout=popover)"
x-decorator="FormItem"
x-component="Input"
:x-decorator-props="{
feedbackStatus: 'success',
feedbackText: 'success message',
feedbackLayout: 'popover',
}"
/>
</SchemaField>
</FormProvider>
</template>尺寸控制案例
vue
<script setup lang="tsx">
import type { DataField } from '@formily/core'
import { createForm, onFieldChange } from '@formily/core'
import { createSchemaField } from '@formily/vue'
import {
Cascader,
DatePicker,
Form,
FormItem,
Input,
InputNumber,
Radio,
Select,
Switch,
} from '@silver-formily/element-plus'
function Div(props, { slots }) {
return <div {...props}>{slots?.default()}</div>
}
const form = createForm({
values: {
size: 'default',
},
effects: () => {
onFieldChange('size', ['value'], (field, form) => {
form.setFieldState('sizeWrap.*', (state) => {
if (state.decorator[1]) {
state.decorator[1].size = (field as DataField).value
}
})
})
},
})
const { SchemaField, SchemaStringField, SchemaVoidField, SchemaBooleanField }
= createSchemaField({
components: {
FormItem,
Input,
Select,
Cascader,
DatePicker,
Switch,
InputNumber,
Radio,
Div,
},
})
</script>
<template>
<Form :form="form">
<SchemaField>
<SchemaStringField
name="size"
title="Radio.Group"
x-decorator="FormItem"
x-component="Radio.Group"
:enum="[
{ value: 'small', label: 'Small' },
{ value: 'default', label: 'Default' },
{ value: 'large', label: 'Large' },
]"
/>
<SchemaVoidField name="sizeWrap" x-component="Div">
<SchemaStringField
name="input"
title="Input"
x-decorator="FormItem"
x-component="Input"
required
/>
<SchemaStringField
name="select1"
title="Multiple Select"
x-decorator="FormItem"
x-component="Select"
:enum="[
{
label: '选项1',
value: 1,
},
{
label: '选项2',
value: 2,
},
]"
:x-component-props="{
multiple: true,
placeholder: '请选择',
}"
required
/>
<SchemaStringField
name="select2"
title="Select"
x-decorator="FormItem"
x-component="Select"
:enum="[
{
label: '选项1',
value: 1,
},
{
label: '选项2',
value: 2,
},
]"
:x-component-props="{
placeholder: '请选择',
}"
required
/>
<SchemaStringField
name="Cascader"
title="Cascader"
x-decorator="FormItem"
x-component="Cascader"
required
/>
<SchemaStringField
name="DatePicker"
title="DatePicker"
x-decorator="FormItem"
x-component="DatePicker"
required
/>
<SchemaStringField
name="InputNumber"
title="InputNumber"
x-decorator="FormItem"
x-component="InputNumber"
required
/>
<SchemaBooleanField
name="Switch"
title="Switch"
x-decorator="FormItem"
x-component="Switch"
required
/>
</SchemaVoidField>
</SchemaField>
</Form>
</template>API
FormItem Attributes
| 属性名 | 类型 | 描述 | 默认值 |
|---|---|---|---|
| label | string | VNode | 标签 | - |
| for | string | 关联的表单字段 | - |
| tooltip | string | VNode | 问号提示 | - |
| addonBefore | string | VNode | 前缀内容 | - |
| addonAfter | string | VNode | 后缀内容 | - |
| extra | string | VNode | 扩展描述文案 | - |
| feedbackText | string | 反馈文案 | - |
| feedbackStatus | enum | 反馈状态 | - |
| asterisk | boolean | 星号提醒 | - |
| colon | boolean | 是否显示冒号 | true |
| labelAlign | enum | 标签文本对齐方式 | - |
| wrapperAlign | enum | 内容文本对齐方式 | - |
| labelWrap | boolean | 标签换行,超出部分显示省略号,hover 显示 tooltip | false |
| labelWidth | number | 标签固定宽度 | - |
| wrapperWidth | number | 内容固定宽度 | - |
| wrapperWrap | boolean | 内容换行,超出部分显示省略号,hover 显示 tooltip | false |
| labelCol | number | 标签网格所占列数,和内容列数加起来总和为 24 | - |
| wrapperCol | number | 内容网格所占列数,和标签列数加起来总和为 24 | - |
| fullness | boolean | 内容是否撑满 | false |
| size | enum | 尺寸 | default |
| layout | enum | 布局模式 | - |
| feedbackLayout | enum | 反馈布局 | 'loose' |
| tooltipLayout | enum | 提示布局 | - |
FormItem.BaseItem
纯样式组件,属性与 FormItem 一样,与 Formily Core 不做状态桥接,主要用于一些需要依赖 FormItem 的样式布局能力,但不希望接入 Field 状态的场景