Skip to content

Commit

Permalink
feat(DateField): improve month segment behavior for invalid months st…
Browse files Browse the repository at this point in the history
…arting 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
  • Loading branch information
acabreragnz committed Jan 16, 2025
1 parent 121e305 commit 4021da8
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions packages/core/src/shared/date/useDateField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -825,7 +844,7 @@ export function useDateField(props: UseDateFieldProps) {
return
const segmentKeydownHandlers = {
day: handleDaySegmentKeydown,
month: handleMonthSegmentKeydown,
month: SegmentKeydown,
year: handleYearSegmentKeydown,
hour: handleHourSegmentKeydown,
minute: handleMinuteSegmentKeydown,
Expand Down

0 comments on commit 4021da8

Please sign in to comment.