Skip to content

Commit

Permalink
Merge pull request #3 from retyui/update-module
Browse files Browse the repository at this point in the history
Update modules
  • Loading branch information
cuongtranduc authored Nov 1, 2021
2 parents 697ba07 + 4e7ddf8 commit 9bf7a47
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 44 deletions.
15 changes: 0 additions & 15 deletions .npmignore

This file was deleted.

46 changes: 17 additions & 29 deletions index.tsx
Original file line number Diff line number Diff line change
@@ -1,32 +1,24 @@
// @flow

import React from "react";
import React, { FC, RefObject } from "react";
import { View, ViewProps, GestureResponderEvent } from "react-native";

type OutsideViewProps = ViewProps & {
/**
* Either children or a render prop that receives a boolean reflecting whether
* the component is currently pressed.
*/
children?: React.ReactNode;

interface OutsideViewProps extends ViewProps {
/**
* Ref of element you want to detect press event outside of
*/
childRef: any;
childRef: RefObject<any>;

/**
* callback funtion when press outside of ref component
* callback function when press outside of ref component
*/
onPressOutside: () => void;
};
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) => {
const isTapInsideComponent = (target: any, nestedViewRef: any): boolean => {
if (
target &&
nestedViewRef &&
Expand All @@ -42,43 +34,39 @@ const isTapInsideComponent = (target: any, nestedViewRef: any) => {
}
}
}

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 = ({
children,
const OutsideView: FC<OutsideViewProps> = ({
childRef,
onPressOutside,
onMoveShouldSetResponder,
onStartShouldSetResponder,
...rest
}: OutsideViewProps) => (
}) => (
<View
{...rest}
onStartShouldSetResponder={(evt: GestureResponderEvent) => {
evt.persist();

// if press outside, execute onPressOutside callback
if (
onPressOutside &&
childRef &&
!isTapInsideComponent(evt.target, childRef.current || childRef)
) {
onPressOutside && onPressOutside();
}

// return onMoveShouldSetResponder in case it is passed to OutsideView
if (onMoveShouldSetResponder) {
return onMoveShouldSetResponder(evt);
onPressOutside();
}

return true;
// return onStartShouldSetResponder in case it is passed to OutsideView
return onStartShouldSetResponder?.(evt) ?? true;
}}
>
{children}
</View>
/>
);

export default OutsideView;
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "a wrapper view help to detect when user press outside a child component by passing a ref to this component as a prop",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"files": ["lib"],
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "tsc"
Expand Down

0 comments on commit 9bf7a47

Please sign in to comment.