Skip to content

动态表单

动态表单集成的功能

  • 自定义输入组件,可参考@erp/biz里的 ErpInput input(text, number, integer, switch, radio, checkbox, update, select, cascader, tree)

  • 自定义布局功能通过简单的类栅格配置能实现通用的布局

  • 自定义校验功能通过简单的配置,就能支持通用强大的校验功能。

动态表单组件集成形式

基础信息
收货信息
收货信息
商品信息
卖家备注
加工信息
附件信息

动态表单弹窗形式

动态表单通用配置实例

vue
<template>
	<div class="form-demo1">
		<DynamicForm :model-value="model" :config="formConfig">
			<!-- 支持slots -->
			<template #image="{ field, model, modelValue }">
				{{ model }}
				{{ field }}
				{{ modelValue }}
			</template>
		</DynamicForm>
	</div>
</template>

<script lang="ts" setup>
import { ref } from 'vue';
import { cloneDeep } from 'lodash-es';
import { DynamicForm } from '@erp/biz';
import { config } from './form-config';
import type { PageExpose } from '@erp/biz';

const formConfig = cloneDeep(config);
/**
 * 关闭左侧导航
 */
formConfig.showSide = false;

// formConfig.fields[0] = reactive(formConfig.fields[0]);
const model = ref({
	key13: '1',
});
setTimeout(() => {
	// formConfig.fields[1].type = 'custom';
	// formConfig.fields[1].component = 'image';
	// /**
	//  * field 自带响应式
	//  */
	// formConfig.fields[0].options = [
	// 	{
	// 		value: '1',
	// 		label: '订单类型一',
	// 	},
	// 	{
	// 		value: '2',
	// 		label: '订单类型二',
	// 	},
	// 	{
	// 		value: '3',
	// 		label: '订单类型三',
	// 	},
	// 	{
	// 		value: '4',
	// 		label: '订单类型四',
	// 	},
	// ];
}, 3000);

defineExpose<PageExpose>({
	getModel() {
		return model.value;
	},
});
</script>

<style lang="scss" scoped>
.form-demo1 {
	padding: 0;
	border: 1px solid var(--color-border-1);
	border-radius: var(--border-radius-small);
	height: 560px;
}
</style>
ts
import GoodsGrid from './goods-grid.vue';
import type { DynamicFormI } from '@erp/biz';

