|
| 1 | +import { useEffect, useMemo } from 'react'; |
| 2 | +import type { FieldValues } from 'react-hook-form'; |
| 3 | +import { useRemixFormContext } from 'remix-hook-form'; |
| 4 | +import type { UseRemixFormReturn } from 'remix-hook-form'; |
| 5 | +import { type ScrollToErrorOptions, scrollToFirstError } from '../../utils/scrollToError'; |
| 6 | + |
| 7 | +export interface UseScrollToErrorOnSubmitOptions extends ScrollToErrorOptions { |
| 8 | + delay?: number; |
| 9 | + enabled?: boolean; |
| 10 | + scrollOnServerErrors?: boolean; |
| 11 | + scrollOnMount?: boolean; |
| 12 | + methods?: UseRemixFormReturn<FieldValues>; // Optional methods parameter |
| 13 | +} |
| 14 | + |
| 15 | +export const useScrollToErrorOnSubmit = (options: UseScrollToErrorOnSubmitOptions = {}) => { |
| 16 | + // Use provided methods or fall back to context |
| 17 | + const contextMethods = useRemixFormContext(); |
| 18 | + const { |
| 19 | + methods, |
| 20 | + delay = 100, |
| 21 | + enabled = true, |
| 22 | + scrollOnServerErrors = true, |
| 23 | + scrollOnMount = true, |
| 24 | + ...scrollOptions |
| 25 | + } = options; |
| 26 | + const formMethods = methods || contextMethods; |
| 27 | + |
| 28 | + const { formState } = formMethods; |
| 29 | + |
| 30 | + // Memoize scroll options to prevent unnecessary re-renders |
| 31 | + const { behavior, block, inline, offset, shouldFocus, retryAttempts, selectors } = scrollOptions; |
| 32 | + |
| 33 | + // biome-ignore lint: Compare `selectors` by value via join to avoid unstable array identity. |
| 34 | + const memoizedScrollOptions = useMemo( |
| 35 | + () => ({ |
| 36 | + behavior, |
| 37 | + block, |
| 38 | + inline, |
| 39 | + offset, |
| 40 | + shouldFocus, |
| 41 | + retryAttempts, |
| 42 | + selectors, |
| 43 | + }), |
| 44 | + [behavior, block, inline, offset, shouldFocus, retryAttempts, selectors?.join(',')], |
| 45 | + ); |
| 46 | + |
| 47 | + // Handle form submission errors |
| 48 | + useEffect(() => { |
| 49 | + if (!enabled) return; |
| 50 | + const hasErrors = Object.keys(formState.errors).length > 0; |
| 51 | + |
| 52 | + // Scroll after submission attempt when errors exist |
| 53 | + if (!formState.isSubmitting && hasErrors) { |
| 54 | + const timeoutId = setTimeout(() => { |
| 55 | + scrollToFirstError(formState.errors, memoizedScrollOptions); |
| 56 | + }, delay); |
| 57 | + |
| 58 | + return () => clearTimeout(timeoutId); |
| 59 | + } |
| 60 | + }, [formState.errors, formState.isSubmitting, enabled, delay, memoizedScrollOptions]); |
| 61 | + |
| 62 | + // Handle server-side validation errors on mount (Remix SSR) |
| 63 | + useEffect(() => { |
| 64 | + if (!(enabled && scrollOnMount && scrollOnServerErrors)) return; |
| 65 | + const hasErrors = Object.keys(formState.errors).length > 0; |
| 66 | + |
| 67 | + if (hasErrors && !formState.isSubmitting) { |
| 68 | + const timeoutId = setTimeout(() => { |
| 69 | + scrollToFirstError(formState.errors, memoizedScrollOptions); |
| 70 | + }, delay); |
| 71 | + |
| 72 | + return () => clearTimeout(timeoutId); |
| 73 | + } |
| 74 | + }, [ |
| 75 | + enabled, |
| 76 | + scrollOnMount, |
| 77 | + scrollOnServerErrors, |
| 78 | + formState.errors, |
| 79 | + formState.isSubmitting, |
| 80 | + delay, |
| 81 | + memoizedScrollOptions, |
| 82 | + ]); |
| 83 | +}; |
0 commit comments