[原创]react-vio-form 快速构建React表单应用
react-vio-form 是一个react的快速轻量表单库,能快速实现表单构建。提供自定义表单格式、表单校验、表单信息反馈、表单信息隔离等功能。可采用组件声明或者API的形式来实现表单的功能
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表单应用的更多相关文章
- 使用create-react-app 快速构建 React 开发环境以及react-router 4.x路由配置
create-react-app 是来自于 Facebook,通过该命令我们无需配置就能快速构建 React 开发环境. create-react-app 自动创建的项目是基于 Webpack + E ...
- 【React】使用 create-react-app 快速构建 React 开发环境
create-react-app 是来自于 Facebook,通过该命令我们无需配置就能快速构建 React 开发环境. create-react-app 自动创建的项目是基于 Webpack + E ...
- 快速构建 React 开发环境
使用 create-react-app 快速构建 React 开发环境 create-react-app 是来自于 Facebook,通过该命令我们无需配置就能快速构建 React 开发环境. cre ...
- 利用 Create React Native App 快速创建 React Native 应用
本文介绍的 Create-React-Native-App 是非常 Awesome 的工具,而其背后的 Expo 整个平台也让笔者感觉非常的不错.笔者目前公司是采用 APICloud 进行移动应用开发 ...
- 基于react hooks,antd4 配置生成表单并自动排列
react后台项目,大多都是表单处理,比如下列4种常见1*n布局 (如果手工编码,大量的Row,Col, Form.Item的嵌套,排列,如果加上联动处理,代码将十分臃肿,不易维护) 一行一列 一行两 ...
- 快速创建InfoPath表单
快速创建InfoPath表单 2010年已经过去了一半了,这时候再说初识InfoPath可能会被很多人笑话,但是又有多少人真正认识InfoPath呢?无论你是刚刚 听说这个东西还是它的老相好都请同我一 ...
- Ext JS4 学习笔记之发送表单(Form)时也将表单下的表格(Grid)数据一同发送的方法
Ext JS4 学习笔记之发送表单(Form)时也将表单下的表格(Grid)数据一同发送的方法 昨天在开发的时候遇到个小问题,就是如何将Grid的内容与Form一起发送到服务器端.默认情况下,表单(F ...
- 在一般处理程序中,把Form Post过来的表单集合转换成对象 ,仿 MVC post,反射原理
using System; using System.Collections.Generic; using System.Collections.Specialized; using System.L ...
- 10天学会phpWeChat——第八天:Form类,丰富表单提交的字段类型
通过前面七讲的系列教程,我们完成了一个包含后台并自适应PC+h5移动端的文章管理模块. 在实际的生产环境中,文章投稿.商品上传等操作并不会简单局限于一个text和textarea组成的表单.在实际中, ...
随机推荐
- Select 选择算法 - 编程珠玑(续) 笔记
Select 算法 I 编程珠玑(续)介绍的 Quickselect 算法 选择 N 个元素中的第 K 小(大)值,是日常场景中常见的问题,也是经典的算法问题. 选取 N 个元素的数组的中的第 K 小 ...
- Python学习笔记 capture 1
最近开始学习Python3.x,真的感觉Python的语法与C++,Java有很大的不同,Python从某些方面来说语法更简单.Python作为一种解释性语言和编译型语言如C++来说,还是各有千秋的. ...
- Codefroces432 div2 A,B,C
A. Arpa and a research in Mexican wave Arpa is researching the Mexican wave. There are n spectators ...
- cf B. Black Square
B. Black Square time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- ubuntu server 网络配置,主机名配置
一.通过命令ifconfig -a 查看可用网络设备 通过上面的命令,本机可用的网络设备为enp4s0f0 和enp4s0f1 ,不同的系统版本和硬件配置网络设备名可能不一样,所以一定要先确认本机可用 ...
- 对win2012 server 虚拟机hyper-V 硬盘管理,容量变更及新增硬盘
目的:对win2012 server 虚拟机hyper-V 硬盘管理,容量变更及新增硬盘 一.压缩虚拟机硬盘容量 进入Server 2012的操作系统,打开CMD框,输入:diskmgmt.msc,回 ...
- Linux下QQ的使用并手动设置QQ文件保存路径
一.背景&&目标 马化腾迟迟不肯做linux版本的QQ和微信,实在抠脚. 没有办法,要在linux上使用QQ,目前我找到最好的办法就是使用wine,然而wine这个杀千刀的又是个坑货, ...
- Codeforces 429D Tricky Function 近期点对
题目链接:点击打开链接 暴力出奇迹. 正解应该是近期点对.以i点为x轴,sum[i](前缀和)为y轴,求随意两点间的距离. 先来个科学的暴力代码: #include<stdio.h> #i ...
- 在oracle存储过程中创建暂时表
在oracle的存储过程中,不能直接使用DDL语句,比方create.alter.drop.truncate等. 那假设我们想在存储过程中建立一张暂时表就仅仅能使用动态sql语句了: create o ...
- POJ 2533 Longest Ordered Subsequence(dp LIS)
Language: Default Longest Ordered Subsequence Time Limit: 2000MS Memory Limit: 65536K Total Submis ...