-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
Copy pathignore-nested-events.js
91 lines (78 loc) · 2.73 KB
/
ignore-nested-events.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/**
* External dependencies
*/
import { reduce } from 'lodash';
/**
* WordPress dependencies
*/
import { Component } from '@wordpress/element';
/**
* Component which renders a div with passed props applied except the optional
* `childHandledEvents` prop. Event prop handlers are replaced with a proxying
* event handler to capture and prevent events from being handled by ancestor
* `IgnoreNestedEvents` elements by testing the presence of a private property
* assigned on the event object.
*
* Optionally accepts an `childHandledEvents` prop array, which can be used in
* instances where an inner `IgnoreNestedEvents` element exists and the outer
* element should stop propagation but not invoke a callback handler, since it
* would be assumed these are invoked by the child element.
*
* @type {Component}
*/
class IgnoreNestedEvents extends Component {
constructor() {
super( ...arguments );
this.proxyEvent = this.proxyEvent.bind( this );
// The event map is responsible for tracking an event type to a React
// component prop name, since it is easy to determine event type from
// a React prop name, but not the other way around.
this.eventMap = {};
}
/**
* General event handler which only calls to its original props callback if
* it has not already been handled by a descendant IgnoreNestedEvents.
*
* @param {Event} event Event object.
*
* @return {void}
*/
proxyEvent( event ) {
// Skip if already handled (i.e. assume nested block)
if ( event.nativeEvent._blockHandled ) {
return;
}
// Assign into the native event, since React will reuse their synthetic
// event objects and this property assignment could otherwise leak.
//
// See: https://reactjs.org/docs/events.html#event-pooling
event.nativeEvent._blockHandled = true;
// Invoke original prop handler
const propKey = this.eventMap[ event.type ];
if ( this.props[ propKey ] ) {
this.props[ propKey ]( event );
}
}
render() {
const { childHandledEvents = [], ...props } = this.props;
const eventHandlers = reduce( [
...childHandledEvents,
...Object.keys( props ),
], ( result, key ) => {
// Try to match prop key as event handler
const match = key.match( /^on([A-Z][a-zA-Z]+)$/ );
if ( match ) {
// Re-map the prop to the local proxy handler to check whether
// the event has already been handled.
result[ key ] = this.proxyEvent;
// Assign event -> propName into an instance variable, so as to
// avoid re-renders which could be incurred either by setState
// or in mapping values to a newly created function.
this.eventMap[ match[ 1 ].toLowerCase() ] = key;
}
return result;
}, {} );
return <div { ...props } { ...eventHandlers } />;
}
}
export default IgnoreNestedEvents;