@@ -11,7 +11,7 @@ function noop() {}
1111class Form extends React . Component {
1212 static getDerivedStateFromProps ( nextProps , prevState ) {
1313 return {
14- formValue : nextProps . formValue || prevState . formValue
14+ formValue : nextProps . formValue || prevState . formValue ,
1515 } ;
1616 }
1717
@@ -25,7 +25,7 @@ class Form extends React.Component {
2525 state = {
2626 formError : { } ,
2727 validatingFields : { } ,
28- formValue : this . props . defaultFormValue || { }
28+ formValue : this . props . defaultFormValue || { } ,
2929 } ;
3030
3131 addField ( field ) {
@@ -132,7 +132,7 @@ class Form extends React.Component {
132132
133133 if ( ! isControlled ) {
134134 this . setState ( {
135- formValue : nextFormValue
135+ formValue : nextFormValue ,
136136 } ) ;
137137 }
138138
@@ -177,7 +177,7 @@ class Form extends React.Component {
177177
178178 if ( ! isControlled ) {
179179 this . setState ( {
180- formValue : nextFormValue
180+ formValue : nextFormValue ,
181181 } ) ;
182182 }
183183
@@ -232,8 +232,8 @@ class Form extends React.Component {
232232 this . setState ( {
233233 formError : {
234234 ...formError ,
235- [ name ] : null
236- }
235+ [ name ] : null ,
236+ } ,
237237 } ) ;
238238 }
239239
@@ -242,22 +242,22 @@ class Form extends React.Component {
242242 this . setState ( {
243243 formError : {
244244 ...formError ,
245- [ name ] : message
246- }
245+ [ name ] : message ,
246+ } ,
247247 } ) ;
248248 }
249249
250250 cleanErrors ( ) {
251251 this . setState ( {
252- formError : { }
252+ formError : { } ,
253253 } ) ;
254254 }
255255
256256 setErrors ( errors ) {
257257 const { formError } = this . state ;
258258 this . setState ( {
259259 ...formError ,
260- ...errors
260+ ...errors ,
261261 } ) ;
262262 }
263263
@@ -266,7 +266,11 @@ class Form extends React.Component {
266266 this . fields
267267 . filter ( field => field . props . name === name )
268268 . forEach ( field => {
269+ const disableValidator = field . getProp ( "disableValidator" ) ;
270+ if ( disableValidator ) return ;
271+
269272 const fieldProps = field . props ;
273+
270274 if ( fieldProps . required ) {
271275 fieldValidators . unshift ( value => {
272276 if ( isEmptyValue ( value ) ) {
@@ -339,7 +343,7 @@ class Form extends React.Component {
339343
340344 return {
341345 name,
342- message
346+ message,
343347 } ;
344348 } ) ;
345349 }
@@ -362,7 +366,10 @@ class Form extends React.Component {
362366 } else if ( ret === false ) {
363367 cb ( `${ name } check fail` ) ;
364368 } else if ( ret && ret . then ) {
365- ret . then ( ( ) => cb ( ) , e => cb ( e ) ) ;
369+ ret . then (
370+ ( ) => cb ( ) ,
371+ e => cb ( e )
372+ ) ;
366373 } else {
367374 cb ( ret ) ;
368375 }
@@ -381,7 +388,7 @@ class Form extends React.Component {
381388
382389 const lockId = ++ this . fieldLocks [ name ] ;
383390
384- //是否异步探测
391+ //是否异步检测
385392 let asyncTimer = setTimeout ( ( ) => {
386393 asyncTimer = null ;
387394
@@ -390,19 +397,21 @@ class Form extends React.Component {
390397 this . setState ( {
391398 validatingFields : {
392399 ...validatingFields ,
393- [ name ] : true
394- }
400+ [ name ] : true ,
401+ } ,
402+ formError : {
403+ ...formError ,
404+ [ name ] : null ,
405+ } ,
395406 } ) ;
396407 } , asyncTestDelay ) ;
397- // let isAsync = true;
398408
399409 this . _validateField (
400410 name ,
401411 ( errors , value ) => {
402412 if ( asyncTimer ) {
403413 clearTimeout ( asyncTimer ) ;
404414 }
405- // isAsync = false;
406415
407416 if ( lockId !== this . fieldLocks [ name ] ) {
408417 callback ( errors , value , true /* abort state */ ) ;
@@ -413,12 +422,12 @@ class Form extends React.Component {
413422 {
414423 formError : {
415424 ...formError ,
416- [ name ] : errors ? errors [ 0 ] . message : null
425+ [ name ] : errors ? errors [ 0 ] . message : null ,
417426 } ,
418427 validatingFields : {
419428 ...validatingFields ,
420- [ name ] : false
421- }
429+ [ name ] : false ,
430+ } ,
422431 } ,
423432 ( ) => {
424433 callback ( errors , value ) ;
@@ -431,9 +440,11 @@ class Form extends React.Component {
431440
432441 validate ( callback , triggerType ) {
433442 callback = typeof callback === "function" ? callback : noop ;
434-
443+ const formError = { } ;
444+ let asyncUpdateTimer = null ;
445+ let hasRunComplete = false ;
435446 const { asyncTestDelay } = this . props ;
436- const { formValue, formError } = this . state ;
447+ const { formValue } = this . state ;
437448 this . fieldLocks = { } ; //validate优先级高于validateField
438449 const lockId = ++ this . formLockId ;
439450 const fields = this . fields ;
@@ -446,7 +457,7 @@ class Form extends React.Component {
446457
447458 this . setState ( {
448459 formError,
449- validatingFields
460+ validatingFields,
450461 } ) ;
451462 } ;
452463
@@ -459,22 +470,25 @@ class Form extends React.Component {
459470 }
460471
461472 if ( validCounter <= 0 ) {
473+ hasRunComplete = true ;
474+ if ( asyncUpdateTimer ) {
475+ clearTimeout ( asyncUpdateTimer ) ;
476+ asyncUpdateTimer = null ;
477+ }
478+
462479 if ( lockId !== this . formLockId ) {
463480 callback (
464481 allErrors . length ? allErrors : null ,
465482 formValue ,
466483 true /* abort state */
467484 ) ;
468- console . log ( "abort" ) ;
469485 return ;
470486 }
471487
472- console . log ( "validate" ) ;
473-
474488 this . setState (
475489 {
476490 formError,
477- validatingFields : { }
491+ validatingFields : { } ,
478492 } ,
479493 ( ) => {
480494 callback (
@@ -487,17 +501,15 @@ class Form extends React.Component {
487501 } ;
488502
489503 if ( fields . length ) {
490- //包含多个异步校验的情况下只执行一次
491- let hasUpdate = false ;
492504 //校验初始化
493505 fields . forEach ( field => {
494506 const name = field . props . name ;
495507 validCounter ++ ;
496508 validatingFields [ name ] = true ;
497509
498- if ( ! ( name in formError ) ) {
499- formError [ name ] = null ;
500- }
510+ // if (!(name in formError)) {
511+ // formError[name] = null;
512+ // }
501513 } ) ;
502514
503515 //开始进行字段校验
@@ -509,11 +521,6 @@ class Form extends React.Component {
509521 let asyncTimer = setTimeout ( ( ) => {
510522 isAsyncValidate = true ;
511523 asyncTimer = null ;
512-
513- if ( hasUpdate ) return ;
514- hasUpdate = true ;
515-
516- updateFormState ( ) ;
517524 } , asyncTestDelay ) ;
518525
519526 this . _validateField (
@@ -536,6 +543,14 @@ class Form extends React.Component {
536543 triggerType
537544 ) ;
538545 } ) ;
546+
547+ //如果校验方法中存在异步校验则先显示同步校验的信息及异步状态
548+ asyncUpdateTimer = setTimeout ( ( ) => {
549+ asyncUpdateTimer = null ;
550+ //如果不存在异步校验,hasRunComplete会为true
551+ if ( hasRunComplete ) return ;
552+ updateFormState ( ) ;
553+ } , asyncTestDelay ) ;
539554 } else {
540555 callback ( null , formValue ) ;
541556 }
@@ -579,7 +594,7 @@ class Form extends React.Component {
579594 className,
580595 onSubmit,
581596 component : Component ,
582- children
597+ children,
583598 } = this . props ;
584599
585600 return (
@@ -605,11 +620,12 @@ Form.propTypes = {
605620 getDefaultFieldValue : PropTypes . func ,
606621 renderControlExtra : PropTypes . func ,
607622 formValue : PropTypes . object ,
623+ disableValidator : PropTypes . func ,
608624 validators : PropTypes . object ,
609625 validateDelay : PropTypes . number ,
610626 validateTrigger : PropTypes . oneOfType ( [
611627 PropTypes . oneOf ( [ "blur" , "change" , "none" ] ) ,
612- PropTypes . array
628+ PropTypes . array ,
613629 ] ) ,
614630 asyncTestDelay : PropTypes . number ,
615631 component : PropTypes . node ,
@@ -626,23 +642,24 @@ Form.propTypes = {
626642 normalizeFieldValue : PropTypes . func ,
627643 onSubmit : PropTypes . func ,
628644 onChange : PropTypes . func ,
629- getInputProps : PropTypes . func
645+ getInputProps : PropTypes . func ,
630646} ;
631647
632648Form . defaultProps = {
633649 prefixCls : "nex-form" ,
634650 className : "" ,
635651 style : { } ,
652+ disableValidator : false ,
636653 validators : { } ,
637654 path2obj : true ,
638655 component : "form" ,
639- asyncTestDelay : 100 ,
656+ asyncTestDelay : 16 ,
640657 validateDelay : 0 ,
641658 validateTrigger : [ "change" ] , //"blur",
642659 labelPosition : "left" ,
643660 labelAlign : "right" ,
644661 clearErrorOnFocus : true ,
645- inline : false
662+ inline : false ,
646663} ;
647664
648665export default Form ;
0 commit comments