Skip to content

快速开始

5 分钟内完成第一个 v3sf 表单。

安装

v3sf 采用核心 + 适配器的架构,需要同时安装 @v3sf/core 和一个 UI 适配器。

bash
pnpm add @v3sf/core @v3sf/vant
bash
pnpm add @v3sf/core @v3sf/element-plus

也可以使用 npm installyarn add

基本使用

1. 创建表单组件

通过 createSchemaForm 将核心引擎与 UI 适配器绑定,生成一个可直接使用的 Vue 组件。

ts
import { createSchemaForm } from '@v3sf/core'
import { vantAdapter } from '@v3sf/vant'

const SchemaForm = createSchemaForm(vantAdapter)
ts
import { createSchemaForm } from '@v3sf/core'
import { elementPlusAdapter } from '@v3sf/element-plus'

const SchemaForm = createSchemaForm(elementPlusAdapter)

2. 定义 Schema

Schema 是一个 JSON 对象,根节点 typeobject,在 properties 中定义各个字段。

ts
const schema = {
  type: 'object',
  properties: {
    username: {
      type: 'string',
      title: '用户名',
      required: true,
      placeholder: '请输入用户名',
    },
    age: {
      type: 'number',
      title: '年龄',
      widget: 'stepper',
      rules: { min: 1, max: 150, message: '请输入有效年龄' },
    },
    gender: {
      type: 'string',
      title: '性别',
      widget: 'radio',
      enum: ['male', 'female'],
      enumNames: ['男', '女'],
    },
  },
}
基础示例
*姓名
年龄
签名

3. 渲染表单

使用 v-model 双向绑定表单数据。

vue
<script setup>
import { ref } from 'vue'
import { createSchemaForm } from '@v3sf/core'
import { vantAdapter } from '@v3sf/vant'

const SchemaForm = createSchemaForm(vantAdapter)

const formData = ref({})
const schema = {
  type: 'object',
  properties: {
    username: {
      type: 'string',
      title: '用户名',
      required: true,
      placeholder: '请输入用户名',
    },
    age: {
      type: 'number',
      title: '年龄',
      widget: 'stepper',
    },
    gender: {
      type: 'string',
      title: '性别',
      widget: 'radio',
      enum: ['male', 'female'],
      enumNames: ['男', '女'],
    },
  },
}
</script>

<template>
  <SchemaForm v-model="formData" :schema="schema" />
</template>
vue
<script setup>
import { ref } from 'vue'
import { createSchemaForm } from '@v3sf/core'
import { elementPlusAdapter } from '@v3sf/element-plus'

const SchemaForm = createSchemaForm(elementPlusAdapter)

const formData = ref({})
const schema = {
  type: 'object',
  properties: {
    username: {
      type: 'string',
      title: '用户名',
      required: true,
      placeholder: '请输入用户名',
    },
    age: {
      type: 'number',
      title: '年龄',
      widget: 'number',
    },
    department: {
      type: 'string',
      title: '部门',
      widget: 'select',
      enum: ['engineering', 'product', 'design'],
      enumNames: ['工程部', '产品部', '设计部'],
    },
  },
}
</script>

<template>
  <SchemaForm v-model="formData" :schema="schema" />
</template>

表单校验

通过 ref 获取表单实例,调用 validate 方法触发校验。

vue
<script setup>
import { ref } from 'vue'

const formRef = ref()

async function handleSubmit() {
  const errors = await formRef.value.validate()
  if (errors.length === 0) {
    console.log('提交成功', formRef.value.getFormData())
  }
}
</script>

<template>
  <SchemaForm ref="formRef" v-model="formData" :schema="schema" />
  <button @click="handleSubmit">提交</button>
</template>

validate() 返回一个 ErrorMessage[] 数组。数组为空表示校验通过。默认会自动滚动到第一个错误字段。

表达式联动

requireddisabledhidden 等属性中使用 {{ }} 表达式实现字段联动。

ts
const schema = {
  type: 'object',
  properties: {
    hasDiscount: {
      type: 'boolean',
      title: '有优惠码',
      widget: 'switch',
    },
    discountCode: {
      type: 'string',
      title: '优惠码',
      hidden: '{{ !$values.hasDiscount }}',
      required: '{{ $values.hasDiscount }}',
      placeholder: '请输入优惠码',
    },
  },
}

hasDiscountfalse 时,discountCode 字段自动隐藏;为 true 时变为必填。

下一步

Released under the MIT License.