Skip to content

Commit 0a24c56

Browse files
fix: allow passing methods to useScrollToErrorOnSubmit hook
- Added optional methods parameter to UseScrollToErrorOnSubmitOptions - Hook now accepts methods explicitly or falls back to context - Updated story to pass methods explicitly to fix TypeError - This resolves the 'Cannot read properties of null' error in Storybook
1 parent 06f8435 commit 0a24c56

2 files changed

Lines changed: 8 additions & 2 deletions

File tree

apps/docs/src/remix-hook-form/scroll-to-error.stories.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ const ScrollToErrorHookExample = () => {
8686

8787
// Use the scroll-to-error hook with custom options
8888
useScrollToErrorOnSubmit({
89+
methods, // Pass the methods explicitly
8990
offset: 100, // Account for fixed header
9091
behavior: 'smooth',
9192
shouldFocus: true,

packages/components/src/remix-hook-form/hooks/useScrollToErrorOnSubmit.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
import { useEffect, useMemo } from 'react';
22
import { useRemixFormContext } from 'remix-hook-form';
3+
import type { UseRemixFormReturn } from 'remix-hook-form';
34
import { type ScrollToErrorOptions, scrollToFirstError } from '../../utils/scrollToError';
45

56
export interface UseScrollToErrorOnSubmitOptions extends ScrollToErrorOptions {
67
delay?: number;
78
enabled?: boolean;
89
scrollOnServerErrors?: boolean;
910
scrollOnMount?: boolean;
11+
methods?: UseRemixFormReturn<any>; // Optional methods parameter
1012
}
1113

1214
export const useScrollToErrorOnSubmit = (options: UseScrollToErrorOnSubmitOptions = {}) => {
13-
const { formState } = useRemixFormContext();
14-
const { delay = 100, enabled = true, scrollOnServerErrors = true, scrollOnMount = true, ...scrollOptions } = options;
15+
// Use provided methods or fall back to context
16+
const contextMethods = useRemixFormContext();
17+
const { methods, delay = 100, enabled = true, scrollOnServerErrors = true, scrollOnMount = true, ...scrollOptions } = options;
18+
const formMethods = methods || contextMethods;
19+
const { formState } = formMethods;
1520

1621
// Memoize scroll options to prevent unnecessary re-renders
1722
const memoizedScrollOptions = useMemo(

0 commit comments

Comments
 (0)