-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
DateTimePicker
: set PM hours correctly
#36878
Conversation
Size Change: +44 B (0%) Total Size: 1.1 MB
ℹ️ View Unchanged
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for picking up this issue @ciampo! Unfortunately this introduces a regression, but it looks like it shouldn't be too hard to fix if we move some of the logic in update
to a separate updateHours
function. I've added some notes.
It might be nice to add an extra unit test to cover the AM/PM issue, too. We've already got a should switch to AM correctly
test, but it looks like it only covers clicking the AM
button, rather than the issue in #29720.
Thanks again, and happy to do more testing next week!
I can't reproduce the problem 😄 . Can you share a bit more testing instructions? |
Sure! Basically, when the component is using the 12 hour format and the period is set to datetimepicker-bug.mp4#29720 should also include some reproducible tests case |
Thanks! I was changing the hour and selecting |
Yes, I believe that's because the inputs in this component "commit" their new values on blur |
ea80f88
to
3532e49
Compare
Thank you for finding the time to review this and discover the regression (which was confirmed by some failing e2e/unit tests too 😅 ). That should be addressed now (more details under your inline comment).
Definitely — I've added a new unit test to the suite. I also think that this fix highlighted a bug in another unit test, which I corrected in this PR too — I'll add more details in an inline comment below |
expect( onChangeSpy ).toHaveBeenCalledWith( '2018-12-22T00:00:00' ); | ||
onChangeSpy.mockClear(); | ||
|
||
fireEvent.change( minutesInput, { target: { value: '35' } } ); | ||
fireEvent.blur( minutesInput ); | ||
|
||
expect( onChangeSpy ).toHaveBeenCalledWith( '2018-12-22T12:35:00' ); | ||
expect( onChangeSpy ).toHaveBeenCalledWith( '2018-12-22T00:35:00' ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The fix introduced in this PR caused this unit test to fail. After looking more into it, I believe that the unit test was previously wrong:
- the time picker in the test is using the 12 hours period
- the initial time set to the picker is
1986-10-18T11:00:00
, and therefore the picker automatically picksAM
as the period - the period is not changed explicitly throughout the test
- the
hours
input's value is changed to12
(remember, the period is still set toam
) - the test currently expected
12am
to be converted to midday - the test was passing until now because of the lack of value adjustment prior to this PR
I believe that it is correct to update the unit test in this case, since 12am
represents midnight and therefore should be converted to 00
in the 24h format
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think so too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, +1 from me!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @ciampo ! LGTM 💯
expect( onChangeSpy ).toHaveBeenCalledWith( '2018-12-22T00:00:00' ); | ||
onChangeSpy.mockClear(); | ||
|
||
fireEvent.change( minutesInput, { target: { value: '35' } } ); | ||
fireEvent.blur( minutesInput ); | ||
|
||
expect( onChangeSpy ).toHaveBeenCalledWith( '2018-12-22T12:35:00' ); | ||
expect( onChangeSpy ).toHaveBeenCalledWith( '2018-12-22T00:35:00' ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think so too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is testing well for me, too. Thanks for fixing it up and adding in the extra test @ciampo!
* DateTimePicker: Extract `from12hTo24h` function * DateTimePicker: correctly convert hours from 12 to 24 format when updating the time * CHANGELOG * Only apply the value adjustment when the hours input updates * Add unit test to check setting hours with PM period selected * Update unit test on the assumption that 12am should be intended as midnight
I've added the backport to WP beta/RC label in case it's not too late (cc @noisysocks ) |
I backported this on 29 November. You can tell if a PR was backported by checking if there's a commit in |
That's great! And thank you for the clarification 🙇 |
Description
Fixes #29720
After some tweaking, the issue seems related to a bug in the
update
function. This function doesn't seem to take the period (AM
/PM
) into account, and therefore it sets the hours for the new datetime always as a number between 1 and 12 (while the new datetime expects a 24h format).This PR uses some existing logic for the "12h to 24h format" conversion also in the
update
functionA couple of notes:
How has this been tested?
Without this PR's code applied, setting an time with the
PM
period results in theDateTimePicker
forcing it back to theAM
period. With this PR, setting a time in thePM
period should work as expected.Can be verified in:
Screenshots
Before this PR: (when the component is using the 12 hour format and the period is set to PM, changing the hours always causes the period to automatically switch to AM)
datetimepicker-bug.mp4
With the fix from this PR:
datetimepicker-am-pm.mp4
Types of changes
Fix
Checklist:
*.native.js
files for terms that need renaming or removal).