1.4.41 • Published 2 years ago

@xuanmo/v-form v1.4.41

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

基于 Vant-UI 表单封装的动态表单组件

  • 目前已经集成的组件(Address/Checkbox/DatePicker/Input/Radio/Select/Text/Switch/Upload)
  • 组件不满足的情况可自定义开发组件或者使用 slot 的形式
  • 组件的调用方式采取 JSON 配置的形式,具体参数见model数据说明
  • 校验规则已经集成 VeeValidate 插件,也可以自定义扩展规则,更多资料 https://logaretm.github.io/vee-validate
  • 在线演示,可编辑

语法约定

  • Mixin 公用方法使用 __ 作为前缀
  • 事件传递使用 e__ 作为前缀
  • @ 为组件校验规则保留关键字

本地运行本项目

# 安装依赖
$ npm run bootstarp

# 启动项目
$ npm run dev

安装

# 推荐使用 yarn 安装,使用 npm 可能会存在依赖不全的情况
yarn add @xuanmo/v-form

使用

引入注册组件

// 引入组件
import VForm from '@xuanmo/v-form'
import '@xuanmo/v-form/packages/style/index.less'

// 不经过编译引入方式,(不推荐)
// import VForm from '@xuanmo/v-form/dist/v-form.umd.min.js'
// import '@xuanmo/v-form/dist/index.css'

// 覆盖变量引入此文件替换变量即可,可参考 example/App.vue
// import '@xuanmo/v-form/packages/style/var.less'

// 注册组件
// 更多参数说明:https://github.com/D-xuanmo/v-form/blob/master/src/index.js#L6-L14
/**
 * 设置地址选择组件数据 JSON,组件默认不注册数据
 * 如果需要自定义数据,参考此文件结构即可
 */
import ADDRESS_JSON from '@xuanmo/v-form/packages/Address/data.json'
Vue.use(VForm, {
  addressJSON: ADDRESS_JSON
})

// 设置防抖时间,默认200ms
Vue.use(VForm, {
  debounceTime: 200
})

修改打包配置(注:如果组件引入采取的后编译需要配置这一项)

// vue.config.js
module.exports = {
  transpileDependencies: [
    '@xuanmo/v-form'
  ]
}

修改数据模型配置

Vue.use(VForm, {
  primaryData: true
})

// 默认的数据模型
const model1 = [
  {
    key: 'text1',
    value: '',
    rules: {
      label: '文字1',
      type: 'VInput',
      vRules: 'required|custom:@text2,@text3',
      placeholder: '请输入文字',
      errMsg: '请输入文字'
    }
  }
]

// primaryData 为 true 时的数据模型
const model2 = [
  {
    key: 'text1',
    value: '',
    label: '文字1',
    type: 'VInput',
    vRules: 'required|custom:@text2,@text3',
    placeholder: '请输入文字',
    errMsg: '请输入文字'
  }
]

注册全局自定义校验规则

/**
 * 自定义校验规则示例,规则遵循VeeValidate规则扩展
 * 更多资料查看:https://logaretm.github.io/vee-validate/guide/basics.html#validation-provider
 */
Vue.use(VForm, {
  validator: {
    custom: {
      // 此处定义的值可以在`validate`函数的第二个参数接收
      params: ['length'],
      message: '长度不能大于{length}',
      validate: (value, { length }) => {
        return value.length <= length
      }
    },

    // 关联校验,可将多个表单项的值做比对
    target: {
      params: ['target1', 'target2'],
      message: '关联校验失败',
      // validate 第三个参数为正在执行校验相关联的组件实例
      validate: (value, { target1, target2 }, ctx) => {
        return value === target1 && value === target2
      }
    }
  }
})

HTML

<v-form v-model="value" :model="model"></v-form>

可用组件(所有的组件的属性都继承自Vant-UI属性,不包含上传文件组件)

组件名描述
VAddress地址选择器
VCheckbox复选框
VRadio单选框
VInput输入框
VNumberKeyboard数字输入框
VVerificationCode短信验证码
VDatePicker时间选择器
VDatePickerRange时间区间选择器
VSelect下拉选择框
VSwitch开关按钮
VText纯文字展示
VUpload文件上传

