@haroldtran/react-native-modals 0.0.1 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +6 -2
- package/src/ModalPortal.tsx +23 -14
- package/src/type.ts +12 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@haroldtran/react-native-modals",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"description": "React Native Modals Library for IOS & Android.",
|
|
5
5
|
"main": "/src/index.tsx",
|
|
6
6
|
"scripts": {
|
|
@@ -79,5 +79,9 @@
|
|
|
79
79
|
"modals-example/node_modules"
|
|
80
80
|
]
|
|
81
81
|
},
|
|
82
|
-
"packageManager": "yarn@4.9.2"
|
|
82
|
+
"packageManager": "yarn@4.9.2",
|
|
83
|
+
"trustedDependencies": [
|
|
84
|
+
"core-js",
|
|
85
|
+
"fsevents"
|
|
86
|
+
]
|
|
83
87
|
}
|
package/src/ModalPortal.tsx
CHANGED
|
@@ -2,13 +2,7 @@ import React from "react";
|
|
|
2
2
|
import { DeviceEventEmitter } from "react-native";
|
|
3
3
|
import BaseModal from "./components/BaseModal";
|
|
4
4
|
import BottomModal from "./components/BottomModal";
|
|
5
|
-
import type { ModalProps } from "./type";
|
|
6
|
-
|
|
7
|
-
type StackItem = ModalProps & {
|
|
8
|
-
key: string;
|
|
9
|
-
type?: "modal" | "bottomModal";
|
|
10
|
-
onDismiss?: () => void;
|
|
11
|
-
};
|
|
5
|
+
import type { EmitModalPortal, ModalProps, StackItem } from "./type";
|
|
12
6
|
|
|
13
7
|
let modal: ModalPortal | null = null;
|
|
14
8
|
|
|
@@ -31,9 +25,9 @@ class ModalPortal extends React.Component<{}, { stack: StackItem[] }> {
|
|
|
31
25
|
|
|
32
26
|
static show(
|
|
33
27
|
children: React.ReactNode,
|
|
34
|
-
props: ModalProps & { type?: "modal" | "bottomModal" } = {},
|
|
28
|
+
props: ModalProps & { key?: string; type?: "modal" | "bottomModal" } = {},
|
|
35
29
|
) {
|
|
36
|
-
return modal!.show({ children, ...props });
|
|
30
|
+
return modal!.show({ children, ...props, key: props.key });
|
|
37
31
|
}
|
|
38
32
|
|
|
39
33
|
static update(key: string, props: ModalProps) {
|
|
@@ -62,13 +56,20 @@ class ModalPortal extends React.Component<{}, { stack: StackItem[] }> {
|
|
|
62
56
|
getProps = (
|
|
63
57
|
props: ModalProps & { key?: string; type?: "modal" | "bottomModal" },
|
|
64
58
|
) => {
|
|
65
|
-
const key =
|
|
59
|
+
const key = props?.key || this.generateKey();
|
|
66
60
|
return { visible: true, ...props, key } as StackItem;
|
|
67
61
|
};
|
|
68
62
|
|
|
69
|
-
show = (
|
|
63
|
+
show = (
|
|
64
|
+
props: ModalProps & { key?: string; type?: "modal" | "bottomModal" },
|
|
65
|
+
) => {
|
|
70
66
|
const mergedProps = this.getProps(props);
|
|
71
67
|
this.setState(({ stack }) => ({ stack: stack.concat(mergedProps) }));
|
|
68
|
+
DeviceEventEmitter.emit("ModalPortal", {
|
|
69
|
+
type: "show",
|
|
70
|
+
key: mergedProps.key,
|
|
71
|
+
stack: this.state.stack,
|
|
72
|
+
} as EmitModalPortal);
|
|
72
73
|
return mergedProps.key;
|
|
73
74
|
};
|
|
74
75
|
|
|
@@ -78,6 +79,11 @@ class ModalPortal extends React.Component<{}, { stack: StackItem[] }> {
|
|
|
78
79
|
const index = this.getIndex(key);
|
|
79
80
|
if (index >= 0) {
|
|
80
81
|
stack[index] = { ...stack[index], ...mergedProps };
|
|
82
|
+
DeviceEventEmitter.emit("ModalPortal", {
|
|
83
|
+
type: mergedProps.visible ? "update" : "dismiss",
|
|
84
|
+
key,
|
|
85
|
+
stack,
|
|
86
|
+
} as EmitModalPortal);
|
|
81
87
|
}
|
|
82
88
|
return { stack };
|
|
83
89
|
});
|
|
@@ -88,13 +94,11 @@ class ModalPortal extends React.Component<{}, { stack: StackItem[] }> {
|
|
|
88
94
|
const idx = this.getIndex(key);
|
|
89
95
|
if (idx < 0) return;
|
|
90
96
|
const props = { ...this.state.stack[idx], visible: false };
|
|
91
|
-
DeviceEventEmitter.emit("ModalDismiss", key);
|
|
92
97
|
this.update(key, props);
|
|
93
98
|
};
|
|
94
99
|
|
|
95
100
|
dismissAll = () => {
|
|
96
101
|
this.state.stack.forEach(({ key }) => this.dismiss(key));
|
|
97
|
-
DeviceEventEmitter.emit("ModalDismiss", "all");
|
|
98
102
|
};
|
|
99
103
|
|
|
100
104
|
dismissHandler = (key: string) => {
|
|
@@ -130,7 +134,12 @@ class ModalPortal extends React.Component<{}, { stack: StackItem[] }> {
|
|
|
130
134
|
};
|
|
131
135
|
|
|
132
136
|
render() {
|
|
133
|
-
return this.state
|
|
137
|
+
return this.state?.stack
|
|
138
|
+
?.filter(
|
|
139
|
+
(item, index, self) =>
|
|
140
|
+
index === self?.findIndex((t) => t?.key === item?.key),
|
|
141
|
+
)
|
|
142
|
+
?.map(this.renderModal);
|
|
134
143
|
}
|
|
135
144
|
}
|
|
136
145
|
|
package/src/type.ts
CHANGED
|
@@ -3,6 +3,18 @@
|
|
|
3
3
|
import { ReactNode } from "react";
|
|
4
4
|
import Animation from "./animations/Animation";
|
|
5
5
|
|
|
6
|
+
export type StackItem = ModalProps & {
|
|
7
|
+
key: string;
|
|
8
|
+
type?: "modal" | "bottomModal";
|
|
9
|
+
onDismiss?: () => void;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export type EmitModalPortal = {
|
|
13
|
+
type: "show" | "update" | "dismiss";
|
|
14
|
+
key?: string;
|
|
15
|
+
stack: StackItem[];
|
|
16
|
+
};
|
|
17
|
+
|
|
6
18
|
export type SwipeDirection = "up" | "down" | "left" | "right";
|
|
7
19
|
|
|
8
20
|
export type DragEvent = {
|