From 16a115348ca28416b9900b3d6ec0b9dd9530618d Mon Sep 17 00:00:00 2001 From: Sven van de Scheur Date: Fri, 11 Oct 2024 09:59:39 +0200 Subject: [PATCH] :bug: - fix: attempt to fix some timing issues --- src/components/form/dateinput/dateinput.tsx | 46 ++++++--------------- 1 file changed, 12 insertions(+), 34 deletions(-) diff --git a/src/components/form/dateinput/dateinput.tsx b/src/components/form/dateinput/dateinput.tsx index bc761f4f..2aa5a2b5 100644 --- a/src/components/form/dateinput/dateinput.tsx +++ b/src/components/form/dateinput/dateinput.tsx @@ -209,41 +209,17 @@ export const DateInput: React.FC = ({ * @param section */ const focusSection = useCallback( - (direction: "forwards" | "backwards") => { + (target: HTMLInputElement | null) => { const fn = () => { - const active = document.activeElement as HTMLElement; - const activeSection = active.dataset?.section; - const nextSection = - format === "DDMMYYYY" - ? activeSection === "DD" - ? "MM" - : "YY" - : activeSection === "YY" - ? "MM" - : "DD"; - const previousSection = - format === "DDMMYYYY" - ? activeSection === "YY" - ? "MM" - : "DD" - : activeSection === "DD" - ? "MM" - : "YY"; - - const section = - direction === "forwards" ? nextSection : previousSection; - - const input = fakeInputRef.current; - if (!input) return; - - const parent = input.parentElement; - parent - ?.querySelector(`[data-section="${section}"]`) - ?.focus(); + if (!target) { + return; + } + target?.focus(); + target?.select(); }; clearTimeout(debounceRef.current); - debounceRef.current = setTimeout(fn, 60); + debounceRef.current = setTimeout(fn, 30); }, [debounceRef.current, fakeInputRef.current], ); @@ -349,10 +325,11 @@ export const DateInput: React.FC = ({ section === "YY" ? value.length === 4 : value.length === 2; const isCleared = !value && event.key === "Backspace"; + const input = event.target as HTMLInputElement; if (isCompleted) { - focusSection("forwards"); + focusSection(input.nextElementSibling); } else if (isCleared) { - focusSection("backwards"); + focusSection(input.previousElementSibling); } }, [focusSection], @@ -373,7 +350,8 @@ export const DateInput: React.FC = ({ form, pattern: "[0-9]*", onChange: handleChange, - onFocus: (e: React.FocusEvent) => e.target.select(), + onFocus: (e: React.FocusEvent) => + setTimeout(() => e.target.select()), onKeyDown: onKeyDown, onKeyUp: onKeyUp, size,