关于自定义组件的使用(目前仅支持将该组件注册为全局组件使用)

在组件不能满足当前业务的需求时,可以使用 slot 或者自定义组件实现,slot 使用参考后续介绍

制作组件,具体实现可参考 example/components/FormItemTest.vue

// 导入公用组件的 Mixin
import { vFormItemBaseMixin } from '@xuanmo/v-form'
export default {
  name: 'FormItemTest',

  // 使用 Mixin
  mixins: [vFormItemBaseMixin],

  methods: {
    input(value) {
      // 此方法必须调用,否则组件将不能接收数据
      // 每次数据发生改变需要使用 e__input 方法对组件的数据进行上报
      this.e__input(value)

      // 如果需要发送自定义事件,可使用下边方法
      this.__eventHandler('input', value)
    }
  }
}

全局注册组件

import Vue from 'vue'
import FormItemTest from 'path'
Vue.component(FormItemTest.name, FormItemTest)

使用组件

const model = [
  {
    key: 'test',
    value: '',
    rules: {
      label: '自定义组件',
      // 传入组件名即可
      type: 'FormItemTest',
      placeholder: '点击输入',
      vRules: 'required',
      pattern: /^\d+$/,
      errorMsg: '自定义组件错误信息'
    }
  }
]

Attributes

字段名说明类型默认值
v-model(value)获取组件处理完成的数据object{}
model数据模型(具体类型参考后续文档)array[]
disabled是否禁用表单booleanfalse
label-widthlabel宽度string20%
label-positionlabel对齐方式,可选:left/rightstringleft
label-colorlabel文字颜色string-
show-label是否显示labelbooleantrue
validator局部校验规则object{}

Methods

方法说明参数
validate对整个表单执行校验(callback: Function, ErrorList: []) => void
toggleFormItemVisible切换表单单元的显示与隐藏状态(key: string, visible: boolean) => void
setModelItemOptions设置表单options,目前支持的组件:VAddress、VCheckbox、VRadio、VSelect(key, data)接受两个参数,1. 数据key,2. data为一个数组或者 () => <Promise>[]

Events

事件名说明回调参数
change数据更改时触发object{value,errorMsg,isValid}
event数据发生改变所发送的事件object{event,formModel}

Slots

组件可接受多个slot,用于替换当前行的表单组件,会为该slot传入该组件的原始数据,每个slot的name为当前行的key,注:该slot不继承所有校验规则

<!-- 示例如下 -->
<v-form :model="model">
  <template v-slot:text="{ data }">
    <van-field v-model="data.value"></van-field>
  </template>

<template #text-label> 自定义label

<template #text-extra> extra

## model数据格式
> 以下是一个简单的数据格式,生成一个输入框,详细使用见目录example

