From b29a01176a8de19bd3c39491422dfec8862897be Mon Sep 17 00:00:00 2001 From: Toni Cabrera Date: Thu, 16 Jan 2025 16:26:25 -0300 Subject: [PATCH] feat(DateField): improve month segment behavior for invalid months starting with 1 When typing invalid months starting with 1 (13-19): - Keeps 1 as the month value - Automatically uses the second digit as the initial value for the day segment For example, when typing "13": - "1" remains as the month - Moves to day segment - "3" becomes the initial value of the day --- packages/core/src/shared/date/useDateField.ts | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/packages/core/src/shared/date/useDateField.ts b/packages/core/src/shared/date/useDateField.ts index 70891c0a4..1952162a2 100644 --- a/packages/core/src/shared/date/useDateField.ts +++ b/packages/core/src/shared/date/useDateField.ts @@ -386,6 +386,21 @@ export function useDateField(props: UseDateFieldProps) { */ if (digits === 2 || total > max) { + + /** + * If we're updating months (max === 12) and user types a number + * that starts with 1 but would result in an invalid month (13-19), + * we keep the 1 as the month value and use the second digit + * as the initial value for the next segment (day) + */ + if (max === 12 && prev === 1 && total > max) { + return { + moveToNext: true, + value: 1, + nextSegmentInitialValue: num, + } + } + /** * As we're doing elsewhere, we're checking if the number is greater * than the max start digit (0-3 in most months), and if so, we're @@ -645,9 +660,13 @@ export function useDateField(props: UseDateFieldProps) { if (isNumberString(e.key)) { const num = Number.parseInt(e.key) - const { value, moveToNext } = updateDayOrMonth(12, num, prevValue) + const { value, moveToNext, nextSegmentInitialValue } = updateDayOrMonth(12, num, prevValue) props.segmentValues.value.month = value + + if (nextSegmentInitialValue) { + props.segmentValues.value.day = nextSegmentInitialValue + } if (moveToNext) props.focusNext()