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

chore: use callbacks for dropdown event handlers #2602

Merged
merged 1 commit into from
Nov 25, 2024
Merged
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
52 changes: 21 additions & 31 deletions src/DayPicker.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import React, { useCallback, useMemo } from "react";
import type {
ChangeEventHandler,
MouseEvent,
FocusEvent,
KeyboardEvent
} from "react";
import type { MouseEvent, FocusEvent, KeyboardEvent, ChangeEvent } from "react";

import { UI, DayFlag, SelectionState } from "./UI.js";
import type { CalendarDay } from "./classes/CalendarDay.js";
Expand Down Expand Up @@ -227,6 +222,24 @@ export function DayPicker(props: DayPickerProps) {
[onDayMouseLeave]
);

const handleMonthChange = useCallback(
(date: Date) => (e: ChangeEvent<HTMLSelectElement>) => {
const selectedMonth = Number(e.target.value);
const month = dateLib.setMonth(dateLib.startOfMonth(date), selectedMonth);
goToMonth(month);
},
[dateLib, goToMonth]
);

const handleYearChange = useCallback(
(date: Date) => (e: ChangeEvent<HTMLSelectElement>) => {
const selectedYear = Number(e.target.value);
const month = dateLib.setYear(dateLib.startOfMonth(date), selectedYear);
goToMonth(month);
},
[dateLib, goToMonth]
);

const { className, style } = useMemo(
() => ({
className: [classNames[UI.Root], props.className]
Expand Down Expand Up @@ -284,29 +297,6 @@ export function DayPicker(props: DayPickerProps) {
/>
)}
{months.map((calendarMonth, displayIndex) => {
const handleMonthChange: ChangeEventHandler<HTMLSelectElement> = (
e
) => {
const selectedMonth = Number(
(e.target as HTMLSelectElement).value
);
const month = dateLib.setMonth(
dateLib.startOfMonth(calendarMonth.date),
selectedMonth
);
goToMonth(month);
};

const handleYearChange: ChangeEventHandler<HTMLSelectElement> = (
e
) => {
const month = dateLib.setYear(
dateLib.startOfMonth(calendarMonth.date),
Number(e.target.value)
);
goToMonth(month);
};

const dropdownMonths = getMonthOptions(
calendarMonth.date,
navStart,
Expand Down Expand Up @@ -350,7 +340,7 @@ export function DayPicker(props: DayPickerProps) {
classNames={classNames}
components={components}
disabled={Boolean(props.disableNavigation)}
onChange={handleMonthChange}
onChange={handleMonthChange(calendarMonth.date)}
options={dropdownMonths}
style={styles?.[UI.Dropdown]}
value={calendarMonth.date.getMonth()}
Expand All @@ -371,7 +361,7 @@ export function DayPicker(props: DayPickerProps) {
classNames={classNames}
components={components}
disabled={Boolean(props.disableNavigation)}
onChange={handleYearChange}
onChange={handleYearChange(calendarMonth.date)}
options={dropdownYears}
style={styles?.[UI.Dropdown]}
value={calendarMonth.date.getFullYear()}
Expand Down