diff --git a/components/Search.tsx b/components/Search.tsx index 60c416db..ef67627e 100644 --- a/components/Search.tsx +++ b/components/Search.tsx @@ -1,6 +1,6 @@ import Link from 'next/link' import { CaretRight, MagnifyingGlass, XCircle } from 'phosphor-react' -import React, { useState, useEffect, FC, Dispatch, SetStateAction } from 'react' +import React, { useState, useEffect, FC, Dispatch, SetStateAction, useCallback } from 'react' import { Modal, Skeleton, TextInput } from '~/src' import { cn } from '~/src/helpers/cn' import { getData, storeData } from '~/utils/Searching' @@ -29,12 +29,21 @@ interface SearchProps { setShowMainModal: Dispatch> } +type InputFocusCallback = (n: HTMLInputElement) => void + const projectUrl: string = `https://react.keepdesign.io` const Search: FC = ({ showModal, setShowMainModal }) => { const [query, setQuery] = useState('') const [results, setResults] = useState([]) const [loading, setLoading] = useState(false) + const inputFocus = useCallback((node) => { + if (node) { + setTimeout(() => { + node.focus(); + }, 1); + } + }, []) useEffect(() => { let timeout: NodeJS.Timeout @@ -100,6 +109,7 @@ const Search: FC = ({ showModal, setShowMainModal }) => { addonPosition="left" value={query} handleOnChange={(e) => setQuery(e.target.value)} + ref={inputFocus} />