Skip to content
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
4 changes: 2 additions & 2 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ export type DatePickerProps = OmitUnion<
) => void;
}
| {
selectsRange?: true;
selectsRange: true;
selectsMultiple?: false | undefined;
formatMultipleDates?: never;
onChange?: (
Expand All @@ -270,7 +270,7 @@ export type DatePickerProps = OmitUnion<
}
| {
selectsRange?: false | undefined;
selectsMultiple?: true;
selectsMultiple: true;
formatMultipleDates?: (
dates: Date[],
formatDate: (date: Date) => string,
Expand Down
6 changes: 3 additions & 3 deletions src/test/calendar_test.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1373,7 +1373,7 @@ describe("Calendar", () => {
<DatePicker
selected={newDate("2017-07-28")}
adjustDateOnChange
onChange={(d: Date | null) => {
onChange={(d) => {
date = d;
}}
/>,
Expand All @@ -1397,7 +1397,7 @@ describe("Calendar", () => {
<DatePicker
selected={newDate("2017-07-28")}
adjustDateOnChange
onChange={(d: Date | null) => {
onChange={(d) => {
date = d;
}}
/>,
Expand All @@ -1421,7 +1421,7 @@ describe("Calendar", () => {
<DatePicker
selected={newDate("2017-12-31")}
adjustDateOnChange
onChange={(d: Date | null) => {
onChange={(d) => {
date = d;
}}
/>,
Expand Down
14 changes: 7 additions & 7 deletions src/test/datepicker_test.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1150,7 +1150,7 @@ describe("DatePicker", () => {
<DatePicker
inline
selected={selected}
onChange={(d: Date | null) => {
onChange={(d) => {
date = d;
}}
/>,
Expand All @@ -1172,7 +1172,7 @@ describe("DatePicker", () => {
const { container } = render(
<DatePicker
selected={selected}
onChange={(d: Date | null) => {
onChange={(d) => {
date = d;
}}
/>,
Expand Down Expand Up @@ -4610,7 +4610,7 @@ describe("DatePicker", () => {
const { container } = render(
<DatePicker
selected={selected}
onChange={(d: Date | null) => {
onChange={(d) => {
date = d;
}}
showTimeSelect
Expand Down Expand Up @@ -4641,7 +4641,7 @@ describe("DatePicker", () => {
const { container: datepicker } = render(
<DatePicker
selected={selected}
onChange={(d: Date | null) => {
onChange={(d) => {
date = d;
}}
showTimeSelect
Expand All @@ -4667,7 +4667,7 @@ describe("DatePicker", () => {
const { container: datepicker } = render(
<DatePicker
selected={selected}
onChange={(d: Date | null) => {
onChange={(d) => {
date = d;
}}
showTimeSelectOnly
Expand All @@ -4691,7 +4691,7 @@ describe("DatePicker", () => {
const { container } = render(
<DatePicker
selected={selected}
onChange={(d: Date | null) => (date = d)}
onChange={(d) => (date = d)}
dateFormat="MM/yyyy"
minDate={newDate("2022-12-31")}
showMonthYearPicker
Expand Down Expand Up @@ -4720,7 +4720,7 @@ describe("DatePicker", () => {
const { container } = render(
<DatePicker
selected={selected}
onChange={(d: Date | null) => (date = d)}
onChange={(d) => (date = d)}
dateFormat="yyyy"
minDate={newDate("2022-12-31")}
showYearPicker
Expand Down
2 changes: 1 addition & 1 deletion src/test/timezone_test.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ describe("DatePicker with timeZone prop", () => {
const { container } = render(
<DatePicker
selected={utcDate}
onChange={(date: Date | null) => {
onChange={(date) => {
selectedDate = date;
}}
timeZone="America/New_York"
Expand Down
89 changes: 89 additions & 0 deletions src/test/type_inference_test.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/**
* Type inference tests for DatePicker onChange callback
*
* These tests verify that TypeScript can properly infer the type of the
* onChange callback parameter without explicit type annotations.
*
* Related issues:
* - #6202: onChange type breaks after updating to 9.1.0
* - #6131: selectsMultiple prop type incompatibility when spreading props
*/
import React from "react";
import { render, fireEvent } from "@testing-library/react";

import DatePicker from "../index";
import { safeQuerySelector } from "./test_utils";

describe("DatePicker onChange type inference", () => {
it("should infer Date | null for single date picker without explicit type annotation", () => {
// This test verifies fix for issue #6202
// If TypeScript cannot infer the type, this test will fail to compile
let selectedDate: Date | null = null;

const { container } = render(
<DatePicker
selected={selectedDate}
// The key test: NO explicit type annotation on `date` parameter
// TypeScript should infer `date` as `Date | null`
onChange={(date) => {
// If type inference works, this assignment should compile without error
selectedDate = date;
}}
/>,
);

const input = safeQuerySelector<HTMLInputElement>(container, "input");
fireEvent.change(input, { target: { value: "01/01/2024" } });

expect(selectedDate).not.toBeNull();
});

it("should infer [Date | null, Date | null] for range picker without explicit type annotation", () => {
let startDate: Date | null = null;
let endDate: Date | null = null;

const { container } = render(
<DatePicker
selectsRange={true}
startDate={startDate}
endDate={endDate}
// The key test: NO explicit type annotation on `dates` parameter
// TypeScript should infer `dates` as `[Date | null, Date | null]`
onChange={(dates) => {
// If type inference works, destructuring should work without error
const [start, end] = dates;
startDate = start;
endDate = end;
}}
/>,
);

const input = safeQuerySelector<HTMLInputElement>(container, "input");
fireEvent.change(input, { target: { value: "01/01/2024" } });

expect(startDate).not.toBeNull();
});

it("should infer Date[] | null for multiple picker without explicit type annotation", () => {
let selectedDates: Date[] | null = null;

const { container } = render(
<DatePicker
selectsMultiple={true}
selectedDates={selectedDates ?? []}
// The key test: NO explicit type annotation on `dates` parameter
// TypeScript should infer `dates` as `Date[] | null`
onChange={(dates) => {
// If type inference works, this assignment should compile without error
selectedDates = dates;
}}
/>,
);

const input = safeQuerySelector<HTMLInputElement>(container, "input");
fireEvent.change(input, { target: { value: "01/01/2024" } });

// Multiple picker doesn't respond to text input the same way, just verify render
expect(container.querySelector("input")).toBeTruthy();
});
});
4 changes: 2 additions & 2 deletions src/test/year_picker_test.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ describe("YearPicker", () => {
selected={newDate("2020-01-01")}
adjustDateOnChange
showYearPicker
onChange={(d: Date | null) => {
onChange={(d) => {
date = d;
}}
/>,
Expand Down Expand Up @@ -674,7 +674,7 @@ describe("YearPicker", () => {
selected={newDate("2020-01-01")}
adjustDateOnChange
showYearPicker
onChange={(d: Date | null) => {
onChange={(d) => {
date = d;
}}
/>,
Expand Down
Loading