0.6.8 • Published 5 months ago

element-form-render v0.6.8

Weekly downloads
-
License
ISC
Repository
-
Last release
5 months ago

Introduction

Create Vue Form by JSON (for ElementPlus + TypeScript + Vue3)

Install

npm i element-form-render

Usage

  1. Add below code to main.ts to initialize axios params which used in form render
import { setAxiosConfig, setConfig, setAxios } from "element-form-render";

// set your axios instance that must be implements 'RequestInterface' interface if you don't want to use the buildin request library
setAxios(request);
// It you want to use the buildin request lib,
// you should config the axios parameters like bellow
setAxiosConfig({
  timeout: 50000,
  baseURL: "https://domain.com", //
  headers: { // set headers when you need
    "Authorization": "token",
  }
});
setConfig({
  static_url: "https://static.domain.com", // Static resource host domain
  base_url: "https://api.domain.com", // Api data, File Upload domain
});
  1. Follow the JSON struct rules to create form
<template>
  <div>
    <form-render :json="json" @on-submit="handleSubmit" />
  </div>
</template>
<script lang="ts" setup>
  import { type FormStructure } from 'element-form-render'
  import { ref } from 'vue'

  const json = ref<FormStructure>({
    type: 'create', // create OR update OR customer
    config: {
      cols: 3 // Cols number, Default is 1
    },
    title: 'title', // Form title, Leave empty to hide title
    api: '/form/create',// Leave empty that can use @on-submit to catch form data
    elements: [
      // Element layout will follow the config.cols with fill mode
      {
        type: 'input',
        name: 'title',
        label: 'Site Name',
        validator: ['required']
      },
      {
        type: 'input',
        name: 'website',
        label: 'Site URL',
        width: '300px',
        prefix: 'https://www',
        suffix: '.com',
        validator: ['required']
      },
      {
        type: 'radio',
        name: 'type',
        label: 'Type',
        source: {
          type: 'static',
          options: [
            { label: 'Portal', value: 'portal' },
            { label: 'Software', value: 'source' }
          ]
        }
      },
      {
        type: 'checkbox',
        name: 'tags',
        label: 'Tags',
        source: {
          type: 'ajax',
          options: {
            api: '/api/dict',
            params: { section: 'site_tag' },
            labelField: 'name',
            valueField: 'id'
          }
        }
      }
    ]
  })

  // 当表单 api 不设置时 定义获取数据的函数
  function handleSubmit(data: Record<string, any>) {
    console.log(data)
  }
</script>
  1. FormStructure definition
type FormStructure = {
  type: 'create' | 'update' | 'customer',// If type is update, the form will load data automatily
  title: string,
  elements: FormElementField[], // Form Elements
  api?: string,// form action
  primary_key?: string, // Primary key name
  primary_value?: string | number,// primary key valye
  axios?: RequestInterface,// Overwrite the buildin request instance
  base_url?: string,// Overwrite global config: base_url
  static_url?: string,// Overwrite global config: static_url
  config?: { // Form config
    size?: '' | 'default' | 'large' | 'small',
    gutter?: number,
    cols?: number,
    labelWidth?: number,
    labelPosition?: 'left' | 'right' | 'top',
    showSubmit?: boolean,
    submitText?: string,
    showReset?: boolean,
    resetText?: string,
  },
}

API

  1. 属性
属性类型备注
modelValueRecord<string, any>formData
jsonFormStructure参见类型定义
:beforeSubmit() => Promise | boolean提交前回调,函数返回false阻止提交
:afterValidate() => Promise | boolean表单验证后回调,返回false阻止提交,在beforeSubmit后执行
  1. 事件方法
名称类型备注
@on-data(data:Record<string, any>) => void当编辑数据加载完毕时触发
@on-submit(data:Record<string, any>) => void仅当未设置json.api时,点击提交按钮时触发,在beforeSubmit和afterValidate之后
@after-submit(data:Record<string, any>,res:any) => void仅当设置了json.api时,提交接口响应后触发
@update:modelValue(data:Record<string, any>) => void表单数据变化时触发
@on-cancel() => void重设表单点击事件
  1. 实例方法
名称类型备注
doSubmit() => Promise手动提交数据,适用于不显示提交按钮的场景
resetForm() => void清空表单数据
  1. 支持的字段类型,详细配置见类型声明文件
