Skip to content

Commit 8681817

Browse files
presleyppull[bot]
authored andcommitted
Feat: hide pagination widget when not needed (#4957)
* Hide pagination widget when not needed. Don't merge until count is in. * Format * Refactor * Remove story * Fix bug * Format
1 parent 26fd6f3 commit 8681817

File tree

2 files changed

+42
-48
lines changed

2 files changed

+42
-48
lines changed

site/src/components/PaginationWidget/PaginationWidget.stories.tsx

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@ const Template: Story<PaginationWidgetProps> = (
2525
args: PaginationWidgetProps,
2626
) => <PaginationWidget {...args} />
2727

28-
export const UnknownPageNumbers = Template.bind({})
29-
UnknownPageNumbers.args = {
30-
numRecords: undefined,
31-
}
32-
3328
export const LessThan8Pages = Template.bind({})
3429
LessThan8Pages.args = {
3530
numRecords: 84,

site/src/components/PaginationWidget/PaginationWidget.tsx

Lines changed: 42 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,24 @@ export const PaginationWidget = ({
3434
const currentPage = paginationState.context.page
3535
const numRecordsPerPage = paginationState.context.limit
3636

37-
const numPages = numRecords
38-
? Math.ceil(numRecords / numRecordsPerPage)
39-
: undefined
37+
const numPages = numRecords ? Math.ceil(numRecords / numRecordsPerPage) : 0
4038
const firstPageActive = currentPage === 1 && numPages !== 0
4139
const lastPageActive = currentPage === numPages && numPages !== 0
40+
// if beyond page 1, show pagination widget even if there's only one true page, so user can navigate back
41+
const showWidget = numPages > 1 || currentPage > 1
4242

4343
return (
44-
<div style={containerStyle} className={styles.defaultContainerStyles}>
45-
<Button
46-
className={styles.prevLabelStyles}
47-
aria-label="Previous page"
48-
disabled={firstPageActive}
49-
onClick={() => send({ type: "PREVIOUS_PAGE" })}
50-
>
51-
<KeyboardArrowLeft />
52-
<div>{prevLabel}</div>
53-
</Button>
54-
<Maybe condition={numPages !== undefined}>
44+
<Maybe condition={showWidget}>
45+
<div style={containerStyle} className={styles.defaultContainerStyles}>
46+
<Button
47+
className={styles.prevLabelStyles}
48+
aria-label="Previous page"
49+
disabled={firstPageActive}
50+
onClick={() => send({ type: "PREVIOUS_PAGE" })}
51+
>
52+
<KeyboardArrowLeft />
53+
<div>{prevLabel}</div>
54+
</Button>
5555
<ChooseOne>
5656
<Cond condition={isMobile}>
5757
<PageButton
@@ -61,37 +61,36 @@ export const PaginationWidget = ({
6161
/>
6262
</Cond>
6363
<Cond>
64-
{numPages &&
65-
buildPagedList(numPages, currentPage).map((page) =>
66-
typeof page !== "number" ? (
67-
<PageButton
68-
key={`Page${page}`}
69-
activePage={currentPage}
70-
placeholder="..."
71-
disabled
72-
/>
73-
) : (
74-
<PageButton
75-
key={`Page${page}`}
76-
activePage={currentPage}
77-
page={page}
78-
numPages={numPages}
79-
onPageClick={() => send({ type: "GO_TO_PAGE", page })}
80-
/>
81-
),
82-
)}
64+
{buildPagedList(numPages, currentPage).map((page) =>
65+
typeof page !== "number" ? (
66+
<PageButton
67+
key={`Page${page}`}
68+
activePage={currentPage}
69+
placeholder="..."
70+
disabled
71+
/>
72+
) : (
73+
<PageButton
74+
key={`Page${page}`}
75+
activePage={currentPage}
76+
page={page}
77+
numPages={numPages}
78+
onPageClick={() => send({ type: "GO_TO_PAGE", page })}
79+
/>
80+
),
81+
)}
8382
</Cond>
8483
</ChooseOne>
85-
</Maybe>
86-
<Button
87-
aria-label="Next page"
88-
disabled={lastPageActive}
89-
onClick={() => send({ type: "NEXT_PAGE" })}
90-
>
91-
<div>{nextLabel}</div>
92-
<KeyboardArrowRight />
93-
</Button>
94-
</div>
84+
<Button
85+
aria-label="Next page"
86+
disabled={lastPageActive}
87+
onClick={() => send({ type: "NEXT_PAGE" })}
88+
>
89+
<div>{nextLabel}</div>
90+
<KeyboardArrowRight />
91+
</Button>
92+
</div>
93+
</Maybe>
9594
)
9695
}
9796

0 commit comments

Comments
 (0)