Skip to content

Commit

Permalink
✨ Allow dot paginations to be created without pages prop
Browse files Browse the repository at this point in the history
  • Loading branch information
Frontendland committed Sep 20, 2024
1 parent c3e491f commit 60eeddd
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
9 changes: 8 additions & 1 deletion src/components/Pagination/Pagination.astro
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ const calculatedCurrentPage = currentPage
const calculatedTotalPages = totalPages
|| pages?.length
|| 0
const generatedPages = pages?.length
? pages
: Array(totalPages || 0).fill(0).map((_, index) => ({
...(index === 0 && { active: true }),
label: index + 1
}))
---

<ul
Expand All @@ -51,7 +58,7 @@ const calculatedTotalPages = totalPages
>
{type === 'dots' ? (
<Fragment>
{pages?.map(page => (
{generatedPages?.map(page => (
<li>
<button
data-active={page.active ? 'true' : undefined}
Expand Down
13 changes: 10 additions & 3 deletions src/components/Pagination/Pagination.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@
|| pages?.length
|| 0
const generatedPages = pages?.length
? pages
: Array(totalPages || 0).fill(0).map((_, index) => ({
...(index === 0 && { active: true }),
label: index + 1
}))
const paginate = (to: string | number) => {
if (to === 'prev') {
calculatedCurrentPage = calculatedCurrentPage - 1
Expand All @@ -54,14 +61,14 @@
})
}
let calculatedCurrentPage = currentPage
$: calculatedCurrentPage = currentPage
|| (pages?.findIndex(page => page.active) || -1) + 1
|| 1
</script>

<ul class={classes}>
{#if type === 'dots' && pages?.length}
{#each pages as _, index}
{#if type === 'dots' && generatedPages?.length}
{#each generatedPages as _, index}
<li>
<button
data-active={calculatedCurrentPage === index + 1 ? 'true' : null}
Expand Down
17 changes: 15 additions & 2 deletions src/components/Pagination/Pagination.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from 'react'
import React, { useEffect, useState } from 'react'
import type { ReactPaginationProps } from './pagination'

import Button from '../Button/Button.tsx'
Expand Down Expand Up @@ -45,6 +45,13 @@ const Pagination = ({
|| pages?.length
|| 0

const generatedPages = pages?.length
? pages
: Array(totalPages || 0).fill(0).map((_, index) => ({
...(index === 0 && { active: true }),
label: index + 1
}))

const paginate = (to: string | number) => {
let currentPage = calculatedCurrentPage

Expand All @@ -66,11 +73,17 @@ const Pagination = ({
})
}

useEffect(() => {
if (currentPage) {
setCalculatedCurrentPage(currentPage)
}
}, [currentPage])

return (
<ul className={classes}>
{type === 'dots' ? (
<React.Fragment>
{pages?.map((_, index) => (
{generatedPages?.map((_, index) => (
<li key={index}>
<button
data-active={calculatedCurrentPage === index + 1 ? 'true' : null}
Expand Down

0 comments on commit 60eeddd

Please sign in to comment.