import React, { useState, useEffect } from 'react' import { Card } from 'react-bootstrap'; import { getSearchResults } from './TabulatorData'; import '../jqueryloader' const Search = () => { const [searchTerm, updateSearchTerm] = useState(''); const [filteredResults, updateFilteredResults] = useState([]); const [searchResults, updateSearchResults] = useState([]); const [displayResults, updateDisplayResults] = useState(false); const [focusIndex, updateFocusIndex] = useState(-1); const linkRefs = []; const keys = { ENTER: 13, UP: 38, DOWN: 40 }; useEffect(() => { const getSearchResult = () => { // ⚠️ This is where you should pull data in from your server const searchResultsResponse = getSearchResults(); updateSearchResults(searchResultsResponse); }; getSearchResult(); }, []); const updateSearch = e => { var input = e.target?.value; updateSearchTerm(e.target.value); updateFilteredResults(searchResults.filter(result =>{ if(input?.length >= 3){ return result.title.match(new RegExp(e.target.value, 'gi')) } })) }; const hideAutoSuggest = e => { e.persist(); if (e.relatedTarget && e.relatedTarget.className === 'autosuggest-link') { return false; } updateDisplayResults(true); updateFocusIndex(-1); }; const showAutoSuggest = () => { updateDisplayResults(false); }; const handleInput = (e) =>{ var input = e.target?.value; $('.search-suggestions').css('display', 'block'); if(input?.length === 0){ $('.search-suggestions').css('display', 'none'); } } const handleNavigation = e => { switch (e.keyCode) { case keys.ENTER: if (focusIndex !== -1) { window.open(linkRefs[focusIndex].href); } hideAutoSuggest(e); break; case keys.UP: if (focusIndex > -1) { updateFocusIndex(focusIndex - 1); } break; case keys.DOWN: if (focusIndex < filteredResults.length - 1) { updateFocusIndex(focusIndex + 1); } break; } }; const SearchResults = () => { const Message = ({ text }) => (

{text}


); if (!displayResults) { return null; } if (!searchResults.length) { return } if (!searchTerm) { return } if (!filteredResults.length) { return } return (
    {filteredResults.map((article, index) => (
  • {/* ⚠️ You may want to outsource this part to make the component less heavy */}
  • ))}
); } return (
Search {searchTerm.length ? `results for: ${searchTerm}` : null}
    {(!displayResults && searchTerm) &&
  • {`Search for ${searchTerm}`}
  • } {!displayResults && filteredResults.map((article, index) => (
  • {linkRefs[index] = link}}> {article.title}
  • ))}
); } function Highlight({ children: text = "", tags }) { if (!tags?.length) return text; const matches = [...text.matchAll(new RegExp(tags, "ig"))]; const startText = text.slice(0, matches[0]?.index); return ( {startText} {matches.map((match, i) => { const startIndex = match.index; const currentText = match[0]; const endIndex = startIndex + currentText.length; const nextIndex = matches[i + 1]?.index; const untilNextText = text.slice(endIndex, nextIndex); return (

{currentText}{untilNextText}

); })}
); } export default Search;