@@ -261,17 +261,44 @@ function TimePicker(
261261 // There is an active selection of the second digit
262262 newHour = trimmedText . substring ( 0 , 2 ) . padEnd ( 2 , '0' ) ;
263263 newSelection = trimmedText . length === 1 ? 1 : 2 ;
264- } else if ( trimmedText . length === 1 && Number ( trimmedText ) <= 1 ) {
265- /*
266- The trimmed text is either 0 or 1.
267- We are either replacing hours with a single digit, or removing the last digit.
268- In both cases, we should append 0 to the remaining value.
269- Note: we must check the length of the filtered text to avoid incorrectly handling e.g. "01" as "1".
270- */
271- newHour = `${ trimmedText } 0` ;
272- newSelection = 1 ;
264+ } else if ( trimmedText . length === 1 && / ^ \d $ / . test ( trimmedText ) ) {
265+ // Handles any single digit '0'-'9'
266+ const digit = trimmedText [ 0 ] ;
267+ if ( digit === '0' ) {
268+ newHour = '00' ;
269+ newSelection = 1 ;
270+ } else if ( digit === '1' ) {
271+ newHour = '01' ;
272+ // Check if it was a full replacement of a 2-digit hour field
273+ if ( selectionHour . start === 0 && typeof selectionHour . end === 'number' && selectionHour . end >= 2 && hours . length >= 2 ) {
274+ newSelection = 2 ;
275+ } else {
276+ newSelection = 1 ;
277+ }
278+ } else {
279+ // Digit is '2' through '9'
280+ newHour = `0${ digit } ` ;
281+ newSelection = 2 ;
282+ }
273283 } else {
274- newHour = trimmedText . substring ( 0 , 2 ) . padStart ( 2 , '0' ) ;
284+ // Handle empty input or multiple digits
285+ if ( trimmedText . length === 0 ) {
286+ newHour = '00' ;
287+ newSelection = 0 ;
288+ return ;
289+ }
290+
291+ const candidate = trimmedText . substring ( 0 , 2 ) ;
292+ if ( / ^ \d \d $ / . test ( candidate ) ) {
293+ // e.g. "05", "12"
294+ newHour = candidate ;
295+ } else if ( / ^ \d $ / . test ( candidate ) ) {
296+ // e.g. "5" became candidate (should be rare here)
297+ newHour = `0${ candidate } ` ;
298+ } else {
299+ // Invalid input like "aa"
300+ newHour = hours ; // Revert to previous valid hours
301+ }
275302 newSelection = 2 ;
276303 }
277304
0 commit comments