在线演示html
以前用过 React 的 Form Render 的小伙伴应该比较清楚 Form Render 能够基于 JSON Schema 快速构建出表单区块。不得不说 For Render 在如下场景中的使用会给开发带来巨大的便利:前端
可是如今咱们的场景是基于 vue 3.x
的框架基础上去使用 form render
可是 form render
目前也只支持 react
。而后我再 Google 上搜了一大圈,也没找到一个还能够的 vue 3.x
的 form render
,不过 vue 2.x
的仍是挺多的。出于这样的诉求,本身动手撸了一个。vue
vue-form-render
是基于 Form Render 基本能力做为原型实现的 Vue 3.x
版本的表单渲染器,目前支持 90% 左右的 Form Render
功能,后续会不断的维护支持。react
excel
导入数据,方便快快捷生成form Data
"listName2": { "title": "对象数组", "description": "对象数组嵌套功能", "type": "array", "minItems": 1, "maxItems": 3, "ui:displayType": "row", "items": { "type": "object", "properties": { "input1": { "title": "简单输入框", "type": "string" }, "selet1": { "title": "单选", "type": "string", "enum": [ "a", "b", "c" ], "enumNames": [ "早", "中", "晚" ] } } } }
"string": { "title": "字符串", "type": "string", "maxLength": 4, "ui:options": { "placeholder": "试着输入超过4个字符" } }
"color": { "title": "颜色选择", "type": "string", "format": "color" }
"date": { "title": "日期选择", "type": "string", "format": "date" }
"image": { "title": "图片展现", "type": "string", "format": "image" }
"allNumber": { "title": "number类", "type": "object", "properties": { "number1": { "title": "数字输入框", "description": "1 - 1000", "type": "number", "min": 1, "max": 1000 }, "number2": { "title": "带滑动条", "type": "number", "ui:widget": "slider" } } }
"allBoolean": { "title": "boolean类", "type": "object", "properties": { "radio": { "title": "是否经过", "type": "boolean" }, "switch": { "title": "开关控制", "type": "boolean", "ui:widget": "switch" } } }
"allRange": { "title": "range类", "type": "object", "properties": { "dateRange": { "title": "日期范围", "type": "range", "format": "dateTime", "ui:options": { "placeholder": [ "开始时间", "结束时间" ] } } } }
"allEnum": { "title": "选择类", "type": "object", "properties": { "select": { "title": "单选", "type": "string", "enum": [ "a", "b", "c" ], "enumNames": [ "早", "中", "晚" ] }, "radio": { "title": "单选", "type": "string", "enum": [ "a", "b", "c" ], "enumNames": [ "早", "中", "晚" ], "ui:widget": "radio" }, "multiSelect": { "title": "多选", "description": "下拉多选", "type": "array", "items": { "type": "string" }, "enum": [ "A", "B", "C", "D" ], "enumNames": [ "杭州", "武汉", "湖州", "贵阳" ], "ui:widget": "multiSelect" }, "boxes": { "title": "多选", "description": "checkbox", "type": "array", "items": { "type": "string" }, "enum": [ "A", "B", "C", "D" ], "enumNames": [ "杭州", "武汉", "湖州", "贵阳" ] } } }
"obj1": { "title": "可折叠对象", "description": "这是个对象类型", "type": "object", "ui:options": { "collapsed": true }, "properties": { "input1": { "title": "输入框1", "type": "string" }, "input2": { "title": "输入框2", "type": "string" } } }
{ "type": "object", "properties": { "content": { "title": "富文本编辑器", "type": "string", "format": "richText" } } }
ant-design-vue
import { createApp } from 'vue' import App from './App.vue' import Antd from 'ant-design-vue'; import 'ant-design-vue/dist/antd.css'; const app = createApp(App); app.use(Antd); app.mount('#app');
vue-form-render
npm i kaer-form-render --save
<template> <div> <formRender :schema="schema" :formData="formData" @on-change="change" @on-validate="validate" /> </div> </template> <script> import {reactive, toRefs} from 'vue'; // render index import FormRender from 'kaer-form-render'; // form render style import 'kaer-form-render/lib/kaer-form-render.css'; export default { name: 'App', setup() { const state = reactive({ schema: { type: 'object', properties: { string: { title: 'string', type: 'string', maxLength: 4, 'ui:options': { placeholder: 'enter more than 4 characters', }, } }, }, formData: { string: 'aaa' }, }); const change = (v) => { state.formData = v; console.log(v); } const validate = (v) => { console.log(v); } return { ...toRefs(state), change, validate, } }, components: { FormRender, } } </script>
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
schame | JSON Schema | object | -- |
formData | 表单的数据 | object | -- |
事件名 | 说明 | 回调函数 |
---|---|---|
on-change | 用户触发表单更新的回调函数 | function(value: formData) |
on-validate | 用户触发表单更新的校验回调函数 | function(value: validates) |
欢迎你们使用并pr,咱们一块儿打造一款好用的vue form render
git
github: vue form rendergithub
在线演示shell