Skip to content

Commit

Permalink
Merge pull request #461 from ClickHouse/pagination_all_rows
Browse files Browse the repository at this point in the history
Conditional `All rows` option for `<Pagination/>`
  • Loading branch information
rndD authored Aug 12, 2024
2 parents 41e0a70 + ce329ff commit 5b34c8c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/components/Pagination/Pagination.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,22 @@ describe("Pagination", () => {
});
expect(onChange).not.toHaveBeenCalled();
});

it("should not show `All rows` in the page sizes if it's disabled", async () => {
const onPageSizeChange = vi.fn();
const { getByText, queryByText } = renderPagination({
currentPage: 1,
onChange,
onPageSizeChange,
maxRowsPerPageList: [250, 500],
allowAllRows: false,
});
expect(queryByText("All rows")).not.toBeInTheDocument();

await vi.waitFor(() => {
fireEvent.click(getByText("250 rows"));
fireEvent.click(getByText("500 rows"));
expect(onPageSizeChange).toBeCalledTimes(1);
});
});
});
9 changes: 8 additions & 1 deletion src/components/Pagination/Pagination.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export interface PaginationProps
onPageNumberFocus?: FocusEventHandler<HTMLInputElement>;
onPageNumberBlur?: FocusEventHandler<HTMLInputElement>;
disableNextButton?: boolean;
allowAllRows?: boolean;
}
const CustomSelect = styled.div`
width: 150px;
Expand All @@ -51,6 +52,7 @@ export const Pagination = ({
onPageNumberFocus,
onPageNumberBlur,
disableNextButton,
allowAllRows = true,
...props
}: PaginationProps): ReactElement => {
const hasRowCount = ["number", "string"].includes(typeof rowCount);
Expand Down Expand Up @@ -129,6 +131,11 @@ export const Pagination = ({
[currentPage, onChangeProp, onNextPageClick, rightButtonDisabled]
);

// if `allowAllRows` is false we need to switch to the first option in the list
if (pageSize === -1 && !allowAllRows && maxRowsPerPageList.length > 0) {
pageSize = maxRowsPerPageList[0];
}

return (
<Container
gap={gap}
Expand Down Expand Up @@ -201,7 +208,7 @@ export const Pagination = ({
onSelect={onPageSizeChange}
value={pageSize.toString()}
>
<Select.Item value="-1">All rows</Select.Item>
{allowAllRows && <Select.Item value="-1">All rows</Select.Item>}
{maxRowsPerPageList.map(option => (
<Select.Item
key={option}
Expand Down

0 comments on commit 5b34c8c

Please sign in to comment.