Skip to content

Commit 5e37c79

Browse files
authored
fix: disable input controls when there is only one page in TablePagination (#1213)
1 parent 44b4829 commit 5e37c79

File tree

4 files changed

+57
-2
lines changed

4 files changed

+57
-2
lines changed

src/components/TablePagination/TablePaginationControls/TablePaginationControls.test.tsx

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,48 @@ describe("<TablePaginationControls />", () => {
154154
document.querySelector(".description")?.textContent,
155155
).not.toBeUndefined();
156156
});
157+
158+
it("disables page input when there is only one page", () => {
159+
render(
160+
<TablePaginationControls
161+
visibleCount={5}
162+
itemName="item"
163+
pageLimits={[20, 50, 100]}
164+
totalItems={5}
165+
currentPage={1}
166+
pageSize={20}
167+
onPageChange={jest.fn()}
168+
onPageSizeChange={jest.fn()}
169+
/>,
170+
);
171+
172+
const pageInput = screen.getByRole("spinbutton", {
173+
name: Label.PAGE_NUMBER,
174+
});
175+
expect(pageInput).toBeDisabled();
176+
expect(pageInput).toHaveAttribute("min", "1");
177+
expect(pageInput).toHaveAttribute("max", "1");
178+
});
179+
180+
it("enables page input when there are multiple pages", () => {
181+
render(
182+
<TablePaginationControls
183+
visibleCount={20}
184+
itemName="item"
185+
pageLimits={[20, 50, 100]}
186+
totalItems={100}
187+
currentPage={1}
188+
pageSize={20}
189+
onPageChange={jest.fn()}
190+
onPageSizeChange={jest.fn()}
191+
/>,
192+
);
193+
194+
const pageInput = screen.getByRole("spinbutton", {
195+
name: Label.PAGE_NUMBER,
196+
});
197+
expect(pageInput).not.toBeDisabled();
198+
expect(pageInput).toHaveAttribute("min", "1");
199+
expect(pageInput).toHaveAttribute("max", "5");
200+
});
157201
});

src/components/TablePagination/TablePaginationControls/TablePaginationControls.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ const TablePaginationControls = ({
103103
onPageSizeChange(parseInt(e.target.value));
104104
};
105105

106+
const isInputDisabled = !totalPages || totalPages == 1;
107+
const maxPageValue = typeof totalPages === "number" ? totalPages : 1;
108+
106109
return (
107110
<div
108111
className={classnames("pagination", className)}
@@ -117,7 +120,7 @@ const TablePaginationControls = ({
117120
className="back"
118121
appearance="base"
119122
hasIcon
120-
disabled={currentPage === 1}
123+
disabled={isInputDisabled || currentPage === 1}
121124
onClick={() => handleDecrementPage(currentPage)}
122125
{...previousButtonProps}
123126
>
@@ -137,6 +140,9 @@ const TablePaginationControls = ({
137140
onChange={handleInputPageChange}
138141
value={currentPage}
139142
type="number"
143+
disabled={isInputDisabled}
144+
min={1}
145+
max={maxPageValue}
140146
/>{" "}
141147
{typeof totalPages === "number" ? <>of&nbsp;{totalPages}</> : null}
142148
</>
@@ -146,7 +152,7 @@ const TablePaginationControls = ({
146152
className="next"
147153
appearance="base"
148154
hasIcon
149-
disabled={currentPage === totalPages}
155+
disabled={isInputDisabled || currentPage === totalPages}
150156
onClick={() => handleIncrementPage(currentPage, totalPages)}
151157
{...nextButtonProps}
152158
>

src/components/TablePagination/TablePaginationControls/__snapshots__/TablePaginationControls.test.tsx.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ exports[`<TablePaginationControls /> renders table pagination controls and match
2626
aria-invalid="false"
2727
class="p-form-validation__input u-no-margin--bottom pagination-input"
2828
id="paginationPageInput"
29+
max="5"
30+
min="1"
2931
type="number"
3032
value="0"
3133
/>

src/components/TablePagination/__snapshots__/TablePagination.test.tsx.snap

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ exports[`<TablePagination /> renders table pagination and matches the snapshot 1
3535
<input
3636
aria-invalid="false"
3737
class="p-form-validation__input u-no-margin--bottom pagination-input"
38+
disabled=""
3839
id="paginationPageInput"
40+
max="1"
41+
min="1"
3942
type="number"
4043
value="1"
4144
/>

0 commit comments

Comments
 (0)