Skip to content

Commit b280240

Browse files
authored
Do not crash in useFormValidation if ref is undefined (#5372)
1 parent b1e20e2 commit b280240

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

packages/@react-aria/form/src/useFormValidation.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ interface FormValidationProps<T> extends Validation<T> {
2222
focus?: () => void
2323
}
2424

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) {
2626
let {validationBehavior, focus} = props;
2727

2828
// This is a useLayoutEffect so that it runs before the useEffect in useFormValidationState, which commits the validation change.
2929
useLayoutEffect(() => {
30-
if (validationBehavior === 'native' && ref.current) {
30+
if (validationBehavior === 'native' && ref?.current) {
3131
let errorMessage = state.realtimeValidation.isInvalid ? state.realtimeValidation.validationErrors.join(' ') || 'Invalid value.' : '';
3232
ref.current.setCustomValidity(errorMessage);
3333

@@ -55,8 +55,8 @@ export function useFormValidation<T>(props: FormValidationProps<T>, state: FormV
5555
}
5656

5757
// 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) {
6060
if (focus) {
6161
focus();
6262
} else {
@@ -76,7 +76,7 @@ export function useFormValidation<T>(props: FormValidationProps<T>, state: FormV
7676
});
7777

7878
useEffect(() => {
79-
let input = ref.current;
79+
let input = ref?.current;
8080
if (!input) {
8181
return;
8282
}

0 commit comments

Comments
 (0)