Skip to content

Commit 1d77296

Browse files
authored
fix(TimeField): change focus after pressing 0 more than once on hour segment with 12 hour locales (#2581)
* fix(TimeFile): change focus after pressing 0 more than once on hour segment * test(TimeField): improve robustness of test case to verify that spamming 0 properly advances segment * fix(TestField): replace previous fix with a more localized approach
1 parent fbd9f04 commit 1d77296

2 files changed

Lines changed: 28 additions & 1 deletion

File tree

packages/core/src/TimeField/TimeField.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,31 @@ describe('timeField', async () => {
446446
expect(dayPeriod).toHaveFocus()
447447
})
448448

449+
it('takes you all the way through the segment with spamming 0', async () => {
450+
const { getByTestId, user, hour } = setup({
451+
timeFieldProps: {
452+
modelValue: zonedDateTime,
453+
granularity: 'second',
454+
},
455+
})
456+
457+
const { minute, second, dayPeriod } = getTimeSegments(getByTestId)
458+
459+
await user.click(hour)
460+
await user.keyboard('{0}')
461+
expect(hour).toHaveFocus()
462+
await user.keyboard('{0}')
463+
expect(minute).toHaveFocus()
464+
await user.keyboard('{0}')
465+
expect(minute).toHaveFocus()
466+
await user.keyboard('{0}')
467+
expect(second).toHaveFocus()
468+
await user.keyboard('{0}')
469+
expect(second).toHaveFocus()
470+
await user.keyboard('{0}')
471+
expect(dayPeriod).toHaveFocus()
472+
})
473+
449474
it('updates the hour on the modelValue if the dayPeriod is updated', async () => {
450475
const { getByTestId, user, value, rerender } = setup({
451476
timeFieldProps: {

packages/core/src/shared/date/useDateField.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,9 @@ export function useDateField(props: UseDateFieldProps) {
755755

756756
let displayPrev = prevValue
757757
if (is12Hour && prevValue !== null) {
758-
displayPrev = prevValue === 0 ? 12 : (prevValue > 12 ? prevValue - 12 : prevValue)
758+
// 12 AM/PM should be treated as 0 internally even if it doesn't match the display
759+
// otherwise repeatedly typing 0 will not advance to the next segment
760+
displayPrev = prevValue % 12 === 0 ? 0 : (prevValue > 12 ? prevValue - 12 : prevValue)
759761
}
760762

761763
const { value, moveToNext } = updateHour(max, num, displayPrev)

0 commit comments

Comments
 (0)