@@ -22,12 +22,12 @@ interface FormValidationProps<T> extends Validation<T> {
22
22
focus ?: ( ) => void
23
23
}
24
24
25
- export function useFormValidation < T > ( props : FormValidationProps < T > , state : FormValidationState , ref : RefObject < ValidatableElement > ) {
25
+ export function useFormValidation < T > ( props : FormValidationProps < T > , state : FormValidationState , ref : RefObject < ValidatableElement > | undefined ) {
26
26
let { validationBehavior, focus} = props ;
27
27
28
28
// This is a useLayoutEffect so that it runs before the useEffect in useFormValidationState, which commits the validation change.
29
29
useLayoutEffect ( ( ) => {
30
- if ( validationBehavior === 'native' && ref . current ) {
30
+ if ( validationBehavior === 'native' && ref ? .current ) {
31
31
let errorMessage = state . realtimeValidation . isInvalid ? state . realtimeValidation . validationErrors . join ( ' ' ) || 'Invalid value.' : '' ;
32
32
ref . current . setCustomValidity ( errorMessage ) ;
33
33
@@ -55,8 +55,8 @@ export function useFormValidation<T>(props: FormValidationProps<T>, state: FormV
55
55
}
56
56
57
57
// Auto focus the first invalid input in a form, unless the error already had its default prevented.
58
- let form = ref . current ?. form ;
59
- if ( ! e . defaultPrevented && form && getFirstInvalidInput ( form ) === ref . current ) {
58
+ let form = ref ? .current ?. form ;
59
+ if ( ! e . defaultPrevented && ref && form && getFirstInvalidInput ( form ) === ref . current ) {
60
60
if ( focus ) {
61
61
focus ( ) ;
62
62
} else {
@@ -76,7 +76,7 @@ export function useFormValidation<T>(props: FormValidationProps<T>, state: FormV
76
76
} ) ;
77
77
78
78
useEffect ( ( ) => {
79
- let input = ref . current ;
79
+ let input = ref ? .current ;
80
80
if ( ! input ) {
81
81
return ;
82
82
}
0 commit comments