From 2e4a688a3f760331694209a7a15455c898364dca Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Fri, 17 Aug 2018 00:05:27 +0100 Subject: [PATCH] Resume onSelect tracking after dragend --- .../react-dom/src/events/SelectEventPlugin.js | 3 ++ .../__tests__/SelectEventPlugin-test.js | 35 +++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/packages/react-dom/src/events/SelectEventPlugin.js b/packages/react-dom/src/events/SelectEventPlugin.js index 68f059cab600b..e440c3f438e41 100644 --- a/packages/react-dom/src/events/SelectEventPlugin.js +++ b/packages/react-dom/src/events/SelectEventPlugin.js @@ -14,6 +14,7 @@ import shallowEqual from 'shared/shallowEqual'; import { TOP_BLUR, TOP_CONTEXT_MENU, + TOP_DRAG_END, TOP_FOCUS, TOP_KEY_DOWN, TOP_KEY_UP, @@ -39,6 +40,7 @@ const eventTypes = { dependencies: [ TOP_BLUR, TOP_CONTEXT_MENU, + TOP_DRAG_END, TOP_FOCUS, TOP_KEY_DOWN, TOP_KEY_UP, @@ -200,6 +202,7 @@ const SelectEventPlugin = { break; case TOP_CONTEXT_MENU: case TOP_MOUSE_UP: + case TOP_DRAG_END: mouseDown = false; return constructSelectEvent(nativeEvent, nativeEventTarget); // Chrome and IE fire non-standard event when selection is changed (and diff --git a/packages/react-dom/src/events/__tests__/SelectEventPlugin-test.js b/packages/react-dom/src/events/__tests__/SelectEventPlugin-test.js index 3ff0398f738cf..6f4753896dda3 100644 --- a/packages/react-dom/src/events/__tests__/SelectEventPlugin-test.js +++ b/packages/react-dom/src/events/__tests__/SelectEventPlugin-test.js @@ -108,4 +108,39 @@ describe('SelectEventPlugin', () => { node.dispatchEvent(nativeEvent); expect(select).toHaveBeenCalledTimes(1); }); + + // Regression test for https://github.com/facebook/react/issues/11379 + it('should not wait for `mouseup` after receiving `dragend`', () => { + const select = jest.fn(); + const onSelect = event => { + expect(typeof event).toBe('object'); + expect(event.type).toBe('select'); + expect(event.target).toBe(node); + select(event.currentTarget); + }; + + const node = ReactDOM.render( + , + container, + ); + node.focus(); + + let nativeEvent = new MouseEvent('focus', { + bubbles: true, + cancelable: true, + }); + node.dispatchEvent(nativeEvent); + expect(select).toHaveBeenCalledTimes(0); + + nativeEvent = new MouseEvent('mousedown', { + bubbles: true, + cancelable: true, + }); + node.dispatchEvent(nativeEvent); + expect(select).toHaveBeenCalledTimes(0); + + nativeEvent = new MouseEvent('dragend', {bubbles: true, cancelable: true}); + node.dispatchEvent(nativeEvent); + expect(select).toHaveBeenCalledTimes(1); + }); });