@@ -23,16 +23,32 @@ export const noPropagation = <E extends React.BaseSyntheticEvent>(
2323    callback ( event ) ; 
2424} 
2525
26+ /** 
27+  * An input validation function combines check logic with an explanatory error 
28+  * message, and provides a convenient mechanism to check the validity of an 
29+  * input itself (in which case it sets the input's validity state) or a raw 
30+  * input value (in which case it just returns the result, with no side effects). 
31+  */ 
32+ export  interface  InputValidationFunction  { 
33+     ( input : HTMLInputElement  |  string ) : boolean ; 
34+ } 
35+ 
2636export  const  inputValidation  =  ( 
2737    checkFn : ( input : string )  =>  boolean , 
2838    errorMessage : string 
29- )  =>  ( input : HTMLInputElement )  =>  { 
30-     const  inputValue  =  input . value ; 
31-     if  ( ! inputValue  ||  checkFn ( inputValue ) )  { 
32-         input . setCustomValidity ( '' ) ; 
39+ ) : InputValidationFunction  =>  ( input )  =>  { 
40+     const  isInput  =  typeof  input  !==  'string' ; 
41+     const  inputValue  =  isInput  ? input . value  : input ; 
42+ 
43+     if  ( isInput )  { 
44+         if  ( ! inputValue  ||  checkFn ( inputValue ) )  { 
45+             input . setCustomValidity ( '' ) ; 
46+         }  else  { 
47+             input . setCustomValidity ( errorMessage ) ; 
48+         } 
49+         input . reportValidity ( ) ; 
50+         return  input . validity . valid ; 
3351    }  else  { 
34-         input . setCustomValidity ( errorMessage ) ; 
52+         return   checkFn ( inputValue ) ; 
3553    } 
36-     input . reportValidity ( ) ; 
37-     return  input . validity . valid ; 
3854} 
0 commit comments