diff --git a/src/components/TimePicker/TimePicker.tsx b/src/components/TimePicker/TimePicker.tsx index 2c3aff898dbe..50bf066bdb66 100644 --- a/src/components/TimePicker/TimePicker.tsx +++ b/src/components/TimePicker/TimePicker.tsx @@ -261,17 +261,44 @@ function TimePicker( // There is an active selection of the second digit newHour = trimmedText.substring(0, 2).padEnd(2, '0'); newSelection = trimmedText.length === 1 ? 1 : 2; - } else if (trimmedText.length === 1 && Number(trimmedText) <= 1) { - /* - The trimmed text is either 0 or 1. - We are either replacing hours with a single digit, or removing the last digit. - In both cases, we should append 0 to the remaining value. - Note: we must check the length of the filtered text to avoid incorrectly handling e.g. "01" as "1". - */ - newHour = `${trimmedText}0`; - newSelection = 1; + } else if (trimmedText.length === 1 && /^\d$/.test(trimmedText)) { + // Handles any single digit '0'-'9' + const digit = trimmedText[0]; + if (digit === '0') { + newHour = '00'; + newSelection = 1; + } else if (digit === '1') { + newHour = '01'; + // Check if it was a full replacement of a 2-digit hour field + if (selectionHour.start === 0 && typeof selectionHour.end === 'number' && selectionHour.end >= 2 && hours.length >= 2) { + newSelection = 2; + } else { + newSelection = 1; + } + } else { + // Digit is '2' through '9' + newHour = `0${digit}`; + newSelection = 2; + } } else { - newHour = trimmedText.substring(0, 2).padStart(2, '0'); + // Handle empty input or multiple digits + if (trimmedText.length === 0) { + newHour = '00'; + newSelection = 0; + return; + } + + const candidate = trimmedText.substring(0, 2); + if (/^\d\d$/.test(candidate)) { + // e.g. "05", "12" + newHour = candidate; + } else if (/^\d$/.test(candidate)) { + // e.g. "5" became candidate (should be rare here) + newHour = `0${candidate}`; + } else { + // Invalid input like "aa" + newHour = hours; // Revert to previous valid hours + } newSelection = 2; }