Skip to content

Commit 0261be9

Browse files
author
time-to-program
committed
Filter and Search list of objects example in React
1 parent 8d9bc97 commit 0261be9

File tree

7 files changed

+157
-78
lines changed

7 files changed

+157
-78
lines changed

public/index.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@
1515
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
1616
-->
1717
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
18+
19+
<link rel="preconnect" href="https://fonts.googleapis.com" />
20+
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
21+
<link
22+
href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;600;700&display=swap"
23+
rel="stylesheet"
24+
/>
1825
<!--
1926
Notice the use of %PUBLIC_URL% in the tags above.
2027
It will be replaced with the URL of the `public` folder during the build.

src/App.css

Lines changed: 0 additions & 38 deletions
This file was deleted.

src/App.js

Lines changed: 75 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,81 @@
1-
import logo from './logo.svg';
2-
import './App.css';
1+
import React, { useState } from "react";
32

43
function App() {
4+
const planetList = [
5+
{ id: "01", name: "Mercury", size: "2440", unit: "km" },
6+
{ id: "02", name: "Venus", size: "6052", unit: "km" },
7+
{ id: "03", name: "Earth", size: "6371", unit: "km" },
8+
{ id: "04", name: "Mars", size: "3390", unit: "km" },
9+
{ id: "05", name: "Jupiter", size: "69911", unit: "km" },
10+
{ id: "06", name: "Saturn", size: "58232", unit: "km" },
11+
{ id: "07", name: "Uranus", size: "25362", unit: "km" },
12+
{ id: "08", name: "Neptune", size: "24622", unit: "km" },
13+
];
14+
15+
const [filteredList, setFilteredList] = useState(planetList)
16+
const [searchQuery, setSearchQuery] = useState("");
17+
18+
//Search list of objects
19+
const handleSearch = (event) => {
20+
const query = event.target.value
21+
setSearchQuery(query)
22+
23+
const searchList = planetList.filter((item)=>{
24+
return item.name.toLowerCase().indexOf(query.toLowerCase()) !== -1
25+
})
26+
27+
setFilteredList(searchList)
28+
};
29+
30+
//Filter List of object
31+
const onFilterChange=(event)=>{
32+
const selectedSize = Number(event.target.value)
33+
34+
const filterList = planetList.filter((item)=>{
35+
return Number(item.size) > selectedSize
36+
})
37+
38+
setFilteredList(filterList)
39+
}
40+
541
return (
6-
<div className="App">
7-
<header className="App-header">
8-
<img src={logo} className="App-logo" alt="logo" />
9-
<p>
10-
Edit <code>src/App.js</code> and save to reload.
11-
</p>
12-
<a
13-
className="App-link"
14-
href="https://reactjs.org"
15-
target="_blank"
16-
rel="noopener noreferrer"
17-
>
18-
Learn React
19-
</a>
20-
</header>
42+
<div className="container">
43+
<h2>Search Filter Array of Objects</h2>
44+
45+
<div className="list-wrapper">
46+
<div className="filter-container">
47+
<input
48+
type="text"
49+
name="search"
50+
placeholder="Search"
51+
value={searchQuery}
52+
onChange={handleSearch}
53+
/>
54+
<div>
55+
<select name="size" onChange={onFilterChange}>
56+
<option value="">Filter by Size</option>
57+
<option value="2000">Greater Than 2000km</option>
58+
<option value="6000">Greater Than 6000km</option>
59+
<option value="10000">Greater Than 10000km</option>
60+
<option value="25000">Greater Than 25000km</option>
61+
</select>
62+
</div>
63+
</div>
64+
65+
{filteredList.map((item, index) => {
66+
return (
67+
<div className="card" key={index}>
68+
<p className="num-text">{item.id}</p>
69+
<div>
70+
<p className="title">{item.name}</p>
71+
<p className="description">
72+
{item.size} {item.unit}
73+
</p>
74+
</div>
75+
</div>
76+
);
77+
})}
78+
</div>
2179
</div>
2280
);
2381
}

src/App.test.js

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/index.css

Lines changed: 75 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,79 @@
1-
body {
1+
* {
2+
font-family: "Poppins", sans-serif;
3+
padding: 0;
24
margin: 0;
3-
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
4-
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
5-
sans-serif;
6-
-webkit-font-smoothing: antialiased;
7-
-moz-osx-font-smoothing: grayscale;
85
}
96

10-
code {
11-
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
12-
monospace;
7+
.container {
8+
width: 100vw;
9+
height: 100vh;
10+
display: flex;
11+
flex-direction: column;
12+
align-items: center;
13+
background-color: #f0f1f3;
14+
}
15+
16+
h2 {
17+
color: #737373;
18+
margin-top: 120px;
19+
margin-bottom: 25px;
20+
}
21+
22+
.list-wrapper {
23+
width: 50%;
24+
display: flex;
25+
align-items: center;
26+
justify-content: center;
27+
flex-wrap: wrap;
28+
gap: 10px;
29+
}
30+
31+
.filter-container {
32+
width: 90%;
33+
display: flex;
34+
align-items: center;
35+
justify-content: center;
36+
gap: 20px;
37+
margin-bottom: 20px;
38+
}
39+
40+
input[type="text"],
41+
select {
42+
border-radius: 6px;
43+
padding: 8px 15px;
44+
border-width: 0px;
45+
}
46+
47+
input[type="text"]:focus,
48+
select:focus {
49+
outline: none;
50+
}
51+
52+
.card {
53+
width: 150px;
54+
display: flex;
55+
align-items: center;
56+
gap: 15px;
57+
padding: 10px 25px;
58+
border-radius: 8px;
59+
background-color: #fff;
60+
}
61+
62+
.num-text {
63+
font-size: 2rem;
64+
font-weight: 600;
65+
color: #c3c3c3;
66+
}
67+
68+
.title {
69+
font-size: 0.9rem;
70+
font-weight: 500;
71+
color: #343434;
72+
margin-left: 0.5rem;
73+
}
74+
75+
.description {
76+
font-size: 0.8rem;
77+
color: #343434;
78+
margin-left: 0.5rem;
1379
}

src/logo.svg

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/setupTests.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)