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.
@@ -1,369 +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
- 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
84
- className={wrapperClass}
85
- style={isDefaultStyles ? defaultStyles.wrapperClass : undefined}
86
- >
87
- <ul
88
- className={itemListClass}
89
- style={isDefaultStyles ? defaultStyles.itemListClass : undefined}
90
- >
91
- <li
92
- className={disableItemClass}
93
- style={isDefaultStyles ? defaultStyles.itemClass : undefined}
94
- >{`<<`}</li>
95
- <li
96
- className={disableItemClass}
97
- style={isDefaultStyles ? defaultStyles.itemClass : undefined}
98
- >{`>>`}</li>
99
- </ul>
100
- </div>
101
- );
102
- }
103
-
104
- if (totalPages > 1 && totalPages < 4) {
105
- return (
106
- <div
107
- className={wrapperClass}
108
- style={isDefaultStyles ? defaultStyles.wrapperClass : undefined}
109
- >
110
- <ul
111
- className={itemListClass}
112
- style={isDefaultStyles ? defaultStyles.itemListClass : undefined}
113
- >
114
- {[1, 2, 3].map((el) => {
115
- const index = paginationStart === 0 ? el - 1 : el;
116
- if (currentIndex === index) {
117
- return (
118
- <li
119
- className={selectedItemClass}
120
- style={
121
- isDefaultStyles
122
- ? defaultStyles.selectedItemClass
123
- : undefined
124
- }
125
- key={el}
126
- >
127
- {el}
128
- </li>
129
- );
130
- }
131
- return (
132
- <li
133
- className={selectedItemClass}
134
- style={isDefaultStyles ? defaultStyles.itemClass : undefined}
135
- key={el}
136
- onClick={() => setIndex(index)}
137
- >
138
- {el}
139
- </li>
140
- );
141
- })}
142
- </ul>
143
- </div>
144
- );
145
- }
146
-
147
- if (totalPages === 4) {
148
- return (
149
- <div
150
- className={wrapperClass}
151
- style={isDefaultStyles ? defaultStyles.wrapperClass : undefined}
152
- >
153
- <ul
154
- className={itemListClass}
155
- style={isDefaultStyles ? defaultStyles.itemListClass : undefined}
156
- >
157
- {currentIndex > (paginationStart === 0 ? 0 : 1) ? (
158
- <li
159
- className={itemClass}
160
- style={isDefaultStyles ? defaultStyles.itemClass : undefined}
161
- onClick={() => setIndex(currentIndex - 1)}
162
- >{`<<`}</li>
163
- ) : (
164
- <li
165
- className={disableItemClass}
166
- style={
167
- isDefaultStyles ? defaultStyles.disableItemClass : undefined
168
- }
169
- >{`<<`}</li>
170
- )}
171
- {countItems.map((el) => {
172
- const index = paginationStart === 0 ? el - 1 : el;
173
- if (currentIndex === index) {
174
- return (
175
- <li
176
- className={selectedItemClass}
177
- style={
178
- isDefaultStyles
179
- ? defaultStyles.selectedItemClass
180
- : undefined
181
- }
182
- key={el}
183
- >
184
- {el}
185
- </li>
186
- );
187
- }
188
- return (
189
- <li
190
- className={selectedItemClass}
191
- style={isDefaultStyles ? defaultStyles.itemClass : undefined}
192
- key={el}
193
- onClick={() => setIndex(index)}
194
- >
195
- {el}
196
- </li>
197
- );
198
- })}
199
- {currentIndex < (paginationStart === 0 ? 3 : 4) ? (
200
- <li
201
- className={itemClass}
202
- style={isDefaultStyles ? defaultStyles.itemClass : undefined}
203
- >{`>>`}</li>
204
- ) : (
205
- <li
206
- className={disableItemClass}
207
- style={
208
- isDefaultStyles ? defaultStyles.disableItemClass : undefined
209
- }
210
- >{`>>`}</li>
211
- )}
212
- </ul>
213
- </div>
214
- );
215
- }
216
-
217
- return (
218
- <>
219
- <div
220
- className={wrapperClass && className}
221
- style={isDefaultStyles ? defaultStyles.wrapperClass : undefined}
222
- >
223
- <ul
224
- className={itemListClass}
225
- style={isDefaultStyles ? defaultStyles.itemListClass : undefined}
226
- >
227
- {/*Previous button*/}
228
- {currentIndex > (paginationStart === 0 ? 0 : 1) ? (
229
- <li
230
- style={isDefaultStyles ? defaultStyles.itemClass : undefined}
231
- onClick={() => setIndex(currentIndex - 1)}
232
- >{`<<`}</li>
233
- ) : (
234
- <li
235
- className={disableItemClass}
236
- style={
237
- isDefaultStyles ? defaultStyles.disableItemClass : undefined
238
- }
239
- >{`<<`}</li>
240
- )}
241
- {/*First page button*/}
242
- {currentIndex > (paginationStart == 0 ? 2 : 3) ? (
243
- <li
244
- className={itemClass}
245
- style={isDefaultStyles ? defaultStyles.itemClass : undefined}
246
- onClick={() => setIndex(paginationStart === 0 ? 0 : 1)}
247
- >{`1`}</li>
248
- ) : null}
249
-
250
- {currentIndex < (paginationStart === 0 ? 3 : 4)
251
- ? countItems.map((el) => {
252
- if (
253
- paginationStart === 0 ? currentIndex + 1 : currentIndex === el
254
- ) {
255
- return (
256
- <li
257
- className={selectedItemClass}
258
- style={
259
- isDefaultStyles
260
- ? defaultStyles.selectedItemClass
261
- : undefined
262
- }
263
- key={el}
264
- >
265
- {el}
266
- </li>
267
- );
268
- }
269
- return (
270
- <li
271
- className={itemClass}
272
- style={
273
- isDefaultStyles ? defaultStyles.itemClass : undefined
274
- }
275
- onClick={() =>
276
- setIndex(paginationStart === 0 ? el - 1 : el)
277
- }
278
- key={el}
279
- >
280
- {el}
281
- </li>
282
- );
283
- })
284
- : null}
285
-
286
- {currentIndex > (paginationStart === 0 ? 2 : 3) ? (
287
- <li
288
- className={disableItemClass}
289
- style={
290
- isDefaultStyles ? defaultStyles.disableItemClass : undefined
291
- }
292
- >{`...`}</li>
293
- ) : null}
294
-
295
- {currentIndex > (paginationStart === 0 ? 2 : 3) &&
296
- (paginationStart === 0 ? currentIndex : currentIndex - 1) <
297
- totalPages ? (
298
- <>
299
- <li
300
- className={itemClass}
301
- style={isDefaultStyles ? defaultStyles.itemClass : undefined}
302
- onClick={() => setIndex(currentIndex - 1)}
303
- >
304
- {paginationStart === 0 ? currentIndex : currentIndex - 1}
305
- </li>
306
- <li
307
- className={selectedItemClass}
308
- style={
309
- isDefaultStyles ? defaultStyles.selectedItemClass : undefined
310
- }
311
- >
312
- {paginationStart === 0 ? currentIndex + 1 : currentIndex}
313
- </li>
314
- {currentIndex <
315
- (paginationStart === 0 ? totalPages - 1 : totalPages) ? (
316
- <li
317
- className={itemClass}
318
- style={isDefaultStyles ? defaultStyles.itemClass : undefined}
319
- onClick={() => setIndex(currentIndex + 1)}
320
- >
321
- {paginationStart === 0 ? currentIndex + 2 : currentIndex + 1}
322
- </li>
323
- ) : null}
324
- </>
325
- ) : null}
326
-
327
- {currentIndex <
328
- (paginationStart === 0 ? totalPages - 3 : totalPages - 2) ? (
329
- <li
330
- className={disableItemClass}
331
- style={
332
- isDefaultStyles ? defaultStyles.disableItemClass : undefined
333
- }
334
- >{`...`}</li>
335
- ) : null}
336
-
337
- {currentIndex <
338
- (paginationStart === 0 ? totalPages - 2 : totalPages - 1) ? (
339
- <li
340
- className={itemClass}
341
- style={isDefaultStyles ? defaultStyles.itemClass : undefined}
342
- onClick={() => setIndex(paginationStart === 0 ? totalPages - 1 : totalPages)}
343
- >
344
- {totalPages}
345
- </li>
346
- ) : null}
347
-
348
- {(paginationStart === 0 ? currentIndex + 1 : currentIndex) <
349
- totalPages ? (
350
- <li
351
- className={itemClass}
352
- style={isDefaultStyles ? defaultStyles.itemClass : undefined}
353
- onClick={() => setIndex(currentIndex + 1)}
354
- >{`>>`}</li>
355
- ) : (
356
- <li
357
- className={disableItemClass}
358
- style={
359
- isDefaultStyles ? defaultStyles.disableItemClass : undefined
360
- }
361
- >{`>>`}</li>
362
- )}
363
- </ul>
364
- </div>
365
- </>
366
- );
367
- };
368
-
369
- 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;