Skip to content

Commit

Permalink
Merge branch 'main' of github.com:gpbl/react-day-picker
Browse files Browse the repository at this point in the history
  • Loading branch information
gpbl committed Jul 24, 2024
2 parents 5b6e030 + 0a41d13 commit 5d42b37
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ While we tried to keep the API as stable as possible, some breaking changes were
- Some typings have been renamed or deprecated.
- The `useInput` hook has been removed. See [Input fields](https://daypicker.dev/guides/input-fields) guide for more details.
- `onWeekNumberClick` has been removed. Use a custom component to handle week number clicks.
- Some of the `onDay*` events, like `onDayTouchStart` or `onDayDoubleClick` have been removed. To reimplement them, use a custom `DayButton` component ([example](https://daypicker.dev/guides/custom-components#intercepting-click-events)).
- The updated build system to ESM and CommonJS could break some custom bundler.

### Upgrading Guide
Expand Down
27 changes: 25 additions & 2 deletions src/DayPicker.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from "react";

import { render, screen } from "@testing-library/react";
import { startOfMonth } from "date-fns";
import { fireEvent, render, screen } from "@testing-library/react";
import { startOfDay, startOfMonth } from "date-fns";

import {
activeElement,
Expand Down Expand Up @@ -108,3 +108,26 @@ describe("when the grid is focused", () => {
});
});
});

describe("when a day is mouse entered", () => {
const handleDayMouseEnter = jest.fn();
const handleDayMouseLeave = jest.fn();
const today = startOfDay(new Date());
beforeEach(async () => {
render(
<DayPicker
today={today}
defaultMonth={today}
mode="single"
onDayMouseEnter={handleDayMouseEnter}
onDayMouseLeave={handleDayMouseLeave}
/>
);
fireEvent.mouseEnter(dateButton(today));
fireEvent.mouseLeave(dateButton(today));
});
test("should call the event handler", async () => {
expect(handleDayMouseEnter).toHaveBeenCalled();
expect(handleDayMouseLeave).toHaveBeenCalled();
});
});
24 changes: 24 additions & 0 deletions src/DayPicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ export function DayPicker(props: DayPickerProps) {
onDayClick,
onDayFocus,
onDayKeyDown,
onDayMouseEnter,
onDayMouseLeave,
onNextClick,
onPrevClick,
showWeekNumber,
Expand Down Expand Up @@ -209,6 +211,20 @@ export function DayPicker(props: DayPickerProps) {
[moveFocus, onDayKeyDown, props.dir]
);

const handleDayMouseEnter = useCallback(
(day: CalendarDay, modifiers: Modifiers) => (e: MouseEvent) => {
onDayMouseEnter?.(day.date, modifiers, e);
},
[onDayMouseEnter]
);

const handleDayMouseLeave = useCallback(
(day: CalendarDay, modifiers: Modifiers) => (e: MouseEvent) => {
onDayMouseLeave?.(day.date, modifiers, e);
},
[onDayMouseLeave]
);

const { className, style } = useMemo(
() => ({
className: [classNames[UI.Root], props.className]
Expand Down Expand Up @@ -552,6 +568,14 @@ export function DayPicker(props: DayPickerProps) {
onBlur={handleDayBlur(day, modifiers)}
onFocus={handleDayFocus(day, modifiers)}
onKeyDown={handleDayKeyDown(day, modifiers)}
onMouseEnter={handleDayMouseEnter(
day,
modifiers
)}
onMouseLeave={handleDayMouseLeave(
day,
modifiers
)}
>
{formatDay(date, formatOptions, dateLib)}
</components.DayButton>
Expand Down
4 changes: 4 additions & 0 deletions src/types/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,10 @@ export interface PropsBase {
onDayBlur?: DayEventHandler<React.FocusEvent>;
/** Event handler when a key is pressed on a day. */
onDayKeyDown?: DayEventHandler<React.KeyboardEvent>;
/** Event handler when the mouse enters a day. */
onDayMouseEnter?: DayEventHandler<React.MouseEvent>;
/** Event handler when the mouse leaves a day. */
onDayMouseLeave?: DayEventHandler<React.MouseEvent>;

/**
* Replace the default date library with a custom one.
Expand Down

0 comments on commit 5d42b37

Please sign in to comment.