-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathToast.js
More file actions
118 lines (96 loc) · 2.47 KB
/
Toast.js
File metadata and controls
118 lines (96 loc) · 2.47 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
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import {
Animated,
TouchableWithoutFeedback,
View,
Text
} from 'react-native'
import ToastStyles from './ToastStyles'
const noop = () => 0
class Toast extends Component {
static propTypes = {
id: PropTypes.string.isRequired,
text: PropTypes.oneOfType([PropTypes.string, PropTypes.node]).isRequired,
styles: PropTypes.object,
duration: PropTypes.number,
height: PropTypes.number,
onShow: PropTypes.func,
onHide: PropTypes.func,
onPress: PropTypes.func
}
static defaultProps = {
styles: ToastStyles.info,
duration: 3000,
height: 100,
onShow: noop,
onHide: noop,
onPress: noop
}
state = { animatedValue: new Animated.Value(0), timeoutId: null }
componentWillMount () {
this.showToast()
}
componentWillUnmount () {
const { timeoutId } = this.state;
clearTimeout(timeoutId)
}
componentWillReceiveProps (nextProps) {
if (this.props.id !== nextProps.id) {
this.showToast()
}
}
showToast () {
const animatedValue = new Animated.Value(0)
this.setState({ animatedValue })
Animated
.timing(animatedValue, { toValue: 1, duration: 350 })
.start()
const { duration, onShow } = this.props
const timeoutId = setTimeout(() => this.hideToast(), duration + 350)
this.setState({ timeoutId }, onShow)
}
hideToast () {
const { timeoutId, animatedValue } = this.state
clearTimeout(timeoutId)
Animated
.timing(animatedValue, { toValue: 0, duration: 350 })
.start()
setTimeout(this.props.onHide, 350)
}
onPress = () => {
this.hideToast()
this.props.onPress()
}
render () {
const y = this.state.animatedValue.interpolate({
inputRange: [0, 1],
outputRange: [-this.props.height, 0]
})
const { styles } = this.props
let text = this.props.text
if (Object.prototype.toString.call(text) === '[object String]') {
text = (
<View style={styles.container}>
<Text style={styles.text}>{text}</Text>
</View>
)
}
return (
<Animated.View style={{
position: 'absolute',
top: 0,
right: 0,
left: 0,
zIndex: 9999,
elevation: 9999,
transform: [{ translateY: y }]
}}>
<TouchableWithoutFeedback onPress={this.onPress}>
{text}
</TouchableWithoutFeedback>
</Animated.View>
)
}
}
export default Toast