forked from software-mansion/react-native-gesture-handler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
createNativeWrapper.js
58 lines (53 loc) · 1.48 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
import React from 'react';
import NativeViewGestureHandler from './NativeViewGestureHandler';
/*
* 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 = {}) {
const ComponentWrapper = React.forwardRef((props, ref) => {
// filter out props that should be passed to gesture handler wrapper
const gestureHandlerProps = Object.keys(props).reduce(
(res, key) => {
if (NATIVE_WRAPPER_PROPS_FILTER.indexOf(key) !== -1) {
res[key] = props[key];
}
return res;
},
{ ...config } // watch out not to modify config
);
return (
<NativeViewGestureHandler {...gestureHandlerProps}>
<Component {...props} ref={ref} />
</NativeViewGestureHandler>
);
});
ComponentWrapper.propTypes = {
...Component.propTypes,
};
ComponentWrapper.displayName = Component.displayName || 'ComponentWrapper';
return ComponentWrapper;
}