-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.tsx
72 lines (63 loc) · 1.8 KB
/
index.tsx
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
import React, { FC, RefObject } from "react";
import { View, ViewProps, GestureResponderEvent } from "react-native";
interface OutsideViewProps extends ViewProps {
/**
* Ref of element you want to detect press event outside of
*/
childRef: RefObject<any>;
/**
* callback function when press outside of ref component
*/
onPressOutside?: () => void;
}
/**
* use recursive to check if press inside that component
* @param target - this is childRef component
* @param nestedViewRef - all of children element of childRef
*/
const isTapInsideComponent = (target: any, nestedViewRef: any): boolean => {
if (
target &&
nestedViewRef &&
target._nativeTag === nestedViewRef._nativeTag
) {
return true;
}
if (nestedViewRef._children && nestedViewRef._children.length > 0) {
for (let index = 0; index <= nestedViewRef._children.length - 1; index++) {
if (isTapInsideComponent(target, nestedViewRef._children[index])) {
return true;
}
}
}
return false;
};
/**
* Wrapper component to detect event outside a specific element
* Inherit from View, so it will take all View's props
* @param OutsideViewProps - acceptance props
*/
const OutsideView: FC<OutsideViewProps> = ({
childRef,
onPressOutside,
onStartShouldSetResponder,
...rest
}) => (
<View
{...rest}
onStartShouldSetResponder={(evt: GestureResponderEvent) => {
evt.persist();
// if press outside, execute onPressOutside callback
if (
onPressOutside &&
childRef &&
!isTapInsideComponent(evt.target, childRef.current || childRef)
) {
onPressOutside();
}
// return onStartShouldSetResponder in case it is passed to OutsideView
return onStartShouldSetResponder?.(evt) ?? true;
}}
/>
);
export default OutsideView;