-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample-pagination
More file actions
143 lines (127 loc) · 2.99 KB
/
sample-pagination
File metadata and controls
143 lines (127 loc) · 2.99 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import React, { useEffect, useState } from 'react';
import './App.css';
export default function App() {
const [data, setData] = useState([]);
const [currentPage, setPerPage] = useState(1);
const itemsPerPage = 10;
const fetchProductData = async () => {
const fetchResponse = await fetch(
'https://dummyjson.com/products?limit=100'
);
const resp = await fetchResponse.json();
console.log(resp);
if (resp && resp.products) {
setData(resp.products);
}
};
useEffect(() => {
fetchProductData();
}, []);
const selectedPageProducts = (seletedPage) => {
if (
seletedPage >= 1 &&
seletedPage <= data.length / 10 &&
seletedPage !== currentPage
) {
setPerPage(seletedPage);
}
};
return (
<div className="App">
<div className="products">
{data
.slice(
currentPage * itemsPerPage - itemsPerPage,
currentPage * itemsPerPage
)
.map((item) => {
return (
<span className="products-card" key={item.id}>
<img src={item.thumbnail} alt={item.images[1]} />
<h1 className="product-title">{item.title}</h1>
</span>
);
})}
</div>
{data.length > 0 && (
<div className="pagination">
<button
onClick={() => selectedPageProducts(currentPage - 1)}
className="prev"
>
◀️
</button>
{[...Array(data.length / itemsPerPage)].map((_, index) => {
return (
<button
className={currentPage === index + 1 ? 'active' : ''}
onClick={() => selectedPageProducts(index + 1)}
key={index}
>
{index + 1}
</button>
);
})}
<button
onClick={() => selectedPageProducts(currentPage + 1)}
className="next"
>
▶️
</button>
</div>
)}
</div>
);
}
// export default App;
Css
.products {
display: flex;
align-items: center;
gap: 6px;
column-gap: 4px;
flex-wrap: wrap;
}
.products-card {
width: 30%;
max-height: 400px;
height: auto;
border: 1px solid rgba(0, 0, 0, 0.5);
padding: 0 16px 16px;
cursor: pointer;
background-color: cornsilk;
}
.products-card img {
width: 100%;
max-height: 220px;
height: 100%;
object-fit: contain;
}
.products-card h1 {
font-size: 16px;
font-weight: 600;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
.pagination {
display: flex;
justify-content: center;
gap: 12px;
margin: 14px auto;
}
.pagination > button {
border: 1px solid rgb(168, 163, 163);
background-color: antiquewhite;
border-radius: 4px;
cursor: pointer;
}
.pagination .prev {
background-color: transparent;
border: none;
}
.pagination .next {
background-color: transparent;
border: none;
}
.pagination .active {
background-color: aqua;
}