Skip to content

Commit 69da2fe

Browse files
Merge pull request #6107 from Hacker0x01/fix/issue-5718-dates-before-1800
fix: allow manually typing dates before 1800 when minDate permits
2 parents c6bc68f + 0593c0d commit 69da2fe

File tree

3 files changed

+61
-14
lines changed

3 files changed

+61
-14
lines changed

src/date_utils.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -155,17 +155,12 @@ export function parseDate(
155155
export { isDate, set };
156156

157157
/**
158-
* Checks if a given date is valid and not before the minimum date.
158+
* Checks if a given date is a valid Date object.
159159
* @param date - The date to be checked.
160-
* @param minDate - The minimum date allowed. If not provided, defaults to "1/1/1800".
161-
* @returns A boolean value indicating whether the date is valid and not before the minimum date.
160+
* @returns A boolean value indicating whether the date is valid.
162161
*/
163-
export function isValid(date: Date, minDate?: Date): boolean {
164-
/* the fallback date is essential to not break test case
165-
* `should auto update calendar when the updated date text is after props.minDate`
166-
* and backward compatibility respectfully
167-
*/
168-
return isValidDate(date) && !isBefore(date, minDate ?? new Date("1/1/1800"));
162+
export function isValid(date: Date): boolean {
163+
return isValidDate(date);
169164
}
170165

171166
// ** Date Formatting **

src/test/date_utils_test.test.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -621,13 +621,15 @@ describe("date_utils", () => {
621621
});
622622

623623
describe("isValid", () => {
624-
it("should return true if date is valid and equal or after minDate", () => {
625-
expect(isValid(newDate("2021-11-15"), newDate("2021-11-15"))).toBe(true);
626-
expect(isValid(newDate("2021-11-30"), newDate("2021-11-15"))).toBe(true);
624+
it("should return true for valid dates", () => {
625+
expect(isValid(newDate("2021-11-15"))).toBe(true);
626+
expect(isValid(newDate("1350-03-20"))).toBe(true);
627+
expect(isValid(newDate("1000-01-01"))).toBe(true);
627628
});
628629

629-
it("should return false if date is valid and before minDate", () => {
630-
expect(isValid(newDate("2021-11-01"), newDate("2021-11-15"))).toBe(false);
630+
it("should return false for invalid dates", () => {
631+
expect(isValid(new Date("invalid"))).toBe(false);
632+
expect(isValid(new Date(NaN))).toBe(false);
631633
});
632634
});
633635

src/test/datepicker_test.test.tsx

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1998,6 +1998,56 @@ describe("DatePicker", () => {
19981998
});
19991999
});
20002000

2001+
describe("when minDate is before 1800", () => {
2002+
it("should allow manually typing dates before 1800 when minDate allows it", () => {
2003+
const onChange = jest.fn();
2004+
const { container } = render(
2005+
<DatePicker
2006+
selected={new Date("1500-06-15")}
2007+
minDate={new Date("1000-01-01")}
2008+
dateFormat="yyyy-MM-dd"
2009+
onChange={onChange}
2010+
open
2011+
/>,
2012+
);
2013+
2014+
const input = safeQuerySelector<HTMLInputElement>(container, "input");
2015+
fireEvent.change(input, {
2016+
target: {
2017+
value: "1350-03-20",
2018+
},
2019+
});
2020+
2021+
expect(onChange).toHaveBeenCalled();
2022+
const calledWith = onChange.mock.calls[0][0];
2023+
expect(calledWith.getFullYear()).toBe(1350);
2024+
expect(calledWith.getMonth()).toBe(2); // March is 0-indexed as 2
2025+
expect(calledWith.getDate()).toBe(20);
2026+
});
2027+
2028+
it("should update calendar view when typing a date before 1800 with appropriate minDate", () => {
2029+
const { container } = render(
2030+
<DatePicker
2031+
selected={new Date("1500-06-15")}
2032+
minDate={new Date("1000-01-01")}
2033+
dateFormat="yyyy-MM-dd"
2034+
open
2035+
/>,
2036+
);
2037+
2038+
const input = safeQuerySelector<HTMLInputElement>(container, "input");
2039+
fireEvent.change(input, {
2040+
target: {
2041+
value: "1350-03-20",
2042+
},
2043+
});
2044+
2045+
expect(
2046+
container.querySelector(".react-datepicker__current-month")?.innerHTML,
2047+
).toBe("March 1350");
2048+
});
2049+
});
2050+
20012051
it("should not manual select date if before minDate", () => {
20022052
const minDate = subDays(newDate(), 1);
20032053
const data = getOnInputKeyDownStuff({

0 commit comments

Comments
 (0)