From c59ff42734d676dd9a7686de89ac19082c2ed2c7 Mon Sep 17 00:00:00 2001 From: Giorgi M Date: Wed, 27 May 2026 08:33:53 -0400 Subject: [PATCH] fix: apply safeToDate in calendar.tsx renderInputTime Commit 149d5470 introduced `safeToDate()` to prevent crashes when date props are passed as strings, and adopted it across `src/index.tsx` and `src/time.tsx`. The three sibling callsites in `Calendar.renderInputTime` were missed: `startDate`, `endDate`, and `selected` still flow through raw `new Date(...)` construction, which silently accepts ISO strings and other non-Date values that the rest of the codebase now rejects. For TS-correct usage (Date-typed props), behavior is unchanged -- `safeToDate(Date)` returns the same Date instance. For consumers that pass non-Date values via `as any` or untyped `JSON.parse`, `Calendar.renderInputTime` now matches the rest of the codebase: returns `null` (and the time input does not render) rather than relying on `new Date(string)`, which is locale- and runtime- dependent and inconsistent across browsers. No new tests added -- the existing `renderInputTime` tests in `src/test/timepicker_test.test.tsx` cover the valid-Date and the null/undefined branches that `safeToDate` preserves. --- src/calendar.tsx | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/calendar.tsx b/src/calendar.tsx index 4bedbdc06..660cf5260 100644 --- a/src/calendar.tsx +++ b/src/calendar.tsx @@ -37,6 +37,7 @@ import { getEffectiveMaxDate, addZero, isValid, + safeToDate, getYearsPeriod, DEFAULT_YEAR_ITEM_NUMBER, getMonthInLocale, @@ -1189,14 +1190,14 @@ export default class Calendar extends Component { if (this.props.selectsRange) { const { startDate, endDate } = this.props; - const startTime = startDate ? new Date(startDate) : undefined; + const startTime = safeToDate(startDate) ?? undefined; const startTimeValid = startTime && isValid(startTime) && Boolean(startDate); const startTimeString = startTimeValid ? `${addZero(startTime.getHours())}:${addZero(startTime.getMinutes())}` : ""; - const endTime = endDate ? new Date(endDate) : undefined; + const endTime = safeToDate(endDate) ?? undefined; const endTimeValid = endTime && isValid(endTime) && Boolean(endDate); const endTimeString = endTimeValid ? `${addZero(endTime.getHours())}:${addZero(endTime.getMinutes())}` @@ -1229,9 +1230,7 @@ export default class Calendar extends Component { } // Single date mode (original behavior) - const time = this.props.selected - ? new Date(this.props.selected) - : undefined; + const time = safeToDate(this.props.selected) ?? undefined; const timeValid = time && isValid(time) && Boolean(this.props.selected); const timeString = timeValid ? `${addZero(time.getHours())}:${addZero(time.getMinutes())}`