-
Notifications
You must be signed in to change notification settings - Fork 3
/
LoadingButton.js
100 lines (91 loc) · 2.7 KB
/
LoadingButton.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
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
TouchableOpacity,
Animated,
ActivityIndicator
} from 'react-native';
export default class LoadingButton extends Component{
constructor(props){
super(props);
this.state = {
loading: false
};
this.defaultLoadingValue = {
width: new Animated.Value(props.viewStyle.width)
}
}
defaultLoadingAnimation(start, end){
this.defaultLoadingValue.width.setValue(start)
Animated.timing(
this.defaultLoadingValue.width,
{
toValue: end,
duration: this.props.animationDelay,
}
).start()
}
loadingContent(){
return (
<ActivityIndicator color={this.props.activityIndicatorColor} size={this.props.activityIndicatorSize}/>
)
}
componentWillReceiveProps(nextProps){
if(nextProps.isLoading)
this.defaultLoadingAnimation(this.props.viewStyle.width, this.props.whenAnimationViewWidth);
else
this.defaultLoadingAnimation(this.props.whenAnimationViewWidth, this.props.viewStyle.width);
}
defaultLoadingButton(){
return (
<Animated.View style={[this.props.viewStyle, {width: this.props.enableWidthAnimation ? this.defaultLoadingValue.width : this.props.viewStyle.width}]}>
<TouchableOpacity onPress={()=>{!this.props.isLoading && this.props.onPress()}}
style={[styles.defaultLoadingTouch, {width: this.props.enableWidthAnimation ? styles.defaultLoadingTouch.width : this.props.viewStyle.width}]}>
{this.props.isLoading ? this.loadingContent() : this.props.childView || <Text style={styles.defaultLoadingText}>{this.props.title}</Text>}
</TouchableOpacity>
</Animated.View>
)
}
render(){
return (
<View>
{this.defaultLoadingButton()}
</View>
)
}
}
LoadingButton.defaultProps = {
onPress: ()=>{
alert('press');
},
title: 'button',
isLoading: false,
activityIndicatorColor: '#FFF',
activityIndicatorSize: 'small',
viewStyle: {
width: 100,
height: 30,
backgroundColor: '#25CED1',
borderRadius: 20
},
animationDelay: 200,
whenAnimationViewWidth: 40,
enableWidthAnimation: true,
};
const styles = StyleSheet.create({
root: {
width: 100,
height: 30,
},
defaultLoadingTouch: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
defaultLoadingText: {
color: '#FFF'
}
});