export const config: DynamicFormI = {
	title: '商品订单',
	showSide: true,
	// 这个按钮配置需要考虑 modal / drawer,此配置与该组件的buttons配置重复
	// buttons: [
	// 	{
	// 		label: '提交',
	// 		code: 'submit',
	// 		type: 'primary',
	// 		validator: 'field',
	// 	},
	// 	{
	// 		label: '取消',
	// 		code: 'cancel',
	// 		type: 'info',
	// 	},
	// ],
	cols: 3,
	colGap: 12,
	components: {
		GoodsGrid,
	},
	// fields: [
	// 	{
	// 		key: 'address',
	// 		label: '地市',
	// 		type: 'cascader',
	// 		componentProps: {
	// 			pathMode: true,
	// 		},
	// 		options: [
	// 			{
	// 				value: 'beijing',
	// 				label: 'Beijing',
	// 				children: [
	// 					{
	// 						value: 'chaoyang',
	// 						label: 'ChaoYang',
	// 						children: [
	// 							{
	// 								value: 'datunli',
	// 								label: 'Datunli',
	// 							},
	// 						],
	// 					},
	// 					{
	// 						value: 'haidian',
	// 						label: 'Haidian',
	// 					},
	// 					{
	// 						value: 'dongcheng',
	// 						label: 'Dongcheng',
	// 					},
	// 					{
	// 						value: 'xicheng',
	// 						label: 'Xicheng',
	// 						children: [
	// 							{
	// 								value: 'jinrongjie',
	// 								label: 'Jinrongjie',
	// 							},
	// 							{
	// 								value: 'tianqiao',
	// 								label: 'Tianqiao',
	// 							},
	// 						],
	// 					},
	// 				],
	// 			},
	// 			{
	// 				value: 'shanghai',
	// 				label: 'Shanghai',
	// 				children: [
	// 					{
	// 						value: 'huangpu',
	// 						label: 'Huangpu',
	// 					},
	// 				],
	// 			},
	// 		],
	// 	},
	// 	{
	// 		key: 'orderType',
	// 		label: '订单类型',
	// 		type: 'select',
	// 		required: true,
	// 		options: [
	// 			{
	// 				value: '1',
	// 				label: '订单类型一',
	// 			},
	// 		],
	// 	},
	// 	{
	// 		key: 'key2.key',
	// 		label: '店铺',
	// 		required: true,
	// 		type: 'select',
	// 		options: [
	// 			{
	// 				value: 1,
	// 				label: '店铺一',
	// 			},
	// 		],
	// 	},
	// 	{
	// 		key: 'key3',
	// 		label: '下单时间',
	// 		type: 'date',
	// 	},
	// 	{
	// 		key: 'key4',
	// 		label: '运费',
	// 		type: 'number',
	// 	},
	// 	{
	// 		key: 'key5',
	// 		label: '买家账号',
	// 		type: 'text',
	// 		required: true,
	// 	},
	// 	{
	// 		key: 'key6',
	// 		label: '买家名称',
	// 		type: 'text',
	// 		required: true,
	// 	},
	// 	{
	// 		key: 'key7',
	// 		label: '业务员',
	// 		type: 'select',
	// 		required: true,
	// 		options: [],
	// 	},
	// 	{
	// 		key: 'key8',
	// 		label: '商机编号',
	// 		type: 'text',
	// 	},
	// 	{
	// 		key: 'key9',
	// 		label: '经销商',
	// 		type: 'text',
	// 	},
	// 	{
	// 		key: 'key10',
	// 		label: '派单人员',
	// 		type: 'select',
	// 		options: [],
	// 	},
	// 	{
	// 		key: 'key11',
	// 		label: '归属手机',
	// 		type: 'text',
	// 		rules: {
	// 			type: 'string',
	// 			validator(value, callback) {
	// 				// 校验手机
	// 				callback();
	// 			},
	// 		},
	// 	},
	// ],
	groups: [
		{
			label: '基础信息',
			fields: [
				{
					key: 'orderType',
					label: '订单类型',
					type: 'select',
					required: true,
					options: [
						{
							value: '1',
							label: '订单类型一',
						},
					],
				},
				{
					key: 'key2.key',
					label: '店铺',
					required: true,
					type: 'select-shop',
				},
				{
					key: 'key3',
					label: '下单时间',
					type: 'date',
				},
				{
					key: 'key4',
					label: '运费',
					type: 'number',
				},
				{
					key: 'key5',
					label: '买家账号',
					type: 'text',
					required: true,
				},
				{
					key: 'key6',
					label: '买家名称',
					type: 'text',
					required: true,
				},
				{
					key: 'key7',
					label: '业务员',
					type: 'select',
					required: true,
					options: [
						{
							label: '张三',
							value: '1',
						},
					],
				},
				{
					key: 'key8',
					label: '商机编号',
					type: 'text',
				},
				{
					key: 'key9',
					label: '经销商',
					type: 'text',
				},
				{
					key: 'key10',
					label: '派单人员',
					type: 'select',
					options: [],
				},
				{
					key: 'key113',
					label: '归属手机',
					type: 'text',
					rules: {
						type: 'string',
						validator(value, callback) {
							// 校验手机
							callback();
						},
					},
				},
			],
		},
		{
			label: '收货信息',
			cols: 3,
			tip: '收货信息',
			fields: [
				{
					key: 'address',
					label: '收货人',
					type: 'text',
					placeholder: '请输入收货人姓名',
					required: true,
				},
				{
					key: 'phone',
					label: '联系人电话',
					type: 'text',
					required: true,
					tooltip: '请输入联系电话',
				},
				{
					key: 'key13',
					label: '智能解析',
					type: 'textarea',
				},
				{
					key: 'key115',
					label: '收货地址',
					type: 'cascader',
					required: true,
					options: [
						{
							value: 'beijing',
							label: 'Beijing',
							children: [
								{
									value: 'chaoyang',
									label: 'ChaoYang',
									children: [
										{
											value: 'datunli',
											label: 'Datunli',
										},
									],
								},
								{
									value: 'haidian',
									label: 'Haidian',
								},
								{
									value: 'dongcheng',
									label: 'Dongcheng',
								},
								{
									value: 'xicheng',
									label: 'Xicheng',
									children: [
										{
											value: 'jinrongjie',
											label: 'Jinrongjie',
										},
										{
											value: 'tianqiao',
											label: 'Tianqiao',
										},
									],
								},
							],
						},
						{
							value: 'shanghai',
							label: 'Shanghai',
							children: [
								{
									value: 'huangpu',
									label: 'Huangpu',
								},
							],
						},
					],
				},
				{
					key: 'key12',
					label: '详细地址',
					type: 'text',
					required: true,
					placeholder: '请输入具体街道/楼栋/房号等信息',
				},
				{
					key: 'key13',
					label: '使用代发地址',
					tooltip: '使用代发地址',
					type: 'switch',
					row: {
						wrap: false,
						align: 'end',
					},
					options: [
						{
							value: 1,
							label: '是',
						},
						{
							value: 2,
							label: '否',
						},
					],
				},
			],
		},
		{
			label: '商品信息',
			buttons: [
				{
					code: 'add-shop',
					label: '添加商品',
					type: 'text',
					status: 'normal',
					size: 'small',
				},
			],
			fields: [
				{
					key: 'goods',
					span: 2,
					multiple: true,
					type: 'custom',
					component: 'GoodsGrid',
					hideLabel: true,
				},
			],
		},
		{
			label: '卖家备注',
			fields: [
				{
					key: 'key111',
					label: '旗帜颜色',
					tooltip: '旗帜颜色',
					type: 'text',
					span: 2,
				},
				{
					key: 'key112',
					label: '定制内容',
					type: 'textarea',
					placeholder: '请输入需要定制的内容,最多不超过300字',
				},
				{
					key: 'key113',
					label: '备注',
					type: 'textarea',
					placeholder: '系统会根据您选择的商品+定制内容,自动生成备注',
				},
			],
		},
		{
			label: '加工信息',
			fields: [],
		},
		{
			label: '附件信息',
			fields: [
				{
					key: 'key123',
					type: 'upload',
					span: 2,
				},
			],
		},
	],
};

