FormCreate 是一個可以通過 JSON 生成具有動態渲染、數據收集、驗證和提交功能的表單生成組件。支持5個UI框架,並且支持生成任何 Vue 組件。內置20種常用表單組件和自定義組件,再複雜的表單都可以輕鬆搞定 FormCreate官網:https://www.form-create.com ...
FormCreate 是一個可以通過 JSON 生成具有動態渲染、數據收集、驗證和提交功能的表單生成組件。支持5個UI框架,並且支持生成任何 Vue 組件。內置20種常用表單組件和自定義組件,再複雜的表單都可以輕鬆搞定
FormCreate官網:https://www.form-create.com
幫助文檔:https://form-create.com/v3/
1. 生成內置組件
<template> <div> <form-create :rule="rule" v-model:api="fApi" :option="options" v-model="value"> </form-create> </div> </template> <script> export default { data() { return { fApi: {}, value: {}, options: { onSubmit: (formData) => { alert(JSON.stringify(formData)) }, resetBtn: true }, rule: [ {type: 'input', field: 'field1', title: 'input', value: ''}, {type: 'datePicker', field: 'field2', title: 'date', value: ''}, ] } } } </script>
2. 生成UI組件
<template> <div> <form-create :rule="rule" v-model:api="fApi" :option="options" v-model="value"> </form-create> </div> </template> <script> export default { data() { return { fApi: {}, value: {}, options: { onSubmit: (formData) => { alert(JSON.stringify(formData)) }, resetBtn: true }, rule: [ { type: 'AButton', props: { size: 'mini' }, children: ['AButton'] }, { type: 'AImage', //非表單組件不會自動生成col,自定義組件需要通過native控制是否生成col native: false, props: { width: '200px', src: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png', } }, ] } } } </script>
3. 生成自定義組件
<template> <div> <form-create :rule="rule" v-model:api="fApi" :option="options" v-model="value"> </form-create> </div> </template> <script> import {h, defineComponent, resolveComponent} from 'vue' import formCrate from '@form-create/ant-design-vue' import formCreate from "@form-create/ant-design-vue"; // import UserSelect from "./UserSelect.vue"; //自定義表單組件 const UserSelect = defineComponent({ props: { //預定義 disabled: Boolean, modelValue: Number, options: Array, }, emits: ['update:modelValue'], data: function () { return { value: this.modelValue, } }, render() { return h(resolveComponent('ASelect'), { disabled: this.disabled, value: this.value, 'onUpdate:value': this.onChange, options: this.options }); }, watch: { modelValue(n) { //同步 value 的值 this.value = n } }, methods: { onChange: function (val) { this.value = val; //更新組件內部的值 this.$emit('update:modelValue', val) }, }, }); export default { beforeCreate() { //掛載組件 formCrate.component('UserSelect', UserSelect); }, data() { return { fApi: {}, value: {}, options: { onSubmit: (formData) => { alert(JSON.stringify(formData)) }, resetBtn: true }, rule: [ { type: 'UserSelect', field: 'field1', title: '用戶選擇', value: '小王', props: { options: [ {label: '小王', value: '小王'}, {label: '小李', value: '小李'}, ] }, }, ] } } } </script>
4. 通過插槽生成組件
<template> <div> <form-create :rule="rule" v-model:api="fApi" :option="options" v-model="value"> </form-create> </div> </template> <script> export default { data() { return { fApi: {}, value: {}, options: { onSubmit: (formData) => { alert(JSON.stringify(formData)) }, resetBtn: true }, rule: [ { type: 'div', style: 'width:200px;height:200x;background-color:#FF7271;', children: [ { type: 'span', style: 'color:#FFFFFF;', children: ['FormCreate'] } ] }, ] } } } </script>
5. 生成HTML標簽
<template> <div> <form-create :rule="rule" v-model:api="fApi" :option="options" v-model="value"> <template #type-UserSelect="scope"> <ASelect :value="scope.model.value" v-bind="scope.prop" @update:value="scope.model.callback"> </ASelect> </template> </form-create> </div> </template> <script> export default { data() { return { fApi: {}, value: {}, options: { onSubmit: (formData) => { alert(JSON.stringify(formData)) }, resetBtn: true }, rule: [ { type: 'UserSelect', field: 'field1', title: '用戶選擇', value: '小王', props: { options: [ {label: '小王', value: '小王'}, {label: '小李', value: '小李'}, ] }, }, ] } } } </script>