blob: c999f970903c029e95c1c3e7a1bdabd9cc3c8316 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
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<React.SetStateAction<Song | undefined>>;
}
export function Search({ currentTry, setSelectedSong }: Props) {
const [value, setValue] = React.useState<string>("");
const [results, setResults] = React.useState<Song[]>([]);
React.useEffect(() => {
if (value) {
setResults(searchSong(value));
} else if (value === "") {
setResults([]);
}
}, [value]);
// clear value on selection
React.useEffect(() => {
setValue("");
}, [currentTry]);
return (
<Styled.Container>
<Styled.ResultsContainer>
{results.map((song) => (
<Styled.Result
key={song.youtubeId}
onClick={() => {
setSelectedSong(song);
setValue(`${song.artist} - ${song.name}`);
setResults([]);
}}
>
<Styled.ResultText>
{song.artist} - {song.name}
</Styled.ResultText>
</Styled.Result>
))}
</Styled.ResultsContainer>
<Styled.SearchContainer>
<Styled.SearchPadding>
<IoSearch size={20} />
<Styled.Input
onChange={(e) => setValue(e.currentTarget.value)}
placeholder="Search"
value={value}
/>
</Styled.SearchPadding>
</Styled.SearchContainer>
</Styled.Container>
);
}
|