Skip to content

fix: prevent crash when date props are passed as strings#6166

Merged
martijnrusschen merged 1 commit intomainfrom
fix/string-date-props-crash
Dec 18, 2025
Merged

fix: prevent crash when date props are passed as strings#6166
martijnrusschen merged 1 commit intomainfrom
fix/string-date-props-crash

Conversation

@martijnrusschen
Copy link
Copy Markdown
Member

Summary

  • Added safeToDate() helper function in date_utils.ts that validates date values at runtime
  • Returns null for invalid inputs (strings, numbers, invalid Date objects), allowing graceful fallback
  • Fixes crash with "getTime/getFullYear is not a function" when date props are passed as strings

Fixed Locations

  • time.tsx: isSelectedTime(), renderTimes() - validate selected/openToDate props
  • index.tsx: handleTimeOnlyArrowKey(), handleTimeOnlyInputKeyDown() - validate selected prop
  • index.tsx: handleInputChange() - validate startDate/endDate before calling .getTime()

Root Cause

TypeScript types selected, startDate, endDate, etc. as Date | null, but at runtime JavaScript consumers or incorrect usage could pass strings. The code assumed these were always valid Date objects and called methods like .getTime() directly, causing crashes.

Solution

The safeToDate() function checks:

  1. If the value is null or undefined → returns null
  2. If the value is a valid Date object (using isDate() and isValid() from date-fns) → returns the date
  3. Otherwise → returns null

This allows the existing fallback chains (e.g., selected || openToDate || newDate()) to work correctly.

Test Plan

  • Added 11 unit tests for safeToDate() function covering valid dates, null, undefined, strings, numbers, invalid Date objects
  • Added 5 integration tests for TimePicker with string date props
  • All 1464 tests pass
  • Linting passes

Fixes #5964

🤖 Generated with Claude Code

Added safeToDate() helper function that validates date values at runtime,
returning null for invalid inputs (strings, numbers, invalid Date objects).
This allows graceful fallback instead of crashing with "getTime/getFullYear
is not a function" errors.

Fixed locations:
- time.tsx: isSelectedTime(), renderTimes() - validate selected/openToDate
- index.tsx: handleTimeOnlyArrowKey(), handleTimeOnlyInputKeyDown() - validate selected
- index.tsx: handleInputChange() - validate startDate/endDate before .getTime()

Added tests:
- 11 unit tests for safeToDate() function
- 5 integration tests for string date prop handling in TimePicker

Fixes #5964

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@codecov
Copy link
Copy Markdown

codecov Bot commented Dec 18, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.26%. Comparing base (be38873) to head (149d547).
⚠️ Report is 12 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #6166   +/-   ##
=======================================
  Coverage   99.26%   99.26%           
=======================================
  Files          30       30           
  Lines        3792     3801    +9     
  Branches     1631     1635    +4     
=======================================
+ Hits         3764     3773    +9     
  Misses         27       27           
  Partials        1        1           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@martijnrusschen martijnrusschen merged commit 9e4795d into main Dec 18, 2025
6 checks passed
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.

Component crashes on some actions when selected={dateString}

1 participant