react-vio-form 是一個react的快速輕量表單庫,能快速實現表單構建。提供自定義表單格式、表單校驗、表單信息反饋、表單信息隔離等功能。可採用組件聲明或者API的形式來實現表單的功能react
demolinux
react-vio-form 基於React.createContext實現,要求開發者使用React16+的版本git
github:地址github
npm install --save react-vio-form
複製代碼
首先咱們先自定義本身的輸入框組件正則表達式
InputGroup.jsnpm
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; 複製代碼
接着咱們配置咱們的表格api
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>
);
}
}
複製代碼
表單實例form能夠獲取設置表單的信息,獲取表單實例的方法有兩種:bash
表單實例方法:ide
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>
);
}
}
複製代碼
持續更新中...svg
MIT © violinux666