(errors?: FieldErrors)
| 35 | }; |
| 36 | |
| 37 | export const useFormErrorScrollIntoView = (errors?: FieldErrors) => { |
| 38 | const errorRef = useRef(errors); |
| 39 | const beforeScroll = useRef<(names: string[]) => void>(); |
| 40 | |
| 41 | useEffect(() => { |
| 42 | errorRef.current = errors; |
| 43 | }, [errors]); |
| 44 | |
| 45 | const scrollToError = useCallback(() => { |
| 46 | if(errorRef.current) { |
| 47 | const names = Object.keys(errorRef.current); |
| 48 | if(beforeScroll.current) { |
| 49 | beforeScroll.current(names); |
| 50 | } |
| 51 | setTimeout(() => { |
| 52 | document.querySelector(`[name=${names[0]}]`)?.scrollIntoView({ behavior: 'smooth', block: 'center' }); |
| 53 | }); |
| 54 | } |
| 55 | }, [errorRef]); |
| 56 | |
| 57 | const registerErrorName = useCallback((name: string) => { |
| 58 | return { |
| 59 | ref: (ref: unknown) => { |
| 60 | (ref as HTMLElement)?.setAttribute('name', name); |
| 61 | } |
| 62 | }; |
| 63 | }, []); |
| 64 | |
| 65 | const setBeforeScrollCallback = useCallback((fc: (names: string[]) => void) => { |
| 66 | beforeScroll.current = fc; |
| 67 | }, [beforeScroll]); |
| 68 | |
| 69 | return { |
| 70 | scrollToError, |
| 71 | registerErrorName, |
| 72 | setBeforeScrollCallback, |
| 73 | }; |
| 74 | }; |
| 75 | |
| 76 | export const useQuery = () => { |
| 77 | const { search } = useLocation(); |
no outgoing calls
no test coverage detected