Skip to content

Commit

Permalink
feat: add first and last to the Page interface (#11176)
Browse files Browse the repository at this point in the history
* feat: add first and last to the Page interface

* Update .changeset/twenty-maps-glow.md

* Update .changeset/twenty-maps-glow.md

Co-authored-by: Sarah Rainsberger <[email protected]>

---------

Co-authored-by: Bjorn Lu <[email protected]>
Co-authored-by: Matthew Phillips <[email protected]>
Co-authored-by: Sarah Rainsberger <[email protected]>
  • Loading branch information
4 people authored Jul 17, 2024
1 parent 49b5145 commit a751458
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/twenty-maps-glow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': minor
---

Adds two new values to the [pagination `page` prop](https://docs.astro.build/en/reference/api-reference/#the-pagination-page-prop): `page.first` and `page.last` for accessing the URLs of the first and last pages.
4 changes: 4 additions & 0 deletions packages/astro/src/@types/astro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2647,6 +2647,10 @@ export interface Page<T = any> {
prev: string | undefined;
/** url of the next page (if there is one) */
next: string | undefined;
/** url of the first page (if the current page is not the first page) */
first: string | undefined;
/** url of the next page (if the current page in not the last page) */
last: string | undefined;
};
}

Expand Down
15 changes: 14 additions & 1 deletion packages/astro/src/core/render/paginate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,19 @@ export function generatePaginateFunction(
!includesFirstPageNumber && pageNum - 1 === 1 ? undefined : String(pageNum - 1),
})
);
const first =
pageNum === 1
? undefined
: correctIndexRoute(
routeMatch.generate({
...params,
page: includesFirstPageNumber? "1": undefined,
})
);
const last =
pageNum === lastPage
? undefined
: correctIndexRoute(routeMatch.generate({ ...params, page: String(lastPage)}));
return {
params,
props: {
Expand All @@ -68,7 +81,7 @@ export function generatePaginateFunction(
total: data.length,
currentPage: pageNum,
lastPage: lastPage,
url: { current, next, prev },
url: { current, next, prev, first, last },
} as Page,
},
};
Expand Down

0 comments on commit a751458

Please sign in to comment.