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
145 lines (141 loc) · 4.08 KB
/
ToastContainer.js
File metadata and controls
145 lines (141 loc) · 4.08 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import React, { Component } from "react";
import PropTypes from "prop-types";
import { View, Modal, Platform, Animated, ViewPropTypes } 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 });
}
static hide() {
if (this.toastInstance._root.getModalState()) {
this.toastInstance._root.closeToast("functionCall");
}
}
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;
}
}
getButtonText(buttonText) {
if (buttonText) {
if (buttonText.trim().length === 0) {
return undefined;
} else return buttonText;
}
return undefined;
}
getModalState() {
return this.state.modalVisible;
}
showToast({ config }) {
this.setState({
modalVisible: true,
text: config.text,
buttonText: this.getButtonText(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,
onClose: config.onClose
});
// If we have a toast already open, cut off its close timeout so that it won't affect *this* toast.
if (this.closeTimeout) {
clearTimeout(this.closeTimeout)
}
// Set the toast to close after the duration.
if (config.duration !== 0) {
const duration = (config.duration > 0) ? config.duration : 1500;
this.closeTimeout = setTimeout(this.closeToast.bind(this, 'timeout'), duration);
}
// Fade the toast in now.
Animated.timing(this.state.fadeAnim, {
toValue: 1,
duration: 200
}).start();
}
closeModal(reason) {
this.setState({
modalVisible: false
});
const { onClose } = this.state;
if (onClose && typeof onClose === "function") {
onClose(reason);
}
}
closeToast(reason) {
clearTimeout(this.closeTimeout);
Animated.timing(this.state.fadeAnim, {
toValue: 0,
duration: 200
}).start(this.closeModal.bind(this, reason));
}
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('user')}
>
<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 };