From d1bae71579d33f0730a9ed9bdf8c441828205ea6 Mon Sep 17 00:00:00 2001 From: Hasan Ramezani Date: Fri, 22 Mar 2024 18:02:55 +0100 Subject: [PATCH] Make pagination query param configurable --- src/npm-fastui-bootstrap/src/pagination.tsx | 20 +++++++++++++------ src/npm-fastui/src/models.d.ts | 1 + src/python-fastui/fastui/components/tables.py | 1 + 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/npm-fastui-bootstrap/src/pagination.tsx b/src/npm-fastui-bootstrap/src/pagination.tsx index fa9e11bb..d57adb07 100644 --- a/src/npm-fastui-bootstrap/src/pagination.tsx +++ b/src/npm-fastui-bootstrap/src/pagination.tsx @@ -7,11 +7,11 @@ interface Link { locked?: boolean active?: boolean page?: number + pageQueryParam?: string } export const Pagination: FC = (props) => { - const { page, pageCount } = props - + const { page, pageCount, pageQueryParam } = props if (pageCount === 1) return null const links: Link[] = [ @@ -20,17 +20,19 @@ export const Pagination: FC = (props) => { ariaLabel: 'Previous', locked: page === 1, page: page - 1, + pageQueryParam, }, { Display: () => <>1, locked: page === 1, active: page === 1, page: 1, + pageQueryParam, }, ] if (page > 4) { - links.push({ Display: () => <>... }) + links.push({ Display: () => <>..., pageQueryParam }) } for (let p = page - 2; p <= page + 2; p++) { @@ -40,17 +42,19 @@ export const Pagination: FC = (props) => { locked: page === p, active: page === p, page: p, + pageQueryParam, }) } if (page < pageCount - 3) { - links.push({ Display: () => <>... }) + links.push({ Display: () => <>..., pageQueryParam }) } links.push({ Display: () => <>{pageCount}, locked: page === pageCount, page: pageCount, + pageQueryParam, }) links.push({ @@ -58,6 +62,7 @@ export const Pagination: FC = (props) => { ariaLabel: 'Next', locked: page === pageCount, page: page + 1, + pageQueryParam, }) return ( @@ -71,7 +76,7 @@ export const Pagination: FC = (props) => { ) } -const PaginationLink: FC = ({ Display, ariaLabel, locked, active, page }) => { +const PaginationLink: FC = ({ Display, ariaLabel, locked, active, page, pageQueryParam }) => { if (!page) { return (
  • @@ -82,7 +87,10 @@ const PaginationLink: FC = ({ Display, ariaLabel, locked, active, page }) ) } const className = renderClassName({ 'page-link': true, disabled: locked && !active, active } as models.ClassName) - const onClick: models.GoToEvent = { type: 'go-to', query: { page } } + const onClick: models.GoToEvent = { + type: 'go-to', + query: { [pageQueryParam !== undefined ? pageQueryParam : 'page']: page }, + } return (
  • diff --git a/src/npm-fastui/src/models.d.ts b/src/npm-fastui/src/models.d.ts index a0ca91cd..cda0a45d 100644 --- a/src/npm-fastui/src/models.d.ts +++ b/src/npm-fastui/src/models.d.ts @@ -300,6 +300,7 @@ export interface Pagination { total: number className?: ClassName type: 'Pagination' + pageQueryParam?: string pageCount: number } /** diff --git a/src/python-fastui/fastui/components/tables.py b/src/python-fastui/fastui/components/tables.py index d83b4e49..28bfd95f 100644 --- a/src/python-fastui/fastui/components/tables.py +++ b/src/python-fastui/fastui/components/tables.py @@ -59,6 +59,7 @@ class Pagination(pydantic.BaseModel): total: int class_name: _class_name.ClassNameField = None type: _t.Literal['Pagination'] = 'Pagination' + page_query_param: str = pydantic.Field('page', serialization_alias='pageQueryParam') @pydantic.computed_field(alias='pageCount') def page_count(self) -> int: