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
224 lines (205 loc) · 5.8 KB
/
ToastContainer.js
File metadata and controls
224 lines (205 loc) · 5.8 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/* eslint-disable class-methods-use-this */
import React, { Component } from 'react';
import {
Keyboard,
Platform,
Animated,
ViewPropTypes,
PanResponder,
} from 'react-native';
import { connectStyle } from 'native-base-shoutem-theme';
import mapPropsToStyleNames from '../utils/mapPropsToStyleNames';
import { PLATFORM } from '../theme/variables/commonColor';
import { Text } from './Text';
import { Button } from './Button';
import { Toast } from './Toast';
const POSITION = {
ABSOLUTE: 'absolute',
BOTTOM: 'bottom',
TOP: 'top',
};
class ToastContainer extends Component {
static show({ ...config }) {
this.toastInstance._root.showToast({ config });
}
static hide() {
if (this.toastInstance._root.getModalState()) {
this.toastInstance._root.closeToast('functionCall');
}
}
constructor(props) {
super(props);
this.state = {
fadeAnim: new Animated.Value(0),
pan: new Animated.ValueXY({ x: 0, y: 0 }),
keyboardHeight: 0,
isKeyboardVisible: false,
modalVisible: false,
};
this.keyboardDidHide = this.keyboardDidHide.bind(this);
this.keyboardDidShow = this.keyboardDidShow.bind(this);
this._panResponder = PanResponder.create({
onMoveShouldSetPanResponderCapture: () => true,
onPanResponderRelease: (evt, { dx }) => {
if (dx !== 0) {
Animated.timing(this.state.pan, {
toValue: { x: dx, y: 0 },
duration: 100,
useNativeDriver: false
}).start(() => this.closeToast('swipe'));
}
},
});
}
componentDidMount() {
Keyboard.addListener('keyboardDidShow', this.keyboardDidShow);
Keyboard.addListener('keyboardDidHide', this.keyboardDidHide);
}
componentWillUnmount() {
Keyboard.removeListener('keyboardDidShow', this.keyboardDidShow);
Keyboard.removeListener('keyboardDidHide', this.keyboardDidHide);
}
getToastStyle() {
return {
position: POSITION.ABSOLUTE,
opacity: this.state.fadeAnim,
width: '100%',
elevation: 9,
paddingHorizontal: Platform.OS === PLATFORM.IOS ? 20 : 0,
top: this.state.position === POSITION.TOP ? 30 : undefined,
bottom:
this.state.position === POSITION.BOTTOM ? this.getTop() : undefined,
};
}
getTop() {
if (Platform.OS === PLATFORM.IOS) {
if (this.state.isKeyboardVisible) {
return this.state.keyboardHeight;
}
return 30;
}
return 0;
}
getButtonText(buttonText) {
if (buttonText) {
if (buttonText.trim().length === 0) {
return undefined;
}
return buttonText;
}
return undefined;
}
getModalState() {
return this.state.modalVisible;
}
static toastInstance;
keyboardDidHide() {
this.setState({
keyboardHeight: 0,
isKeyboardVisible: false,
});
}
keyboardDidShow(e) {
this.setState({
keyboardHeight: e.endCoordinates.height,
isKeyboardVisible: true,
});
}
showToast({ config }) {
this.setState({
modalVisible: true,
text: config.text,
buttonText: this.getButtonText(config.buttonText),
type: config.type,
position: config.position ? config.position : POSITION.BOTTOM,
supportedOrientations: config.supportedOrientations,
style: config.style,
buttonTextStyle: config.buttonTextStyle,
buttonStyle: config.buttonStyle,
textStyle: config.textStyle,
onClose: config.onClose,
swipeDisabled: config.swipeDisabled || false
});
// 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,
useNativeDriver: false,
}).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,
useNativeDriver: false,
}).start(() => {
this.closeModal(reason);
this.state.pan.setValue({ x: 0, y: 0 });
});
}
render() {
if (this.state.modalVisible) {
const { x, y } = this.state.pan;
return (
<Animated.View
{...this.state.swipeDisabled ? {} : this._panResponder.panHandlers}
style={[
this.getToastStyle(),
{ transform: [{ translateX: x }, { translateY: y }] },
]}
>
<Toast
style={[this.state.style]}
danger={this.state.type === 'danger'}
success={this.state.type === 'success'}
warning={this.state.type === 'warning'}
>
<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>
);
}
return null;
}
}
ToastContainer.propTypes = {
...ViewPropTypes,
};
const StyledToastContainer = connectStyle(
'NativeBase.ToastContainer',
{},
mapPropsToStyleNames
)(ToastContainer);
export { StyledToastContainer as ToastContainer };