```js
const model = [
  // 以下三个文字输入示例为关联校验
  // 关联校验采取{rule}:@{field},@{field}格式
  // 接收字段采取@{rule}格式
  {
    key: 'text1',
    value: '',
    rules: {
      label: '文字1',
      type: 'VInput',
      vRules: 'required|custom:@text2,@text3',
      placeholder: '请输入文字',
      errMsg: '请输入文字'
    }
  },

  {
    key: 'text2',
    value: '',
    rules: {
      label: '文字2',
      type: 'VInput',
      vRules: 'required|@custom',
      placeholder: '请输入文字',
      errMsg: '请输入文字'
    }
  },

  {
    key: 'text3',
    value: '',
    rules: {
      label: '文字3',
      type: 'VInput',
      vRules: 'required|@custom',
      placeholder: '请输入文字',
      errMsg: '请输入文字'
    }
  },

  // 时间选择器
  {
    key: 'date',
    value: Date.now(),
    rules: {
      label: '时间',
      // 共4种类型:datetime、year-month、date、time
      type: 'VDatePicker|datetime',
      // 数据格式处理:timestamp时间戳,其他用法参考:https://github.com/D-xuanmo/datejs
      valueFormat: 'yyyy-MM-dd'
    }
  },

  // 图片上传
  {
    key: 'file',
    // 用于显示列表
    value: [{ path: 'https://upyun.xuanmo.xin/test/20200418225229.png' }],
    rules: {
      label: '文件上传',
      type: 'VUpload',
      action: 'xxx',
      accept: 'image/png',
      multiple: true,
      name: 'file',
      headers: {},
      // 上传附加的数据
      data: {
        dir: 'test'
      },
      // 自定义配置项,用于指定url字段为某个属性值
      props: {
        url: 'path'
      }
    }
  }
]

组件 change 事件返回的数据

字段名说明
value所有的数据经过处理后会以一个对象存放在这个字段
errorMsg所有的校验失败的错误信息集合
isValid是否有通过所有的校验标识
{
  "value": {},
  "errorMsg": [],
  "isValid": false
}

示例图片

示例

1.4.39

2 years ago

1.4.38

2 years ago

1.4.40

2 years ago

1.4.41

2 years ago

1.4.33

3 years ago

1.4.32

3 years ago

1.4.35

2 years ago

1.4.34

3 years ago

1.4.37

2 years ago

1.4.36

2 years ago

1.4.31

3 years ago

1.4.30

3 years ago

1.4.29

3 years ago

1.4.28

3 years ago

1.4.27

3 years ago

1.4.26

3 years ago

1.4.25

3 years ago

1.4.24

3 years ago

1.4.20

3 years ago

1.4.22

3 years ago

1.4.21

3 years ago

1.4.23

3 years ago

1.4.19

3 years ago

1.4.18

3 years ago

1.4.11

3 years ago

1.4.10

3 years ago

1.4.13

3 years ago

1.4.12

3 years ago

1.4.15

3 years ago

1.4.14

3 years ago

1.4.17

3 years ago

1.4.16

3 years ago

1.4.6

3 years ago

1.4.5

3 years ago

1.4.4

3 years ago

1.4.9

3 years ago

1.4.8

3 years ago

1.4.7

3 years ago

1.4.3

3 years ago

1.4.2

3 years ago

1.4.1

3 years ago

1.4.0

3 years ago

1.3.9

3 years ago

1.3.8

3 years ago

1.3.7

3 years ago

1.3.6

3 years ago

1.3.5

3 years ago

1.3.4

3 years ago

1.3.3

3 years ago

1.3.2

3 years ago

1.2.0

3 years ago

1.2.8

3 years ago

1.2.7

3 years ago

1.2.6

3 years ago

1.2.5

3 years ago

1.2.4

3 years ago

1.2.3

3 years ago

1.2.2

3 years ago

1.2.1

3 years ago

1.2.12

3 years ago

1.2.13

3 years ago

1.2.10

3 years ago

1.2.11

3 years ago

1.2.16

3 years ago

1.2.17

3 years ago

1.2.14

3 years ago

1.2.15

3 years ago

1.3.1

3 years ago

1.3.0

3 years ago

1.2.9

3 years ago

1.1.4

4 years ago

1.1.3

4 years ago

1.1.2

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.61

4 years ago

1.0.60

4 years ago

1.0.59

4 years ago

1.0.58

4 years ago

1.0.56

4 years ago

1.0.55

4 years ago

1.0.54

4 years ago

1.0.53

4 years ago

1.0.52

4 years ago

1.0.51

4 years ago

1.0.50

4 years ago

1.0.48

4 years ago

1.0.47

4 years ago

1.0.49

4 years ago

1.0.46

4 years ago

1.0.45

4 years ago

1.0.43

4 years ago

1.0.42

4 years ago

1.0.41

4 years ago

1.0.39

4 years ago

1.0.37

4 years ago

1.0.36

4 years ago

1.0.35

4 years ago

1.0.31

4 years ago

1.0.30

4 years ago

1.0.34

4 years ago

1.0.26

4 years ago

1.0.29

4 years ago

1.0.28

4 years ago

1.0.27

4 years ago

1.0.25

4 years ago

1.0.24

4 years ago

1.0.22

4 years ago

1.0.21

4 years ago

1.0.23

4 years ago

1.0.19

4 years ago

1.0.18

4 years ago

1.0.17

4 years ago

1.0.20

4 years ago

1.0.16

4 years ago

1.0.15

4 years ago

1.0.14

4 years ago

1.0.13

4 years ago

1.0.9

4 years ago

1.0.11

4 years ago

1.0.10

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.0

4 years ago

0.1.0

4 years ago