Skip to content
This repository was archived by the owner on Nov 26, 2021. It is now read-only.

Commit 056b94d

Browse files
committed
add 中文 doc
1 parent 1f8a8df commit 056b94d

File tree

2 files changed

+163
-1
lines changed

2 files changed

+163
-1
lines changed

README-zh_CN.md

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
# react-validate-framework
2+
3+
一个轻量、简单、易用的 `React` 表单验证组件
4+
5+
[![npm](https://img.shields.io/npm/v/react-validate-framework.svg?style=flat-square)](https://www.npmjs.com/package/react-validate-framework)
6+
[![travis-ci](https://travis-ci.org/MinJieLiu/react-validate-framework.svg?branch=master)](https://travis-ci.org/MinJieLiu/react-validate-framework)
7+
[![npm](https://img.shields.io/npm/dt/react-validate-framework.svg?style=flat-square)](https://github.com/MinJieLiu/react-validate-framework)
8+
9+
Demo: [https://minjieliu.github.io/react-validate-framework](https://minjieliu.github.io/react-validate-framework)
10+
11+
### 特性
12+
13+
1. 轻量的体积
14+
1. 亲和的 API
15+
1. 内置常用验证方法
16+
1. 动态的验证
17+
1. 多表单组合
18+
1. 自定义规则
19+
20+
21+
## 开始使用
22+
23+
npm i react-validate-framework --save
24+
25+
引入:
26+
27+
```js
28+
import formConnect, {
29+
Checkbox,
30+
Radio,
31+
Select,
32+
Text,
33+
Textarea,
34+
Message,
35+
} from 'react-validate-framework';
36+
```
37+
38+
验证规则像这样:
39+
40+
```js
41+
const schemas = {
42+
email: {
43+
rules: 'required | isEmail | maxLength(32)',
44+
messages: '不能为空 | 请输入有效的电子邮件地址 | 不能超过{{param}}个字符',
45+
},
46+
hobby: {
47+
rules: 'required | selectLimit(2)',
48+
messages: '不能为空 | 至少选择{{param}}项',
49+
},
50+
};
51+
52+
const methods = {
53+
selectLimit(field, param) {
54+
if (Array.isArray(field.value)) {
55+
return field.value.length >= param;
56+
}
57+
return false;
58+
},
59+
};
60+
```
61+
62+
* 验证规则和扩展方法不是必需
63+
64+
表单像这样:
65+
66+
```js
67+
const BasicForm = () => (
68+
<div className="form-group">
69+
<Text
70+
name="email"
71+
placeholder="Please input your email"
72+
/>
73+
<Message className="valid-error-message" name="email" />
74+
</div>
75+
);
76+
```
77+
78+
* 组件中 `name` 为必需值
79+
80+
导出模块:
81+
82+
```js
83+
export default formConnect(schemas, methods)(BasicForm);
84+
```
85+
86+
最后,初始化表单值和类名:
87+
88+
```js
89+
<BasicForm
90+
classNames={{
91+
static: 'form-control',
92+
success: 'valid-success',
93+
error: 'valid-error',
94+
}}
95+
values={this.state.formValues}
96+
/>
97+
```
98+
99+
100+
* `values` 的值类似于 { email: '', hobby: ['2'] }
101+
* 这些参数也可以在 `BasicForm` 中使用 `init` 方法初始化
102+
103+
基础验证方法可以参考 [validate-framework-utils](https://github.com/MinJieLiu/validate-framework-utils)
104+
105+
### 表单组件
106+
107+
* `Checkbox`
108+
* `Radio`
109+
* `Select`
110+
* `Text`
111+
* `Textarea`
112+
* `Message`
113+
114+
表单 `name` 属性是必需的,其他参数可以被覆盖。
115+
116+
当然,你也可以使用自定义的表单受控组件,只需指定 `value``onChange`
117+
118+
```js
119+
const {
120+
formControl: {
121+
fields,
122+
onFormChange,
123+
},
124+
} = this.props;
125+
126+
return (
127+
<input
128+
className={fields.email.className}
129+
name="email"
130+
type="text"
131+
onChange={onFormChange}
132+
value={fields.email.value}
133+
placeholder="请输入电子邮件"
134+
/>
135+
);
136+
```
137+
138+
### API
139+
140+
#### `FormControl` 参数
141+
142+
| 名称 | 类型 | 必需 | 默认值 | 描述 |
143+
| :--- | :--- | :--- | :--- | :--- |
144+
| values | Object | false | | `values` 集合 |
145+
| classNames | Object | false | {} | 其 key 值包含 `static``success``error` 三种类名 |
146+
147+
#### Form params
148+
149+
| 名称 | 类型 | 默认值 | setState | 描述 |
150+
| :--- | :--- | :--- | :--- | :--- |
151+
| fields | Object | | | `fields` 集合 |
152+
| isAllValid | Boolean | | | 全局验证状态 |
153+
| formValues | Object | | | 表单值的列表 |
154+
| init | function | | false | 初始化表单值和类 |
155+
| onFormChange | function | | true | 表单更改事件监听方法 |
156+
| changeValues | function | | true | 自定义改变表单方法 |
157+
| validate | function | | true | 验证所有字段 |
158+
| validateByNames | function | | true | 通过 `name` 验证组件 |
159+
| addValues | function | | true | 添加一个或多个值 |
160+
| removeValues | function | | true | 删除一个或多个值 |
161+
| addSchemas | function | | false | 添加一个或多个验证规则 |
162+
| removeSchemas | function | | true | 删除一个或多个验证规则 |

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ A lightweight and extensible React validation component
66
[![travis-ci](https://travis-ci.org/MinJieLiu/react-validate-framework.svg?branch=master)](https://travis-ci.org/MinJieLiu/react-validate-framework)
77
[![npm](https://img.shields.io/npm/dt/react-validate-framework.svg?style=flat-square)](https://github.com/MinJieLiu/react-validate-framework)
88

9-
This component relies on `react`.
9+
[中文 README](README-zh_CN.md)
1010

1111
Demo: [https://minjieliu.github.io/react-validate-framework](https://minjieliu.github.io/react-validate-framework)
1212

0 commit comments

Comments
 (0)