GeezerVison is showing github.com — [EnterLeaveEventPlugin] Fix bug when dealing with unhandled DOM nodes… · react/react@26ba38a
File tree
Expand file treeCollapse file tree
Open diff view settings
packages/react-dom/src/events
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: packages/react-dom/src/events/EnterLeaveEventPlugin.js +9-1Lines changed: 9 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -42,6 +42,12 @@ const eventTypes = { |
42 | 42 | }, |
43 | 43 | }; |
44 | 44 |
|
| 45 | +// We track the lastNativeEvent to ensure that when we encounter |
| 46 | +// cases where we process the same nativeEvent multiple times, |
| 47 | +// which can happen when have multiple ancestors, that we don't |
| 48 | +// duplicate enter |
| 49 | +let lastNativeEvent; |
| 50 | + |
45 | 51 | const EnterLeaveEventPlugin = { |
46 | 52 | eventTypes: eventTypes, |
47 | 53 |
|
@@ -163,9 +169,11 @@ const EnterLeaveEventPlugin = { |
163 | 169 |
|
164 | 170 | accumulateEnterLeaveDispatches(leave, enter, from, to); |
165 | 171 |
|
166 | | - if (isOutEvent && from && nativeEventTarget !== fromNode) { |
| 172 | + if (nativeEvent === lastNativeEvent) { |
| 173 | + lastNativeEvent = null; |
167 | 174 | return [leave]; |
168 | 175 | } |
| 176 | + lastNativeEvent = nativeEvent; |
169 | 177 |
|
170 | 178 | return [leave, enter]; |
171 | 179 | }, |
|
Collapse file
Copy file name to clipboardExpand all lines: packages/react-dom/src/events/__tests__/EnterLeaveEventPlugin-test.js +51Lines changed: 51 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -185,4 +185,55 @@ describe('EnterLeaveEventPlugin', () => { |
185 | 185 |
|
186 | 186 | ReactDOM.render(<Parent />, container); |
187 | 187 | }); |
| 188 | + |
| 189 | + it('should call mouseEnter when pressing a non tracked React node', done => { |
| 190 | + const mockFn = jest.fn(); |
| 191 | + |
| 192 | + class Parent extends React.Component { |
| 193 | + constructor(props) { |
| 194 | + super(props); |
| 195 | + this.parentEl = React.createRef(); |
| 196 | + } |
| 197 | + |
| 198 | + componentDidMount() { |
| 199 | + ReactDOM.render(<MouseEnterDetect />, this.parentEl.current); |
| 200 | + } |
| 201 | + |
| 202 | + render() { |
| 203 | + return <div ref={this.parentEl} />; |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + class MouseEnterDetect extends React.Component { |
| 208 | + constructor(props) { |
| 209 | + super(props); |
| 210 | + this.divRef = React.createRef(); |
| 211 | + this.siblingEl = React.createRef(); |
| 212 | + } |
| 213 | + |
| 214 | + componentDidMount() { |
| 215 | + const attachedNode = document.createElement('div'); |
| 216 | + this.divRef.current.appendChild(attachedNode); |
| 217 | + attachedNode.dispatchEvent( |
| 218 | + new MouseEvent('mouseout', { |
| 219 | + bubbles: true, |
| 220 | + cancelable: true, |
| 221 | + relatedTarget: this.siblingEl.current, |
| 222 | + }), |
| 223 | + ); |
| 224 | + expect(mockFn.mock.calls.length).toBe(1); |
| 225 | + done(); |
| 226 | + } |
| 227 | + |
| 228 | + render() { |
| 229 | + return ( |
| 230 | + <div ref={this.divRef}> |
| 231 | + <div ref={this.siblingEl} onMouseEnter={mockFn} /> |
| 232 | + </div> |
| 233 | + ); |
| 234 | + } |
| 235 | + } |
| 236 | + |
| 237 | + ReactDOM.render(<Parent />, container); |
| 238 | + }); |
188 | 239 | }); |