From 9476523311c689fc3091338979a0db76e701f5ef Mon Sep 17 00:00:00 2001 From: raunofreiberg Date: Wed, 13 May 2020 01:36:25 +0300 Subject: [PATCH] fix(slider): prevent keyboard events when disabled --- packages/slider/__tests__/slider.test.tsx | 19 +++++++++++++++++++ packages/slider/src/index.tsx | 4 ++++ 2 files changed, 23 insertions(+) diff --git a/packages/slider/__tests__/slider.test.tsx b/packages/slider/__tests__/slider.test.tsx index 7c33f8df1..a57602af9 100644 --- a/packages/slider/__tests__/slider.test.tsx +++ b/packages/slider/__tests__/slider.test.tsx @@ -110,6 +110,25 @@ describe("", () => { fireEvent.keyDown(handle, { key: "PageDown", code: 34 }); expect(getCurrentValue(handle)).toEqual(min + tenSteps); }); + + it("does not move when disabled", () => { + const { getByRole } = render( + + + + + + + ); + const handle = getByRole("slider"); + + fireEvent.click(handle); + + fireEvent.keyDown(handle, { key: "ArrowRight", code: 39 }); + fireEvent.keyDown(handle, { key: "ArrowRight", code: 39 }); + + expect(getCurrentValue(handle)).toEqual(0); + }); }); }); diff --git a/packages/slider/src/index.tsx b/packages/slider/src/index.tsx index 47515b202..d9642bb42 100644 --- a/packages/slider/src/index.tsx +++ b/packages/slider/src/index.tsx @@ -392,6 +392,10 @@ const SliderInput = forwardRefWithAs< // https://www.w3.org/TR/wai-aria-practices-1.2/#slider_kbd_interaction let handleKeyDown = useEventCallback((event: React.KeyboardEvent) => { + if (disabled) { + return; + } + let newValue: number; let tenSteps = (max - min) / 10; let keyStep = step || (max - min) / 100;