-
Notifications
You must be signed in to change notification settings - Fork 15
/
CAlert.js
244 lines (217 loc) · 6.65 KB
/
CAlert.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/** react 组建的引用 */
import React, {Component} from "react";
import {
StyleSheet, ImageBackground, Text, View, Animated,
} from "react-native";
import {Layout} from "../styles/layout";
/** 全局样式的引用 */
/** 第三方依赖库的引用 */
import PropTypes from 'prop-types';
import RootSiblings from 'react-native-root-siblings';
/** 自定义组建的引用 */
import CGradientButton from './CGradientButton';
/** 自定义工具方法的使用 */
import {bouncedUtils} from '../utils/bouncedUtils';
import {easeInOut} from '../utils/curve';
/** 声明一些常量 */
const TOAST_ANIMATION_DURATION = 200;
class CToast extends Component {
static propTypes = {
params: PropTypes.shape({
borderRadius: PropTypes.number,
isOnlyOneBtn: PropTypes.bool,
title: PropTypes.string,
contentText: PropTypes.string.isRequired,
})
}
constructor(props) {
super(props);
this.defaultParams = {
title: '', // 默认不显示
isOnlyOneBtn: true, // 默认是一个按钮
borderRadius: 12, // 圆角默认取值 12
contentText: '',
}
this.state = {
scaleAni: new Animated.Value(0.1),
};
}
componentDidMount() {
this._startAnimated() // 执行开启动画
}
componentWillMount() {
}
componentWillUnmount() {
this._endAnimated()
}
componentWillReceiveProps(nextProps, nextState) {
}
shouldComponentUpdate(nextProps) {
return true
}
_startAnimated = () => {
Animated.timing(this.state.scaleAni, {
toValue: 1.1,
duration: TOAST_ANIMATION_DURATION,
easing: easeInOut(),
useNativeDriver: true
}).start(() => {
Animated.timing(this.state.scaleAni, {
toValue: 1,
duration: TOAST_ANIMATION_DURATION,
easing: easeInOut(),
useNativeDriver: true
}).start();
});
}
_endAnimated = () => {
Animated.timing(this.state.scaleAni, {
toValue: 1.1,
duration: 100,
easing: easeInOut(),
useNativeDriver: true
}).start(() => {
Animated.timing(this.state.scaleAni, {
toValue: 0.01,
duration: TOAST_ANIMATION_DURATION,
easing: easeInOut(),
useNativeDriver: true
}).start();
});
};
render() {
const {title, isOnlyOneBtn, borderRadius, contentText} = Object.assign(this.defaultParams, this.props.params)
return (
<Animated.View
style={[styles.container,
{
opacity: this.state.scaleAni.interpolate({
inputRange: [0, 1],
outputRange: [0, 1]
})
}
]}
pointerEvents={'box-none'}
>
<Animated.View
style={
{transform: [{scale: this.state.scaleAni}]}
}
>
<ImageBackground
source={require('../images/common/index_img_toastshadow.png')}
resizeMode={'stretch'}
style={styles.toastBG}
>
<View style={[styles.wrapper, {borderRadius: borderRadius}]}>
{/*只有在有标题的情况下才显示*/}
{
title.length ? <Text style={{
fontSize: Layout.font.Title3,
fontFamily: 'PingFangSC-Medium',
color: Layout.color.wblack,
marginBottom: 10,
lineHeight: 20,
}}>
{title}
</Text> : null
}
{/*通用文案部分*/}
<View style={{paddingHorizontal: 14, ...Layout.layout.rcc}}>
<Text style={{
fontSize: Layout.font.Subtle2,
color: Layout.color.wblack,
lineHeight: 20,
}}>
{contentText}
</Text>
</View>
{/*根据时间情况来判断显示按钮的个数*/}
<View style={{marginTop: 30, width: '100%', ...Layout.layout.rcc}}>
{
isOnlyOneBtn ? <CGradientButton
disabled={false}
gradientType={'btn_m'}
contentText={'我知道了'}
textStyle={{
color: Layout.color.white_bg,
fontSize: Layout.font.Subtle1,
fontFamily: 'PingFangSC-Regular',
}}
onPress={() => {bouncedUtils.alert.hide()}}
/> : <View style={{
...Layout.layout.rsbc,
width: '100%',
paddingHorizontal: 12
}}>
<CGradientButton
colorsArray={[Layout.color.white_bg, Layout.color.white_bg]}
gradientType={'btn_xs'}
isGradientButton={false}
contentText={'取消'}
colorsPressArray={[Layout.color.gray_press, Layout.color.gray_press]}
additionalStyle={{
borderWidth: 1,
borderColor: Layout.color.gray_line,
}}
textStyle={{
fontSize: Layout.font.Subtle1,
fontFamily: 'PingFangSC-Regular',
color: Layout.color.wgray_main,
}}
/>
<CGradientButton
gradientType={'btn_xs'}
contentText={'去还款'}
textStyle={{
color: Layout.color.white_bg,
fontSize: Layout.font.Subtle1,
fontFamily: 'PingFangSC-Regular',
}}
/>
</View>
}
</View>
</View>
</ImageBackground>
</Animated.View>
</Animated.View>
);
}
}
export default class SiblingsAlert extends Component {
static siblingInstance = null;
// 创建及显示
static showSiblings = (params) => {
SiblingsAlert.siblingInstance = new RootSiblings(<CToast params={params}/>)
};
// 销毁及隐藏
static hideSiblings = () => {
SiblingsAlert.siblingInstance && SiblingsAlert.siblingInstance.destroy();
};
render() {
return null;
}
}
const styles = StyleSheet.create({
container: {
position: 'absolute',
top: 0,
left: 0,
right: 0,
bottom: 0,
...Layout.layout.ccc,
backgroundColor: 'rgba(0,0,0,0.6)'
},
wrapper: {
width: 280,
paddingTop: 30,
paddingBottom: 15,
backgroundColor: '#fff',
...Layout.layout.ccc,
},
toastBG: {
paddingHorizontal: 6,
paddingVertical: 11,
},
});