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

Commit dde4b74

Browse files
committed
add Promise support async
1 parent 8cd6b0f commit dde4b74

30 files changed

Lines changed: 505 additions & 245 deletions

.babelrc

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,5 @@
33
"es2015",
44
"stage-0",
55
"react"
6-
],
7-
"plugins": [
8-
"transform-object-assign"
96
]
107
}

README-zh_CN.md

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ const methods = {
6060
const otherField = this.fields[param];
6161
return this.required(field) || (otherField.result && this.required(otherField));
6262
},
63+
async validateFromServer(field, param) {
64+
return await doServerAPI();
65+
},
6366
};
6467
```
6568

@@ -104,6 +107,7 @@ export default formConnect(schemas, methods)(BasicForm);
104107

105108
* `values` 的值类似于 { email: '', hobby: ['2'] }
106109
* `classNames``values` 也可以在 `BasicForm` 中使用 `init` 方法初始化
110+
* `delay` 防抖
107111

108112
基础验证方法可以参考 [validate-framework-utils](https://github.com/MinJieLiu/validate-framework-utils)
109113

@@ -152,18 +156,19 @@ return (
152156

153157
#### Form params
154158

155-
| 名称 | 类型 | 默认值 | setState | 描述 |
159+
| 名称 | 类型 | 返回值 | setState | 描述 |
156160
| :--- | :--- | :--- | :--- | :--- |
157161
| fields | Object | | | `fields` 集合 |
158162
| isAllValid | Boolean | | | 全局验证状态 |
159163
| formValues | Object | | | 表单值的列表 |
160-
| init | function | | false | 初始化表单值和类 |
164+
| init | function | this | false | 初始化表单值和类 |
165+
| initClassNames | function | this | false | 初始化类 |
161166
| onFormChange | function | | true | 表单更改事件监听方法 |
162-
| changeValues | function | | true | 自定义改变表单方法 |
163-
| validate | function | | true | 验证所有字段 |
164-
| validateByNames | function | | true | 通过 `name` 验证组件 |
165-
| addValues | function | | true | 添加一个或多个值 |
166-
| removeValues | function | | true | 删除一个或多个值 |
167-
| addSchemas | function | | false | 添加一个或多个验证规则 |
168-
| removeSchemas | function | | true | 删除一个或多个验证规则 |
167+
| changeValues | function | this | true | 自定义改变表单方法 |
168+
| validate | function | Promise => Boolean | true | 验证所有字段 |
169+
| validateByNames | function | Promise => Boolean | true | 通过 `name` 验证组件 |
170+
| addValues | function | this | true | 添加一个或多个值 |
171+
| removeValues | function | this | true | 删除一个或多个值 |
172+
| addSchemas | function | this | false | 添加一个或多个验证规则 |
173+
| removeSchemas | function | Promise => this | true | 删除一个或多个验证规则 |
169174
| formDidChange | function | | | 回调函数 |

README.md

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ const methods = {
5454
const otherField = this.fields[param];
5555
return this.required(field) || (otherField.result && this.required(otherField));
5656
},
57+
async validateFromServer(field, param) {
58+
return await doServerAPI();
59+
},
5760
};
5861
```
5962

@@ -90,10 +93,12 @@ Finally, sets the initialized value:
9093
delay={100}
9194
/>
9295

