Skip to content

Commit

Permalink
refactor: pagination monitor (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
bouassaba authored Nov 13, 2024
1 parent c3f3e0c commit 43cb124
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@koupr/ui",
"version": "1.10.10",
"version": "1.10.11",
"license": "MIT",
"type": "module",
"source": "src/index.ts",
Expand Down
6 changes: 3 additions & 3 deletions src/components/page-pagination.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const PagePagination = ({
isFastForwardDisabled = false,
isRewindDisabled = false,
}: PagePaginationProps) => {
const { hasPageSwitcher, hasSizeSelector } = usePageMonitor({
const { hasPageSwitcher, hasSizeSelector, hasPagination } = usePageMonitor({
totalElements,
totalPages,
steps,
Expand All @@ -51,7 +51,7 @@ export const PagePagination = ({

return (
<>
{!hasPageSwitcher && !hasSizeSelector ? null : (
{hasPagination ? (
<div
className={cx(
'koupr-flex',
Expand Down Expand Up @@ -83,7 +83,7 @@ export const PagePagination = ({
</Select>
) : null}
</div>
)}
) : null}
</>
)
}
11 changes: 9 additions & 2 deletions src/hooks/page-monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,20 @@ export type UsePageMonitorMonitorOptions = {
steps: number[]
}

export type UsePageMonitorMonitorResult = {
hasPageSwitcher: boolean
hasSizeSelector: boolean
hasPagination: boolean
}

export const usePageMonitor = ({
totalPages,
totalElements,
steps,
}: UsePageMonitorMonitorOptions) => {
}: UsePageMonitorMonitorOptions): UsePageMonitorMonitorResult => {
const hasPageSwitcher = totalPages > 1
const hasSizeSelector = totalElements > steps[0]
const hasPagination = hasPageSwitcher || hasSizeSelector

return { hasPageSwitcher, hasSizeSelector }
return { hasPageSwitcher, hasSizeSelector, hasPagination }
}
10 changes: 9 additions & 1 deletion src/hooks/page-pagination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ export type UsePagePaginationOptions = {
steps?: number[]
}

export type UsePagePaginationResult = {
page: number
size: number
steps: number[]
setPage: (page: number) => void
setSize: (size: number) => void
}

export const usePagePagination = ({
navigateFn,
searchFn,
Expand All @@ -16,7 +24,7 @@ export const usePagePagination = ({
namespace: 'main',
},
steps = [5, 10, 20, 40, 80, 100],
}: UsePagePaginationOptions) => {
}: UsePagePaginationOptions): UsePagePaginationResult => {
const search = searchFn()
const queryParams = useMemo(() => new URLSearchParams(search), [search])
const page = Number(queryParams.get('page')) || 1
Expand Down

0 comments on commit 43cb124

Please sign in to comment.