Skip to content

Commit 5d42b37

Browse files
committed
Merge branch 'main' of github.com:gpbl/react-day-picker
2 parents 5b6e030 + 0a41d13 commit 5d42b37

4 files changed

Lines changed: 54 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ While we tried to keep the API as stable as possible, some breaking changes were
3636
- Some typings have been renamed or deprecated.
3737
- The `useInput` hook has been removed. See [Input fields](https://daypicker.dev/guides/input-fields) guide for more details.
3838
- `onWeekNumberClick` has been removed. Use a custom component to handle week number clicks.
39+
- 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)).
3940
- The updated build system to ESM and CommonJS could break some custom bundler.
4041

4142
### Upgrading Guide

src/DayPicker.test.tsx

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from "react";
22

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

66
import {
77
activeElement,
@@ -108,3 +108,26 @@ describe("when the grid is focused", () => {
108108
});
109109
});
110110
});
111+
112+
describe("when a day is mouse entered", () => {
113+
const handleDayMouseEnter = jest.fn();
114+
const handleDayMouseLeave = jest.fn();
115+
const today = startOfDay(new Date());
116+
beforeEach(async () => {
117+
render(
118+
<DayPicker
119+
today={today}
120+
defaultMonth={today}
121+
mode="single"
122+
onDayMouseEnter={handleDayMouseEnter}
123+
onDayMouseLeave={handleDayMouseLeave}
124+
/>
125+
);
126+
fireEvent.mouseEnter(dateButton(today));
127+
fireEvent.mouseLeave(dateButton(today));
128+
});
129+
test("should call the event handler", async () => {
130+
expect(handleDayMouseEnter).toHaveBeenCalled();
131+
expect(handleDayMouseLeave).toHaveBeenCalled();
132+
});
133+
});

src/DayPicker.tsx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ export function DayPicker(props: DayPickerProps) {
6969
onDayClick,
7070
onDayFocus,
7171
onDayKeyDown,
72+
onDayMouseEnter,
73+
onDayMouseLeave,
7274
onNextClick,
7375
onPrevClick,
7476
showWeekNumber,
@@ -209,6 +211,20 @@ export function DayPicker(props: DayPickerProps) {
209211
[moveFocus, onDayKeyDown, props.dir]
210212
);
211213

214+
const handleDayMouseEnter = useCallback(
215+
(day: CalendarDay, modifiers: Modifiers) => (e: MouseEvent) => {
216+
onDayMouseEnter?.(day.date, modifiers, e);
217+
},
218+
[onDayMouseEnter]
219+
);
220+
221+
const handleDayMouseLeave = useCallback(
222+
(day: CalendarDay, modifiers: Modifiers) => (e: MouseEvent) => {
223+
onDayMouseLeave?.(day.date, modifiers, e);
224+
},
225+
[onDayMouseLeave]
226+
);
227+
212228
const { className, style } = useMemo(
213229
() => ({
214230
className: [classNames[UI.Root], props.className]
@@ -552,6 +568,14 @@ export function DayPicker(props: DayPickerProps) {
552568
onBlur={handleDayBlur(day, modifiers)}
553569
onFocus={handleDayFocus(day, modifiers)}
554570
onKeyDown={handleDayKeyDown(day, modifiers)}
571+
onMouseEnter={handleDayMouseEnter(
572+
day,
573+
modifiers
574+
)}
575+
onMouseLeave={handleDayMouseLeave(
576+
day,
577+
modifiers
578+
)}
555579
>
556580
{formatDay(date, formatOptions, dateLib)}
557581
</components.DayButton>

src/types/props.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,10 @@ export interface PropsBase {
406406
onDayBlur?: DayEventHandler<React.FocusEvent>;
407407
/** Event handler when a key is pressed on a day. */
408408
onDayKeyDown?: DayEventHandler<React.KeyboardEvent>;
409+
/** Event handler when the mouse enters a day. */
410+
onDayMouseEnter?: DayEventHandler<React.MouseEvent>;
411+
/** Event handler when the mouse leaves a day. */
412+
onDayMouseLeave?: DayEventHandler<React.MouseEvent>;
409413

410414
/**
411415
* Replace the default date library with a custom one.

0 commit comments

Comments
 (0)