0.5.0 • Published 13 days ago

element-form-render v0.5.0

Weekly downloads
-
License
ISC
Repository
-
Last release
13 days ago

简介

写JSON创建form (适用于ElementPlus + TypeScript + Vue3)

安装

npm i element-form-render

使用

  1. 首先在您的项目全局的 main.ts 中设置表单生成器axios的baseURL(主要是避免每个页面都设置)
import {setAxiosConfig} from "element-form-render/src";
setAxiosConfig({
    timeout: 50000,
    baseURL: "https://domain.com", // 如果想在表单组件使用相对URL,需要设置 baseURL
    headers: { // 根据自己的需求设置headers,如无特殊需求可不设置
        "Authorization": "token",
        "Content-Type": "application/json"
    }
});
  1. 通过本组件约定的结构定义编写JSON结构,创建表单
<template>
  <div>
    <form-render :json="json" @on-submit="handleSubmit" />
  </div>
</template>
<script setup lang="ts">
// 您可以从"element-form-render/src" 中引入本组件所有的组件、类型、类库和函数
import {FormRender} from "element-form-render/src";
import type {FormStructure} from "element-form-render/src/types";
import {reactive} from "vue";
const json = reactive<FormStructure>({
    type: "create",
    name: "test",
    title: "title", //设置为空则不显示标题
    api: "/api/form/create",// 不设置的话可以使用 @on-submit 获取表单提交的数据
    elements : [// 二维数组进行布局,第一维是行 每一行内可以放置多个组件(注意:使用了Element的24等分格,注意个数适配)
        [
            {
              type:"input",
              name:"title",
              label:"网站名称",
              validator:["required"]
            },
        ],
        [
            {
                type:"input",
                name:"website",
                label:"网站网址",
                width:"300px",
                prefix:"https://www",
                suffix:".com",
                validator:["required"]
            }
       ],
       [
            {
              type:"radio",
              name:"type",
              label:"网站类型",
              source:{
                type:"static",
                 options:[
                     {label:"门户站", value:"portal"},
                     {label:"资源站", value:"source"}
                 ]
              }
            },
            {
                type: "checkbox",
                name: "tags",
                label: "网站类型",
                source: {
                    type: "ajax",
                    options: {
                      api: "/api/dict",
                      params: {section: "site_tag"},
                      labelField: "name",
                      valueField: "id"
                    }
                }
            },
        ]
    ]
});

// 当表单 api 不设置时 定义获取数据的函数
const handleSubmit = (data) => {
    console.log(data);
}
</script>
<style scoped>
</style>
  1. FormStructure 类型定义
export type FormStructure = {
    type: 'create' | 'update' | 'customer',// 当值为”update“时,表单将会自动请求api获取需要修改的数据
    name: string,
    title: string,
    elements: FormElementField[][], // 字段布局
    api?: string,// 表单数据提交URL
    primary_key?: string, // 修改数据时数据主键名
    primary_value?: string|number,// 修改数据时数据主键值
    config?: { // 表单设置项
        labelWidth?: number,
        labelPosition?: 'left' | 'right' | 'top',
        showSubmit?: boolean,
        submitText?: string,
        showReset?: boolean,
        resetText?: string,
    },
    dynamicData?: Record<string, any>,// 当使用自定义字段组件时,可以通过该对象写数据
    appendData?: Record<string, any>
}

API

  1. 属性
属性类型备注
jsonFormStructure参见类型定义
  1. 事件方法
名称参数返回值备注
@on-submitdata:Record<string, any>void提交回调(仅未设置API时可用)
:before-submitdata:Record<string, any>data:Record<string, any> | boolean提交前回调,可修改,函数返回后提交至api ,返回false 阻止提交
@on-datadata:Record<string, any>void修改表单从api载入的数据,一般供slot组件初始化用
@on-cancelvoid重设表单点击事件
  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{name:'原文件名.docx',url:'/uploads/xxxxx/xxxxxx.docx'}
files附件上传multiple filesyes同上对象组成的数组
cascader联动选择cascaderyes
tree_select树状单选tree_selectyes数据项配置参考select radio checkbox
  1. 支持的validator
require
required
string
integer
float
alpha
alphaNum
alphaDash
number
upper
lower
url
email
mobile (中国国内手机号码)
phone (座机号码)
telephone 国内固话+国内手机号(不含400 800电话)
bank_code 银行账号 (非严谨)
license_code 企业统一信用代码
chinese 汉字
amount 金额
idcard(CHN)
date
datetime
time
length:36
length:1:10
max:100
min:100
between:1:10
gt:1
gte:2
lt:2
lte:2
regexp:^\d{6}$
0.5.0

13 days ago

0.4.6

1 month ago

0.4.5

1 month ago

0.4.4

1 month ago

0.4.3

2 months ago

0.4.1

4 months ago

0.4.2

4 months ago

0.4.0

4 months ago

0.3.6

7 months ago

0.3.5

7 months ago

0.3.8

7 months ago

0.3.7

7 months ago

0.3.4

7 months ago

0.3.9

7 months ago

0.3.15

6 months ago

0.3.14

6 months ago

0.3.13

6 months ago

0.3.12

6 months ago

0.3.11

6 months ago

0.3.10

7 months ago

0.3.0

7 months ago

0.3.2

7 months ago

0.3.1

7 months ago

0.3.3

7 months ago

0.2.7

7 months ago

0.2.6

7 months ago

0.2.8

7 months ago

0.2.5

7 months ago

0.2.4

8 months ago

0.2.3

8 months ago

0.2.2

8 months ago

0.1.33

9 months ago

0.1.34

9 months ago

0.1.35

8 months ago

0.1.36

8 months ago

0.1.37

8 months ago

0.2.1

8 months ago

0.2.0

8 months ago

0.1.38

8 months ago

0.1.39

8 months ago

0.1.30

10 months ago

0.1.31

10 months ago

0.1.32

10 months ago

0.1.15

11 months ago

0.1.27

10 months ago

0.1.28

10 months ago

0.1.29

10 months ago

0.1.20

10 months ago

0.1.21

10 months ago

0.1.23

10 months ago

0.1.24

10 months ago

0.1.25

10 months ago

0.1.26

10 months ago

0.1.16

11 months ago

0.1.17

11 months ago

0.1.18

11 months ago

0.1.19

11 months ago

0.1.14

11 months ago

0.1.13

11 months ago

0.1.12

11 months ago

0.1.11

11 months ago

0.1.10

11 months ago

0.1.9

11 months ago

0.1.8

11 months ago

0.1.7

11 months ago

0.1.6

11 months ago

0.1.5

12 months ago

0.1.4

12 months ago

0.1.3

12 months ago

0.1.2

12 months ago

0.1.1

12 months ago

0.0.2

12 months ago