在線演示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