vue3 element-plus el-form的二次封装

form表单的二次封装

vue3 element-plus el-form的二次封装

属性说明

属性名类型默认值说明
dataArray[]页面展示数据内容
onChangeFunctionfalse表单事件
bindPropsObject{}表单属性
formRefObject{}表单ref
ruleFormObject{}数据

使用示例

	//形式一
	 <formCustom
        :data="searchHeaders"
        :bindProps="{ inline: true }"
        :ruleForm="searchRuleForm"
      ></formCustom>

	//形式二
	 <formCustom
      :data="formHeaders"
      :bindProps="{ 'label-width': '124px', 'scroll-to-error': true }"
      v-model:formRef="formRef"
      :ruleForm="ruleForm"
    ></formCustom>
// setup 形式下
import { ref,active } from 'vue'
import formCustom from "@/components/custom-form-v3/searchForm"
const formProps = {
  style: {
    marginBottom: "0",
    marginRight: "16px",
  },
};
const searchRuleForm = reactive({}); //搜索的数据

const ruleForm = reactive({}); //表单的数据
const formRef = ref()
const searchHeaders = reactive([
	{
		formProps,
		render: () => {
		return (
			<div class="flex-d-r-center">
			<div
				class="db-primary-button flex-d-r-center"
				onClick={() => addTools()}
			>
				<span class="iconfont icon-style icon-left">
				&#xe62d;
				</span>
				新增
			</div>
			</div>
		);
		},
	},
	{
		formProps: {
			style: {
				flex: 1,
				"margin-bottom": 0,
			},
		},
  	},
	{
		Component: "ElSelect",
		formProps,
		prop: "status",
		componentProps: {
			clearable:true,
			placeholder: "状态",
			style: {
				width: "101px",
			},
		},
		componentSlots: {
			default: () => {
				const data = [
				{
					label: "进行中",
					value: 1,
				},
				{
					label: "已结束",
					value: 2,
				},
				{
					label: "未开始",
					value: 3,
				},
				];
				return data.map((item) => {
					return (
						<el-option
						value={item.value}
						label={item.label}
						key={item.value}
						></el-option>
					);
				});
			},
		},
  	},
	{
		Component: "ElInput",
		formProps,
		prop: "title",
		componentProps: {
			placeholder: "请输入名称",
			style: {
				width: "205px",
			},
		},
  	},
	{
		formProps: {
			style: {
				marginBottom: "0",
				marginRight: "0",
			},
		},
		render: () => {
			return (
				<div
				class="db-primary-button flex-d-r-center"
				onClick={() => searchRes()}
				>
				<span class="iconfont icon-style icon-left">
					&#xe624;
				</span>
				查询
				</div>
			);
		},
  	},
])

const formHeaders = reactive([
	{
		label:"时间",
		prop: "times",
		span: 24,
		defaultValue: "",
		Component: "ElDatePicker",
		on: {
			change: () => {},
		},
		componentProps: {
			type: "datetimerange",
			"value-format": "YYYY-MM-DD HH:mm:ss",
			format: "YYYY-MM-DD HH:mm:ss",
			rangeSeparator: "-",
			startPlaceholder: "开始时间",
			endPlaceholder: "结束时间",
			disabled: computed(() => isDisabled(1)),
			style:{
				width: '340px'
			}
		},
		formProps: {
			rules: {
				required: true,
				trigger: "blur",
				validator: (rule, value, callback) => {
				if (value === "") {
					callback(new Error("请输入抢购时间"));
				}
				callback();
				},
			},
		},
  	},
	{
		label: "名称",
		prop: "name",
		Component: "ElInput",
		defaultValue: "",
		componentProps: {
			placeholder: "名称需2-30个字符(汉字占两个字符)",
			style: {
				width: "540px",
			},
		},
		formProps: {
			rules: {
				required: true,
				trigger: "blur",
				// message: "请输入讲师名称",
				validator: (rule, value, callback) => {
				if(value === '') {
					callback(new Error('请输入讲师名称'))
				}
				const len = strLength(value)
				if(len < 2 || len > 30) {
					callback(new Error('讲师名称需2-30个字符'))
				}
				callback()
				}
			},
		},
  	},
	{
		label: "状态",
		prop: "status",
		span: 24,
		Component: switchCons,
		defaultValue: 0,
		componentProps: {
			style: {
				width: "540px",
			},
		},
		formProps: {
			rules: {
				required: true,
				trigger: "blur",
			},
		},
  	},
])

const isDisabled = (type) => {
  const obj = {
    1: [1],
    2: [1,3]
  }
  return obj[type].includes(ruleForm.status)
}

const saveForm = () => {
  formRef.value.validate(async (valid) => { // 提交数据时验证表单
    if(valid) {
      
    }
  })
};

const addTools = () => {
  router.push({
    path: "/add"
  });
};
const searchRes = () => {
  useTableRef.value.getRequestRes("firstPage"); //刷新表格
};


//也可以注册到全局 页面中直接使用不需要import
// main.ts文件
import App from '@/App.vue'
import formCustom from "@/components/custom-form-v3/searchForm"
const app = createApp(App)
app.component('formCustom', formCustom)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值