Skip to content

Commit 12f3bdc

Browse files
authored
Merge pull request #3 from dcangulo/bugfix/events-run-earlier
Bugfix/events run earlier
2 parents ec80c9e + 72e750f commit 12f3bdc

7 files changed

Lines changed: 37 additions & 19 deletions

File tree

CHANGELOGS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Changelogs
22

3+
## 1.0.2 (2022-07-12)
4+
* Fixed unexpected behavior on iOS and Android.
5+
36
## 1.0.1 (2022-07-11)
47
* Fixed a bug where a dev dependency is in dependencies.
58

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export default function App() {
2828
);
2929
}
3030
```
31-
`EventProvider` accepts `ViewProps`. See: https://reactnative.dev/docs/view#props
31+
`EventProvider` accepts `ViewProps`. See: [ViewProps](https://reactnative.dev/docs/view#props)
3232

3333
Then wrap every component you want to detect outside press.
3434
```js
@@ -47,6 +47,7 @@ export default function MyComponent() {
4747
);
4848
}
4949
```
50+
`OutsidePressHandler` accepts `onOutsidePress` prop and `ViewProps`. See: [ViewProps](https://reactnative.dev/docs/view#props)
5051

5152
## Changelogs
5253
See [CHANGELOGS.md](CHANGELOGS.md)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-outside-press",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"description": "airbnb/react-outside-click-handler but for React Native",
55
"main": "lib/commonjs/index",
66
"module": "lib/module/index",

src/components/container.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ export default function Container(props: ViewProps) {
88
const { events, skippedEventId, setSkippedEventId } = useEvent();
99
const runEvents = () => {
1010
events.forEach((event: IEvent) => {
11-
if (event.id === skippedEventId) return;
11+
if (event.id === (global as any).rnopSkippedEventId) return;
1212
event.onOutsidePress();
1313
});
1414

15-
if (skippedEventId) setSkippedEventId('');
15+
if ((global as any).rnopSkippedEventId) setSkippedEventId('');
1616
};
1717

1818
useEffect(() => {
Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import React, { useRef, useEffect } from 'react';
2-
import { Pressable } from 'react-native';
2+
import { View, Platform } from 'react-native';
3+
import type { ViewProps } from 'react-native';
34
import useEvent from '../hooks/use-event';
45
import deepClone from '../utils/deep-clone';
56

6-
interface IOutsidePressHandlerProps {
7-
children: JSX.Element;
7+
interface IOutsidePressHandlerProps extends ViewProps {
88
onOutsidePress: () => void;
99
}
1010

1111
export default function OutsidePressHandler(props: IOutsidePressHandlerProps) {
1212
const { children, onOutsidePress } = props;
13-
const id = useRef(Math.random().toString()).current;
13+
const id: string = useRef(Math.random().toString()).current;
1414
const { appendEvent, removeEvent, setSkippedEventId } = useEvent();
1515
const setSkippedEventIdFunc = () => setSkippedEventId(id);
1616

@@ -20,9 +20,21 @@ export default function OutsidePressHandler(props: IOutsidePressHandlerProps) {
2020
return () => removeEvent(id);
2121
}, [onOutsidePress]);
2222

23-
return (
24-
<Pressable onPress={setSkippedEventIdFunc}>
25-
{deepClone(children, setSkippedEventIdFunc)}
26-
</Pressable>
27-
);
23+
return Platform.select({
24+
web: (
25+
<View
26+
{...props}
27+
/*
28+
// @ts-ignore */
29+
onClick={setSkippedEventIdFunc}
30+
>
31+
{deepClone(children, setSkippedEventIdFunc)}
32+
</View>
33+
),
34+
default: (
35+
<View {...props} onTouchStart={setSkippedEventIdFunc}>
36+
{children}
37+
</View>
38+
),
39+
});
2840
}

src/hooks/use-event-store.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ export default function useEventStore() {
1313
appendEvent: (newEvent: IEvent) => setEvents((state) => [...state, newEvent]),
1414
removeEvent: (id: string) => setEvents((state) => state.filter((event) => event.id !== id)),
1515
skippedEventId,
16-
setSkippedEventId,
16+
setSkippedEventId: (id: string) => {
17+
(global as any).rnopSkippedEventId = id;
18+
setSkippedEventId(id);
19+
},
1720
}), [skippedEventId, events]);
1821

1922
return eventActions;

src/utils/deep-clone.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
import React from 'react';
22

3-
export default function deepClone(children: JSX.Element, event: () => void) {
4-
return React.Children.map(children, (child: React.ReactNode): JSX.Element => {
5-
// @ts-ignore
3+
export default function deepClone(children: React.ReactNode, func: () => void) {
4+
return React.Children.map(children, (child: React.ReactNode): React.ReactNode => {
65
if (!React.isValidElement(child)) return child;
76

87
const props: any = { ...child.props };
98

109
if (typeof child.props.onPress === 'function') {
1110
props.onPress = () => {
12-
event();
11+
func();
1312
return child.props.onPress();
1413
};
1514
}
1615

17-
return React.cloneElement(child, props, deepClone(child.props.children, event));
16+
return React.cloneElement(child, props, deepClone(child.props.children, func));
1817
});
1918
}

0 commit comments

Comments
 (0)