From 34aaec6f908e85d56cbc510677bc1c85d87aa57b Mon Sep 17 00:00:00 2001 From: Dominic Gannaway Date: Wed, 28 Aug 2019 17:39:07 +0100 Subject: [PATCH] [react-events] Ensure screen reader virtual clicks support preventDefault (#16600) --- packages/react-events/src/dom/Press.js | 5 +++++ .../react-events/src/dom/__tests__/Press-test.internal.js | 4 +++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/react-events/src/dom/Press.js b/packages/react-events/src/dom/Press.js index ce8b745254c61..267240776af6e 100644 --- a/packages/react-events/src/dom/Press.js +++ b/packages/react-events/src/dom/Press.js @@ -635,6 +635,11 @@ const pressResponderImpl = { if (isFunction(onPress) && isScreenReaderVirtualClick(nativeEvent)) { state.pointerType = 'keyboard'; state.pressTarget = event.responderTarget; + const preventDefault = props.preventDefault; + + if (preventDefault !== false) { + nativeEvent.preventDefault(); + } dispatchEvent(event, onPress, context, state, 'press', DiscreteEvent); } break; diff --git a/packages/react-events/src/dom/__tests__/Press-test.internal.js b/packages/react-events/src/dom/__tests__/Press-test.internal.js index 159a039bbc691..5e155f5ad839f 100644 --- a/packages/react-events/src/dom/__tests__/Press-test.internal.js +++ b/packages/react-events/src/dom/__tests__/Press-test.internal.js @@ -423,7 +423,9 @@ describe.each(environmentTable)('Press responder', hasPointerEvents => { it('is called once after virtual screen reader "click" event', () => { const target = createEventTarget(ref.current); - target.virtualclick(); + const preventDefault = jest.fn(); + target.virtualclick({preventDefault}); + expect(preventDefault).toBeCalled(); expect(onPress).toHaveBeenCalledTimes(1); expect(onPress).toHaveBeenCalledWith( expect.objectContaining({