@@ -12,7 +12,7 @@ import {
12
12
IUpdateInputsWithValue ,
13
13
ValidationError ,
14
14
} from './interfaces' ;
15
- import { throttle , isObject , isString } from './utils' ;
15
+ import { debounce , isObject , isString } from './utils' ;
16
16
import * as utils from './utils' ;
17
17
import validationRules from './validationRules' ;
18
18
import { PassDownProps } from './withFormsy' ;
@@ -103,7 +103,7 @@ export class Formsy extends React.Component<FormsyProps, FormsyState> {
103
103
formElement : 'form' ,
104
104
} ;
105
105
106
- private readonly throttledValidateForm : ( ) => void ;
106
+ private readonly debouncedValidateForm : ( ) => void ;
107
107
108
108
public constructor ( props : FormsyProps ) {
109
109
super ( props ) ;
@@ -122,7 +122,7 @@ export class Formsy extends React.Component<FormsyProps, FormsyState> {
122
122
} ;
123
123
this . inputs = [ ] ;
124
124
this . emptyArray = [ ] ;
125
- this . throttledValidateForm = throttle ( this . validateForm , ONE_RENDER_FRAME ) ;
125
+ this . debouncedValidateForm = debounce ( this . validateForm , ONE_RENDER_FRAME ) ;
126
126
}
127
127
128
128
public componentDidMount = ( ) => {
@@ -336,20 +336,15 @@ export class Formsy extends React.Component<FormsyProps, FormsyState> {
336
336
onChange ( this . getModel ( ) , this . isChanged ( ) ) ;
337
337
}
338
338
339
- // Will be triggered immediately & every one frame rate
340
- this . throttledValidateForm ( ) ;
339
+ this . debouncedValidateForm ( ) ;
341
340
} ;
342
341
343
342
// Method put on each input component to unregister
344
343
// itself from the form
345
344
public detachFromForm = < V > ( component : InputComponent < V > ) => {
346
- const componentPos = this . inputs . indexOf ( component ) ;
345
+ this . inputs = this . inputs . filter ( ( input ) => input !== component ) ;
347
346
348
- if ( componentPos !== - 1 ) {
349
- this . inputs = this . inputs . slice ( 0 , componentPos ) . concat ( this . inputs . slice ( componentPos + 1 ) ) ;
350
- }
351
-
352
- this . throttledValidateForm ( ) ;
347
+ this . debouncedValidateForm ( ) ;
353
348
} ;
354
349
355
350
// Checks if the values have changed from their initial value
@@ -437,7 +432,7 @@ export class Formsy extends React.Component<FormsyProps, FormsyState> {
437
432
// and check their state
438
433
public validateForm = ( ) => {
439
434
// We need a callback as we are validating all inputs again. This will
440
- // run when the last component has set its state
435
+ // run when the last input has set its state
441
436
const onValidationComplete = ( ) => {
442
437
const allIsValid = this . inputs . every ( ( component ) => component . state . isValid ) ;
443
438
@@ -449,24 +444,17 @@ export class Formsy extends React.Component<FormsyProps, FormsyState> {
449
444
} ) ;
450
445
} ;
451
446
452
- // Run validation again in case affected by other inputs. The
453
- // last component validated will run the onValidationComplete callback
454
- this . inputs . forEach ( ( component , index ) => {
455
- const validationState = this . runValidation ( component ) ;
456
- const isFinalInput = index === this . inputs . length - 1 ;
457
- const callback = isFinalInput ? onValidationComplete : null ;
458
- component . setState ( validationState , callback ) ;
459
- } ) ;
460
-
461
- // If there are no inputs, set state where form is ready to trigger
462
- // change event. New inputs might be added later
463
- if ( ! this . inputs . length ) {
464
- this . setState (
465
- {
466
- canChange : true ,
467
- } ,
468
- onValidationComplete ,
469
- ) ;
447
+ if ( this . inputs . length === 0 ) {
448
+ onValidationComplete ( ) ;
449
+ } else {
450
+ // Run validation again in case affected by other inputs. The
451
+ // last component validated will run the onValidationComplete callback
452
+ this . inputs . forEach ( ( component , index ) => {
453
+ const validationState = this . runValidation ( component ) ;
454
+ const isLastInput = index === this . inputs . length - 1 ;
455
+ const callback = isLastInput ? onValidationComplete : null ;
456
+ component . setState ( validationState , callback ) ;
457
+ } ) ;
470
458
}
471
459
} ;
472
460
0 commit comments