1.0.14 • Published 4 years ago

config-form v1.0.14

Weekly downloads
4
License
MIT
Repository
github
Last release
4 years ago

config-form

generate form based on the json config

项目简介

中后台系统往往需要编写大量的表单逻辑,而且其中大量工作重复度高,为了减少没必要的重复工作,对 antd 组件库中的表单组件结合一些常用的表单逻辑封装成一个组件,该组件可以根据 json 配置生成表单逻辑。

环境安装

  • 安装本项目
npm i config-form --save

使用例子

import { ConfigForm } from "config-form";

const UserForm = ({ data }) => {
    const [value, setValue] = useState("");
    const formInfos = [
        {
            formType: "Input",
            label: "账号",
            help: "输入邮箱或者手机号作为账号",
            placeholder: "请输入账号",
            errorHint: "账号不能为空",
            required: true,
            value: value,
            onChange: v => setValue(v)
        }
    ];
    return <ConfigForm formInfos={formInfos}></ConfigForm>;
};

上面的例子中,ConfigForm 根据 formInfos 里的信息生成了一个包含了标签,提示,占位符,错误提示等逻辑的字符串输入框。

目录

ConfigForm

属性

字段类型说明
formInfosobject arry要生成表单的信息
checkboolean默认值为 false, 当值为 true 时,才会显示错误信息
layoutobject即 antd 组件库中的 Form 组件的 layout 属性
hiddenboolean默认值为 false, 当值为 true 时,表单隐藏

formInfos 属性配置

字段类型说明
formTypestring要成表单的类型
.........

formType 指定了用那一个表单组件,formInfos 属性的配置会根据不同的表单组件有不同的属性,下面我们分别介绍。

ItemView

antd 组件库中的 Form.Item 的再一层封装的容器组件,下面所有的输入组件都用这个组件包装的,所以都有以下配置属性,除了 children 属性。

属性配置

字段类型说明
requiredboolean是否必要
checkboolean默认值为 false, 当值为 true 时,才会显示错误信息
disabledboolean默认值为 false, 当值为 true 时,禁止修改
isIllegalboolean默认值为 false, 当值为 true 时表明当前输入错误
errorHintstring错误提示
labelstring标签
layoutobject即 antd 组件库中的 Form 组件的 layout 属性
hiddenboolean默认值为 false, 当值为 true 时,表单隐藏
helpstring帮助提示
childrenobject or funciton子元素

Input

字符串输入框

属性配置

字段类型说明
requiredboolean是否必要
checkboolean默认值为 false, 当值为 true 时,才会显示错误信息
disabledboolean默认值为 false, 当值为 true 时,禁止修改
isIllegalboolean默认值为 false, 当值为 true 时表明当前输入错误
errorHintstring错误提示
labelstring标签
layoutobject即 antd 组件库中的 Form 组件的 layout 属性
hiddenboolean默认值为 false, 当值为 true 时,表单隐藏
helpstring帮助提示
styleobjectinput 输入框的样式
typestring输入框的类型,值为"text" 或 "textArea" 默认为 "text"
valuestring输入框的值
focusControlboolean默认值为 false, 当值为 true 时,组件为受控组件
placeholderstring输入框的占位符
rownumber当 type 的值为 "textArea" 时,输入框的行数
onChangefunction当输入值变化的回调函数

PatternInput

带格式校验的字符串输入框

属性配置

字段类型说明
requiredboolean是否必要
checkboolean默认值为 false, 当值为 true 时,才会显示错误信息
disabledboolean默认值为 false, 当值为 true 时,禁止修改
isIllegalboolean默认值为 false, 当值为 true 时表明当前输入错误
errorHintstring错误提示
labelstring标签
layoutobject即 antd 组件库中的 Form 组件的 layout 属性
hiddenboolean默认值为 false, 当值为 true 时,表单隐藏
helpstring帮助提示
styleobjectinput 输入框的样式
typestring输入框的类型,值为"text" 或 "textArea" 默认为 "text"
valuestring输入框的值
focusControlboolean默认值为 false, 当值为 true 时,组件为受控组件
placeholderstring输入框的占位符
rownumber当 type 的值为 "textArea" 时,输入框的行数
patternstring对当前输入值的格式要求
patternInfoobject自定义对当前输入值的格式要求
onChangefunction当输入值变化的回调函数

