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

fix: onNextClick, onPrevClick not being called #2172

Merged
merged 3 commits into from
May 30, 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
25 changes: 25 additions & 0 deletions src/components/Nav.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import React from "react";

import { nextButton, previousButton } from "@/test/elements";
import { render } from "@/test/render";
import { user } from "@/test/user";

import { Nav } from "./Nav";

describe("when clicking the next button", () => {
test("should call the onNextClick callback", async () => {
const onNextClick = jest.fn();
render(<Nav />, { onNextClick });
await user.click(nextButton());
expect(onNextClick).toHaveBeenCalled();
});
});

describe("when clicking the previous button", () => {
test("should call the onPrevClick callback", async () => {
const onPrevClick = jest.fn();
render(<Nav />, { onPrevClick });
await user.click(previousButton());
expect(onPrevClick).toHaveBeenCalled();
});
});
22 changes: 18 additions & 4 deletions src/components/Nav.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from "react";
import React, { MouseEventHandler } from "react";

import { UI } from "../UI";
import { useCalendar } from "../contexts/calendar";
Expand All @@ -22,11 +22,25 @@ export function Nav() {
labels: { labelNext, labelPrevious },
locale,
components,
id
id,
onNextClick,
onPrevClick
} = useProps();

const calendar = useCalendar();

const handlePreviousClick = () => {
if (!calendar.previousMonth) return;
calendar.goToPreviousMonth();
onPrevClick?.(calendar.previousMonth);
};

const handleNextClick = () => {
if (!calendar.nextMonth) return;
calendar.goToNextMonth();
onNextClick?.(calendar.nextMonth);
};

const Button = components?.Button ?? DefaultButton;
const Chevron = components?.Chevron ?? DefaultChevron;

Expand All @@ -40,7 +54,7 @@ export function Nav() {
disabled={calendar.previousMonth ? undefined : true}
aria-label={labelPrevious(calendar.previousMonth, { locale })}
aria-controls={id}
onClick={calendar.goToPreviousMonth}
onClick={handlePreviousClick}
>
<Chevron />
</Button>
Expand All @@ -52,7 +66,7 @@ export function Nav() {
disabled={calendar.nextMonth ? undefined : true}
aria-label={labelNext(calendar.nextMonth, { locale })}
aria-controls={id}
onClick={calendar.goToNextMonth}
onClick={handleNextClick}
>
<Chevron orientation="right" />
</Button>
Expand Down
Loading