import React, { useEffect, useState, useRef } from "react"; import { useRouter } from "next/router"; import type mapboxgl from "mapbox-gl"; import Fuse from "fuse.js"; import styles from "../styles/SearchBar.module.css"; type Props = { clearSelectedCell: () => void; map: React.MutableRefObject; }; interface Mod { title: string; nexus_mod_id: number; } interface SearchResult { item: Mod; refIndex: number; } const list: Mod[] = [ { title: "Unofficial Skyrim Special Edition Patch", nexus_mod_id: 1 }, { title: "Enhanced Lights and FX", nexus_mod_id: 2 }, { title: "Majestic Mountains", nexus_mod_id: 3 }, ]; const SearchBar: React.FC = ({ clearSelectedCell, map }) => { const router = useRouter(); const fuse = new Fuse(list, { keys: ["title"] }); const searchInput = useRef(null); const [search, setSearch] = useState(""); const [searchFocused, setSearchFocused] = useState(false); const [clickingResult, setClickingResult] = useState(false); const [results, setResults] = useState([]); useEffect(() => { if (searchInput.current) { if ( searchFocused && global.document.activeElement !== searchInput.current ) { searchInput.current.focus(); } else if ( !searchFocused && global.document.activeElement === searchInput.current ) { searchInput.current.blur(); } } }, [searchFocused]); const onChange = (e: React.ChangeEvent) => { setSearch(e.target.value); const results: { item: Mod; refIndex: number }[] = fuse.search( e.target.value ); setResults(results); }; const onChooseResult = (item: Mod) => (e: React.MouseEvent | React.TouchEvent) => { clearSelectedCell(); router.push({ query: { mod: item.nexus_mod_id } }); setSearch(""); setResults([]); setClickingResult(false); setSearchFocused(false); }; return (
setSearchFocused(true)} onBlur={() => { if (!clickingResult) { setSearch(""); setResults([]); setSearchFocused(false); } }} value={search} ref={searchInput} /> {results.length > 0 && (
    {results.map((result) => (
  • setClickingResult(true)} onMouseDown={() => setClickingResult(true)} > {result.item.title}
  • ))}
)}
); }; export default SearchBar;