forked from software-mansion/react-native-gesture-handler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
createNativeWrapper.js
86 lines (79 loc) · 2.47 KB
/
createNativeWrapper.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
import React from 'react';
import NativeViewGestureHandler from './NativeViewGestureHandler';
const NATIVE_WRAPPER_BIND_BLACKLIST = new Set(['replaceState', 'isMounted']);
/*
* This array should consist of:
* - All keys in propTypes from NativeGestureHandler
* (and all keys in GestureHandlerPropTypes)
* - 'onGestureHandlerEvent'
* - 'onGestureHandlerStateChange'
*/
const NATIVE_WRAPPER_PROPS_FILTER = [
'id',
'minPointers',
'enabled',
'waitFor',
'simultaneousHandlers',
'shouldCancelWhenOutside',
'hitSlop',
'onGestureEvent',
'onHandlerStateChange',
'onBegan',
'onFailed',
'onCancelled',
'onActivated',
'onEnded',
'shouldActivateOnStart',
'disallowInterruption',
'onGestureHandlerEvent',
'onGestureHandlerStateChange',
];
export default function createNativeWrapper(Component, config = {}) {
class ComponentWrapper extends React.Component {
static propTypes = {
...Component.propTypes,
};
static displayName = Component.displayName || 'ComponentWrapper';
_refHandler = node => {
// bind native component's methods
let source = node;
while (source != null) {
for (let methodName of Object.getOwnPropertyNames(source)) {
if (
!methodName.startsWith('_') && // private methods
!methodName.startsWith('component') && // lifecycle methods
!NATIVE_WRAPPER_BIND_BLACKLIST.has(methodName) && // other
typeof source[methodName] === 'function' &&
this[methodName] === undefined
) {
if (source[methodName].prototype) {
// determine if it's not bound already
this[methodName] = source[methodName].bind(node);
} else {
this[methodName] = source[methodName];
}
}
}
source = Object.getPrototypeOf(source);
}
};
render() {
// filter out props that should be passed to gesture handler wrapper
const gestureHandlerProps = Object.keys(this.props).reduce(
(props, key) => {
if (NATIVE_WRAPPER_PROPS_FILTER.indexOf(key) !== -1) {
props[key] = this.props[key];
}
return props;
},
{ ...config } // watch out not to modify config
);
return (
<NativeViewGestureHandler {...gestureHandlerProps}>
<Component {...this.props} ref={this._refHandler} />
</NativeViewGestureHandler>
);
}
}
return ComponentWrapper;
}