0.1.5 • Published 5 years ago

json-config-form v0.1.5

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

jsonForm

Project setup

npm install

Compiles and hot-reloads for development

npm run serve

Compiles and minifies for production

npm run build

Lints and fixes files

npm run lint

Run your unit tests

npm run test:unit

表单组件

具体示例 demo

安装与使用方式

npm install json-config-form --save
import jsonConfigForm from 'json-config-form'

如需全局引用则全局注册,如单个引用则单个components注册

组件传参:

字段类型是否必填注释
ref----用于获取dom,进行validate验证
formSchemaObject用于渲染form的参数

例如:

    <json-config-form ref="myDemoForm" :formSchema="formSchema"></json-config-form>

formSchema内部参数列表:

字段类型是否必填注释
labelWidth数字--label的宽度
propertiesObject用于渲染form列表的参数,后台参数字段为键值,配置为值,如name:{ ... }

properties的值通用参数列表:

字段类型是否必填注释
typestring表单类型,input,textarea,number,select,cascader,datepicker,timepicker,radio,check,switch,slot(插槽类型)
inputTypestring--input类型,默认为text,常用还有password,email,url
dateTypestring--date,time类型,daterange等
value--对应的数据,与后台对接的数据
defaultValue----初始数据
disabledboolean--是否禁用
uiobjectui类配置,不同类型具体配置详见代码示例
rulesobject--表单校验规则
changeEvfunction--组件内部值变化的回调函数,number,select,datepicker,timepicker,switch类型有回调
rules列表
字段类型是否必填注释
requiredboolean--是否必填
typestring--字段类型
minnumber--最小值,数字为值,其他类型为长度
maxnumber--最大值,数字为值,其他类型为长度
otherRules数组--其他规则,参照async-validator

select,radio,check中的list列表内必有id,name字段,id用于key,name用于label显示

formSchema内部参数代码示例:

    formSchema: {
        labelWidth: 80,//label宽度
        properties: {
          name: {
            type: 'input', //* **必填 */
            value: '', //* **必填 */
            defaultValue: 'name',
            disabled: true,
            ui: {
              offset: 1, // 偏移量
              col: 23, // 占用格数,总分24格
              label: 'first name',
              placeholder: 'first name'
            },
            rules: {
              required: true,
              // type: 'number',
              // min:1,
              max: 100,
              otherRules: [{ pattern: /^1[345789]\d{9}$/, trigger: 'blur' }]
            }
          },
          password: {
            type: 'input',
            inputType: 'password',
            value: '',
            ui: {
              showLabel: false, //是否显示label,优先级高于label
              label: 'password',
              // col: 6,
              placeholder: 'passowrd'
            }
          },
          textarea: {
            type: 'textarea',
            value: '',
            ui: {
              label: 'textarea',
              // col: 6,
              placeholder: 'textarea',
              minRows: 3, //最小行数
              maxRows: 6 //最大行数
            }
          },
          number: {
            type: 'number',
            value: 0,
            ui: {
              label: 'number',
              col: 8,
              precision: 2 // 精度
            },
            rules: {
              min: 1,
              max: 100,
              type: 'number' //* **必填 */
            }
          },
          select: {
            type: 'select',
            value: '',
            ui: {
              label: 'select',
              col: 8,
              list: [],
              filterable: true, // 是否搜索
              // hasEmptyOption: true, // 是否需要空项,用于展示列表‘全部’,‘请选择’等空项
              sendField: 'code' // 发给后台的字段,默认发id
            },
            changeEv: (val) => { // 变化值的回调函数
              console.log('select', val)
            }
          },
          cascader: {
            type: 'cascader',
            value: [],
            ui: {
              label: 'cascader',
              col: 8,
              list: [
                {
                  value: 'beijing',
                  label: '北京',
                  children: [
                    {
                      value: 'gugong',
                      label: '故宫'
                    },
                    {
                      value: 'tiantan',
                      label: '天坛'
                    },
                    {
                      value: 'wangfujing',
                      label: '王府井'
                    }
                  ]
                }
              ]
            }
          },
          datepicker: {
            type: 'datepicker',
            dateType: 'daterange',
            value: [],
            ui: {
              label: 'datepicker',
              col: 8,
              options: { // 日期选择配置
                disabledDate (date) {
                  return date && date.valueOf() < Date.now() - 88400000
                }
              },
              format: 'yyyy-MM-dd' //格式
            },
            changeEv: (params) => {
              console.log('date', params)
            }
          },
          timepicker: {
            type: 'timepicker',
            dateType: 'timerange',
            value: [],
            ui: {
              label: 'timepicker',
              col: 8,
              format: 'HH:mm:ss'
            },
            changeEv: (params) => {
              console.log('time', params)
            }
          },
          radio: {
            type: 'radio',
            value: '',
            ui: {
              label: 'radio',
              col: 8,
              list: []
            }
          },
          check: {
            type: 'check',
            value: [],
            ui: {
              label: 'check',
              col: 8,
              list: [
                {
                  id: '123',
                  name: '数据'
                }
              ]
            }
          },
          switch: {
            type: 'switch',
            value: true,
            ui: {
              label: 'switch',
              col: 8,
              size: 'small'
            },
            changeEv: (params) => {
              console.log('switch', params)
            }
          },
          slot: { //插槽类型,适用于不属于上列类型之外的自定义组件等更灵活的组件
            type: 'slot',
            slotName: 'myItem',
            ui: {
              col: 8,
              label: 'slot'
            }
          }
        }
      }
    

插槽类型示例(配置如上所述,dom如下所示):

    <json-config-form ref="myDemoForm" :formSchema="formSchema">
      <div slot="myItem">
        <Button>插槽示例</Button>
      </div>
    </json-config-form>

表单校验

this.$refs.yourDomRefName.$emit('validateCommonForm',(valid)=>{
  if (valid) {
    console.log(1,this.sendCode.obj)
  }else{
    console.log(2)
  }
})

表单重置

this.$refs.yourDomRefName.$emit('resetCommonForm')
0.1.5

5 years ago

0.1.4

5 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago