forked from GeekyAnts/NativeBase
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToastContainer.js
More file actions
116 lines (112 loc) · 2.99 KB
/
ToastContainer.js
File metadata and controls
116 lines (112 loc) · 2.99 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import React, { Component } from "react";
import PropTypes from "prop-types";
import { View, Modal, Platform, ViewPropTypes, Animated } from "react-native";
import { connectStyle } from "native-base-shoutem-theme";
import { Text } from "./Text";
import { Button } from "./Button";
import { ViewNB } from "./View";
import { Toast } from "./Toast";
import mapPropsToStyleNames from "../Utils/mapPropsToStyleNames";
class ToastContainer extends Component {
constructor(props) {
super(props);
this.state = {
modalVisible: false,
fadeAnim: new Animated.Value(0),
};
}
static toastInstance;
static show({ ...config }) {
this.toastInstance._root.showToast({ config });
}
getToastStyle() {
return {
position: "absolute",
opacity: this.state.fadeAnim,
width: "100%",
elevation: 9,
paddingHorizontal: Platform.OS === "ios" ? 20 : 0,
top: this.state.position === "top" ? this.getTop() : undefined,
bottom: this.state.position === "bottom" ? this.getTop() : undefined,
};
}
getTop() {
if (Platform.OS === "ios") {
return 30;
} else {
return 0;
}
}
showToast({ config }) {
this.setState({
modalVisible: true,
text: config.text,
buttonText: config.buttonText,
type: config.type,
position: config.position ? config.position : "bottom",
supportedOrientations: config.supportedOrientations,
style: config.style,
buttonTextStyle: config.buttonTextStyle,
buttonStyle: config.buttonStyle,
textStyle: config.textStyle,
});
if (config.duration > 0) {
setTimeout(() => {
Animated.timing(this.state.fadeAnim, {
toValue: 0,
duration: 200,
}).start();
setTimeout(() => {
this.setState({
modalVisible: false,
});
}, 500);
}, config.duration);
}
Animated.timing(this.state.fadeAnim, {
toValue: 1,
duration: 200,
}).start();
}
closeToast() {
Animated.timing(this.state.fadeAnim, {
toValue: 0,
duration: 200,
}).start();
setTimeout(() => {
this.setState({
modalVisible: false,
});
}, 500);
}
render() {
if (this.state.modalVisible) {
return (
<Animated.View style={this.getToastStyle()}>
<Toast
style={this.state.style}
danger={this.state.type == "danger" ? true : false}
success={this.state.type == "success" ? true : false}
warning={this.state.type == "warning" ? true : false}
>
<Text style={this.state.textStyle}>
{this.state.text}
</Text>
{this.state.buttonText &&
<Button style={this.state.buttonStyle} onPress={() => this.closeToast()}>
<Text style={this.state.buttonTextStyle}>
{this.state.buttonText}
</Text>
</Button>}
</Toast>
</Animated.View>
);
} else return null;
}
}
ToastContainer.propTypes = {
...ViewPropTypes,
style: PropTypes.oneOfType([PropTypes.object, PropTypes.number, PropTypes.array]),
};
const StyledToastContainer = connectStyle("NativeBase.ToastContainer", {}, mapPropsToStyleNames)(ToastContainer);
export { StyledToastContainer as ToastContainer };