best-react-pagination 0.0.1-alpha.1 → 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.tsx CHANGED
@@ -1,368 +1,331 @@
1
- import React, { FC } from "react";
2
-
3
- interface IPagination {
4
- paginationStart: 0 | 1;
5
- currentIndex: number;
6
- totalPages: number;
7
- setIndex: React.Dispatch<React.SetStateAction<number>>;
8
- isDefaultStyles?: boolean;
9
-
10
- wrapperClass?: string;
11
- itemListClass?: string;
12
- itemClass?: string;
13
- selectedItemClass?: string;
14
- disableItemClass?: string;
15
-
16
- className?: string;
17
- }
18
-
19
- const Pagination: FC<IPagination> = ({
20
- paginationStart,
21
- currentIndex,
22
- totalPages,
23
- setIndex,
24
- isDefaultStyles,
25
-
26
- wrapperClass = "",
27
- itemListClass = "",
28
- itemClass = "",
29
- selectedItemClass = "",
30
- disableItemClass = "",
31
- className,
32
- }) => {
33
- const countItems = [1, 2, 3, 4];
34
-
35
- enum defaultStylesEnum {
36
- wrapperClass = "wrapperClass",
37
- itemListClass = "itemListClass",
38
- itemClass = "itemClass",
39
- selectedItemClass = "selectedItemClass",
40
- disableItemClass = "disableItemClass",
41
- }
42
-
43
- const defaultStyles: Record<defaultStylesEnum, React.CSSProperties> = {
44
- wrapperClass: {
45
- position: "sticky",
46
- display: "flex",
47
- justifyContent: "center",
48
- alignItems: "center",
49
- width: "100%",
50
- bottom: 0,
51
- },
52
- itemListClass: {
53
- padding: "10px 0",
54
- },
55
- itemClass: {
56
- display: "inline",
57
- cursor: "pointer",
58
- padding: "6px 12px",
59
- borderRadius: "4px",
60
- margin: "0 2px",
61
- backgroundColor: "#45c954",
62
- },
63
- selectedItemClass: {
64
- display: "inline",
65
- cursor: "pointer",
66
- padding: "6px 12px",
67
- borderRadius: "4px",
68
- margin: "0 2px",
69
- backgroundColor: "#48bbff",
70
- },
71
- disableItemClass: {
72
- display: "inline",
73
- cursor: "not-allowed",
74
- padding: "6px 12px",
75
- borderRadius: "4px",
76
- margin: "0 2px",
77
- },
78
- };
79
-
80
- if (totalPages <= 1) {
81
- return (
82
- <div
83
- className={wrapperClass}
84
- style={isDefaultStyles ? defaultStyles.wrapperClass : undefined}
85
- >
86
- <ul
87
- className={itemListClass}
88
- style={isDefaultStyles ? defaultStyles.itemListClass : undefined}
89
- >
90
- <li
91
- className={disableItemClass}
92
- style={isDefaultStyles ? defaultStyles.itemClass : undefined}
93
- >{`<<`}</li>
94
- <li
95
- className={disableItemClass}
96
- style={isDefaultStyles ? defaultStyles.itemClass : undefined}
97
- >{`>>`}</li>
98
- </ul>
99
- </div>
100
- );
101
- }
102
-
103
- if (totalPages > 1 && totalPages < 4) {
104
- return (
105
- <div
106
- className={wrapperClass}
107
- style={isDefaultStyles ? defaultStyles.wrapperClass : undefined}
108
- >
109
- <ul
110
- className={itemListClass}
111
- style={isDefaultStyles ? defaultStyles.itemListClass : undefined}
112
- >
113
- {[1, 2, 3].map((el) => {
114
- const index = paginationStart === 0 ? el - 1 : el;
115
- if (currentIndex === index) {
116
- return (
117
- <li
118
- className={selectedItemClass}
119
- style={
120
- isDefaultStyles
121
- ? defaultStyles.selectedItemClass
122
- : undefined
123
- }
124
- key={el}
125
- >
126
- {el}
127
- </li>
128
- );
129
- }
130
- return (
131
- <li
132
- className={selectedItemClass}
133
- style={isDefaultStyles ? defaultStyles.itemClass : undefined}
134
- key={el}
135
- onClick={() => setIndex(index)}
136
- >
137
- {el}
138
- </li>
139
- );
140
- })}
141
- </ul>
142
- </div>
143
- );
144
- }
145
-
146
- if (totalPages === 4) {
147
- return (
148
- <div
149
- className={wrapperClass}
150
- style={isDefaultStyles ? defaultStyles.wrapperClass : undefined}
151
- >
152
- <ul
153
- className={itemListClass}
154
- style={isDefaultStyles ? defaultStyles.itemListClass : undefined}
155
- >
156
- {currentIndex > (paginationStart === 0 ? 0 : 1) ? (
157
- <li
158
- className={itemClass}
159
- style={isDefaultStyles ? defaultStyles.itemClass : undefined}
160
- onClick={() => setIndex(currentIndex - 1)}
161
- >{`<<`}</li>
162
- ) : (
163
- <li
164
- className={disableItemClass}
165
- style={
166
- isDefaultStyles ? defaultStyles.disableItemClass : undefined
167
- }
168
- >{`<<`}</li>
169
- )}
170
- {countItems.map((el) => {
171
- const index = paginationStart === 0 ? el - 1 : el;
172
- if (currentIndex === index) {
173
- return (
174
- <li
175
- className={selectedItemClass}
176
- style={
177
- isDefaultStyles
178
- ? defaultStyles.selectedItemClass
179
- : undefined
180
- }
181
- key={el}
182
- >
183
- {el}
184
- </li>
185
- );
186
- }
187
- return (
188
- <li
189
- className={selectedItemClass}
190
- style={isDefaultStyles ? defaultStyles.itemClass : undefined}
191
- key={el}
192
- onClick={() => setIndex(index)}
193
- >
194
- {el}
195
- </li>
196
- );
197
- })}
198
- {currentIndex < (paginationStart === 0 ? 3 : 4) ? (
199
- <li
200
- className={itemClass}
201
- style={isDefaultStyles ? defaultStyles.itemClass : undefined}
202
- >{`>>`}</li>
203
- ) : (
204
- <li
205
- className={disableItemClass}
206
- style={
207
- isDefaultStyles ? defaultStyles.disableItemClass : undefined
208
- }
209
- >{`>>`}</li>
210
- )}
211
- </ul>
212
- </div>
213
- );
214
- }
215
-
216
- return (
217
- <>
218
- <div
219
- className={wrapperClass && className}
220
- style={isDefaultStyles ? defaultStyles.wrapperClass : undefined}
221
- >
222
- <ul
223
- className={itemListClass}
224
- style={isDefaultStyles ? defaultStyles.itemListClass : undefined}
225
- >
226
- {/*Previous button*/}
227
- {currentIndex > (paginationStart === 0 ? 0 : 1) ? (
228
- <li
229
- style={isDefaultStyles ? defaultStyles.itemClass : undefined}
230
- onClick={() => setIndex(currentIndex - 1)}
231
- >{`<<`}</li>
232
- ) : (
233
- <li
234
- className={disableItemClass}
235
- style={
236
- isDefaultStyles ? defaultStyles.disableItemClass : undefined
237
- }
238
- >{`<<`}</li>
239
- )}
240
- {/*First page button*/}
241
- {currentIndex > (paginationStart == 0 ? 2 : 3) ? (
242
- <li
243
- className={itemClass}
244
- style={isDefaultStyles ? defaultStyles.itemClass : undefined}
245
- onClick={() => setIndex(paginationStart === 0 ? 0 : 1)}
246
- >{`1`}</li>
247
- ) : null}
248
-
249
- {currentIndex < (paginationStart === 0 ? 3 : 4)
250
- ? countItems.map((el) => {
251
- if (
252
- paginationStart === 0 ? currentIndex + 1 : currentIndex === el
253
- ) {
254
- return (
255
- <li
256
- className={selectedItemClass}
257
- style={
258
- isDefaultStyles
259
- ? defaultStyles.selectedItemClass
260
- : undefined
261
- }
262
- key={el}
263
- >
264
- {el}
265
- </li>
266
- );
267
- }
268
- return (
269
- <li
270
- className={itemClass}
271
- style={
272
- isDefaultStyles ? defaultStyles.itemClass : undefined
273
- }
274
- onClick={() =>
275
- setIndex(paginationStart === 0 ? el - 1 : el)
276
- }
277
- key={el}
278
- >
279
- {el}
280
- </li>
281
- );
282
- })
283
- : null}
284
-
285
- {currentIndex > (paginationStart === 0 ? 2 : 3) ? (
286
- <li
287
- className={disableItemClass}
288
- style={
289
- isDefaultStyles ? defaultStyles.disableItemClass : undefined
290
- }
291
- >{`...`}</li>
292
- ) : null}
293
-
294
- {currentIndex > (paginationStart === 0 ? 2 : 3) &&
295
- (paginationStart === 0 ? currentIndex : currentIndex - 1) <
296
- totalPages ? (
297
- <>
298
- <li
299
- className={itemClass}
300
- style={isDefaultStyles ? defaultStyles.itemClass : undefined}
301
- onClick={() => setIndex(currentIndex - 1)}
302
- >
303
- {paginationStart === 0 ? currentIndex : currentIndex - 1}
304
- </li>
305
- <li
306
- className={selectedItemClass}
307
- style={
308
- isDefaultStyles ? defaultStyles.selectedItemClass : undefined
309
- }
310
- >
311
- {paginationStart === 0 ? currentIndex + 1 : currentIndex}
312
- </li>
313
- {currentIndex <
314
- (paginationStart === 0 ? totalPages - 1 : totalPages) ? (
315
- <li
316
- className={itemClass}
317
- style={isDefaultStyles ? defaultStyles.itemClass : undefined}
318
- onClick={() => setIndex(currentIndex + 1)}
319
- >
320
- {paginationStart === 0 ? currentIndex + 2 : currentIndex + 1}
321
- </li>
322
- ) : null}
323
- </>
324
- ) : null}
325
-
326
- {currentIndex <
327
- (paginationStart === 0 ? totalPages - 3 : totalPages - 2) ? (
328
- <li
329
- className={disableItemClass}
330
- style={
331
- isDefaultStyles ? defaultStyles.disableItemClass : undefined
332
- }
333
- >{`...`}</li>
334
- ) : null}
335
-
336
- {currentIndex <
337
- (paginationStart === 0 ? totalPages - 3 : totalPages - 2) ? (
338
- <li
339
- className={itemClass}
340
- style={isDefaultStyles ? defaultStyles.itemClass : undefined}
341
- onClick={() => setIndex(totalPages - 1)}
342
- >
343
- {totalPages}
344
- </li>
345
- ) : null}
346
-
347
- {(paginationStart === 0 ? currentIndex + 1 : currentIndex) <
348
- totalPages ? (
349
- <li
350
- className={itemClass}
351
- style={isDefaultStyles ? defaultStyles.itemClass : undefined}
352
- onClick={() => setIndex(currentIndex + 1)}
353
- >{`>>`}</li>
354
- ) : (
355
- <li
356
- className={disableItemClass}
357
- style={
358
- isDefaultStyles ? defaultStyles.disableItemClass : undefined
359
- }
360
- >{`>>`}</li>
361
- )}
362
- </ul>
363
- </div>
364
- </>
365
- );
366
- };
367
-
368
- export default Pagination;
1
+ import React, { FC } from "react";
2
+
3
+ interface IPagination {
4
+ paginationStart: 0 | 1;
5
+ currentIndex: number;
6
+ totalPages: number;
7
+ setIndex: React.Dispatch<React.SetStateAction<number>>;
8
+ isDefaultStyles?: boolean;
9
+
10
+ wrapperClass?: string;
11
+ itemListClass?: string;
12
+ itemClass?: string;
13
+ selectedItemClass?: string;
14
+ disableItemClass?: string;
15
+
16
+ className?: string;
17
+ }
18
+
19
+ const Pagination: FC<IPagination> = ({
20
+ paginationStart,
21
+ currentIndex,
22
+ totalPages,
23
+ setIndex,
24
+ isDefaultStyles,
25
+
26
+ wrapperClass = "",
27
+ itemListClass = "",
28
+ itemClass = "",
29
+ selectedItemClass = "",
30
+ disableItemClass = "",
31
+ className,
32
+ }) => {
33
+ console.log(totalPages, currentIndex);
34
+ const countItems = [1, 2, 3, 4];
35
+
36
+ enum defaultStylesEnum {
37
+ wrapperClass = "wrapperClass",
38
+ itemListClass = "itemListClass",
39
+ itemClass = "itemClass",
40
+ selectedItemClass = "selectedItemClass",
41
+ disableItemClass = "disableItemClass",
42
+ }
43
+
44
+ const defaultStyles: Record<defaultStylesEnum, React.CSSProperties> = {
45
+ wrapperClass: {
46
+ position: "sticky",
47
+ display: "flex",
48
+ justifyContent: "center",
49
+ alignItems: "center",
50
+ width: "100%",
51
+ bottom: 0,
52
+ },
53
+ itemListClass: {
54
+ padding: "10px 0",
55
+ },
56
+ itemClass: {
57
+ display: "inline",
58
+ cursor: "pointer",
59
+ padding: "6px 12px",
60
+ borderRadius: "4px",
61
+ margin: "0 2px",
62
+ backgroundColor: "#45c954",
63
+ },
64
+ selectedItemClass: {
65
+ display: "inline",
66
+ cursor: "pointer",
67
+ padding: "6px 12px",
68
+ borderRadius: "4px",
69
+ margin: "0 2px",
70
+ backgroundColor: "#48bbff",
71
+ },
72
+ disableItemClass: {
73
+ display: "inline",
74
+ cursor: "not-allowed",
75
+ padding: "6px 12px",
76
+ borderRadius: "4px",
77
+ margin: "0 2px",
78
+ },
79
+ };
80
+
81
+ if (totalPages <= 1) {
82
+ return (
83
+ <div className={wrapperClass} style={isDefaultStyles ? defaultStyles.wrapperClass : undefined}>
84
+ <ul className={itemListClass} style={isDefaultStyles ? defaultStyles.itemListClass : undefined}>
85
+ <li className={disableItemClass} style={isDefaultStyles ? defaultStyles.itemClass : undefined}>{`<<`}</li>
86
+ <li className={disableItemClass} style={isDefaultStyles ? defaultStyles.itemClass : undefined}>{`>>`}</li>
87
+ </ul>
88
+ </div>
89
+ );
90
+ }
91
+
92
+ if (totalPages > 1 && totalPages < 4) {
93
+ return (
94
+ <div className={wrapperClass} style={isDefaultStyles ? defaultStyles.wrapperClass : undefined}>
95
+ <ul className={itemListClass} style={isDefaultStyles ? defaultStyles.itemListClass : undefined}>
96
+ {[1, 2, 3].map((el) => {
97
+ const index = paginationStart === 0 ? el - 1 : el;
98
+ if (currentIndex === index) {
99
+ return (
100
+ <li
101
+ className={selectedItemClass}
102
+ style={isDefaultStyles ? defaultStyles.selectedItemClass : undefined}
103
+ key={el}
104
+ >
105
+ {el}
106
+ </li>
107
+ );
108
+ }
109
+ return (
110
+ <li
111
+ className={selectedItemClass}
112
+ style={isDefaultStyles ? defaultStyles.itemClass : undefined}
113
+ key={el}
114
+ onClick={() => setIndex(index)}
115
+ >
116
+ {el}
117
+ </li>
118
+ );
119
+ })}
120
+ </ul>
121
+ </div>
122
+ );
123
+ }
124
+
125
+ if (totalPages === 4) {
126
+ return (
127
+ <div className={wrapperClass} style={isDefaultStyles ? defaultStyles.wrapperClass : undefined}>
128
+ <ul className={itemListClass} style={isDefaultStyles ? defaultStyles.itemListClass : undefined}>
129
+ {currentIndex > (paginationStart === 0 ? 0 : 1) ? (
130
+ <li
131
+ className={itemClass}
132
+ style={isDefaultStyles ? defaultStyles.itemClass : undefined}
133
+ onClick={() => setIndex(currentIndex - 1)}
134
+ >{`<<`}</li>
135
+ ) : (
136
+ <li
137
+ className={disableItemClass}
138
+ style={isDefaultStyles ? defaultStyles.disableItemClass : undefined}
139
+ >{`<<`}</li>
140
+ )}
141
+ {countItems.map((el) => {
142
+ const index = paginationStart === 0 ? el - 1 : el;
143
+ if (currentIndex === index) {
144
+ return (
145
+ <li
146
+ className={selectedItemClass}
147
+ style={isDefaultStyles ? defaultStyles.selectedItemClass : undefined}
148
+ key={el}
149
+ >
150
+ {el}
151
+ </li>
152
+ );
153
+ }
154
+ return (
155
+ <li
156
+ className={selectedItemClass}
157
+ style={isDefaultStyles ? defaultStyles.itemClass : undefined}
158
+ key={el}
159
+ onClick={() => setIndex(index)}
160
+ >
161
+ {el}
162
+ </li>
163
+ );
164
+ })}
165
+ {currentIndex < (paginationStart === 0 ? 3 : 4) ? (
166
+ <li
167
+ className={itemClass}
168
+ style={isDefaultStyles ? defaultStyles.itemClass : undefined}
169
+ onClick={() => setIndex(currentIndex + 1)}
170
+ >{`>>`}</li>
171
+ ) : (
172
+ <li
173
+ className={disableItemClass}
174
+ style={isDefaultStyles ? defaultStyles.disableItemClass : undefined}
175
+ >{`>>`}</li>
176
+ )}
177
+ </ul>
178
+ </div>
179
+ );
180
+ }
181
+
182
+ return (
183
+ <>
184
+ <div className={wrapperClass && className} style={isDefaultStyles ? defaultStyles.wrapperClass : undefined}>
185
+ <ul className={itemListClass} style={isDefaultStyles ? defaultStyles.itemListClass : undefined}>
186
+ {/*Previous button*/}
187
+ {currentIndex > (paginationStart === 0 ? 0 : 1) ? (
188
+ <li
189
+ style={isDefaultStyles ? defaultStyles.itemClass : undefined}
190
+ onClick={() => setIndex(currentIndex - 1)}
191
+ >{`<<`}</li>
192
+ ) : (
193
+ <li
194
+ className={disableItemClass}
195
+ style={isDefaultStyles ? defaultStyles.disableItemClass : undefined}
196
+ >{`<<`}</li>
197
+ )}
198
+ {/*First page button*/}
199
+ {currentIndex > (paginationStart == 0 ? 2 : 3) ? (
200
+ <li
201
+ className={itemClass}
202
+ style={isDefaultStyles ? defaultStyles.itemClass : undefined}
203
+ onClick={() => setIndex(paginationStart === 0 ? 0 : 1)}
204
+ >{`1`}</li>
205
+ ) : null}
206
+
207
+ {currentIndex < (paginationStart === 0 ? 3 : 4)
208
+ ? countItems.map((el) => {
209
+ if (paginationStart === 0 ? currentIndex + 1 === el : currentIndex === el) {
210
+ return (
211
+ <li
212
+ className={selectedItemClass}
213
+ style={isDefaultStyles ? defaultStyles.selectedItemClass : undefined}
214
+ key={el}
215
+ >
216
+ {el}
217
+ </li>
218
+ );
219
+ }
220
+ return (
221
+ <li
222
+ className={itemClass}
223
+ style={isDefaultStyles ? defaultStyles.itemClass : undefined}
224
+ onClick={() => setIndex(paginationStart === 0 ? el - 1 : el)}
225
+ key={el}
226
+ >
227
+ {el}
228
+ </li>
229
+ );
230
+ })
231
+ : null}
232
+
233
+ {currentIndex > (paginationStart === 0 ? 2 : 3) ? (
234
+ <li
235
+ className={disableItemClass}
236
+ style={isDefaultStyles ? defaultStyles.disableItemClass : undefined}
237
+ >{`...`}</li>
238
+ ) : null}
239
+
240
+ {currentIndex > (paginationStart === 0 ? 2 : 3) &&
241
+ (paginationStart === 0 ? currentIndex : currentIndex - 1) < totalPages ? (
242
+ <>
243
+ {currentIndex === (paginationStart === 0 ? totalPages - 2 : totalPages - 1) && (
244
+ <li
245
+ className={itemClass}
246
+ style={isDefaultStyles ? defaultStyles.itemClass : undefined}
247
+ onClick={() => setIndex(paginationStart === 0 ? totalPages - 4 : totalPages - 3)}
248
+ >
249
+ {totalPages - 3}
250
+ </li>
251
+ )}
252
+
253
+ {currentIndex === (paginationStart === 0 ? totalPages - 1 : totalPages) && (
254
+ <>
255
+ <li
256
+ className={itemClass}
257
+ style={isDefaultStyles ? defaultStyles.itemClass : undefined}
258
+ onClick={() => setIndex(paginationStart === 0 ? totalPages - 4 : totalPages - 3)}
259
+ >
260
+ {totalPages - 3}
261
+ </li>
262
+ <li
263
+ className={itemClass}
264
+ style={isDefaultStyles ? defaultStyles.itemClass : undefined}
265
+ onClick={() => setIndex(paginationStart === 0 ? totalPages - 3 : totalPages - 2)}
266
+ >
267
+ {totalPages - 2}
268
+ </li>
269
+ </>
270
+ )}
271
+
272
+ <li
273
+ className={itemClass}
274
+ style={isDefaultStyles ? defaultStyles.itemClass : undefined}
275
+ onClick={() => setIndex(currentIndex - 1)}
276
+ >
277
+ {paginationStart === 0 ? currentIndex : currentIndex - 1}
278
+ </li>
279
+
280
+ <li className={selectedItemClass} style={isDefaultStyles ? defaultStyles.selectedItemClass : undefined}>
281
+ {paginationStart === 0 ? currentIndex + 1 : currentIndex}
282
+ </li>
283
+
284
+ {currentIndex < (paginationStart === 0 ? totalPages - 1 : totalPages) ? (
285
+ <li
286
+ className={itemClass}
287
+ style={isDefaultStyles ? defaultStyles.itemClass : undefined}
288
+ onClick={() => setIndex(currentIndex + 1)}
289
+ >
290
+ {paginationStart === 0 ? currentIndex + 2 : currentIndex + 1}
291
+ </li>
292
+ ) : null}
293
+ </>
294
+ ) : null}
295
+
296
+ {currentIndex < (paginationStart === 0 ? totalPages - 3 : totalPages - 2) ? (
297
+ <li
298
+ className={disableItemClass}
299
+ style={isDefaultStyles ? defaultStyles.disableItemClass : undefined}
300
+ >{`...`}</li>
301
+ ) : null}
302
+
303
+ {currentIndex < (paginationStart === 0 ? totalPages - 2 : totalPages - 1) ? (
304
+ <li
305
+ className={itemClass}
306
+ style={isDefaultStyles ? defaultStyles.itemClass : undefined}
307
+ onClick={() => setIndex(paginationStart === 0 ? totalPages - 1 : totalPages)}
308
+ >
309
+ {totalPages}
310
+ </li>
311
+ ) : null}
312
+
313
+ {(paginationStart === 0 ? currentIndex + 1 : currentIndex) < totalPages ? (
314
+ <li
315
+ className={itemClass}
316
+ style={isDefaultStyles ? defaultStyles.itemClass : undefined}
317
+ onClick={() => setIndex(currentIndex + 1)}
318
+ >{`>>`}</li>
319
+ ) : (
320
+ <li
321
+ className={disableItemClass}
322
+ style={isDefaultStyles ? defaultStyles.disableItemClass : undefined}
323
+ >{`>>`}</li>
324
+ )}
325
+ </ul>
326
+ </div>
327
+ </>
328
+ );
329
+ };
330
+
331
+ export default Pagination;