Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make pagination query param configurable #256

Merged
merged 1 commit into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions src/npm-fastui-bootstrap/src/pagination.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ interface Link {
locked?: boolean
active?: boolean
page?: number
pageQueryParam?: string
}

export const Pagination: FC<models.Pagination> = (props) => {
const { page, pageCount } = props

const { page, pageCount, pageQueryParam } = props
if (pageCount === 1) return null

const links: Link[] = [
Expand All @@ -20,17 +20,19 @@ export const Pagination: FC<models.Pagination> = (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++) {
Expand All @@ -40,24 +42,27 @@ export const Pagination: FC<models.Pagination> = (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({
Display: () => <span aria-hidden="true">&raquo;</span>,
ariaLabel: 'Next',
locked: page === pageCount,
page: page + 1,
pageQueryParam,
})

return (
Expand All @@ -71,7 +76,7 @@ export const Pagination: FC<models.Pagination> = (props) => {
)
}

const PaginationLink: FC<Link> = ({ Display, ariaLabel, locked, active, page }) => {
const PaginationLink: FC<Link> = ({ Display, ariaLabel, locked, active, page, pageQueryParam }) => {
if (!page) {
return (
<li className="page-item">
Expand All @@ -82,7 +87,10 @@ const PaginationLink: FC<Link> = ({ 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 (
<li className="page-item">
<components.LinkRender onClick={onClick} className={className} locked={locked || active} ariaLabel={ariaLabel}>
Expand Down
1 change: 1 addition & 0 deletions src/npm-fastui/src/models.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ export interface Pagination {
total: number
className?: ClassName
type: 'Pagination'
pageQueryParam?: string
pageCount: number
}
/**
Expand Down
1 change: 1 addition & 0 deletions src/python-fastui/fastui/components/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Loading