Index of /prototypes/searchbar
- What photo have you recently seen that spoke to you?
- If you could change the length of each day to make it perfect for you, how many hours would it be?
- What is your current favorite TV show? What was it 5 years ago?
- If you could remove something that exists in this world forever, what would it be?
- How many things you currently own do you think you could throw away and not notice a difference in your life?
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
import { ChangeEvent, useState } from "react";
import { TextList } from "./mocs";
import { Container, Input, Item, List } from "./styles";
import stringSimilarity from "string-similarity";
export const SearchBarComponent = () => {
const [selectedIndex, setSelectedIndex] = useState<number | null>(null);
const handleOnChange = (e: ChangeEvent<HTMLInputElement>) => {
const value = e.target.value;
const toCompare = TextList.map((item) => item.message);
const similarity = stringSimilarity.findBestMatch(value, toCompare);
console.log("input value: ", value);
console.log("similarity: ", similarity);
if (similarity.ratings.every((item) => item.rating === 0)) {
setSelectedIndex(null);
return;
}
setSelectedIndex(similarity.bestMatchIndex);
};
return (
<Container>
<Input type="text" onChange={handleOnChange} placeholder="search term" />
<List>
{TextList.map((item, index) => (
<Item
key={`${item.title}-${index}`}
selected={index === selectedIndex}
>
{item.message}
</Item>
))}
</List>
</Container>
);
};