Skip to content

Add improved day validation workaround for leap year date input bug #695

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

dagjomar
Copy link

Fixes: #659

There is an issue when trying to input a date such as "29 / 02 / 2024" (February 29th 2024) which is a leap year and should be valid.

The validation runs into a state where the input fields are first rendered with the inputs
day: 29
month: 2
year: 202

which is not a leap year, and thus the day 29 is not valid for this state.

The problem occurs when running the onChangeExternal, which tries to read the validity state of the dayInput react Ref. However, that ref has last been rendered with a min/max number value calculated from the previous month and year (month 2 and year 202). Therefore the day input field still believes that the day 29 is not valid, even though we have just entered 2024 into the year field.

This workaround is to just use a new instance of a DOM input field that emulates what the real input field should do,
and check this validity value instead, which will be correct using the current updated values. The real input field values will become valid next render cycle. Ironic to make another "shadow DOM" inside react, but at least it works.

Added a bunch of unit tests for this case, and improved the test coverage of utility methods as well

Fixes: wojtekmaj#659

There is an issue when trying to input a date such as "29 / 02 / 2024"
(February 29th 2024) which is a leap year and should be valid.

The validation runs into a state where the input fields are first rendered
with the inputs
day: 29
month: 2
year: 202

which is not a leap year, and thus the day 29 is not valid for this state.

The problem occurs when running the onChangeExternal,
which tries to read the validity state of the dayInput react Ref.
However, that ref has last been rendered with a min/max number value
calculated from the previous month and year (month 2 and year 202).
Therefore the day input field still believes that the day 29 is not valid,
even though we have just entered 2024 into the year field.

This workaround is to just use a new instance of a DOM input field that
emulates what the real input field should do,
and check this validity value instead, which will be correct using the current updated values.
The real input field values will become valid next render cycle.
Ironic to make another "shadow DOM" inside react, but at least it works.

Added a bunch of unit tests for this case, and improved the
test coverage of utility methods as well
@dagjomar dagjomar mentioned this pull request Apr 25, 2025
2 tasks
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.

issue with leap year while typing
1 participant