From 40ee7d0b80a85289939620a321a308f36412ce49 Mon Sep 17 00:00:00 2001 From: Zakharchenko Aleksander Date: Thu, 17 Apr 2025 16:29:01 +0100 Subject: [PATCH 1/2] parse date range with dashes --- src/date_utils.ts | 7 ++++++- src/index.tsx | 3 ++- src/test/datepicker_test.test.tsx | 29 +++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/date_utils.ts b/src/date_utils.ts index 204884692e..9fc7186f4f 100644 --- a/src/date_utils.ts +++ b/src/date_utils.ts @@ -223,6 +223,11 @@ export function safeDateFormat( return (date && formatDate(date, formatStr, locale)) || ""; } +/** + * Used as a delimiter to separate two dates when formatting a date range + */ +export const DATE_RANGE_SEPARATOR = ' - ' + /** * Safely formats a date range. * @@ -243,7 +248,7 @@ export function safeDateRangeFormat( const formattedStartDate = safeDateFormat(startDate, props); const formattedEndDate = endDate ? safeDateFormat(endDate, props) : ""; - return `${formattedStartDate} - ${formattedEndDate}`; + return `${formattedStartDate}${DATE_RANGE_SEPARATOR}${formattedEndDate}`; } /** diff --git a/src/index.tsx b/src/index.tsx index f91e869ce9..eafa376584 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -49,6 +49,7 @@ import { type HighlightDate, type HolidayItem, KeyType, + DATE_RANGE_SEPARATOR, } from "./date_utils"; import PopperComponent from "./popper_component"; import Portal from "./portal"; @@ -608,7 +609,7 @@ export default class DatePicker extends Component< if (selectsRange) { const [valueStart, valueEnd] = value - .split("-", 2) + .split(dateFormat.includes("-") ? DATE_RANGE_SEPARATOR : "-", 2) .map((val) => val.trim()); const startDateNew = parseDate( valueStart ?? "", diff --git a/src/test/datepicker_test.test.tsx b/src/test/datepicker_test.test.tsx index 54d5a0c5f7..b4c64021d5 100644 --- a/src/test/datepicker_test.test.tsx +++ b/src/test/datepicker_test.test.tsx @@ -3443,6 +3443,35 @@ describe("DatePicker", () => { ); }); + it("should parses date range with dashes correctly", () => { + const onChangeSpy = jest.fn(); + const dateFormat = "yyyy-MM-dd"; + + const { container } = render( + , + ); + + const input = safeQuerySelector(container, "input"); + fireEvent.change(input, { + target: { + value: "2024-03-04 - 2024-05-06", + }, + }); + expect(onChangeSpy).toHaveBeenCalledTimes(1); + expect(Array.isArray(onChangeSpy.mock.calls[0][0])).toBe(true); + const [startDate, endDate] = onChangeSpy.mock.calls[0][0]; + expect(startDate).toBeTruthy(); + expect(endDate).toBeTruthy(); + expect(formatDate(startDate, dateFormat)).toBe("2024-03-04"); + expect(formatDate(endDate, dateFormat)).toBe("2024-05-06"); + }); + it("should not fire onChange a second time if user edits text box without the parsing result changing", () => { const onChangeSpy = jest.fn(); let instance: DatePicker | null = null; From b9b25e61125be5467e54843ac1ca4a820c8063cb Mon Sep 17 00:00:00 2001 From: Zakharchenko Aleksander Date: Thu, 17 Apr 2025 16:35:56 +0100 Subject: [PATCH 2/2] prettier formatting --- src/date_utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/date_utils.ts b/src/date_utils.ts index 9fc7186f4f..01a12fc05a 100644 --- a/src/date_utils.ts +++ b/src/date_utils.ts @@ -226,7 +226,7 @@ export function safeDateFormat( /** * Used as a delimiter to separate two dates when formatting a date range */ -export const DATE_RANGE_SEPARATOR = ' - ' +export const DATE_RANGE_SEPARATOR = " - "; /** * Safely formats a date range.