Skip to content

Form 表单

基于 ElForm + ElRow 封装,通过配置化方式快速构建表单。

基础用法

vue
<template>
  <div>
    <p>编辑表单、双向绑定:{{ modelValue }}</p>
    <el-button @click="modelValue.name += 'a'">name + a</el-button>
    <MForm
      v-model="modelValue"
      style="max-width: 800px; margin-bottom: 50px"
      :fields="fields"
      :inline="false"
      :col-attrs="24"
      :label-width="200"
      @submit="submit"
      @cancel="cancel"
    >
    </MForm>

    <p>搜索表单、有默认值,无双向绑定</p>
    <MForm :fields="fields" @submit="submit" @cancel="cancel"> </MForm>

    <MMarkdownView :value="readme"></MMarkdownView>
  </div>
</template>

<script setup lang="ts">
import readme from '@/components/form/README.md?raw'
import { ref } from 'vue'
import { fields } from './config'

const modelValue = ref({
  name: 'demo'
})

const submit = (val: any) => {
  console.log('submit', val)
}

const cancel = (val: any) => {
  console.log('cancel', val)
}
</script>

<style lang="scss" scoped></style>

字段配置:

ts
import type { FormField } from '@/components'
import { reactive } from 'vue'

export const fields = reactive<FormField[]>([
  {
    tag: 'ElInput',
    key: 'name',
    label: '姓名',
    required: true,
    colAttrs: 12
  },
  {
    tag: 'ElDatePicker',
    key: 'date',
    label: '日期',
    colAttrs: 12,
    attrs: {
      // disabled: true
    }
  },
  {
    tag: 'MSelect',
    key: 'age',
    label: '性别',
    attrs: {
      options: [
        {
          label: '男',
          value: 1
        },
        {
          label: '女',
          value: 2
        },
        {
          label: '保密',
          value: 3,
          disabled: true
        }
      ]
    }
  },
  {
    tag: 'MCheckboxGroup',
    key: 'options',
    label: 'options',
    attrs: {
      options: [
        {
          label: 'Option 1',
          value: 1
        },
        {
          label: 'Option 2',
          value: 2,
          disabled: true
        }
      ]
    }
  },
  {
    tag: 'MRadioGroup',
    key: 'radio',
    label: 'radio',
    attrs: {
      options: [
        {
          label: 'Option 1',
          value: 1
        },
        {
          label: 'Option 2',
          value: 2
        }
      ]
    }
  },
  {
    tag: 'MUpload',
    key: 'files',
    label: '附件'
  }
])

API

Form 属性

属性名说明类型默认值
fields表单字段配置FormField[]
modelValuev-model 表单数据object{}
labelWidth标签宽度(inline: true 时忽略)number | string'auto'
rowAttrsElRow 属性object
colAttrsElCol 属性(inline: true 时忽略)number | object6
disabled禁用所有组件,隐藏默认按钮booleanfalse
inline按钮放入 colbooleantrue
loading提交按钮加载中booleanfalse
submitText提交按钮文字string
cancelText取消按钮文字string
submitIcon提交按钮图标string | false
cancelIcon取消按钮图标string | false

FormField

属性名说明类型默认值
tag渲染的组件,需支持 v-modelComponent | string
key唯一值string
label标签string
show是否显示booleantrue
slot是否使用具名插槽(name 为 key)booleanfalse
attrs绑定在 tag 上的属性object
on绑定在 tag 上的事件object
colAttrsElCol 属性number | object
required是否必填booleanfalse
validateRules校验规则FormItemRule[]

FormFieldOption

attrs.options 中每项的格式:

属性名说明类型默认值
label标签string
value实际值string | number
disabled是否禁用booleanfalse

Form 插槽

插槽名说明
[field key]FormField slot 为 true 时,自定义标签后内容
buttons自定义提交/取消按钮

Form 事件

事件名说明类型
submit点击确认按钮(formData) => void
cancel点击取消按钮(formData) => void
getForm获取 ElForm 实例(elFormRef) => void

基于 MIT 许可发布