动态表单 API

<dynamic-form> Props

参数名描述类型默认值
model-value (v-model)绑定值Record<string, any>function() { return {}; }
config (必填)DynamicFormI 动态表单配置DynamicFormI-

<dynamic-form> Events

事件名描述参数
update:model-value表单数据模型-
ok表单ok事件,可配合 createPage createModal createDrawer使用params: mixed
close表单close事件,可配合 createPage createModal createDrawer使用params: mixed
loading表单 loading 事件,可配合 createModal createDrawer使用params: mixed
click表单里配置的按钮事件button: mixed

<dynamic-form> Slots

插槽名描述参数
form-footer表单底部slot-
form-header表单头部slot-
side-footer侧边栏footer-

FormButtonType

参数名描述类型默认值
code按钮唯一标识string-
visible是否可见Ref<boolean> | ComputedRef<boolean> | boolean | (() => boolean)-
validator校验配置 true 为 自动校验
field 一个一个的字段校验
boolean | 'field' | ((model: Record<string, any>, form: FormInstance) => Promise<Record<string, ValidatedError> | undefined>)-

DynamicFormI

参数名描述类型默认值
$id表单IDstring-
title表单标题string-
fields表单字段DynamicFormFieldI[]-
groups表单组DynamicFormGroupI[]-
showSide是否显示左侧导航
默认显示
booleanfalse
buttons显示buttonFormButtonType[]-
layout表单字段布局'horizontal' | 'vertical' | 'inline'-
labelAlignlabel方向'left' | 'right'-
cols每一行展示的列数number-
colGap列与列之间的间距number-
components动态组件
只能第一次注册
Record<string, Component>-

DynamicFormGroupI

参数名描述类型默认值
$id由框架自己生成string-
label表单域标签string-
fields表单字段DynamicFormFieldI[]-
cols每一行展示的列数number-
colGap列与列之间的间距number-
rowGap行与行之间的间距number-
span跨越的格数 对表单子域有效number-
offset左侧的间隔格数 对表单子域有效number-
buttons显示buttonFormButtonType[]-
tip提示信息string-
children子表单DynamicFormGroupI[]-
visible是否显示Ref<boolean> | ComputedRef<boolean> | boolean | (() => boolean)-

DynamicFormFieldI

参数名描述类型默认值
$idid 自动生成string-
key数据模式key,符合propertyKeystring-
type组件类型InputType | 'custom'-
component组件Component | string-
componentProps组件额外配置
除了 DynamicFormFieldI 自带的属性外,如果组件特有的属性外都在此配置
T-
label标签的文本string-
multiple是否多值booleanfalse
disabled是否禁用boolean | Ref<boolean> | ComputedRef<boolean>-
placeholder占位string-
options组件数据配置OptionData[] | (() => Promise<OptionData[]>)-
tooltip提示内容string-
showColon是否显示冒号booleanfalse
noStyle是否去除样式booleanfalse
help帮助文案string-
required是否必须填写booleanfalse
rules表单项校验规则FieldRule | FieldRule[]-
validateStatus校验状态ValidateStatus-
validateTrigger触发校验的事件ValidateTrigger-
hideLabel是否隐藏标签booleanfalse
hideAsterisk是否隐藏星号booleanfalse
feedback是否显示表单控件的反馈图标booleanfalse
row表单项布局选项RowProps-
labelColProps标签元素布局选项。参数同 <col> 组件一致any-
wrapperColProps表单控件布局选项。参数同 <col> 组件一致any-
span跨越的格数number-
offset左侧的间隔格数number-
classclassstring-
show是否显示 隐藏的字段不做校验boolean | (() => boolean)-
slotsFormItem slots配置{ label?: RenderFunction; help?: RenderFunction; extra?: RenderFunction; prefix?: RenderFunction; suffix?: RenderFunction; }-

DynamicFieldComponentProps

参数名描述类型默认值
field子组件的表单域配置DynamicFormFieldI<T>-
model表单数据模型Record<string, any>-
modelValue (v-model)对应表单的值any-

DynamicFieldComponentExpose

参数名描述类型默认值
validate校验 true 通过 false 未通过() => boolean | string | Promise<boolean | string>-

<buttons> Props

按钮组

参数名描述类型默认值
buttons按钮配置FormButtonType[][]
loading是否加载中booleanfalse

<buttons> Events

事件名描述参数
click点击事件evt: mixed
button: mixed

基于 MIT 许可发布