pattern 支持的格式要求

  • sign (标识)
  • uri (链接)
  • version (版本号)
  • email (邮件地址)
  • phone (手机号)
  • json (json 字符串)

patternInfo 属性配置

字段类型说明
namestring格式要求名
patternHintstring当前输入不符合格式要求时的提示
checkfunction校验格式是否合格的函数

MultiInput

多个字符串输入框

属性配置

字段类型说明
requiredboolean是否必要
checkboolean默认值为 false, 当值为 true 时,才会显示错误信息
disabledboolean默认值为 false, 当值为 true 时,禁止修改
isIllegalboolean默认值为 false, 当值为 true 时表明当前输入错误
errorHintstring错误提示
labelstring标签
layoutobject即 antd 组件库中的 Form 组件的 layout 属性
hiddenboolean默认值为 false, 当值为 true 时,表单隐藏
helpstring帮助提示
styleobjectinput 输入框的样式
typestring输入框的类型,值为"text" 或 "textArea" 默认为 "text"
valuearray包含所有输入框的值的数组
focusControlboolean默认值为 false, 当值为 true 时,组件为受控组件
placeholderstring输入框的占位符
rownumber当 type 的值为 "textArea" 时,输入框的行数
indexHintMapobject用于在某个输入框的下面显示提示
onChangefunction当输入值变化的回调函数

indexHintMap 属性配置

字段类型说明
indexobjectkey index 为输入框的位置 , value 为 提示配置

indexHintMap 某个index的属性配置

字段类型说明
textstring提示文字
statusboolean默认值为 false, 当值为 true 时,提示文字为红色

indexHintMap 配置例子

{
    0: {
        text: '请在第一个输入框输入姓名',
        status: true,
    }
}

该提示会出现在第一个输入框和第二输入框中间

NumberInput

数字输入框

属性配置

字段类型说明
requiredboolean是否必要
checkboolean默认值为 false, 当值为 true 时,才会显示错误信息
disabledboolean默认值为 false, 当值为 true 时,禁止修改
isIllegalboolean默认值为 false, 当值为 true 时表明当前输入错误
errorHintstring错误提示
labelstring标签
layoutobject即 antd 组件库中的 Form 组件的 layout 属性
hiddenboolean默认值为 false, 当值为 true 时,表单隐藏
helpstring帮助提示
valuenumber输入框的值
maxnumber可以输入的最大值
minnumber可以输入的最小值
stepnumber增加数字的步长
onChangefunction当输入值变化的回调函数

Select

选择器

属性配置

字段类型说明
requiredboolean是否必要
checkboolean默认值为 false, 当值为 true 时,才会显示错误信息
disabledboolean默认值为 false, 当值为 true 时,禁止修改
isIllegalboolean默认值为 false, 当值为 true 时表明当前输入错误
errorHintstring错误提示
labelstring标签
layoutobject即 antd 组件库中的 Form 组件的 layout 属性
hiddenboolean默认值为 false, 当值为 true 时,表单隐藏
helpstring帮助提示
styleobject输入框的样式
valuestring选择的选项的值
placeholderstring输入框的占位符
optionsDataobject选项信息
disableOptsobject禁止选择的选项
filterOptionboolean or function当值为 true 时开启默认搜索过滤选项,函数则为自定义过滤规则
onChangefunction当输入值变化的回调函数

optionsData 属性配置

字段类型说明
keyobjectkey 是选项的 value,

optionsData 某个key的属性配置

字段类型说明
textstring选项的文字内容

optionsData 配置例子

{
    man: {
        text: "男";
    },
    woman: {
        text: "女";
    }
}

disableOpts 属性配置

字段类型说明
keybooleankey 是选项的 value, 默认值为 false, 当值为 true 时,选项无法选择
{
    man: true;
}

RadioGroup

单选框组

属性配置

字段类型说明
requiredboolean是否必要
checkboolean默认值为 false, 当值为 true 时,才会显示错误信息
disabledboolean默认值为 false, 当值为 true 时,禁止修改
isIllegalboolean默认值为 false, 当值为 true 时表明当前输入错误
errorHintstring错误提示
labelstring标签
layoutobject即 antd 组件库中的 Form 组件的 layout 属性
hiddenboolean默认值为 false, 当值为 true 时,表单隐藏
helpstring帮助提示
valuestring选择的选项的值
optionsDataobject选项信息
disableOptsobject禁止选择的选项
onChangefunction当输入值变化的回调函数

