From f1132c6b2254701901e268b8469fc025d9d25126 Mon Sep 17 00:00:00 2001 From: Aleksandr Gumroian Date: Thu, 12 Dec 2024 18:44:21 +0300 Subject: [PATCH 1/3] Enhance NumberInput component with keyboard & touch accessibility --- src/bootstrap/NumberInput.js | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/src/bootstrap/NumberInput.js b/src/bootstrap/NumberInput.js index 1a825a5..beaa22d 100644 --- a/src/bootstrap/NumberInput.js +++ b/src/bootstrap/NumberInput.js @@ -50,6 +50,20 @@ function NumberInput({ onChange, inlineText, value, ...props }) { -1 ); + function handleKeyDown(event, enableFunction) { + if (event.key === "Enter" || event.key === " ") { + event.preventDefault(); + enableFunction(true); + } + } + + function handleKeyUp(event, enableFunction) { + if (event.key === "Enter" || event.key === " ") { + event.preventDefault(); + enableFunction(false); + } + } + return ( {inlineText && ( @@ -60,7 +74,15 @@ function NumberInput({ onChange, inlineText, value, ...props }) { className="btn btn-outline-secondary" onMouseDown={() => enableIncrease(true)} onMouseUp={() => enableIncrease(false)} - aria-label="Increase" + onMouseLeave={() => enableIncrease(false)} + onTouchStart={() => enableIncrease(true)} + onTouchEnd={() => enableIncrease(false)} + onTouchCancel={() => enableIncrease(false)} + onKeyDown={(event) => handleKeyDown(event, enableIncrease)} + onKeyUp={(event) => handleKeyUp(event, enableIncrease)} + onBlur={() => enableIncrease(false)} + title={_("Increase value. Hint: Hold to increase faster.")} + aria-label={_("Increase value. Hint: Hold to increase faster.")} > @@ -69,7 +91,15 @@ function NumberInput({ onChange, inlineText, value, ...props }) { className="btn btn-outline-secondary" onMouseDown={() => enableDecrease(true)} onMouseUp={() => enableDecrease(false)} - aria-label="Decrease" + onMouseLeave={() => enableDecrease(false)} + onTouchStart={() => enableDecrease(true)} + onTouchEnd={() => enableDecrease(false)} + onTouchCancel={() => enableDecrease(false)} + onKeyDown={(event) => handleKeyDown(event, enableDecrease)} + onKeyUp={(event) => handleKeyUp(event, enableDecrease)} + onBlur={() => enableDecrease(false)} + title={_("Decrease value. Hint: Hold to decrease faster.")} + aria-label={_("Decrease value. Hint: Hold to decrease faster.")} > From c23616811a068a44d03266356312ad7fdd4990bd Mon Sep 17 00:00:00 2001 From: Aleksandr Gumroian Date: Thu, 12 Dec 2024 18:47:33 +0300 Subject: [PATCH 2/3] Fix tests --- .../__tests__/__snapshots__/NumberInput.test.js.snap | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/bootstrap/__tests__/__snapshots__/NumberInput.test.js.snap b/src/bootstrap/__tests__/__snapshots__/NumberInput.test.js.snap index 306ab26..874e03f 100644 --- a/src/bootstrap/__tests__/__snapshots__/NumberInput.test.js.snap +++ b/src/bootstrap/__tests__/__snapshots__/NumberInput.test.js.snap @@ -20,8 +20,9 @@ exports[` Render number input 1`] = ` value="1" />