0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-30 22:03:56 -05:00

feat: add first and last to the Page interface (#11176)

* 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 <sarah@rainsberger.ca>

---------

Co-authored-by: Bjorn Lu <bjornlu.dev@gmail.com>
Co-authored-by: Matthew Phillips <matthew@matthewphillips.info>
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
This commit is contained in:
Takeo Sawada 2024-07-17 22:55:53 +08:00 committed by Emanuele Stoppa
parent 4afb5fe23c
commit d7502ee904
3 changed files with 23 additions and 1 deletions

View file

@ -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.

View file

@ -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;
};
}

View file

@ -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: {
@ -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,
},
};