optionsData 属性配置

字段类型说明
keyobjectkey 是选项 的 value,

optionsData 某个key的属性配置

字段类型说明
textstring选项的 label

optionsData 配置例子

{
    man: {
        text: "男";
    },
    woman: {
        text: "女";
    }
}

disableOpts 属性配置

字段类型说明
keybooleankey 是选项的 value, 默认值为 false, 当值为 true 时,选项无法选择
{
    man: true;
}

CheckboxGroup

多选框组

属性配置

字段类型说明
requiredboolean是否必要
checkboolean默认值为 false, 当值为 true 时,才会显示错误信息
disabledboolean默认值为 false, 当值为 true 时,禁止修改
isIllegalboolean默认值为 false, 当值为 true 时表明当前输入错误
errorHintstring错误提示
labelstring标签
layoutobject即 antd 组件库中的 Form 组件的 layout 属性
hiddenboolean默认值为 false, 当值为 true 时,表单隐藏
helpstring帮助提示
valuearray包含所有选择的选项的值的组合
optionsDataobject选项信息
disableOptsobject禁止选择的选项
onChangefunction当输入值变化的回调函数

optionsData 属性配置

字段类型说明
keyobjectkey 是选项的 value,

optionsData 某个key的属性配置

字段类型说明
textstring选项的 label

optionsData 配置例子

{
    man: {
        text: "男";
    },
    woman: {
        text: "女";
    }
}

disableOpts 属性配置

字段类型说明
keybooleankey 是选项的 value, 默认值为 false, 当值为 true 时,选项无法选择
{
    man: true;
}

DatePicker

日期选择器

属性配置

字段类型说明
requiredboolean是否必要
checkboolean默认值为 false, 当值为 true 时,才会显示错误信息
disabledboolean默认值为 false, 当值为 true 时,禁止修改
isIllegalboolean默认值为 false, 当值为 true 时表明当前输入错误
errorHintstring错误提示
labelstring标签
layoutobject即 antd 组件库中的 Form 组件的 layout 属性
hiddenboolean默认值为 false, 当值为 true 时,表单隐藏
helpstring帮助提示
styleobject输入框的样式
valuestring选择的日期, 格式必须为"YYYY-MM-DD HH:mm:ss"
disabledDatefunction判断日期是否合法的函数
hmsValueStatusstring日期的时分秒的默认值类型,reset-默认值是 "00:00:00" ,maxValue-默认值是 "23:59:59"
onChangefunction当输入值变化的回调函数

TimeRange

日期范围选择器

属性配置

字段类型说明
requiredboolean是否必要
checkboolean默认值为 false, 当值为 true 时,才会显示错误信息
disabledboolean默认值为 false, 当值为 true 时,禁止修改
isIllegalboolean默认值为 false, 当值为 true 时表明当前输入错误
errorHintstring错误提示
labelstring标签
layoutobject即 antd 组件库中的 Form 组件的 layout 属性
hiddenboolean默认值为 false, 当值为 true 时,表单隐藏
helpstring帮助提示
styleobject输入框的样式
valuearray选择的日期组合,每个值格式必须为"YYYY-MM-DD HH:mm:ss"
disabledDatefunction判断日期是否合法的函数
legalRangearray合法的日期范围,每个值格式必须为"YYYY-MM-DD HH:mm:ss"
itemLayoutobject即 antd 组件库中的 Form 组件的 layout 属性,但是配置给子表单的
onChangefunction当输入值变化的回调函数

Range

数字范围输入框

属性配置

字段类型说明
requiredboolean是否必要
checkboolean默认值为 false, 当值为 true 时,才会显示错误信息
disabledboolean默认值为 false, 当值为 true 时,禁止修改
isIllegalboolean默认值为 false, 当值为 true 时表明当前输入错误
errorHintstring错误提示
labelstring标签
layoutobject即 antd 组件库中的 Form 组件的 layout 属性
hiddenboolean默认值为 false, 当值为 true 时,表单隐藏
helpstring帮助提示
valuearray填入的数字范围
legalRangearray合法的数字范围
onChangefunction当输入值变化的回调函数

License (MIT)

Copyright (c) 2019 gintamamadao

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1.0.14

4 years ago

1.0.12

5 years ago

1.0.11

5 years ago

1.0.10

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago