From cda43b5c7f8f4244375720f931a3c56d43336492 Mon Sep 17 00:00:00 2001 From: Saumya Pandey Date: Fri, 10 Dec 2021 12:20:06 +0530 Subject: [PATCH] fix: focus input when page refresh (#2814) * fix: focus input when page refresh * fix: increase to 100ms --- app/scenes/Search/components/SearchInput.tsx | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/app/scenes/Search/components/SearchInput.tsx b/app/scenes/Search/components/SearchInput.tsx index 2f27779e7..36143a343 100644 --- a/app/scenes/Search/components/SearchInput.tsx +++ b/app/scenes/Search/components/SearchInput.tsx @@ -12,15 +12,22 @@ function SearchInput({ defaultValue, ...rest }: Props) { const theme = useTheme(); const inputRef = React.useRef(null); + const focusInput = React.useCallback(() => { + inputRef.current?.focus(); + }, []); + React.useEffect(() => { // ensure that focus is placed at end of input const len = (defaultValue || "").length; inputRef.current?.setSelectionRange(len, len); - }, [defaultValue]); + const timeoutId = setTimeout(() => { + focusInput(); + }, 100); // arbitrary number - const focusInput = React.useCallback(() => { - inputRef.current?.focus(); - }, []); + return () => { + clearTimeout(timeoutId); + }; + }, [defaultValue, focusInput]); return (