Skip to content

Commit 47c00f5

Browse files
committed
toast
1 parent d52e178 commit 47c00f5

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import React, {useEffect} from "react";
2+
import {
3+
View,
4+
Text,
5+
StyleSheet,
6+
LayoutAnimation,
7+
TouchableOpacity,
8+
} from "react-native";
9+
import {Constants, dispatch, EventEmitter, scale, useSelector} from "@common";
10+
import {deviceHeight} from "@utils";
11+
import {RootState} from "@store/allReducers";
12+
import {addToast, removeToast} from "@store/toast_redux/reducer";
13+
14+
const MyToast = () => {
15+
16+
const {isToast} = useSelector((state: RootState) => state.toast);
17+
const {msg} = useSelector((state: RootState) => state.toast);
18+
19+
useEffect(() => {
20+
const toastListener = EventEmitter.addListener(
21+
Constants.EmitCode.Toast,
22+
doToast
23+
);
24+
//animation
25+
LayoutAnimation.configureNext(LayoutAnimation.Presets.easeInEaseOut);
26+
return () => {
27+
toastListener.remove();
28+
}
29+
}, [isToast]);
30+
31+
const renderToast = (msg: string) => {
32+
if ((msg && !msg) || !isToast) return null;
33+
const onPress = () => dispatch(removeToast());
34+
return (
35+
<TouchableOpacity style={styles.textWrap} onPress={onPress}>
36+
<Text style={styles.text}>{msg}</Text>
37+
</TouchableOpacity>
38+
);
39+
};
40+
41+
const doToast = (msg: string, duration = 4000) => {
42+
if (!isToast) {
43+
dispatch(addToast(msg));
44+
setTimeout(() => {
45+
dispatch(removeToast())
46+
}, duration)
47+
}
48+
};
49+
50+
return (
51+
<View style={styles.container}>{renderToast(msg)}</View>
52+
);
53+
};
54+
55+
const styles = StyleSheet.create({
56+
container: {
57+
position: "absolute",
58+
alignSelf: 'center',
59+
bottom: (deviceHeight - 20) / 10, // padding bottom
60+
left: (deviceHeight - 20) / 20,
61+
right: (deviceHeight - 20) / 20, // padding horizontal
62+
alignItems: "center",
63+
zIndex: 9999,
64+
},
65+
textWrap: {
66+
backgroundColor: "rgba(60,60,60,0.9)",
67+
padding: scale(10),
68+
paddingHorizontal: scale(20),
69+
borderRadius: scale(20),
70+
marginTop: scale(5),
71+
},
72+
text: {
73+
color: "#FFFFFF",
74+
},
75+
});
76+
77+
export default (MyToast);

0 commit comments

Comments
 (0)