Skip to content

HForm 表单组件 ​

HForm 是表单容器组件,负责解析 JSON 配置、管理表单状态、协调组件间通信、处理表单提交和校验。

Props ​

属性名类型默认值描述
v-modelRecord<string, any>-表单数据双向绑定
configReturnType<typeof defineOption>-表单配置对象
label-widthstring | number-标签宽度
readonlybooleanfalse是否只读
disabledbooleanfalse是否禁用
rulesRecord<string, Rule[]>-表单级校验规则

此外,HForm 还继承了 Vant Form 组件的所有属性,如 label-align、input-align、error-message-align 等。

Events ​

事件名参数描述
search(data: Record<string, any>) => void搜索事件
submit(data: Record<string, any>) => void提交事件(覆盖 Vant 默认 submit 事件,传递表单数据)

此外,HForm 还继承了 Vant Form 组件的所有事件。

Methods ​

方法名参数描述
validate() => Promise<boolean>校验表单
validateField(fields: string | string[]) => Promise<boolean>校验指定字段
clearValidate(fields?: string | string[]) => void清空校验
resetValidation(fields?: string | string[]) => void重置表单验证
reset() => void重置表单
submit() => void触发提交
getValues() => Record<string, any>获取所有表单项当前的值
getValidationStatus() => Record<string, any>获取所有表单项的校验状态
scrollToField(name: string, options?: ScrollIntoViewOptions) => void滚动到指定表单字段

Slots ​

插槽名绑定值描述
prepend{ props, wrapper }前置插槽 - 在表单项之前
default{ props, wrapper }默认插槽 - 在表单项之后

插槽参数 ​

参数类型描述
propsExtractPublicPropTypes<typeof formProps>表单属性
wrapperReturnType<typeof useWrapper>表单包装器实例

示例 ​

基础用法 ​

vue
<template>
    <HForm v-model="formData" :config="formConfig" label-align="top">
        <VanButton type="primary" @click="submit">提交</VanButton>
    </HForm>
</template>

<script setup lang="ts">
import { defineOption, HForm } from '@xiaohaih/json-form-vant';
import { ref } from 'vue';

const formData = ref({ name: '', email: '' });

const formConfig = defineOption([
    {
        field: 'name',
        t: 'input',
        label: '姓名',
        placeholder: '请输入姓名',
    },
    {
        field: 'email',
        t: 'input',
        label: '邮箱',
        placeholder: '请输入邮箱',
    },
]);

function submit() {
    console.log('提交数据:', formData.value);
}
</script>

使用 prepend 插槽 ​

vue
<template>
    <HForm v-model="formData" :config="formConfig">
        <template #prepend="{ props, wrapper }">
            <VanCell title="用户信息" />
        </template>
        <VanButton @click="submit">提交</VanButton>
    </HForm>
</template>

调用表单方法 ​

vue
<template>
    <HForm ref="formRef" v-model="formData" :config="formConfig">
        <VanButton @click="handleValidate">校验</VanButton>
        <VanButton @click="handleReset">重置</VanButton>
    </HForm>
</template>

<script setup lang="ts">
import { defineOption, HForm } from '@xiaohaih/json-form-vant';
import type { HFormInstance } from '@xiaohaih/json-form-vant';
import { ref } from 'vue';

const formRef = ref<HFormInstance>();
const formData = ref({ name: '' });

const formConfig = defineOption([
    {
        field: 'name',
        t: 'input',
        label: '姓名',
        rules: [{ required: true, message: '请输入姓名' }],
    },
]);

async function handleValidate() {
    const valid = await formRef.value?.validate();
    if (valid) {
        console.log('校验通过');
    }
}

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