Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Calendar): resolve invalid date error in validateDate function #7582

Merged
merged 2 commits into from
Jan 8, 2025

Conversation

KumJungMin
Copy link
Contributor

@KumJungMin KumJungMin commented Jan 8, 2025

Defect Fixes



How To Resolve

Situation:

  • When setting minDate data in yearNavigator mode, an error occurs that causes the selected date to be cleared (reset).

Problem Cause:

  • minYear is originally intended to be used as a year (number), but under certain conditions, the props.minDate (Date type) is assigned directly.
  • Specifically, the issue arises in the following code:
function validateDate = (value) => {
  const minYear = props.minDate && minRangeYear != null 
      ? Math.max(props.minDate.getFullYear(), minRangeYear) 
      : props.minDate  /* (1) props.minDate (Date object) is assigned to minYear */
        || minRangeYear;

    ...

    if (minYear && minYear > viewYear) {    /* (2) this condition is true */
        viewYear = minYear;                 /* (3) viewYear is set to minYear (Date object). */
    }

    ...

   /* (4) setFullYear is called with a Date object, resulting in 'Invalid Date' error */
   value.setFullYear(viewYear);            
}

Proposed Solution

  • By using getFullYear(), we extract only the numerical year from props.minDate and props.maxDate.
function validateDate = (value) => {
  ...
  if (minRangeYear !== null) {
      minYear = props.minDate ? Math.max(props.minDate.getFullYear(), minRangeYear) : minRangeYear;
  } else {
      minYear = props.minDate?.getFullYear() || minRangeYear; /* use getFullYear() */
  }
}

Test

before: After selecting a date and clicking the input, the selected date is cleared.

2025-01-08.9.50.41.mov
<Calendar value={date} yearNavigator onChange={(e) => setDate(e.value)} minDate={minDate} />

after: After selecting a date and clicking the input, the selected date remains intact.

2025-01-08.9.50.03.mov
<Calendar value={date} yearNavigator onChange={(e) => setDate(e.value)} minDate={minDate} />

Copy link

vercel bot commented Jan 8, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

2 Skipped Deployments
Name Status Preview Updated (UTC)
primereact ⬜️ Ignored (Inspect) Visit Preview Jan 8, 2025 0:55am
primereact-v9 ⬜️ Ignored (Inspect) Visit Preview Jan 8, 2025 0:55am

@melloware melloware merged commit a84236f into primefaces:master Jan 8, 2025
6 checks passed
@KumJungMin KumJungMin deleted the fix/issue-7422 branch January 8, 2025 13:01
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.

Calendar: Using "minDate" with "yearNavigator" removes previous selected date on selection
2 participants