Skip to content

Commit 40ee7d0

Browse files
committed
parse date range with dashes
1 parent cc30861 commit 40ee7d0

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

src/date_utils.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,11 @@ export function safeDateFormat(
223223
return (date && formatDate(date, formatStr, locale)) || "";
224224
}
225225

226+
/**
227+
* Used as a delimiter to separate two dates when formatting a date range
228+
*/
229+
export const DATE_RANGE_SEPARATOR = ' - '
230+
226231
/**
227232
* Safely formats a date range.
228233
*
@@ -243,7 +248,7 @@ export function safeDateRangeFormat(
243248
const formattedStartDate = safeDateFormat(startDate, props);
244249
const formattedEndDate = endDate ? safeDateFormat(endDate, props) : "";
245250

246-
return `${formattedStartDate} - ${formattedEndDate}`;
251+
return `${formattedStartDate}${DATE_RANGE_SEPARATOR}${formattedEndDate}`;
247252
}
248253

249254
/**

src/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import {
4949
type HighlightDate,
5050
type HolidayItem,
5151
KeyType,
52+
DATE_RANGE_SEPARATOR,
5253
} from "./date_utils";
5354
import PopperComponent from "./popper_component";
5455
import Portal from "./portal";
@@ -608,7 +609,7 @@ export default class DatePicker extends Component<
608609

609610
if (selectsRange) {
610611
const [valueStart, valueEnd] = value
611-
.split("-", 2)
612+
.split(dateFormat.includes("-") ? DATE_RANGE_SEPARATOR : "-", 2)
612613
.map((val) => val.trim());
613614
const startDateNew = parseDate(
614615
valueStart ?? "",

src/test/datepicker_test.test.tsx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3443,6 +3443,35 @@ describe("DatePicker", () => {
34433443
);
34443444
});
34453445

3446+
it("should parses date range with dashes correctly", () => {
3447+
const onChangeSpy = jest.fn();
3448+
const dateFormat = "yyyy-MM-dd";
3449+
3450+
const { container } = render(
3451+
<DatePicker
3452+
selectsRange
3453+
startDate={undefined}
3454+
endDate={undefined}
3455+
onChange={onChangeSpy}
3456+
dateFormat={dateFormat}
3457+
/>,
3458+
);
3459+
3460+
const input = safeQuerySelector<HTMLInputElement>(container, "input");
3461+
fireEvent.change(input, {
3462+
target: {
3463+
value: "2024-03-04 - 2024-05-06",
3464+
},
3465+
});
3466+
expect(onChangeSpy).toHaveBeenCalledTimes(1);
3467+
expect(Array.isArray(onChangeSpy.mock.calls[0][0])).toBe(true);
3468+
const [startDate, endDate] = onChangeSpy.mock.calls[0][0];
3469+
expect(startDate).toBeTruthy();
3470+
expect(endDate).toBeTruthy();
3471+
expect(formatDate(startDate, dateFormat)).toBe("2024-03-04");
3472+
expect(formatDate(endDate, dateFormat)).toBe("2024-05-06");
3473+
});
3474+
34463475
it("should not fire onChange a second time if user edits text box without the parsing result changing", () => {
34473476
const onChangeSpy = jest.fn();
34483477
let instance: DatePicker | null = null;

0 commit comments

Comments
 (0)