-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
221 lines (198 loc) · 5.5 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
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
/**
* Created by liuyu on 2017/1/18.
*/
import React, {PureComponent} from 'react';
import {
View,
Text,
Image,
ActivityIndicator,
Animated,
StyleSheet,
} from 'react-native';
import PropTypes from 'prop-types';
/**
* 定义类型
* @type {string}
*/
export const HUD_TYPE_DEFAULT = 'default';//默认loading..
export const HUD_TYPE_TEXT = 'text';//纯文本,toast
export const HUD_TYPE_INFO = 'info';
export const HUD_TYPE_SUCCESS = 'success';
export const HUD_TYPE_ERROR = 'error';
const hudTypes = [HUD_TYPE_DEFAULT, HUD_TYPE_TEXT, HUD_TYPE_INFO, HUD_TYPE_SUCCESS, HUD_TYPE_ERROR];
export default class Hud extends PureComponent {
mount = false;
// 构造
constructor(props) {
super(props);
// 初始状态
this.state = {
hudType: this.props.hudType,
isShow: false,
text: '',
opacityValue: new Animated.Value(this.props.opacity),
};
}
componentDidMount() {
this.mount = true;
}
componentWillUnmount() {
this.mount = false;
}
show(hudType = HUD_TYPE_DEFAULT, text = '', after = null) {
this.setState({
isShow: true,
hudType: hudType,
text: text,
});
this.isShow = true;
Animated.timing(
this.state.opacityValue,
{
toValue: this.props.opacity,
duration: this.props.fadeInDuration,
}
).start();
if (after !== null) {
this.close(after)
}
}
close(after = null) {
if (!this.isShow) return;
let animate = () => {
Animated.timing(
this.state.opacityValue,
{
toValue: 0.0,
duration: this.props.fadeOutDuration,
}
).start(() => {
this.mount && this.setState({
isShow: false,
});
this.isShow = false;
})
};
if (after != null) {
setTimeout(animate, after);
} else {
animate();
}
}
/**
* 带图标的
* @param text_numberOfLines
* @param iconSource
* @returns {XML}
* @private
*/
__renderIcon(iconSource) {
return <Image style={styles.image} source={iconSource}/>;
}
/**
* 带loading转圈
* @param text_numberOfLines
* @returns {XML}
* @private
*/
__renderSpinner(text_numberOfLines) {
return <ActivityIndicator style={styles.image} animating={this.state.isShow}
size={'large'}/>;
}
/**
* 设置纯text布局
* @param numberOfLines
* @returns {XML}
* @private
*/
__renderText(numberOfLines, width=60) {
return <Text numberOfLines={numberOfLines}
style={{
color: '#ffffff',
width: width,
}}>{this.state.text}</Text>
}
render() {
let icon = null;
let text = null;
switch (this.state.hudType) {
case HUD_TYPE_DEFAULT:
icon = this.__renderSpinner(2);
text = this.__renderText(2);
break;
case HUD_TYPE_TEXT:
icon = null;
text = this.__renderText(2,120);
break;
case HUD_TYPE_INFO:
icon = this.__renderIcon(require('./src/info.png'));
text = this.__renderText(2);
break;
case HUD_TYPE_SUCCESS:
icon = this.__renderIcon(require('./src/success.png'));
text = this.__renderText(2);
break;
case HUD_TYPE_ERROR:
icon = this.__renderIcon(require('./src/error.png'));
text = this.__renderText(2);
break;
default:
icon = this.__renderSpinner(2);
text = this.__renderText(2);
break
}
let view = this.state.isShow ?
<View ref="container" pointerEvents={this.props.backgroundTouchable ? 'none' : 'auto'}
style={[styles.container, {paddingTop: this.props.positionValue}]}>
<Animated.View
style={[styles.content, {opacity: this.state.opacityValue}, this.props.style]}>
{icon}
{text}
</Animated.View>
</View> : null;
return view;
}
}
const styles = StyleSheet.create({
container: {
position: 'absolute',
left: 0,
right: 0,
top: 0,
bottom: 0,
alignItems: 'center',
justifyContent: 'center',
padding: 48,
},
content: {
backgroundColor: 'black',
borderRadius: 5,
padding: 16,
flexDirection: 'column',
alignItems: 'center',
justifyContent: 'center',
},
image: {
width: 40,
height: 40,
tintColor: 'white',
}
});
Hud.propTypes = {
style: PropTypes.any,
fadeInDuration: PropTypes.number,
fadeOutDuration: PropTypes.number,
opacity: PropTypes.number,
positionValue: PropTypes.number,
hudType: PropTypes.oneOf(hudTypes),
backgroundTouchable: PropTypes.bool,
};
Hud.defaultProps = {
fadeInDuration: 500,
fadeOutDuration: 500,
opacity: 1,
positionValue: 0,
backgroundTouchable: true,
hudType: HUD_TYPE_TEXT,
};