File tree Expand file tree Collapse file tree 3 files changed +33
-6
lines changed Expand file tree Collapse file tree 3 files changed +33
-6
lines changed Original file line number Diff line number Diff line change @@ -41,6 +41,13 @@ const calculatedCurrentPage = currentPage
41
41
const calculatedTotalPages = totalPages
42
42
|| pages ?.length
43
43
|| 0
44
+
45
+ const generatedPages = pages ?.length
46
+ ? pages
47
+ : Array (totalPages || 0 ).fill (0 ).map ((_ , index ) => ({
48
+ ... (index === 0 && { active: true }),
49
+ label: index + 1
50
+ }))
44
51
---
45
52
46
53
<ul
@@ -51,7 +58,7 @@ const calculatedTotalPages = totalPages
51
58
>
52
59
{ type === ' dots' ? (
53
60
<Fragment >
54
- { pages ?.map (page => (
61
+ { generatedPages ?.map (page => (
55
62
<li >
56
63
<button
57
64
data-active = { page .active ? ' true' : undefined }
Original file line number Diff line number Diff line change 37
37
|| pages ?.length
38
38
|| 0
39
39
40
+ const generatedPages = pages ?.length
41
+ ? pages
42
+ : Array (totalPages || 0 ).fill (0 ).map ((_ , index ) => ({
43
+ ... (index === 0 && { active: true }),
44
+ label: index + 1
45
+ }))
46
+
40
47
const paginate = (to : string | number ) => {
41
48
if (to === ' prev' ) {
42
49
calculatedCurrentPage = calculatedCurrentPage - 1
54
61
})
55
62
}
56
63
57
- let calculatedCurrentPage = currentPage
64
+ $ : calculatedCurrentPage = currentPage
58
65
|| (pages ?.findIndex (page => page .active ) || - 1 ) + 1
59
66
|| 1
60
67
</script >
61
68
62
69
<ul class ={classes }>
63
- {#if type === ' dots' && pages ?.length }
64
- {#each pages as _ , index }
70
+ {#if type === ' dots' && generatedPages ?.length }
71
+ {#each generatedPages as _ , index }
65
72
<li >
66
73
<button
67
74
data-active ={calculatedCurrentPage === index + 1 ? ' true' : null }
Original file line number Diff line number Diff line change 1
- import React , { useState } from 'react'
1
+ import React , { useEffect , useState } from 'react'
2
2
import type { ReactPaginationProps } from './pagination'
3
3
4
4
import Button from '../Button/Button.tsx'
@@ -45,6 +45,13 @@ const Pagination = ({
45
45
|| pages ?. length
46
46
|| 0
47
47
48
+ const generatedPages = pages ?. length
49
+ ? pages
50
+ : Array ( totalPages || 0 ) . fill ( 0 ) . map ( ( _ , index ) => ( {
51
+ ...( index === 0 && { active : true } ) ,
52
+ label : index + 1
53
+ } ) )
54
+
48
55
const paginate = ( to : string | number ) => {
49
56
let currentPage = calculatedCurrentPage
50
57
@@ -66,11 +73,17 @@ const Pagination = ({
66
73
} )
67
74
}
68
75
76
+ useEffect ( ( ) => {
77
+ if ( currentPage ) {
78
+ setCalculatedCurrentPage ( currentPage )
79
+ }
80
+ } , [ currentPage ] )
81
+
69
82
return (
70
83
< ul className = { classes } >
71
84
{ type === 'dots' ? (
72
85
< React . Fragment >
73
- { pages ?. map ( ( _ , index ) => (
86
+ { generatedPages ?. map ( ( _ , index ) => (
74
87
< li key = { index } >
75
88
< button
76
89
data-active = { calculatedCurrentPage === index + 1 ? 'true' : null }
You can’t perform that action at this time.
0 commit comments