form-create 是一個可以通過 JSON 生成具有動態渲染、數據收集、驗證和提交功能的表單生成組件。支持3個UI框架,並且支持生成任何 Vue 組件。內置20種常用表單組件和自定義組件,再復雜的表單都可以輕松搞定。
支持 UI
- element-ui
- iview/view-design
- ant-design-vue
新增功能
2.5版本主要更新了一下功能:
- 重構內部核心代碼
- 優化內部渲染機制
- 優化內部生命周期事件
- 重構
TypeScript
- 新增
nextTick
方法,設置渲染后的回調 - 新增 支持分頁加載組件,加速首屏渲染
- 新增 自定義配置項
effect
- 新增 支持修改
type
- 新增
control
支持配置規則插入位置 - 優化
control
符合條件的都會生效,之前版本只能生效第一個 - 新增 支持給組件配置前綴后綴
prefix
,suffix
- 新增
update
配置,value
發送變化后觸發 - 新增 支持
wrap
配置項,配置FormItem
- 新增
object
組件 - 新增 支持自定義
title
,info
組件 - 新增 富文本組件
wangEditor
- 新增 原生事件支持事件注入
- 支持
value.sync
獲取雙向綁定的formData
- 優化 默認的表單提交按鈕
安裝
根據自己使用的 UI 安裝對應的版本
element-ui
版本
npm i @form-create/element-ui
iview@2.x|3.x
版本
npm i @form-create/iview
iview/view-design@4.x
版本
npm i @form-create/iview4
ant-design-vue@1.5+
版本
npm i @form-create/ant-design-vue
快速上手
本文以
element-ui
為例
- 在 main.js 中寫入以下內容:
import Vue from 'vue'
import ELEMENT from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import formCreate from '@form-create/element-ui'
Vue.use(ELEMENT)
Vue.use(formCreate)
- 生成表單
<template>
<div id="app1">
<form-create v-model="fApi" :rule="rule" :option="option" :value.sync="value"></form-create>
</div>
</template>
export default {
data() {
return {
//實例對象
fApi: {},
//表單數據
value: {},
//表單生成規則
rule: [
{
type: 'input',
field: 'goods_name',
title: '商品名稱'
},
{
type: 'datePicker',
field: 'created_at',
title: '創建時間'
}
],
//組件參數配置
option: {
//表單提交事件
onSubmit: function (formData) {
alert(JSON.stringify(formData))
}
}
}
}
}
功能介紹
1. 自定義組件
form-create 支持的在表單內部生成任何 vue 組件
例如生成一個el-button
組件:
{
//type 可以是內置組件名稱,也可以是vue組件名稱或者 html 標簽
type: 'el-button',
//...
children: ['按鈕內容']
}
生成一個 html 標簽
{
//type 可以是內置組件名稱,也可以是vue組件名稱或者 html 標簽
type: 'span',
//...
children: ['span內容']
}
注意! 生成的組件必須掛載到全局或者通過 form-create 掛載
通過 Vue 掛載
Vue.component(component.name, component);
通過 form-create 掛載
formCreate.component(component.name, component);
2. 自定義布局
通過設置配置項col
或者柵格組件可以實現對組件的布局
通過配置項col
設置組件布局,設置一行顯示兩個組件
[
{
type:'input',
field:'input1',
title:'input1',
col:{span:12}
},{
type:'input',
field:'input2',
title:'input2',
col:{span:12}
},
]
通過柵格組件設置一行顯示三個組件
{
type:'el-row',
children: [
{
type:'el-col',
props:{
span:8
},
children: [{type:'input',field:'input1',title:'input1'}]
},
{
type:'el-col',
props:{
span:8
},
children: [{type:'input',field:'input1',title:'input1'}]
},
{
type:'el-col',
props:{
span:8
},
children: [{type:'input',field:'input1',title:'input1'}]
}
]
}
3. 組件前后綴
通過生成規則的prefix屬性配置組件的前綴,suffix屬性配置組件的后綴
{
type:'input',
field:'text',
title:'text',
prefix:'prefix',
suffix:'suffix',
}
設置前后綴為自定義組件
{
type:'input',
field:'text',
title:'text',
value:'default',
prefix:{
type:'ElButton', children:['prefix']
},
suffix:{
type:'ElButton', children:['suffix']
},
}
4.組件聯動
可以通過control配置項實現通過組件的值控制其他組件是否顯示
例如當評價小於3星時輸入差評原因
{
type:'rate',
field: 'star',
title:'評分',
value:5,
control:[
{
handle(val){
return val < 3
},
rule:[
{
type:'input',
field:'info',
title:'差評原因',
value:'default info',
}
]
}
]
}
參數 | 說明 | 類型 |
---|---|---|
value | 當組件的值和value 全等時顯示rule 中的組件 |
string|Number|Bool |
handle | 當handle 方法返回true 時顯示rule 中的組件 |
Function |
rule | 該組件控制顯示的組件 | Array |
append | 設置rule 中的規則追加的位置 |
string |
prepend | 設置rule 中的規則前置插入的位置 |
string |
child | 設置rule 是否插入到指定位置的children中,默認添加到當前規則的 children 中 |
Boolean |
注意! handle
優先級大於value
,所有符合條件的control
都會生效
5. 表單驗證
可以通過 validate 配置項設置組件的驗證規則,自定義的表單組件也支持校驗
例如設置input 組件必填
{
type:'input',
//...
validate:[{required:true, type:'string', message:'請個輸入內容'}]
}
參數 | 說明 | 類型 | 默認值 |
---|---|---|---|
enum | 枚舉類型 | string | - |
len | 字段長度 | number | - |
max | 最大長度 | number | - |
message | 校驗文案 | string | - |
min | 最小長度 | number | - |
pattern | 正則表達式校驗 | RegExp | - |
required | 是否必選 | boolean | false |
transform | 校驗前轉換字段值 | function(value) => transformedValue:any | - |
type | 內建校驗類型,可選項 | string | 'string' |
validator | 自定義校驗 | function(rule, value, callback) | - |
whitespace | 必選時,空格是否會被視為錯誤 | boolean | false |
注意!type
需要根據組件的value
類型定義
APi 介紹
下列是常用的方法
設置表單值
覆蓋方式,未定義的字段會設置為 null
type coverValue = (formData:{[field:string]:any}) => void
- 用法:
fApi.coverValue({goods_name:'HuaWei'})
合並方式,未定義的字段不做修改
interface setValue {
(formData:{[field:string]:any}): void
(field:string, value:any): void
}
- 用法:
fApi.setValue({goods_name:'HuaWei'})
別名方法changeValue
, changeField
隱藏字段
interface hidden {
//隱藏全部組件
(status:Boolean):void
//隱藏指定組件
(status:Boolean, field:string):void
//隱藏部分組件
(status:Boolean, field:string[]):void
}
- 用法:
fApi.hidden(true, 'goods_name')
獲取組件隱藏狀態
type hiddenStatus = (field:string)=>Boolean
- 用法:
const status = fApi.hiddenStatus('goods_name')
獲取規則
interface getRule {
(field:string):Rule
(field:string, origin: true): FormRule
}
- 用法:
const rule = fApi.getRule('goods_name')
插入規則
前置插入
interface prepend {
//插入到第一個
(rule:FormRule):void
//插入指定字段前面
(rule:FormRule, field:string):void
//插入到指定字段 children 中
(rule:FormRule, field:string, child:true):void
}
- 用法:
fApi.prepend({
type:"input",
title:"商品簡介",
field:"goods_info",
value:"",
props: {
"type": "text",
"placeholder": "請輸入商品簡介",
},
validate:[
{ required: true, message: '請輸入商品簡介', trigger: 'blur' },
],
}, 'goods-name')
后置追加
interface append {
//插入到最后一個
(rule:FormRule):void
//插入指定字段后面
(rule:FormRule, field:string):void
//插入到指定字段 children 中
(rule:FormRule, field:string, child:true):void
}
- 用法:
fApi.append({
type:"input",
title:"商品簡介",
field:"goods_info",
value:"",
props: {
"type": "text",
"placeholder": "請輸入商品簡介",
},
validate:[
{ required: true, message: '請輸入商品簡介', trigger: 'blur' },
],
}, 'goods-name')
刪除指定規則
type removeRule = (rule:FormRule) => FormRule
- 用法:
const rule = {type:'input', /** ... **/}
fApi.removeRule(rule)
驗證表單
type validate = (callback:(...args:any[]) => void)=> void
- 用法:
fApi.validate((valid, fail) => {
if(valid){
//todo 表單驗證通過
}else{
//todo 表單驗證未通過
}
})
驗證指定字段
type validateField = (field, callback:(...args:any[]) => void)=> void
- 用法:
fApi.validateField('goods_name', (valid, fail) => {
if(valid){
//todo 字段驗證通過
}else{
//todo 字段驗證未通過
}
})
獲取表單數據
interface formData {
//獲取全部數據
(): {[field:string]:any }
//獲取部分字段的數據
(field:string[]): {[field:string]:any }
}
- 用法:
const formData = fApi.formData()
修改提交按鈕
type submitBtnProps = (props:Object) => void
- 用法:
fApi.submitBtnProps({disabled:true})
-
快捷操作:
fApi.btn.loading(true)
設置提交按鈕進入loading狀態fApi.btn.disabled(true)
設置提交按鈕禁用狀態fApi.btn.show(true)
設置提交按鈕顯示狀態
修改重置按鈕
type resetBtnProps = ( props:Object) => void
- 用法:
fApi.resetBtnProps({disabled:true})
-
快捷操作:
fApi.resetBtn.loading(true)
設置重置按鈕進入loading狀態fApi.resetBtn.disabled(true)
設置重置按鈕禁用狀態fApi.resetBtn.show(true)
設置重置按鈕顯示狀態
隱藏表單
type hideForm = (hide:Boolean)=>void
- 用法:
fApi.hideForm(true)
提交表單
type submit = (success: (formData: FormData, $f: fApi) => void, fail: ($f: fApi) => void)=> void
- 用法:
fApi.submit((formData, fapi) => {
//todo 提交表單
},()=>{
//todo 表單驗證未通過
})
如果對您有幫助,您可以在GitHub上點 'Star' 支持一下 謝謝!