【vue3组件封装】Form和FormItem表单

历史文章

  1. 含校验的Input
  2. checkbox复选框与复选框组,radioGroup单选框

Form和FormItem

这个组件主要用于表单的布局,下面涉及的其他组件可参考历史文章。

  • Form可以接受widthlabelCol,分别表示表单宽度和label所占宽度
  • FormItem可以接受widthlabelCollabel,前两者可以覆盖父组件传过来的

使用

<s-form width="400px" :labelCol="2">
  <s-form-item label="IP地址">
      <s-input
          :rules="[
             { type: 'required', message: 'ip不能为空' },
             { type: 'ip', message: 'ip格式不对' },
          ]"
          :value="form.ip"
          @update="updateForm"
          name="ip"
      />
  </s-form-item>
</s-form>

封装

SForm.vue

<script lang="ts">
import { h } from "vue"

export default {
  name: "SForm",
  props: {
    width: String,		// 表单宽度
    labelCol: Number	// label所占宽度的比例
  },
  setup (props, context) {
    if (!context.slots || !context.slots.default) return null
      
    // 将form的属性传给formitem
    const slots = context.slots.default().map(slot => ({
      ...slot,
      props: {
        ...props,		// 父组件form的属性
        ...slot.props	// 子组件formitem的属性,如果有,会覆盖父组件form的属性(以增强子组件样式的优先级
      }
    }))
    return () => h("div", {
      className: "s-form"
    }, slots)
  }
}

</script>
<style lang="less">
    .s-form{
        .s-form-item{
            margin-top: 10px
        }
    }
</style>

SFormItem.vue

<template>
    <div class="s-form-item" :style="{width}">
        <div class="label" :style="{width:labelWidth}">{{label?`${label}:`:' '}}</div>
        <slot></slot>
    </div>
</template>

<script lang="ts">
export default {
  name: "s-form-item",
  props: {
    label: {			// label名称
      type: String,
      default: ""
    },
    width: {			// 占表格宽度的百分比
      type: String,
      default: "100%"
    },
    labelCol: {			// label所占宽度
      type: Number,
      default: 1
    }
  },
    
  setup (props: {labelCol:number, width:string}) {
    const persents = ["10%", "20%", "30%", "40%", "50%", "60%", "80%", "90%", "100%"]
    const labelWidth = persents[props.labelCol]
    return {
      labelWidth,
      ...props
    }
  }
}

</script>
<style lang="less" scoped>
  .s-form-item{
    display: flex;
    align-items: baseline;
    .label{
      text-align: right;
    }
  }
</style>
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3中封装form表单组件的思路可以参考以下步骤: 1. 创建一个Form组件,作为整个表单的容器。该组件可以包含一个form元素,并接受一个表单数据对象作为props。 2. 在Form组件内部,使用v-for指令遍历表单数据对象的属性,并根据属性的类型渲染对应的表单项。 3. 对于每个表单项,可以创建一个FormItem组件,并根据属性的类型渲染对应的表单元素,比如input、select、textarea等。 4. FormItem组件可以接受属性的名称、类型和值作为props,并根据类型渲染不同的表单元素。 5. 可以根据需要扩展Form组件FormItem组件,添加更多的表单项类型,比如富文本编辑器、上传图片等。 6. 在Form组件中,可以监听表单元素的值变化,并将变化的值更新到表单数据对象中。 7. 可以在Form组件中添加提交按钮,并在点击按钮时触发表单提交事件,将表单数据对象作为参数传递给父组件。 需要注意的是,这只是一个基本的封装思路,如果需要使用到更多的业务场景,可以根据具体需求进行扩展和定制。<span class="em">1</span><span class="em">2</span> #### 引用[.reference_title] - *1* *2* [vue3 ts element plus form表单二次封装详细步骤 (附参数、类型详细介绍及简单使用示例)](https://blog.csdn.net/weixin_45291937/article/details/126275955)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值