Skip to content

Commit 60eeddd

Browse files
committed
✨ Allow dot paginations to be created without pages prop
1 parent c3e491f commit 60eeddd

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

src/components/Pagination/Pagination.astro

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ const calculatedCurrentPage = currentPage
4141
const calculatedTotalPages = totalPages
4242
|| pages?.length
4343
|| 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+
}))
4451
---
4552

4653
<ul
@@ -51,7 +58,7 @@ const calculatedTotalPages = totalPages
5158
>
5259
{type === 'dots' ? (
5360
<Fragment>
54-
{pages?.map(page => (
61+
{generatedPages?.map(page => (
5562
<li>
5663
<button
5764
data-active={page.active ? 'true' : undefined}

src/components/Pagination/Pagination.svelte

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@
3737
|| pages?.length
3838
|| 0
3939
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+
4047
const paginate = (to: string | number) => {
4148
if (to === 'prev') {
4249
calculatedCurrentPage = calculatedCurrentPage - 1
@@ -54,14 +61,14 @@
5461
})
5562
}
5663
57-
let calculatedCurrentPage = currentPage
64+
$: calculatedCurrentPage = currentPage
5865
|| (pages?.findIndex(page => page.active) || -1) + 1
5966
|| 1
6067
</script>
6168

6269
<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}
6572
<li>
6673
<button
6774
data-active={calculatedCurrentPage === index + 1 ? 'true' : null}

src/components/Pagination/Pagination.tsx

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState } from 'react'
1+
import React, { useEffect, useState } from 'react'
22
import type { ReactPaginationProps } from './pagination'
33

44
import Button from '../Button/Button.tsx'
@@ -45,6 +45,13 @@ const Pagination = ({
4545
|| pages?.length
4646
|| 0
4747

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+
4855
const paginate = (to: string | number) => {
4956
let currentPage = calculatedCurrentPage
5057

@@ -66,11 +73,17 @@ const Pagination = ({
6673
})
6774
}
6875

76+
useEffect(() => {
77+
if (currentPage) {
78+
setCalculatedCurrentPage(currentPage)
79+
}
80+
}, [currentPage])
81+
6982
return (
7083
<ul className={classes}>
7184
{type === 'dots' ? (
7285
<React.Fragment>
73-
{pages?.map((_, index) => (
86+
{generatedPages?.map((_, index) => (
7487
<li key={index}>
7588
<button
7689
data-active={calculatedCurrentPage === index + 1 ? 'true' : null}

0 commit comments

Comments
 (0)