fix: focus input when page refresh (#2814)

* fix: focus input when page refresh

* fix: increase to 100ms
This commit is contained in:
Saumya Pandey
2021-12-10 12:20:06 +05:30
committed by GitHub
parent 7f8db01168
commit cda43b5c7f

View File

@@ -12,15 +12,22 @@ function SearchInput({ defaultValue, ...rest }: Props) {
const theme = useTheme();
const inputRef = React.useRef<HTMLInputElement>(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 (
<Wrapper align="center">