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

Commit 1f8a8df

Browse files
committed
fix schemas
fix chain grammar change function name: addFields -> addValues; removeFields -> removeValues add test
1 parent b063769 commit 1f8a8df

8 files changed

Lines changed: 185 additions & 74 deletions

File tree

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,10 @@ return (
145145
| changeValues | function | | true | Customize to change the values |
146146
| validate | function | | true | Validate all fields |
147147
| validateByNames | function | | true | Validate the component through names |
148-
| addFields | function | | true | Add one or more fields |
149-
| removeFields | function | | true | Deletes one or more fields |
148+
| addValues | function | | true | Add one or more value |
149+
| removeValues | function | | true | Remove one or more value |
150150
| addSchemas | function | | false | Add one or more validation rules |
151-
| removeSchemas | function | | true | Delete one or more validation rules |
151+
| removeSchemas | function | | true | Remove one or more validation rules |
152152

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

example/ChildForm.jsx

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,22 @@ class ChildForm extends Component {
2525
}
2626

2727
handleAddFriends = () => {
28-
const { formControl: { addFields, addSchemas } } = this.props;
29-
// init value
30-
addFields({
31-
friend: {
32-
value: '',
33-
},
34-
});
35-
addSchemas({
28+
const { formControl } = this.props;
29+
formControl.addSchemas({
3630
friend: {
3731
rules: 'required',
3832
messages: 'Can not be empty!',
3933
},
34+
}).addValues({
35+
friend: '',
4036
});
4137
};
4238

4339
handleDeleteFriend = () => {
44-
const { formControl: { removeSchemas, removeFields } } = this.props;
45-
removeSchemas('friend');
46-
removeFields('friend');
40+
const { formControl } = this.props;
41+
formControl
42+
.removeSchemas('friend')
43+
.removeValues('friend');
4744
};
4845

4946
render() {

lib/FormControl.js

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

lib/FormControl.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.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-validate-framework",
3-
"version": "0.9.4",
3+
"version": "0.9.5",
44
"description": "React validation framework.",
55
"main": "lib/index.js",
66
"scripts": {

src/FormControl.jsx

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import Validator from 'validate-framework-utils';
1111
* @param methods Extended Validation Method
1212
* @return Component
1313
*/
14-
export default (schemas = {}, methods) => FormComponent => (
14+
export default (schemas, methods) => FormComponent => (
1515

1616
/**
1717
* Returns a react form
@@ -145,11 +145,17 @@ export default (schemas = {}, methods) => FormComponent => (
145145
Object.assign(classNames, classes);
146146
// Initialize
147147
Object.keys(values).forEach((name) => {
148+
const value = values[name];
148149
fields[name] = {
149150
className: classNames.static,
150-
value: values[name],
151+
value,
151152
};
153+
// Synchronize values external state
154+
if (this.props.values) {
155+
this.props.values[name] = value;
156+
}
152157
});
158+
return this;
153159
};
154160

155161
/**
@@ -255,17 +261,13 @@ export default (schemas = {}, methods) => FormComponent => (
255261
// Initializes
256262
this.init(values);
257263
Object.keys(values).forEach((name) => {
258-
const value = values[name];
259-
// Synchronize values external state
260-
if (this.props.values) {
261-
this.props.values[name] = value;
262-
}
263-
this.validateField(name, value);
264+
this.validateField(name, values[name]);
264265
});
265266
// Update
266267
this.setState({
267268
fields,
268269
});
270+
return this;
269271
};
270272

271273
/**
@@ -274,6 +276,7 @@ export default (schemas = {}, methods) => FormComponent => (
274276
*/
275277
addSchemas = (schema) => {
276278
Object.assign(this.schemas, schema);
279+
return this;
277280
};
278281

279282
/**
@@ -291,32 +294,29 @@ export default (schemas = {}, methods) => FormComponent => (
291294
this.setState({
292295
fields,
293296
});
297+
return this;
294298
};
295299

296300
/**
297301
* Add one or more fields
298-
* @param newFields
302+
* @param values
299303
*/
300-
addFields = (newFields) => {
301-
const { classNames } = this.props;
304+
addValues = (values) => {
302305
const { fields } = this.state;
303-
Object.keys(newFields).forEach((name) => {
304-
Object.assign(newFields[name], {
305-
className: classNames.static,
306-
});
307-
});
308-
Object.assign(fields, newFields);
306+
// Initializes
307+
this.init(values);
309308
// Update
310309
this.setState({
311310
fields,
312311
});
312+
return this;
313313
};
314314

315315
/**
316316
* Deletes one or more fields
317317
* @param names
318318
*/
319-
removeFields = (...names) => {
319+
removeValues = (...names) => {
320320
const { fields } = this.state;
321321
names.forEach((name) => {
322322
delete fields[name];
@@ -325,6 +325,7 @@ export default (schemas = {}, methods) => FormComponent => (
325325
this.setState({
326326
fields,
327327
});
328+
return this;
328329
};
329330

330331
/**

0 commit comments

Comments
 (0)