类型标识类型备注是否实现后台接收处理说明
input小输入框inputtype="text"yes
password密码框password fieldyes
number数字输入框number fieldyes
select单选框select fieldyes
radio单选按钮组radioyes
checkbox多选按钮组checkboxyes
switch开关开是1 关 0yes
color颜色选择器16进制颜色yes'#ffffff'
date日期选择YYYY-MM-DDyes
date_range日期范围选择YYYY-MM-DDyes
year年份选择YYYYyes
month月份选择YYYY-MMyes
datetime日期时间选择YYYY-MM-DD HH:mmyes
time时间选择HH:mm:ssyes
textarea多行纯文本textareayes
rich_text富文本richtext web editoryes
image图片上传single imageyes图片地址字符串
images图片上传multiple imagesyes图片地址数组
file附件上传single fileyes文件地址
files附件上传multiple filesyes文件地址数组
cascader联动选择cascaderyes
tree_select树状单选tree_selectyes数据项配置参考select radio checkbox
empty占位空元素yes空白占位
split表单分组标题yes横线加标题 分割表单字段
slotslot组件yesname 指定 slot 名称后,<template #name>自定义内容
  1. Supported validators
type Validators =
  | 'required'
  | 'string'
  | 'integer'
  | 'float'
  | 'alpha'
  | 'number'
  | 'upper'
  | 'lower'
  | 'alphaNum'
  | 'alphaDash'
  | 'url'
  | 'email'
  | 'mobile' // China mobile phone number rule
  | 'idcard' // China idcard rule
  | 'age'
  | 'date'
  | 'datetime'
  | 'phone' // 座机号码
  | 'telephone' // 国内固话+国内手机号(不含400 800电话)
  | 'license_code' // 企业统一信用代码
  | 'bank_code' // 银行账号 (非严谨)
  | 'amount' // Amount
  | 'chinese' // Chinese
  | `length:${number}` // static length
  | `length:${number},${number}` // length range
  | `regexp:${string}` // RegExp
  | `between:${number},${number}` // Number range
  | `gt:${number}` // 大于
  | `>:${number}` // 大于
  | `lt:${number}` // 小于
  | `<:${number}` // 小于
  | `gte:${number}` // 大于等于
  | `>=:${number}` // 大于等于
  | `lte:${number}` // 小于等于
  | `<=:${number}` // 小于等于
  | Required<FormItemRule>['validator'] // 自定义验证函数
0.6.8

5 months ago

0.6.7

5 months ago

0.6.6

7 months ago

0.6.3

7 months ago

0.6.5

7 months ago

0.6.4

7 months ago

0.5.21

8 months ago

0.5.20

8 months ago

0.6.2

8 months ago

0.6.1

8 months ago

0.6.0-rc.0

8 months ago

0.6.0-rc.1

8 months ago

0.6.0

8 months ago

0.5.19

9 months ago

0.5.18

9 months ago

0.5.17

9 months ago

0.5.16

9 months ago

0.5.14

10 months ago

0.5.15

10 months ago

0.5.13

11 months ago

0.5.11

11 months ago

0.5.12

11 months ago

0.5.10

12 months ago

0.5.8

12 months ago

0.5.4

1 year ago

0.5.3

1 year ago

0.5.6

1 year ago

0.5.5

1 year ago

0.5.2

1 year ago

0.5.1

1 year ago

0.5.7

12 months ago

0.5.0

1 year ago

0.4.6

1 year ago

0.4.5

1 year ago

0.4.4

1 year ago

0.4.3

1 year ago

0.4.1

1 year ago

0.4.2

1 year ago

0.4.0

2 years ago

0.3.6

2 years ago

0.3.5

2 years ago

0.3.8

2 years ago

0.3.7

2 years ago

0.3.4

2 years ago

0.3.9

2 years ago

0.3.15

2 years ago

0.3.14

2 years ago

0.3.13

2 years ago

0.3.12

2 years ago

0.3.11

2 years ago

0.3.10

2 years ago

0.3.0

2 years ago

0.3.2

2 years ago

0.3.1

2 years ago

0.3.3

2 years ago

0.2.7

2 years ago

0.2.6

2 years ago

0.2.8

2 years ago

0.2.5

2 years ago

0.2.4

2 years ago

0.2.3

2 years ago

0.2.2

2 years ago

0.1.33

2 years ago

0.1.34

2 years ago

0.1.35

2 years ago

0.1.36

2 years ago

0.1.37

2 years ago

0.2.1

2 years ago

0.2.0

2 years ago

0.1.38

2 years ago

0.1.39

2 years ago

0.1.30

2 years ago

0.1.31

2 years ago

0.1.32

2 years ago

0.1.15

2 years ago

0.1.27

2 years ago

0.1.28

2 years ago

0.1.29

2 years ago

0.1.20

2 years ago

0.1.21

2 years ago

0.1.23

2 years ago

0.1.24

2 years ago

0.1.25

2 years ago

0.1.26

2 years ago

0.1.16

2 years ago

0.1.17

2 years ago

0.1.18

2 years ago

0.1.19

2 years ago

0.1.14

2 years ago

0.1.13

2 years ago

0.1.12

2 years ago

0.1.11

2 years ago

0.1.10

2 years ago

0.1.9

2 years ago

0.1.8

2 years ago

0.1.7

2 years ago

0.1.6

2 years ago

0.1.5

2 years ago

0.1.4

2 years ago

0.1.3

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago

0.0.2

2 years ago