@@ -253,183 +253,174 @@ const component: React.FC<Props> = ({
253
253
} ,
254
254
[ err , attributesInput , option ] ,
255
255
) ;
256
- const check = useCallback (
257
- async ( ) => {
258
- const { reg, min, max, type, name, check, length, regMsg, locale, compare, required, msgOnSuccess, customFunc } = option ;
259
- if ( ! check ) {
260
- return ;
261
- }
262
- if ( type ) {
263
- if ( VALIDATE_OPTION_TYPE_LIST . indexOf ( type ) !== - 1 ) {
264
- if ( ! message [ locale ] || ! message [ locale ] [ TYPE ] ) {
265
- console . error ( REACT_INPUTS_VALIDATION_CUSTOM_ERROR_MESSAGE_EXAMPLE ) ;
256
+ const check = useCallback ( async ( ) => {
257
+ const { reg, min, max, type, name, check, length, regMsg, locale, compare, required, msgOnSuccess, customFunc } = option ;
258
+ if ( ! check ) {
259
+ return ;
260
+ }
261
+ if ( type ) {
262
+ if ( VALIDATE_OPTION_TYPE_LIST . indexOf ( type ) !== - 1 ) {
263
+ if ( ! message [ locale ] || ! message [ locale ] [ TYPE ] ) {
264
+ console . error ( REACT_INPUTS_VALIDATION_CUSTOM_ERROR_MESSAGE_EXAMPLE ) ;
265
+ return ;
266
+ }
267
+ const msg = message [ locale ] [ TYPE ] ;
268
+ const nameText = name ? name : '' ;
269
+ if ( required ) {
270
+ if ( validator . empty ( internalValue ) ) {
271
+ handleCheckEnd ( true , msg . empty ( nameText ) ) ;
266
272
return ;
267
273
}
268
- const msg = message [ locale ] [ TYPE ] ;
269
- const nameText = name ? name : '' ;
270
- if ( required ) {
271
- if ( validator . empty ( internalValue ) ) {
272
- handleCheckEnd ( true , msg . empty ( nameText ) ) ;
274
+ }
275
+ if ( String ( internalValue ) !== '' ) {
276
+ if ( reg ) {
277
+ if ( validator [ 'reg' ] ( reg , internalValue ) ) {
278
+ handleCheckEnd ( true , regMsg !== '' ? regMsg : msg . invalid ( nameText ) ) ;
273
279
return ;
274
280
}
275
281
}
276
- if ( String ( internalValue ) !== '' ) {
277
- if ( reg ) {
278
- if ( validator [ 'reg' ] ( reg , internalValue ) ) {
279
- handleCheckEnd ( true , regMsg !== '' ? regMsg : msg . invalid ( nameText ) ) ;
280
- return ;
281
- }
282
- }
283
- if ( type === VALIDATE_OPTION_TYPE_LIST [ 0 ] ) {
284
- if ( min || max ) {
285
- if ( min && max ) {
286
- if ( String ( internalValue ) . length < min || String ( internalValue ) . length > max ) {
287
- handleCheckEnd ( true , msg . inBetween ( nameText ) ( min ) ( max ) ) ;
282
+ if ( type === VALIDATE_OPTION_TYPE_LIST [ 0 ] ) {
283
+ if ( min || max ) {
284
+ if ( min && max ) {
285
+ if ( String ( internalValue ) . length < min || String ( internalValue ) . length > max ) {
286
+ handleCheckEnd ( true , msg . inBetween ( nameText ) ( min ) ( max ) ) ;
287
+ return ;
288
+ }
289
+ } else {
290
+ if ( min ) {
291
+ if ( String ( internalValue ) . length < min ) {
292
+ handleCheckEnd ( true , msg . lessThan ( nameText ) ( min ) ) ;
288
293
return ;
289
294
}
290
- } else {
291
- if ( min ) {
292
- if ( String ( internalValue ) . length < min ) {
293
- handleCheckEnd ( true , msg . lessThan ( nameText ) ( min ) ) ;
294
- return ;
295
- }
296
- }
297
- if ( max ) {
298
- if ( String ( internalValue ) . length > max ) {
299
- handleCheckEnd ( true , msg . greaterThan ( nameText ) ( max ) ) ;
300
- return ;
301
- }
302
- }
303
295
}
304
- }
305
- if ( length ) {
306
- if ( String ( internalValue ) . length !== length ) {
307
- handleCheckEnd ( true , msg . lengthEqual ( nameText ) ( length ) ) ;
308
- return ;
296
+ if ( max ) {
297
+ if ( String ( internalValue ) . length > max ) {
298
+ handleCheckEnd ( true , msg . greaterThan ( nameText ) ( max ) ) ;
299
+ return ;
300
+ }
309
301
}
310
302
}
311
303
}
312
- if ( type === VALIDATE_OPTION_TYPE_LIST [ 1 ] ) {
313
- if ( ! validator [ type ] ( internalValue , null , null ) ) {
314
- handleCheckEnd ( true , msg . invalid ( nameText ) ) ;
304
+ if ( length ) {
305
+ if ( String ( internalValue ) . length !== length ) {
306
+ handleCheckEnd ( true , msg . lengthEqual ( nameText ) ( length ) ) ;
315
307
return ;
316
308
}
317
- if ( min || max ) {
318
- if ( min && max ) {
319
- if ( ! validator [ type ] ( internalValue , min , max ) ) {
320
- handleCheckEnd ( true , msg . inBetween ( nameText ) ( min ) ( max ) ) ;
309
+ }
310
+ }
311
+ if ( type === VALIDATE_OPTION_TYPE_LIST [ 1 ] ) {
312
+ if ( ! validator [ type ] ( internalValue , null , null ) ) {
313
+ handleCheckEnd ( true , msg . invalid ( nameText ) ) ;
314
+ return ;
315
+ }
316
+ if ( min || max ) {
317
+ if ( min && max ) {
318
+ if ( ! validator [ type ] ( internalValue , min , max ) ) {
319
+ handleCheckEnd ( true , msg . inBetween ( nameText ) ( min ) ( max ) ) ;
320
+ return ;
321
+ }
322
+ } else {
323
+ if ( min ) {
324
+ if ( ! validator [ type ] ( internalValue , min ) ) {
325
+ handleCheckEnd ( true , msg . lessThan ( nameText ) ( min ) ) ;
321
326
return ;
322
327
}
323
- } else {
324
- if ( min ) {
325
- if ( ! validator [ type ] ( internalValue , min ) ) {
326
- handleCheckEnd ( true , msg . lessThan ( nameText ) ( min ) ) ;
327
- return ;
328
- }
329
- }
330
- if ( max ) {
331
- if ( ! validator [ type ] ( internalValue , 0 , max ) ) {
332
- handleCheckEnd ( true , msg . greaterThan ( nameText ) ( max ) ) ;
333
- return ;
334
- }
335
- }
336
328
}
337
- }
338
- if ( length ) {
339
- if ( String ( internalValue ) . length !== length ) {
340
- handleCheckEnd ( true , msg . lengthEqual ( nameText ) ( length ) ) ;
341
- return ;
329
+ if ( max ) {
330
+ if ( ! validator [ type ] ( internalValue , 0 , max ) ) {
331
+ handleCheckEnd ( true , msg . greaterThan ( nameText ) ( max ) ) ;
332
+ return ;
333
+ }
342
334
}
343
335
}
344
336
}
345
- if ( compare && compare !== '' ) {
346
- if ( internalValue !== compare ) {
347
- handleCheckEnd ( true , msg . twoInputsNotEqual ( ) ) ;
337
+ if ( length ) {
338
+ if ( String ( internalValue ) . length !== length ) {
339
+ handleCheckEnd ( true , msg . lengthEqual ( nameText ) ( length ) ) ;
348
340
return ;
349
341
}
350
342
}
351
343
}
352
- if ( customFunc && typeof customFunc === 'function' ) {
353
- const customFuncResult = await customFunc ( internalValue ) ;
354
- /* istanbul ignore next because of async problem */
355
- if ( typeof customFuncResult === 'object' ) {
356
- if ( typeof customFuncResult . error === 'boolean' && typeof customFuncResult . message === 'string' ) {
357
- if ( customFuncResult . error === false && customFuncResult . showOnSuccess === true ) {
358
- setSuccessMsg ( customFuncResult . message ) ;
359
- }
360
- handleCheckEnd ( customFuncResult . error , customFuncResult . message , true ) ;
361
- }
344
+ if ( compare && compare !== '' ) {
345
+ if ( internalValue !== compare ) {
346
+ handleCheckEnd ( true , msg . twoInputsNotEqual ( ) ) ;
362
347
return ;
363
348
}
364
- /* istanbul ignore next because of async problem */
365
- if ( customFuncResult !== true ) {
366
- handleCheckEnd ( true , customFuncResult , true ) ;
367
- return ;
349
+ }
350
+ }
351
+ if ( customFunc && typeof customFunc === 'function' ) {
352
+ const customFuncResult = await customFunc ( internalValue ) ;
353
+ if ( ! ( $input && $input . current ) ) {
354
+ return ;
355
+ }
356
+ /* istanbul ignore next because of async problem */
357
+ if ( typeof customFuncResult === 'object' ) {
358
+ if ( typeof customFuncResult . error === 'boolean' && typeof customFuncResult . message === 'string' ) {
359
+ if ( customFuncResult . error === false && customFuncResult . showOnSuccess === true ) {
360
+ setSuccessMsg ( customFuncResult . message ) ;
361
+ }
362
+ handleCheckEnd ( customFuncResult . error , customFuncResult . message , true ) ;
368
363
}
364
+ return ;
369
365
}
370
- if ( msgOnSuccess ) {
371
- setSuccessMsg ( msgOnSuccess ) ;
366
+ /* istanbul ignore next because of async problem */
367
+ if ( customFuncResult !== true ) {
368
+ handleCheckEnd ( true , customFuncResult , true ) ;
369
+ return ;
372
370
}
373
- handleCheckEnd ( false , msgOnSuccess ) ;
374
- } else {
375
- console . error ( `The valid ${ utils . toCamelCase ( TYPE ) ( true ) } "type" options in validationOption are [${ VALIDATE_OPTION_TYPE_LIST . map ( i => i ) } ]` ) ;
376
371
}
372
+ if ( msgOnSuccess ) {
373
+ setSuccessMsg ( msgOnSuccess ) ;
374
+ }
375
+ handleCheckEnd ( false , msgOnSuccess ) ;
377
376
} else {
378
- console . error ( 'Please provide "type" in validationOption' ) ;
377
+ console . error ( `The valid ${ utils . toCamelCase ( TYPE ) ( true ) } "type" options in validationOption are [ ${ VALIDATE_OPTION_TYPE_LIST . map ( i => i ) } ]` ) ;
379
378
}
379
+ } else {
380
+ console . error ( 'Please provide "type" in validationOption' ) ;
381
+ }
382
+ } , [ internalValue , option ] ) ;
383
+ const handleCheckEnd = useCallback (
384
+ ( err : boolean , message : string , fromCustomFunc : boolean = false ) => {
385
+ let msg = message ;
386
+ const { msgOnError } = option ;
387
+ if ( err && msgOnError && ! fromCustomFunc ) {
388
+ msg = msgOnError ;
389
+ }
390
+ setErr ( err ) ;
391
+ setMsg ( msg ) ;
392
+ validationCallback && validationCallback ( err ) ;
380
393
} ,
381
- [ internalValue , option ] ,
394
+ [ option . msgOnError ] ,
382
395
) ;
383
- const handleCheckEnd = useCallback ( ( err : boolean , message : string , fromCustomFunc : boolean = false ) => {
384
- let msg = message ;
385
- const { msgOnError } = option ;
386
- if ( err && msgOnError && ! fromCustomFunc ) {
387
- msg = msgOnError ;
396
+ useEffect ( ( ) => {
397
+ if ( validate ) {
398
+ check ( ) ;
388
399
}
389
- setErr ( err ) ;
390
- setMsg ( msg ) ;
391
- validationCallback && validationCallback ( err ) ;
392
- } , [ option . msgOnError ] ) ;
393
- useEffect (
394
- ( ) => {
395
- if ( validate ) {
400
+ } , [ validate ] ) ;
401
+ useEffect ( ( ) => {
402
+ setInternalValue ( String ( value ) ) ;
403
+ } , [ value ] ) ;
404
+ useEffect ( ( ) => {
405
+ /* istanbul ignore next because of https://github.com/airbnb/enzyme/issues/441 && https://github.com/airbnb/enzyme/blob/master/docs/future.md */
406
+ if ( typeof prevInternalValue !== 'undefined' && prevInternalValue !== internalValue ) {
407
+ if ( option . customFunc && onKeyUp ) {
396
408
check ( ) ;
397
409
}
398
- } ,
399
- [ validate ] ,
400
- ) ;
401
- useEffect (
402
- ( ) => {
403
- setInternalValue ( String ( value ) ) ;
404
- } ,
405
- [ value ] ,
406
- ) ;
407
- useEffect (
408
- ( ) => {
409
- /* istanbul ignore next because of https://github.com/airbnb/enzyme/issues/441 && https://github.com/airbnb/enzyme/blob/master/docs/future.md */
410
- if ( typeof prevInternalValue !== 'undefined' && prevInternalValue !== internalValue ) {
411
- if ( option . customFunc && onKeyUp ) {
412
- check ( ) ;
410
+ }
411
+ } , [ internalValue ] ) ;
412
+ useEffect ( ( ) => {
413
+ if ( asyncObj ) {
414
+ if ( asyncObj . message ) {
415
+ if ( asyncObj . showOnError ) {
416
+ handleCheckEnd ( asyncObj . error , asyncObj . message ) ;
413
417
}
414
- }
415
- } ,
416
- [ internalValue ] ,
417
- ) ;
418
- useEffect (
419
- ( ) => {
420
- if ( asyncObj ) {
421
- if ( asyncObj . message ) {
422
- if ( asyncObj . showOnError ) {
423
- handleCheckEnd ( asyncObj . error , asyncObj . message ) ;
424
- }
425
- if ( ! asyncObj . error && asyncObj . showOnSuccess ) {
426
- setSuccessMsg ( asyncObj . message ) ;
427
- }
418
+ if ( ! asyncObj . error && asyncObj . showOnSuccess ) {
419
+ setSuccessMsg ( asyncObj . message ) ;
428
420
}
429
421
}
430
- } ,
431
- [ asyncMsgObj ] ,
432
- ) ;
422
+ }
423
+ } , [ asyncMsgObj ] ) ;
433
424
const wrapperClass = `${ classNameWrapper } ${ reactInputsValidationCss [ `${ TYPE } __wrapper` ] } ${ err && reactInputsValidationCss [ 'error' ] } ${ successMsg !== '' &&
434
425
! err &&
435
426
reactInputsValidationCss [ 'success' ] } ${ disabled && reactInputsValidationCss [ 'disabled' ] } `;
0 commit comments