react-vio-form 是一个react的快速轻量表单库,能快速实现表单构建。提供自定义表单格式、表单校验、表单信息反馈、表单信息隔离等功能。可采用组件声明或者API的形式来实现表单的功能

demo

react-vio-form 基于React.createContext实现,要求开发者使用React16+的版本

github:地址

安装

npm install --save react-vio-form

快速教程

首先我们先自定义自己的输入框组件

InputGroup.js

import React, { Component } from 'react';
class InputGroup extends Component {
render() {
let {
onChange,//必须使用的属性,表单字段的值改变方法
value,//必须使用的属性,表单字段的值
message,//必须使用的属性,表单字段的报错信息
title,//自定义属性
type="text"//自定义属性
}=this.props;
return (
<div>
<label>{title}:</label>
<input type={type} onChange={e=>onChange(e.target.value)}/>
{message&&<span>{message}</span>}
</div>
);
}
}
export default InputGroup;

接着我们配置我们的表格

  • 最外层是Form组件,向它传递一个onSubmit的回调方法,在回调方法内我们输出表单的值。
  • 里面是Field组件,它接收我们刚才写的InputGroup为component属性、fieldName为该字段的名称、regexp为该字段的校验正则表达式、message为当表单校验不通过的时候显示的报错信息,该信息通过props传递给InputGroup
  • 一个简单列表demo就完成了,当在username或者password输入值或者点击Submit按钮就会触发表单的校验逻辑

    App.js
import React, { Component } from 'react';
import InputGroup from './InputGroup';
let requiredExp=/\w{1,}/;
class App extends Component {
handleSubmit=({model})=>{
console.log('form data is :'+JSON.stringify(model));
}
render() {
return (
<Form onSubmit={this.handleSubmit}>
<Field component={InputGroup} fieldName="username" title="Username"
regexp={requiredExp} message="Not be empty"></Field>
<Field component={InputGroup} fieldName="address" title="Address"></Field>
<Field component={InputGroup} fieldName="password" title="Password"
type="password" regexp={requiredExp} message="Not be empty"></Field>
<button type="submit">Submit</button>
</Form>
);
}
}
export default App;

