forked from crazycodeboy/react-native-easy-toast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·165 lines (149 loc) · 4.34 KB
/
index.js
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
/**
* react-native-easy-toast
* https://github.com/crazycodeboy/react-native-easy-toast
* Email:[email protected]
* Blog:http://jiapenghui.com
* @flow
*/
import React, {Component} from 'react';
import {
StyleSheet,
View,
Animated,
Dimensions,
Text,
ViewPropTypes as RNViewPropTypes,
} from 'react-native'
import PropTypes from 'prop-types';
const ViewPropTypes = RNViewPropTypes || View.propTypes;
export const DURATION = {
LENGTH_SHORT: 500,
FOREVER: 0,
};
const {height, width} = Dimensions.get('window');
export default class Toast extends Component {
constructor(props) {
super(props);
this.state = {
isShow: false,
text: '',
opacityValue: new Animated.Value(this.props.opacity),
}
}
show(text, duration, callback) {
this.duration = typeof duration === 'number' ? duration : DURATION.LENGTH_SHORT;
this.callback = callback;
this.setState({
isShow: true,
text: text,
});
this.animation = Animated.timing(
this.state.opacityValue,
{
toValue: this.props.opacity,
duration: this.props.fadeInDuration,
useNativeDriver: this.props.useNativeAnimation
}
)
this.animation.start(() => {
this.isShow = true;
if(duration !== DURATION.FOREVER) this.close();
});
}
close( duration ) {
let delay = typeof duration === 'undefined' ? this.duration : duration;
if(delay === DURATION.FOREVER) delay = this.props.defaultCloseDelay || 250;
if (!this.isShow && !this.state.isShow) return;
this.timer && clearTimeout(this.timer);
this.timer = setTimeout(() => {
this.animation = Animated.timing(
this.state.opacityValue,
{
toValue: 0.0,
duration: this.props.fadeOutDuration,
useNativeDriver: this.props.useNativeAnimation
}
)
this.animation.start(() => {
this.setState({
isShow: false,
});
this.isShow = false;
if(typeof this.callback === 'function') {
this.callback();
}
});
}, delay);
}
componentWillUnmount() {
this.animation && this.animation.stop()
this.timer && clearTimeout(this.timer);
}
render() {
let positionStyle;
switch (this.props.position) {
case 'top':
positionStyle = { top: this.props.positionValue };
break;
case 'center':
positionStyle = { top: height / 2 };
break;
case 'bottom':
positionStyle = { bottom: this.props.positionValue };
break;
}
const view = this.state.isShow ?
<View
style={[styles.container, positionStyle]}
pointerEvents="none"
>
<Animated.View
style={[styles.content, { opacity: this.state.opacityValue }, this.props.style]}
>
{React.isValidElement(this.state.text) ? this.state.text : <Text style={this.props.textStyle}>{this.state.text}</Text>}
</Animated.View>
</View> : null;
return view;
}
}
const styles = StyleSheet.create({
container: {
position: 'absolute',
left: 0,
right: 0,
elevation: 999,
alignItems: 'center',
zIndex: 10000,
},
content: {
backgroundColor: 'black',
borderRadius: 5,
padding: 10,
},
text: {
color: 'white'
}
});
Toast.propTypes = {
style: ViewPropTypes.style,
position: PropTypes.oneOf([
'top',
'center',
'bottom',
]),
textStyle: Text.propTypes.style,
positionValue:PropTypes.number,
fadeInDuration:PropTypes.number,
fadeOutDuration:PropTypes.number,
opacity:PropTypes.number,
useNativeAnimation:PropTypes.bool
}
Toast.defaultProps = {
position: 'bottom',
textStyle: styles.text,
positionValue: 120,
fadeInDuration: 500,
fadeOutDuration: 500,
opacity: 1,
useNativeAnimation: false
}