• 【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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    封装

    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>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    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
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
  • 相关阅读:
    Oracle/PLSQL: Acos Function
    [附源码]SSM计算机毕业设计整形美容咨询网站JAVA
    MongoDB聚合运算符:$lte
    拟态时钟动画
    Kotlin 伴生对象(companion object) VS 包函数
    CompletableFuture 使用教程
    没有上司的舞会 - 树形DP
    背包问题---怎么选取物品,可以使得背包装的物品价值最大?
    Uniapp云开发(Uniapp入门)
    题目:2715.执行可取消的延迟函数
  • 原文地址:https://blog.csdn.net/aaaaapipi/article/details/127419617