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

feat(pagination): allow custom label #99

Merged
merged 1 commit into from
Feb 6, 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
37 changes: 28 additions & 9 deletions src/lib/components/Pagination/Pagination.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ it("renders without crashing", () => {
totalPages={10}
/>,
);
expect(screen.getByRole("navigation", { name: "pagination"})).toBeInTheDocument();
expect(
screen.getByRole("navigation", { name: "pagination" }),
).toBeInTheDocument();
});

it("displays the current page number in the input box", () => {
Expand All @@ -33,7 +35,24 @@ it("displays the current page number in the input box", () => {
);

expect(screen.getByRole("spinbutton")).toHaveValue(1);
})
});

it("allows for a custom accessible label", () => {
render(
<Pagination
aria-label="Custom label"
disabled={false}
onInputBlur={vi.fn()}
onInputChange={vi.fn()}
onNextClick={vi.fn()}
onPreviousClick={vi.fn()}
currentPage={1}
totalPages={10}
/>,
);

expect(screen.getByLabelText("Custom label")).toBeInTheDocument();
});

it("disables the buttons and input box when the disabled prop is true", () => {
render(
Expand All @@ -51,7 +70,7 @@ it("disables the buttons and input box when the disabled prop is true", () => {
expect(screen.getByRole("spinbutton")).toBeDisabled();
expect(screen.getByRole("button", { name: "Next page" })).toBeDisabled();
expect(screen.getByRole("button", { name: "Previous page" })).toBeDisabled();
})
});

it("disables the 'Previous page' button when on the first page", () => {
render(
Expand All @@ -67,7 +86,7 @@ it("disables the 'Previous page' button when on the first page", () => {
);

expect(screen.getByRole("button", { name: "Previous page" })).toBeDisabled();
})
});

it("disables the 'Next page' button when on the last page", () => {
render(
Expand All @@ -83,7 +102,7 @@ it("disables the 'Next page' button when on the last page", () => {
);

expect(screen.getByRole("button", { name: "Next page" })).toBeDisabled();
})
});

it("can call a function when the 'Next page' button is pressed", async () => {
const onNextClick = vi.fn();
Expand All @@ -102,7 +121,7 @@ it("can call a function when the 'Next page' button is pressed", async () => {
await userEvent.click(screen.getByRole("button", { name: "Next page" }));

expect(onNextClick).toHaveBeenCalled();
})
});

it("can call a function when the 'Previous page' button is pressed", async () => {
const onPreviousClick = vi.fn();
Expand All @@ -121,7 +140,7 @@ it("can call a function when the 'Previous page' button is pressed", async () =>
await userEvent.click(screen.getByRole("button", { name: "Previous page" }));

expect(onPreviousClick).toHaveBeenCalled();
})
});

it("can call a function when the input box's value is changed", async () => {
const onInputChange = vi.fn();
Expand All @@ -141,7 +160,7 @@ it("can call a function when the input box's value is changed", async () => {
await userEvent.clear(screen.getByRole("spinbutton"));

expect(onInputChange).toHaveBeenCalled();
})
});

it("can call a function when the input box is blurred", async () => {
const onInputBlur = vi.fn();
Expand All @@ -165,4 +184,4 @@ it("can call a function when the input box is blurred", async () => {
// blur the input
await userEvent.click(screen.getByText("of 10"));
expect(onInputBlur).toHaveBeenCalled();
})
});
15 changes: 11 additions & 4 deletions src/lib/components/Pagination/Pagination.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@ import { ChangeEvent, useState } from "react";

import "./Pagination.scss";
import { Button, Icon, Input } from "@canonical/react-components";
import classNames from "classnames";

export interface PaginationProps {
export interface PaginationProps extends React.AriaAttributes {
className?: string;
currentPage: number | undefined;
error?: string;
disabled: boolean;
onInputBlur: () => void;
disabled?: boolean;
onInputBlur?: () => void;
onInputChange: (e: ChangeEvent<HTMLInputElement>) => void;
onNextClick: () => void;
onPreviousClick: () => void;
totalPages: number;
}

export const Pagination: React.FC<PaginationProps> = ({
className,
currentPage,
error,
disabled,
Expand All @@ -23,9 +26,13 @@ export const Pagination: React.FC<PaginationProps> = ({
onNextClick,
onPreviousClick,
totalPages,
...props
}: PaginationProps) => {
return (
<nav aria-label="pagination" className="p-pagination">
<nav
aria-label={props?.["aria-label"] || "pagination"}
className={classNames("p-pagination", className)}
>
<span className="p-pagination--items">
<Button
aria-label="Previous page"
Expand Down
Loading