From 2b520834dc748e0360e4f557772be88a7114a80b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mose=20M=C3=BCller?= Date: Fri, 17 Jan 2025 15:42:33 +0100 Subject: [PATCH] fix: overwrites left and right arrow key behaviour in NumberComponent The cursor position was not stored when moving the cursor without changing the number. --- frontend/src/components/NumberComponent.tsx | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/frontend/src/components/NumberComponent.tsx b/frontend/src/components/NumberComponent.tsx index 2a64206..8dbdfaa 100644 --- a/frontend/src/components/NumberComponent.tsx +++ b/frontend/src/components/NumberComponent.tsx @@ -199,16 +199,8 @@ export const NumberComponent = React.memo((props: NumberComponentProps) => { const handleKeyDown = (event: React.KeyboardEvent) => { const { key, target } = event; - // Typecast const inputTarget = target as HTMLInputElement; - if ( - key === "F1" || - key === "F5" || - key === "F12" || - key === "Tab" || - key === "ArrowRight" || - key === "ArrowLeft" - ) { + if (key === "F1" || key === "F5" || key === "F12" || key === "Tab") { return; } event.preventDefault(); @@ -223,6 +215,11 @@ export const NumberComponent = React.memo((props: NumberComponentProps) => { // Select everything when pressing Ctrl + a inputTarget.setSelectionRange(0, value.length); return; + } else if (key === "ArrowRight" || key === "ArrowLeft") { + // Move the cursor with the arrow keys and store its position + selectionStart = key === "ArrowRight" ? selectionStart + 1 : selectionStart - 1; + setCursorPosition(selectionStart); + return; } else if ((key >= "0" && key <= "9") || key === "-") { // Check if a number key or a decimal point key is pressed ({ value: newValue, selectionStart } = handleNumericKey(