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

Commit e038ad1

Browse files
committed
add resetValues
1 parent c66d675 commit e038ad1

File tree

7 files changed

+165
-28
lines changed

7 files changed

+165
-28
lines changed

README-zh_CN.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Demo: [https://minjieliu.github.io/react-validate-framework](https://minjieliu.g
1616
1. 动态的验证
1717
1. 多表单组合
1818
1. 自定义规则
19+
1. 基于 `async` 语法,优雅的异步验证
1920

2021

2122
## 开始使用
@@ -168,7 +169,8 @@ return (
168169
| validate | function | Promise => Boolean | true | 验证所有字段 |
169170
| validateByNames | function | Promise => Boolean | true | 通过 `name` 验证组件 |
170171
| addValues | function | this | true | 添加一个或多个值 |
171-
| removeValues | function | this | true | 删除一个或多个值 |
172+
| removeValues | function | this | true | 删除一个或多个值,无参数则删除所有 |
173+
| resetValues | function | this | true | 重置一个或多个值,无参数则重置所有 |
172174
| addSchemas | function | this | false | 添加一个或多个验证规则 |
173-
| removeSchemas | function | this | true | 删除一个或多个验证规则 |
174-
| formDidChange | function | | | 回调函数 |
175+
| removeSchemas | function | this | true | 删除一个或多个验证规则,无参数则删除所有 |
176+
| formDidChange | function | | | 表单改变回调 |

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,10 @@ return (
158158
| validate | function | Promise => Boolean | true | Validate all fields |
159159
| validateByNames | function | Promise => Boolean | true | Validate the component through names |
160160
| addValues | function | this | true | Add one or more value |
161-
| removeValues | function | this | true | Remove one or more value |
161+
| removeValues | function | this | true | Remove one or more value, If there is no name, it will all be removed |
162+
| resetValues | function | this | true | Reset one or more value, If there is no name, it will all be init |
162163
| addSchemas | function | this | false | Add one or more validation rules |
163-
| removeSchemas | function | this | true | Remove one or more validation rules |
164+
| removeSchemas | function | this | true | Remove one or more validation rules, it will all be removed |
164165
| formDidChange | function | | | Callback |
165166

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

lib/FormControl.js

+73-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/FormControl.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-validate-framework",
3-
"version": "0.13.0",
3+
"version": "0.14.0",
44
"description": "React validation framework.",
55
"main": "lib/index.js",
66
"scripts": {

src/FormControl.jsx

+65-11
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ export default (schemas, methods) => FormComponent => (
3838

3939
schemas = Object.assign({}, schemas);
4040

41+
// Original
42+
originalValues = {};
43+
4144
constructor(props) {
4245
super(props);
4346
const {
@@ -141,7 +144,8 @@ export default (schemas, methods) => FormComponent => (
141144
*/
142145
get isAllValid() {
143146
const { fields } = this.state;
144-
return Object.keys(this.schemas)
147+
return Object
148+
.keys(this.schemas)
145149
.every(name => fields[name] && fields[name].result);
146150
}
147151

@@ -161,6 +165,10 @@ export default (schemas, methods) => FormComponent => (
161165
className: this.props.classNames.static,
162166
value,
163167
};
168+
// Only initialized once
169+
if (this.originalValues[name] === undefined) {
170+
this.originalValues[name] = value;
171+
}
164172
// Synchronize values external state
165173
if (this.props.values) {
166174
this.props.values[name] = value;
@@ -304,7 +312,7 @@ export default (schemas, methods) => FormComponent => (
304312
this.setState({
305313
fields,
306314
});
307-
this.formDidChange();
315+
this.formDidChange({ [name]: theValue });
308316
};
309317

310318
/**
@@ -321,7 +329,7 @@ export default (schemas, methods) => FormComponent => (
321329
this.setState({
322330
fields,
323331
});
324-
this.formDidChange();
332+
this.formDidChange(values);
325333
return this;
326334
};
327335

@@ -336,12 +344,17 @@ export default (schemas, methods) => FormComponent => (
336344

337345
/**
338346
* Delete one or more validation rules
347+
* If there is no name, it will all be removed.
339348
* @param names
340349
*/
341350
removeSchemas = (...names) => {
342-
names.forEach((name) => {
343-
delete this.schemas[name];
344-
});
351+
if (names.length) {
352+
names.forEach((name) => {
353+
delete this.schemas[name];
354+
});
355+
} else {
356+
this.schemas = {};
357+
}
345358
// Validate the deleted status
346359
this.validateByNames(...names);
347360
return this;
@@ -359,24 +372,62 @@ export default (schemas, methods) => FormComponent => (
359372
this.setState({
360373
fields,
361374
});
362-
this.formDidChange();
375+
this.formDidChange(values);
363376
return this;
364377
};
365378

366379
/**
367380
* Deletes one or more fields
381+
* If there is no name, it will all be removed.
368382
* @param names
369383
*/
370384
removeValues = (...names) => {
371385
const { fields } = this.state;
372-
names.forEach((name) => {
373-
delete fields[name];
386+
if (names.length) {
387+
names.forEach((name) => {
388+
delete fields[name];
389+
if (this.props.values) {
390+
delete this.props.values[name];
391+
}
392+
});
393+
} else {
394+
// Remove all
395+
this.state.fields = {};
396+
if (this.props.values) {
397+
this.props.values = {};
398+
}
399+
}
400+
// Update
401+
this.setState({
402+
fields,
374403
});
404+
this.formDidChange({});
405+
return this;
406+
};
407+
408+
/**
409+
* Reset one or more fields
410+
* If there is no name, it will all be init.
411+
* @param names
412+
*/
413+
resetValues = (...names) => {
414+
const { fields } = this.state;
415+
const values = {};
416+
if (names.length) {
417+
names.forEach((name) => {
418+
values[name] = this.originalValues[name];
419+
});
420+
this.init(values);
421+
} else {
422+
// Init all
423+
Object.assign(values, this.originalValues);
424+
this.init(values);
425+
}
375426
// Update
376427
this.setState({
377428
fields,
378429
});
379-
this.formDidChange();
430+
this.formDidChange(values);
380431
return this;
381432
};
382433

@@ -395,7 +446,10 @@ export default (schemas, methods) => FormComponent => (
395446
return result;
396447
};
397448

398-
// Validate all
449+
/**
450+
* Validate all
451+
* @return {Boolean}
452+
*/
399453
validate = () => {
400454
const names = Object.keys(this.schemas);
401455
return this.validateByNames(...names);

test/setup.js

+17
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,23 @@ describe('Test Form change validation', () => {
109109
expect(await app.node.validate()).to.equal(true);
110110
});
111111

112+
it('API resetValues executed correctly', async () => {
113+
const app = mount(
114+
<TestApp3 />,
115+
);
116+
// change values
117+
app.node.changeValues({ sex: '1', city: '1', remarks: 'hello', hobby: ['1'] });
118+
expect(await app.node.validateByNames('city', 'remarks')).to.equal(true);
119+
expect(await app.node.validate()).to.equal(true);
120+
app.node.resetValues('remarks');
121+
expect(await app.node.validateByNames('remarks')).to.not.equal(true);
122+
expect(await app.node.formValues.remarks).to.be.empty;
123+
// reset all
124+
app.node.resetValues();
125+
expect(await app.node.formValues.hobby.length).to.be.equal(0);
126+
expect(await app.node.formValues.city).to.be.empty;
127+
});
128+
112129
});
113130

114131

0 commit comments

Comments
 (0)