@@ -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 ) ;
0 commit comments