Skip to content

t-form-dynamic

FormDynamic 动态表单

组件介绍

动态表单组件,根据JSON配置生成表单,支持多种表单字段类型和校验规则,适用于需要根据后端配置生成表单的场景。

平台兼容

HarmonyH5AndroidiOS小程序UTS

基础用法

vue
<template>
  <t-form-dynamic 
    ref="dynamicForm"
    :options="formOptions"
    :forms="formData"
    :rules="formRules"
    direction="column"
  ></t-form-dynamic>
  <t-button type="primary" @click="handleSubmit">提交</t-button>
  <t-button @click="handleReset">重置</t-button>
</template>

<script setup>
const dynamicForm = ref(null);
const formData = ref({
  username: '',
  password: '',
  gender: '',
  hobbies: [],
  agree: false
});

const formOptions = ref([
  {
    type: 'input',
    name: 'username',
    label: '用户名',
    itemAttrs: {
      required: true
    },
    attrs: {
      placeholder: '请输入用户名',
      maxlength: 20
    }
  },
  {
    type: 'input',
    name: 'password',
    label: '密码',
    itemAttrs: {
      required: true
    },
    attrs: {
      placeholder: '请输入密码',
      type: 'password'
    }
  },
  {
    type: 'radio',
    name: 'gender',
    label: '性别',
    itemAttrs: {
      required: true
    },
    attrs: {
      mode: 'button'
    },
    list: [
      { label: '男', value: 'male' },
      { label: '女', value: 'female' }
    ]
  },
  {
    type: 'checkbox',
    name: 'hobbies',
    label: '爱好',
    list: [
      { label: '篮球', value: 'basketball' },
      { label: '足球', value: 'football' },
      { label: '游泳', value: 'swimming' }
    ]
  },
  {
    type: 'switch',
    name: 'agree',
    label: '同意协议',
    itemAttrs: {
      required: true
    }
  }
]);

const formRules = ref({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度至少6位', trigger: 'blur' }
  ],
  gender: [
    { required: true, message: '请选择性别', trigger: 'change' }
  ],
  agree: [
    { required: true, message: '请同意协议', trigger: 'change' }
  ]
});

function handleSubmit() {
  if (dynamicForm.value.validate()) {
    console.log('表单验证通过', formData.value);
    // 提交表单数据
  }
}

function handleReset() {
  dynamicForm.value.reset();
}
</script>

Props

属性名类型默认值说明可选值
sizeString""组件尺寸large, medium, small, mini
typeString""组件类型info, primary, error, warning, success
disabledBooleanfalse组件是否禁用true, false
hoverBooleanfalse是否有点击效果true, false
pathString""点击组件后跳转的页面路径-
effectString""组件显示主题normal, dark, light, plain
optionsArray[]动态表单的配置-
formsObject{}绑定表单的值-
rulesObject{}校验规则配置-
showLabelBooleantrue是否显示headertrue, false
directionString"row"表单的布局的方向row, column
rulesClassString""规则样式-
labelClassString""标签样式-
childClassString""统一设置子组件样式-
requiredAsteriskString"*"必填时的提示符-
requiredAsteriskClassString"mt-10"必填提示符的样式-
hideRequiredAsteriskBooleanfalse隐藏必填提示符true, false
showMessageBooleantrue是否显示验证信息true, false
bottomLineBooleanfalse是否显示底部线条true, false

Events

事件名说明回调参数
click点击时触发-
touchstart触摸开始时触发触摸事件对象
touchend触摸结束时触发触摸事件对象
touchmove触摸移动时触发触摸事件对象
touchcancel触摸取消时触发触摸事件对象

Methods

方法名说明参数返回值
validate验证表单Boolean
reset重置表单

Slots

插槽名说明
default自定义表单字段内容,接收item和index参数