93-
// The values like this { email: '', hobby: ['2'] }
94-
// classNames and values can be initialized in `BasicForm` use `init`
9596
```
9697

98+
* The values like this { email: '', hobby: ['2'] }
99+
* classNames and values can be initialized in `BasicForm` use `init`
100+
* `delay` debounce
101+
97102
Validate methods can refer to [validate-framework-utils](https://github.com/MinJieLiu/validate-framework-utils)
98103

99104
### Form components
@@ -137,24 +142,25 @@ return (
137142
| :--- | :--- | :--- | :--- | :--- |
138143
| values | Object | false | | Key-value pairs for `name` and` value` |
139144
| classNames | Object | false | {} | Its `key` value contains` static`, `success`,` error` |
140-
| delay | number | false | | delay |
145+
| delay | number | false | | debounce |
141146

142147
#### Form params
143148

144-
| name | type | default | setState | description |
149+
| name | type | return | setState | description |
145150
| :--- | :--- | :--- | :--- | :--- |
146151
| fields | Object | | | The collection of fields |
147152
| isAllValid | Boolean | | | Gets the global validation status |
148153
| formValues | Object | | | Gets a list of form values |
149-
| init | function | | false | Initializes the form value and classes |
154+
| init | function | | this | Initializes the form value and classes |
155+
| initClassNames | function | this | false | Initializes classes |
150156
| onFormChange | function | | true | Form change event listener |
151-
| changeValues | function | | true | Customize to change the values |
152-
| validate | function | | true | Validate all fields |
153-
| validateByNames | function | | true | Validate the component through names |
154-
| addValues | function | | true | Add one or more value |
155-
| removeValues | function | | true | Remove one or more value |
156-
| addSchemas | function | | false | Add one or more validation rules |
157-
| removeSchemas | function | | true | Remove one or more validation rules |
157+
| changeValues | function | this | true | Customize to change the values |
158+
| validate | function | Promise => Boolean | true | Validate all fields |
159+
| validateByNames | function | Promise => Boolean | true | Validate the component through names |
160+
| addValues | function | this | true | Add one or more value |
161+
| removeValues | function | this | true | Remove one or more value |
162+
| addSchemas | function | this | false | Add one or more validation rules |
163+
| removeSchemas | function | Promise => this | true | Remove one or more validation rules |
158164
| formDidChange | function | | | Callback |
159165

160166
You can either pass in `values` as an argument, or call the `init` method when the form is initialized.

config/webpack.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export default {
1212
'webpack/hot/dev-server',
1313
'webpack-hot-middleware/client',
1414
] : [],
15+
'babel-polyfill',
1516
path.resolve('./example/index'),
1617
],
1718
output: {

example/App.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ export default class extends Component {
1616
};
1717

1818
// Validate the BasicForm
19-
handleValidateBasicForm = () => {
20-
const isAllValid = this.basicForm.validate();
19+
handleValidateBasicForm = async () => {
20+
const isAllValid = await this.basicForm.validate();
2121
this.setState({
2222
isAllValid,
2323
});

example/BasicForm.jsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
* Created by MingYi on 2016/12/23.
33
*/
44

5-
import React, { Component, PropTypes } from 'react';
5+
import React, { Component } from 'react';
6+
import PropTypes from 'prop-types';
67
import classNames from 'classnames';
78
import formConnect, {
89
Checkbox,
@@ -55,14 +56,18 @@ const schemas = {
5556
},
5657
};
5758

59+
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
60+
5861
// Custom methods
5962
const methods = {
6063
// Rely on other fields
6164
requiredField(field, param) {
6265
const otherField = this.fields[param];
6366
return this.required(field) || (otherField.result && this.required(otherField));
6467
},
65-
selectLimit(field, param) {
68+
async selectLimit(field, param) {
69+
// Simulation
70+
await sleep(1000);
6671
if (Array.isArray(field.value)) {
6772
return field.value.length >= param;
6873
}
@@ -77,9 +82,8 @@ class BasicForm extends Component {
7782
};
7883

7984
handleSubmitClick = () => {
80-
const { formControl } = this.props;
81-
// @return {Boolean}
82-
formControl.validate();
85+
// @return {Promise}
86+
this.props.formControl.validate();
8387
};
8488

8589
render() {

example/ChildForm.jsx

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
* Created by MingYi on 2016/12/23.
33
*/
44

5-
import React, { Component, PropTypes } from 'react';
5+
import React, { Component } from 'react';
6+
import PropTypes from 'prop-types';
67
import {
78
Text,
89
Message,
@@ -36,11 +37,9 @@ class ChildForm extends Component {
3637
});
3738
};
3839

39-
handleDeleteFriend = () => {
40+
handleDeleteFriend = async () => {
4041
const { formControl } = this.props;
41-
formControl
42-
.removeSchemas('friend')
43-
.removeValues('friend');
42+
(await formControl.removeSchemas('friend')).removeValues('friend');
4443
};
4544

4645
render() {
@@ -73,19 +72,18 @@ class ChildForm extends Component {
7372
</button>
7473
</div>
7574
{
76-
fields.friend
77-
? (
78-
<div className="form-group">
79-
<label htmlFor="friend">Name:</label>
80-
<Text
81-
id="friend"
82-
name="friend"
83-
type="text"
84-
placeholder="Please enter a name"
85-
/>
86-
<Message className="valid-error-message" name="friend" />
87-
</div>
88-
) : null
75+
fields.friend ? (
76+
<div className="form-group">
77+
<label htmlFor="friend">Name:</label>
78+
<Text
79+
id="friend"
80+
name="friend"
81+
type="text"
82+
placeholder="Please enter a name"
83+
/>
84+
<Message className="valid-error-message" name="friend" />
85+
</div>
86+
) : null
8987
}
9088
<div className="form-group">
9189
{/* The unencapsulated form components */}

lib/Field.js

Lines changed: 8 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/Field.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)