import React from "react"; import { IoSearch } from "react-icons/io5"; import { searchSong } from "../../helpers"; import { Song } from "../../types/song"; import * as Styled from "./index.styled"; interface Props { currentTry: number; setSelectedSong: React.Dispatch>; /** Optional override of the song pool to search within. */ songs?: Song[]; } export function Search({ currentTry, setSelectedSong, songs }: Props) { const [value, setValue] = React.useState(""); const [results, setResults] = React.useState([]); React.useEffect(() => { if (!value.trim()) { setResults([]); return; } setResults(searchSong(value, songs)); }, [value, songs]); React.useEffect(() => { setValue(""); }, [currentTry]); return ( {results.map((song) => ( { setSelectedSong(song); setValue(`${song.artist} - ${song.name}`); setResults([]); }} > {song.artist} - {song.name} ))} setValue(e.currentTarget.value)} placeholder="Search" /> ); }