🚀 DAY 5 OF LAUNCH WEEK: Introducing Socket Firewall Enterprise.Learn more
Socket
Book a DemoInstallSign in
Socket

v3-schema-form

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

v3-schema-form

An engine that generates forms based on JSON Schema

latest
Source
npmnpm
Version
0.4.0
Version published
Maintainers
1
Created
Source

v3-schema-form 表单

基于 json-schema 协议,使用一段 json 配置化生成表单
vue3.0 中后台表单解决方案
vant ui的依赖是 peerDependency,默认用户是使用了 vant 的

Usage

<template>
  <schema-form
    ref="formRef"
    v-model="formData"
    :schema="schema"
  />
</template>

<script setup>
import { ref } from 'vue';
import SchemaForm , { FormRef } from 'v3-schema-form';

const formRef = ref<FormRef>()
const formData = ref({
  userName: '王小明',
  age: 18,
  bio: '酷!',
});

const schema = {
  type: '`object`',
  properties: {
    userName: {
      type: '`string`',
      title: '用户名',
      required: true,
    },
    age: {
      type: '`number`',
      title: '年龄',
    },
    bio: {
      type: '`string`',
      title: '签名',
    },
  },
};
</script>

Props

Schema-form 组件的 props

属性名类型默认值描述
schemaobject{}表单协议配置
v-modelobject{}表单 data
disabledbooleanfalse全局禁用
readonlybooleanfalse全局只读
removeHiddenDatabooleanfalsegetFormData 时是否过滤 hidden 为 true 的字段
debugbooleanfalse表单值变更时,控制台输出日志
displayType'row' | 'column''row'统一指定 Label 与 Field 的展示关系,row 表示并排展示,column 表示两排展示
borderbooleantrue是否展示边框
inlineErrorMessagebooleanfalse是否在行内展示校验错误信息

Schema

表单的 schema 配置,基于json-schema 规范

属性名类型默认值描述
type'string' | 'object' | 'array' | 'number' | 'boolean' | 'date'{}表单协议配置
titlestring-用于渲染控件 label,title 为空时,label 不渲染
borderbooleantrue是否展示边框
requiredboolean | Functionfalse是否必填,支持函数表达式 (formData)=> boolean
placeholderstring-空值占位(需对应渲染控件支持 placeholder,才可生效)
disabledboolean | Functionfalse是否禁用,支持函数表达式 (formData)=> boolean
readonlyboolean | Functionfalse是否只读,支持函数表达式 (formData)=> boolean
hiddenboolean | Functionfalse是否隐藏当前选项,支持函数表达式 (formData)=> boolean
displayType'row' | 'column''row'指定 Label 与 Field 的展示关系,row 表示并排展示,column 表示两排展示
classNamestring-控件类名(用来做样式覆盖)
widgetstring-指定使用哪个组件来渲染,除内置控件外,自定义控件需要注册才可使用
enumarray-可用来生成组件的 options 的 value,例: [1,2] => [{label:1 , value:1},{label:2 , value:2}],可使用 enumNames 制定 label
enumNamesarray-可用来生成组件的 options 的 label
rulesRule | Rule[]-指定组件校验逻辑, Rule

Ref

可通过 ref 拿到组件暴露的方法

属性名类型描述
getFormData() => FormData获取表单数据(如果配置 removeHiddenData 则过滤掉 hidden 字段)
validate(scrollToError?: boolean) => Promise<ErrorMessage[]>触发整个表单校验(scrollToError 是否在提交表单且校验不通过时滚动至错误的表单项)
validateFields(fields: string[], scrollToError?: boolean) => Promise<ErrorMessage[]>校验单个字段

widgets

内置的控件

组件名称值类型描述
inputstring普通输入框,van-field 的封装
checkboxarray复选框
switchboolean切换开关
steppernumber数字输入框,van-stepper 的封装
radiostring | number单选框
pickerstring | number弹出选择器
cascaderarray弹出-级联选择器
datestring日期选择器
radioButtonstring | number单选框(按钮选项)

Rule

校验规则 基于async-validate

属性名类型默认值描述
type"string" | "number" | "boolean" | "object" | "enum" | "array" | "date" | "pattern" | "url" | "email" | "method" | "regexp" | "integer" | "float" | "hex" | "any"-校验值的类型
messagestring-自定义校验失败时对应的错误提示
requiredbooleanfalse是否必填
patternRegExp | string-正则校验
minnumber-值最小范围 ,用来校验number
maxnumber-值最大范围 ,用来校验number
lennumber-值长度 ,用来校验array|string
validator(rule: any) => boolean-自定义校验逻辑

Addon

自定义组件时,可在组件内部通过 useAddon hook 拿到当前组件的配置

属性名类型描 述
namestring表单项的 key
rootSchemaSchema根节点的 schema
placeholderstring占位符(当前节点 schema 配置时,可通过次属性拿到)
classNamestring类名
requiredboolean是否必填
propsobject当前控件的 props 注入
setFormData(newFormData) => void更新数据
getFormData() => any获取表单值

TODO

  • Ui
    • 字体大小,颜色,间距等使用 css 变量
  • docs
    • demo
    • 自定义组件
  • field
    • 增加 title 字段
    • 去除冗余 props 透传
    • Cell 替换
    • useAttrs 替代 props
  • widget
    • 从 props 中移除 addon, 使用 useAddon 来获取拓展属性
    • 使用 useRegister 来注册组件
  • schema
    • showCellTitle

Keywords

vue

FAQs

Package last updated on 23 Sep 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts