Skip to content

Commit 6c54f20

Browse files
🐛 - fix: attempt to fix some timing issues
1 parent 8cc6372 commit 6c54f20

File tree

1 file changed

+12
-34
lines changed

1 file changed

+12
-34
lines changed

Diff for: src/components/form/dateinput/dateinput.tsx

+12-34
Original file line numberDiff line numberDiff line change
@@ -209,41 +209,17 @@ export const DateInput: React.FC<DateInputProps> = ({
209209
* @param section
210210
*/
211211
const focusSection = useCallback(
212-
(direction: "forwards" | "backwards") => {
212+
(target: HTMLInputElement | null) => {
213213
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();
243219
};
244220

245221
clearTimeout(debounceRef.current);
246-
debounceRef.current = setTimeout(fn, 60);
222+
debounceRef.current = setTimeout(fn, 30);
247223
},
248224
[debounceRef.current, fakeInputRef.current],
249225
);
@@ -349,10 +325,11 @@ export const DateInput: React.FC<DateInputProps> = ({
349325
section === "YY" ? value.length === 4 : value.length === 2;
350326
const isCleared = !value && event.key === "Backspace";
351327

328+
const input = event.target as HTMLInputElement;
352329
if (isCompleted) {
353-
focusSection("forwards");
330+
focusSection(input.nextElementSibling as HTMLInputElement | null);
354331
} else if (isCleared) {
355-
focusSection("backwards");
332+
focusSection(input.previousElementSibling as HTMLInputElement | null);
356333
}
357334
},
358335
[focusSection],
@@ -373,7 +350,8 @@ export const DateInput: React.FC<DateInputProps> = ({
373350
form,
374351
pattern: "[0-9]*",
375352
onChange: handleChange,
376-
onFocus: (e: React.FocusEvent<HTMLInputElement>) => e.target.select(),
353+
onFocus: (e: React.FocusEvent<HTMLInputElement>) =>
354+
setTimeout(() => e.target.select()),
377355
onKeyDown: onKeyDown,
378356
onKeyUp: onKeyUp,
379357
size,

0 commit comments

Comments
 (0)