-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter.js
127 lines (114 loc) · 2.55 KB
/
filter.js
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import React from "react";
import { useState } from "react";
import Navbar from "../src/components/Navbar";
import styles from "../styles/Destination.module.css";
const Destination = () => {
const texts = [
{
id: 1,
name: "moon",
text: "i love the moon",
},
{
id: 2,
name: "mars",
text: "i love mars",
},
{
id: 3,
name: "europa",
text: "i love europa",
},
{
id: 4,
name: "earth",
text: "i love earth",
},
];
const [filteredState, setFilteredState] = useState([]);
const filtered = (e) => {
const filterItem = texts.filter((item) => {
return item.name.toLowerCase() === e.target.innerText.toLowerCase();
});
console.log(filterItem);
setFilteredState([...filterItem]);
};
const changeStyle = (text) => {
const mapArr =
filteredState.length > 0
? filteredState.map((item) => {
return item.name;
})
: ["europa"];
if (mapArr.includes(text)) {
return "red";
}
};
return (
<div className={styles.container}>
<Navbar />
<p style={{ color: changeStyle("moon") }}>destination</p>
<br />
<br />
<h4> destination</h4>
<br />
<br />
<ul>
<li
className={styles.li}
onClick={filtered}
style={{ color: changeStyle("moon") }}
>
moon
</li>
<li
className={styles.li}
onClick={filtered}
style={{ color: changeStyle("mars") }}
>
mars
</li>
<li
className={styles.li}
onClick={filtered}
style={{ color: changeStyle("europa") }}
>
europa
</li>
<li
className={styles.li}
onClick={filtered}
style={{ color: changeStyle("earth") }}
>
earth
</li>
</ul>
<br />
<br />
<div className={styles.div}>
{filteredState.length > 0 ? (
<>
{filteredState.map((item, index) => {
return (
<p className={styles.p} key={index}>
{item.text}
</p>
);
})}
</>
) : (
<>
{texts.map((item, index) => {
return (
<p className={styles.p} key={index}>
{item.text}
</p>
);
})}
</>
)}
</div>
</div>
);
};
// export default Destination;