Skip to content

fix: complete safeToDate adoption in Calendar.renderInputTime#6285

Open
GiorgiM000 wants to merge 1 commit into
Hacker0x01:mainfrom
GiorgiM000:fix/safeToDate-calendar-sibling-sweep
Open

fix: complete safeToDate adoption in Calendar.renderInputTime#6285
GiorgiM000 wants to merge 1 commit into
Hacker0x01:mainfrom
GiorgiM000:fix/safeToDate-calendar-sibling-sweep

Conversation

@GiorgiM000
Copy link
Copy Markdown

Summary

Completes the safeToDate() sibling-sweep started in 149d547 ("fix: prevent crash when date props are passed as strings") by adopting the helper in the three remaining raw-new Date() callsites in Calendar.renderInputTime.

Context

safeToDate(date: Date | null | undefined): Date | null was introduced to guard against runtime crashes from consumers that pass non-Date values (strings, numbers, plain objects) to props typed as Date. After 149d547 the codebase has 7 callsites that adopted the helper:

  • src/index.tsx:809, :811, :1258, :1320
  • src/time.tsx:144, :224, :225

Three sibling callsites in src/calendar.tsx were missed:

  • :1193startDate in selectsRange mode
  • :1200endDate in selectsRange mode
  • :1233selected in single-date mode

This PR closes that gap.

Behavior change

Consumer usage Before After
<DatePicker selected={new Date()} /> (typed Date) renders renders (unchanged)
<DatePicker selected={undefined} /> does not render does not render (unchanged)
<DatePicker selected={"2025-01-01" as any} /> (TS violation) renders time input with parsed Date does not render (consistent with the rest of the codebase)
<DatePicker selected={"garbage" as any} /> does not render (existing isValid guard catches Invalid Date) does not render (unchanged)

The behavior change only affects consumers who already bypass the TypeScript type contract by passing non-Date values. The new behavior aligns Calendar.renderInputTime with the documented safeToDate policy from 149d547.

Diff

 src/calendar.tsx | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
  • Imports safeToDate from ./date_utils alongside the existing isValid import.
  • Replaces three propValue ? new Date(propValue) : undefined constructions with safeToDate(propValue) ?? undefined.
  • The downstream startTime && isValid(startTime) && Boolean(propValue) checks are kept as-is (defensive, harmless redundancy with the new guard).

Test plan

  • yarn type-check — clean
  • yarn eslint — clean
  • yarn jest src/test/timepicker_test.test.tsx src/test/show_time_test.test.tsx — passing (existing renderInputTime coverage in these suites exercises both branches)
  • yarn jest --ci full suite — same 8 pre-existing failures as on main (week_picker_test, datepicker_test); zero regressions introduced by this change. Verified by running the same tests against main with this branch stashed.

Commit 149d547 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant