使用form-create生成表單元件

xaboy發表於2024-06-03

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>

相關文章