回调函数

  • <Form onSubmit={//}>

    只有表单验证通过了才会触发
  • <Field onChange={//}>

    字段每次修改都会触发
class App extends Component {
handleSubmit=({model})=>{
//form submit callback
console.log('form data is :'+JSON.stringify(model));
}
passwordChange=(value,{model,form})=>{
//change callback
//value:该字段的值
//model:为整个表单的数据模型
//form:表单的api中心
console.log(`password:${value}`);
}
render() {
return (
<div>
<Form onSubmit={this.handleSubmit} id="form">
<Field component={InputGroup} fieldName="username" title="Username"></Field>
<Field component={InputGroup} fieldName="password" title="Password"
type="password" onChange={this.passwordChange}></Field>
<button type="submit">Submit</button>
</Form>
</div>
);
}
}

API

表单实例form可以获取设置表单的信息,获取表单实例的方法有两种:

  • formManager.get(id)
  • 回调函数的参数

表单实例方法:

  • setError(fieldName,message)
  • clearError(fieldName)
  • getModel()
  • submit()
import React, { Component } from 'react'
import {Form,Field,formManager} from 'react-vio-form'
let requiredExp=/\w{1,}/;
class App extends Component {
handleSubmit=({model})=>{
//form submit callback
console.log('form data is :'+JSON.stringify(model));
}
handleOutsideSubmit=()=>{
// submit outside Form Component
// param is Form id
formManager.get('form').submit();
}
passwordChange=(value,{model,form})=>{
if(model.password!==model.password2){
//set Error Message
form.setError('password2','password2 must be equaled to password');
}else{
//clear Error Message
form.clearError('password2');
}
}
render() {
return (
<div>
<Form onSubmit={this.handleSubmit} id="form">
<Field component={InputGroup} fieldName="username" title="Username"></Field>
<Field component={InputGroup} fieldName="password" title="Password"
type="password" regexp={requiredExp}
message="Not be empty" onChange={this.passwordChange}></Field>
<Field component={InputGroup} fieldName="password2" title="Password2"
type="password" onChange={this.passwordChange}></Field>
</Form>
<button onClick={this.handleOutsideSubmit}>outside submit</button>
</div>
);
}
}

持续更新中...

反馈与建议

  • 直接在github上提issue

Thanks

License

MIT © violinux666

[原创]react-vio-form 快速构建React表单应用的更多相关文章

  1. 使用create-react-app 快速构建 React 开发环境以及react-router 4.x路由配置

    create-react-app 是来自于 Facebook,通过该命令我们无需配置就能快速构建 React 开发环境. create-react-app 自动创建的项目是基于 Webpack + E ...

  2. 【React】使用 create-react-app 快速构建 React 开发环境

    create-react-app 是来自于 Facebook,通过该命令我们无需配置就能快速构建 React 开发环境. create-react-app 自动创建的项目是基于 Webpack + E ...

  3. 快速构建 React 开发环境

    使用 create-react-app 快速构建 React 开发环境 create-react-app 是来自于 Facebook,通过该命令我们无需配置就能快速构建 React 开发环境. cre ...

  4. 利用 Create React Native App 快速创建 React Native 应用

    本文介绍的 Create-React-Native-App 是非常 Awesome 的工具,而其背后的 Expo 整个平台也让笔者感觉非常的不错.笔者目前公司是采用 APICloud 进行移动应用开发 ...

  5. 基于react hooks,antd4 配置生成表单并自动排列

    react后台项目,大多都是表单处理,比如下列4种常见1*n布局 (如果手工编码,大量的Row,Col, Form.Item的嵌套,排列,如果加上联动处理,代码将十分臃肿,不易维护) 一行一列 一行两 ...

  6. 快速创建InfoPath表单

    快速创建InfoPath表单 2010年已经过去了一半了,这时候再说初识InfoPath可能会被很多人笑话,但是又有多少人真正认识InfoPath呢?无论你是刚刚 听说这个东西还是它的老相好都请同我一 ...

  7. Ext JS4 学习笔记之发送表单(Form)时也将表单下的表格(Grid)数据一同发送的方法

    Ext JS4 学习笔记之发送表单(Form)时也将表单下的表格(Grid)数据一同发送的方法 昨天在开发的时候遇到个小问题,就是如何将Grid的内容与Form一起发送到服务器端.默认情况下,表单(F ...

  8. 在一般处理程序中,把Form Post过来的表单集合转换成对象 ,仿 MVC post,反射原理

    using System; using System.Collections.Generic; using System.Collections.Specialized; using System.L ...

  9. 10天学会phpWeChat——第八天:Form类,丰富表单提交的字段类型

    通过前面七讲的系列教程,我们完成了一个包含后台并自适应PC+h5移动端的文章管理模块. 在实际的生产环境中,文章投稿.商品上传等操作并不会简单局限于一个text和textarea组成的表单.在实际中, ...

随机推荐

  1. windows linux 双系统默认启动windows 的几种方法

    装了双系统后,在开机时总会有想让一个系统默认启动的时候,一般安装完Ubuntu和XP双系统后,开机时默认的是启动Ubuntu系统,可是当想让XP作为默认启动的系统时怎么办呢? 在早期的Ubuntu系统 ...

  2. JSP脚本连接数据库

    入门 简单的jsp文件 <%-- Created by IntelliJ IDEA. User: e550 Date: 2017/1/9 Time: 23:24 To change this t ...

  3. Spring Security 4 Method security using @PreAuthorize,@PostAuthorize, @Secured, EL--转

    原文地址:http://websystique.com/spring-security/spring-security-4-method-security-using-preauthorize-pos ...

  4. Cisco交换机解决网络蠕虫病毒入侵问题

    Cisco交换机解决网络蠕虫病毒入侵问题          今年来网络蠕虫泛滥给ISP和企业都造成了巨大损失,截至目前已发现近百万种病毒及木马.受感染的网络基础设施遭到破坏,以Sql Slammer为 ...

  5. C# Dapper 基本使用 增删改查事务等

    using DapperTest.Models; using System.Collections.Generic; using System.Web.Http; using Dapper; usin ...

  6. Ubuntu系统下的多路径软件 DM Multipath 配置。

    Ubuntu系统下的多路径软件是操作系统自带的 DM Multipath工具.------------------------------------------------------------- ...

  7. rev---将文件中的每行内容以字符为单位反序输出

    rev命令将文件中的每行内容以字符为单位反序输出,即第一个字符最后输出,最后一个字符最先输出,依次类推.

  8. pip-window安装

    windows 安装: 保证计算机联网直接使用cmd 执行 python -m pip install -U pip 自动安装 找到 python安装的路径 C:\Users\Administrato ...

  9. 上下文切换查看 & sar

    怀疑CPU存在瓶颈,可用sar -u 和sar -q来看,怀疑I/O存在瓶颈,可用sar -b.sar -u和 sar-d来看 sar –W 查看页面交换发生状况 [root@localhost ~] ...

  10. c#+windows api SetWindowsHookEx 全局钩子 demo 下载

    效果图 源代码下载地址: http://download.csdn.net/detail/dhfekl/7522141