forked from software-mansion/react-native-gesture-handler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
createHandler.js
352 lines (317 loc) · 10.6 KB
/
createHandler.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
import React from 'react';
import {
findNodeHandle as findNodeHandleRN,
NativeModules,
Touchable,
Platform,
} from 'react-native';
import deepEqual from 'fbjs/lib/areEqual';
import RNGestureHandlerModule from './RNGestureHandlerModule';
import State from './State';
function findNodeHandle(node) {
if (Platform.OS === 'web') return node;
return findNodeHandleRN(node);
}
const { UIManager = {} } = NativeModules;
const customGHEventsConfig = {
onGestureHandlerEvent: { registrationName: 'onGestureHandlerEvent' },
onGestureHandlerStateChange: {
registrationName: 'onGestureHandlerStateChange',
},
};
// Add gesture specific events to genericDirectEventTypes object exported from UIManager
// native module.
// Once new event types are registered with react it is possible to dispatch these
// events to all kind of native views.
UIManager.genericDirectEventTypes = {
...UIManager.genericDirectEventTypes,
...customGHEventsConfig,
};
// In newer versions of RN the `genericDirectEventTypes` is located in the object
// returned by UIManager.getConstants(), we need to add it there as well to make
// it compatible with RN 61+
if (UIManager.getConstants) {
UIManager.getConstants().genericDirectEventTypes = {
...UIManager.getConstants().genericDirectEventTypes,
...customGHEventsConfig,
};
}
// Wrap JS responder calls and notify gesture handler manager
const {
setJSResponder: oldSetJSResponder = () => {},
clearJSResponder: oldClearJSResponder = () => {},
} = UIManager;
UIManager.setJSResponder = (tag, blockNativeResponder) => {
RNGestureHandlerModule.handleSetJSResponder(tag, blockNativeResponder);
oldSetJSResponder(tag, blockNativeResponder);
};
UIManager.clearJSResponder = () => {
RNGestureHandlerModule.handleClearJSResponder();
oldClearJSResponder();
};
let handlerTag = 1;
const handlerIDToTag = {};
function isConfigParam(param, name) {
// param !== Object(param) returns false if `param` is a function
// or an object and returns true if `param` is null
return (
param !== undefined &&
(param !== Object(param) || !('__isNative' in param)) &&
name !== 'onHandlerStateChange' &&
name !== 'onGestureEvent'
);
}
function filterConfig(props, validProps, defaults = {}) {
const res = { ...defaults };
Object.keys(validProps).forEach(key => {
const value = props[key];
if (isConfigParam(value, key)) {
let value = props[key];
if (key === 'simultaneousHandlers' || key === 'waitFor') {
value = transformIntoHandlerTags(props[key]);
} else if (key === 'hitSlop') {
if (typeof value !== 'object') {
value = { top: value, left: value, bottom: value, right: value };
}
}
res[key] = value;
}
});
return res;
}
function transformIntoHandlerTags(handlerIDs) {
if (!Array.isArray(handlerIDs)) {
handlerIDs = [handlerIDs];
}
if (Platform.OS === 'web') {
return handlerIDs.map(({ current }) => current).filter(handle => handle);
}
// converts handler string IDs into their numeric tags
return handlerIDs
.map(
handlerID =>
handlerIDToTag[handlerID] ||
(handlerID.current && handlerID.current._handlerTag) ||
-1
)
.filter(handlerTag => handlerTag > 0);
}
function hasUnresolvedRefs(props) {
const extract = refs => {
if (!Array.isArray(refs)) {
return refs && refs.current === null;
}
return refs.some(r => r && r.current === null);
};
return extract(props['simultaneousHandlers']) || extract(props['waitFor']);
}
const stateToPropMappings = {
[State.BEGAN]: 'onBegan',
[State.FAILED]: 'onFailed',
[State.CANCELLED]: 'onCancelled',
[State.ACTIVE]: 'onActivated',
[State.END]: 'onEnded',
};
export default function createHandler(
handlerName,
propTypes = {},
config = {},
transformProps,
customNativeProps = {}
) {
class Handler extends React.Component {
static displayName = handlerName;
static propTypes = propTypes;
constructor(props) {
super(props);
this._handlerTag = handlerTag++;
this._config = {};
if (props.id) {
if (handlerIDToTag[props.id] !== undefined) {
throw new Error(`Handler with ID "${props.id}" already registered`);
}
handlerIDToTag[props.id] = this._handlerTag;
}
}
_onGestureHandlerEvent = event => {
if (event.nativeEvent.handlerTag === this._handlerTag) {
this.props.onGestureEvent && this.props.onGestureEvent(event);
} else {
this.props.onGestureHandlerEvent &&
this.props.onGestureHandlerEvent(event);
}
};
_onGestureHandlerStateChange = event => {
if (event.nativeEvent.handlerTag === this._handlerTag) {
this.props.onHandlerStateChange &&
this.props.onHandlerStateChange(event);
const stateEventName = stateToPropMappings[event.nativeEvent.state];
if (typeof this.props[stateEventName] === 'function') {
this.props[stateEventName](event);
}
} else {
this.props.onGestureHandlerStateChange &&
this.props.onGestureHandlerStateChange(event);
}
};
_refHandler = node => {
this._viewNode = node;
const child = React.Children.only(this.props.children);
const { ref } = child;
if (ref !== null) {
if (typeof ref === 'function') {
ref(node);
} else {
ref.current = node;
}
}
};
_createGestureHandler = newConfig => {
this._config = newConfig;
RNGestureHandlerModule.createGestureHandler(
handlerName,
this._handlerTag,
newConfig
);
};
_attachGestureHandler = newViewTag => {
this._viewTag = newViewTag;
RNGestureHandlerModule.attachGestureHandler(this._handlerTag, newViewTag);
};
_updateGestureHandler = newConfig => {
this._config = newConfig;
RNGestureHandlerModule.updateGestureHandler(this._handlerTag, newConfig);
};
componentWillUnmount() {
RNGestureHandlerModule.dropGestureHandler(this._handlerTag);
if (this._updateEnqueued) {
clearImmediate(this._updateEnqueued);
}
if (this.props.id) {
delete handlerIDToTag[this.props.id];
}
}
componentDidMount() {
if (hasUnresolvedRefs(this.props)) {
// If there are unresolved refs (e.g. ".current" has not yet been set)
// passed as `simultaneousHandlers` or `waitFor`, we enqueue a call to
// _update method that will try to update native handler props using
// setImmediate. This makes it so _update function gets called after all
// react components are mounted and we expect the missing ref object to
// be resolved by then.
this._updateEnqueued = setImmediate(() => {
this._updateEnqueued = null;
this._update();
});
}
this._createGestureHandler(
filterConfig(
transformProps ? transformProps(this.props) : this.props,
{ ...this.constructor.propTypes, ...customNativeProps },
config
)
);
this._attachGestureHandler(findNodeHandle(this._viewNode));
}
componentDidUpdate() {
const viewTag = findNodeHandle(this._viewNode);
if (this._viewTag !== viewTag) {
this._attachGestureHandler(viewTag);
}
this._update();
}
_update() {
const newConfig = filterConfig(
transformProps ? transformProps(this.props) : this.props,
{ ...this.constructor.propTypes, ...customNativeProps },
config
);
if (!deepEqual(this._config, newConfig)) {
this._updateGestureHandler(newConfig);
}
}
setNativeProps(updates) {
const mergedProps = { ...this.props, ...updates };
const newConfig = filterConfig(
transformProps ? transformProps(mergedProps) : mergedProps,
{ ...this.constructor.propTypes, ...customNativeProps },
config
);
this._updateGestureHandler(newConfig);
}
render() {
let gestureEventHandler = this._onGestureHandlerEvent;
const { onGestureEvent, onGestureHandlerEvent } = this.props;
if (onGestureEvent && typeof onGestureEvent !== 'function') {
// If it's not a method it should be an native Animated.event
// object. We set it directly as the handler for the view
// In this case nested handlers are not going to be supported
if (onGestureHandlerEvent) {
throw new Error(
'Nesting touch handlers with native animated driver is not supported yet'
);
}
gestureEventHandler = this.props.onGestureEvent;
} else {
if (
onGestureHandlerEvent &&
typeof onGestureHandlerEvent !== 'function'
) {
throw new Error(
'Nesting touch handlers with native animated driver is not supported yet'
);
}
}
let gestureStateEventHandler = this._onGestureHandlerStateChange;
const { onHandlerStateChange, onGestureHandlerStateChange } = this.props;
if (onHandlerStateChange && typeof onHandlerStateChange !== 'function') {
// If it's not a method it should be an native Animated.event
// object. We set it directly as the handler for the view
// In this case nested handlers are not going to be supported
if (onGestureHandlerStateChange) {
throw new Error(
'Nesting touch handlers with native animated driver is not supported yet'
);
}
gestureStateEventHandler = this.props.onHandlerStateChange;
} else {
if (
onGestureHandlerStateChange &&
typeof onGestureHandlerStateChange !== 'function'
) {
throw new Error(
'Nesting touch handlers with native animated driver is not supported yet'
);
}
}
const child = React.Children.only(this.props.children);
let grandChildren = child.props.children;
if (
Touchable.TOUCH_TARGET_DEBUG &&
child.type &&
(child.type === 'RNGestureHandlerButton' ||
child.type.name === 'View' ||
child.type.displayName === 'View')
) {
grandChildren = React.Children.toArray(grandChildren);
grandChildren.push(
Touchable.renderDebugView({
color: 'mediumspringgreen',
hitSlop: child.props.hitSlop,
})
);
}
return React.cloneElement(
child,
{
ref: this._refHandler,
collapsable: false,
onGestureHandlerEvent: gestureEventHandler,
onGestureHandlerStateChange: gestureStateEventHandler,
},
grandChildren
);
}
}
return Handler;
}