@@ -209,41 +209,17 @@ export const DateInput: React.FC<DateInputProps> = ({
209
209
* @param section
210
210
*/
211
211
const focusSection = useCallback (
212
- ( direction : "forwards" | "backwards" ) => {
212
+ ( target : HTMLInputElement | null ) => {
213
213
const fn = ( ) => {
214
- const active = document . activeElement as HTMLElement ;
215
- const activeSection = active . dataset ?. section ;
216
- const nextSection =
217
- format === "DDMMYYYY"
218
- ? activeSection === "DD"
219
- ? "MM"
220
- : "YY"
221
- : activeSection === "YY"
222
- ? "MM"
223
- : "DD" ;
224
- const previousSection =
225
- format === "DDMMYYYY"
226
- ? activeSection === "YY"
227
- ? "MM"
228
- : "DD"
229
- : activeSection === "DD"
230
- ? "MM"
231
- : "YY" ;
232
-
233
- const section =
234
- direction === "forwards" ? nextSection : previousSection ;
235
-
236
- const input = fakeInputRef . current ;
237
- if ( ! input ) return ;
238
-
239
- const parent = input . parentElement ;
240
- parent
241
- ?. querySelector < HTMLInputElement > ( `[data-section="${ section } "]` )
242
- ?. focus ( ) ;
214
+ if ( ! target ) {
215
+ return ;
216
+ }
217
+ target ?. focus ( ) ;
218
+ target ?. select ( ) ;
243
219
} ;
244
220
245
221
clearTimeout ( debounceRef . current ) ;
246
- debounceRef . current = setTimeout ( fn , 60 ) ;
222
+ debounceRef . current = setTimeout ( fn , 30 ) ;
247
223
} ,
248
224
[ debounceRef . current , fakeInputRef . current ] ,
249
225
) ;
@@ -349,10 +325,11 @@ export const DateInput: React.FC<DateInputProps> = ({
349
325
section === "YY" ? value . length === 4 : value . length === 2 ;
350
326
const isCleared = ! value && event . key === "Backspace" ;
351
327
328
+ const input = event . target as HTMLInputElement ;
352
329
if ( isCompleted ) {
353
- focusSection ( "forwards" ) ;
330
+ focusSection ( input . nextElementSibling as HTMLInputElement | null ) ;
354
331
} else if ( isCleared ) {
355
- focusSection ( "backwards" ) ;
332
+ focusSection ( input . previousElementSibling as HTMLInputElement | null ) ;
356
333
}
357
334
} ,
358
335
[ focusSection ] ,
@@ -373,7 +350,8 @@ export const DateInput: React.FC<DateInputProps> = ({
373
350
form,
374
351
pattern : "[0-9]*" ,
375
352
onChange : handleChange ,
376
- onFocus : ( e : React . FocusEvent < HTMLInputElement > ) => e . target . select ( ) ,
353
+ onFocus : ( e : React . FocusEvent < HTMLInputElement > ) =>
354
+ setTimeout ( ( ) => e . target . select ( ) ) ,
377
355
onKeyDown : onKeyDown ,
378
356
onKeyUp : onKeyUp ,
379
357
size,
0 commit comments