react bee-form component for tinper-bee
- react >= 15.3.0
- react-dom >= 15.3.0
- prop-types >= 15.6.0
/**
*
* @title 基本form校验
* @description 登录示例
*/
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import Form from '../../src';
import FormControl from 'bee-form-control';
import Label from 'bee-label';
import Button from 'bee-button';
const FormItem = Form.FormItem;
class Demo2 extends Component {
submit = (e) => {
e.preventDefault();
this.props.form.validateFields((err, values) => {
if (err) {
console.log('校验失败', values);
} else {
console.log('提交成功', values)
}
});
}
render() {
const { getFieldProps, getFieldError } = this.props.form;
return (
<div className="demo2">
<Form >
<FormItem>
<Label>用户名:</Label>
<FormControl placeholder="请输入用户名"
{...getFieldProps('username', {
validateTrigger: 'onBlur',
rules: [{
required: true, message: '请输入用户名',
}],
}) }
/>
<span className='error'>
{getFieldError('username')}
</span>
</FormItem>
<FormItem>
<Label>密码:</Label>
<FormControl placeholder="请输入密码" type='password'
{...getFieldProps('password', {
validateTrigger: 'onBlur',
rules: [{
required: true, message: '请输入密码',
}],
}) }
/>
<span className='error'>
{getFieldError('password')}
</span>
</FormItem>
<div className='submit'>
<Button colors="primary" className="login" onClick={this.submit}>登陆</Button>
<Button shape="border" className="reset">取消</Button>
</div>
</Form>
</div>
)
}
}
export default Form.createForm()(Demo2);
参见 rc-form。
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
form | 经过Form.createForm 包装后的组件,都带有this.props.form 属性 |
Object | - |
prefixCls | 类名前缀 | String | u-form |
className | 类名 | String | - |
onSubmit | 提交事件 | Function | - |
style | 样式 | Object | - |
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
prefixCls | 类名前缀 | String | u-form |
className | 类名 | String | - |
style | 样式 | Object | - |
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
enum | 枚举类型 | string | - |
len | 字段长度 | number | - |
max | 最大长度 | number | - |
message | 校验文案 | string | - |
min | 最小长度 | number | - |
pattern | 正则表达式校验 | RegExp | - |
required | 是否必选 | boolean | false |
transform | 校验前转换字段值 | function(value) => transformedValue:any | - |
type | 内建校验类型,可选项 | string | 'string' |
validator | 自定义校验(注意,callback 必须被调用) | function(rule, value, callback) | - |
whitespace | 必选时,空格是否会被视为错误 | boolean | false |
$ npm install -g bee-tools
$ git clone https://github.com/tinper-bee/bee-form
$ cd bee-form
$ npm install
$ npm run dev