forked from ronak301/react-native-submit-button
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
253 lines (232 loc) · 6.79 KB
/
main.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
245
246
247
248
249
250
251
252
253
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import {
StyleSheet,
Text,
View,
TouchableOpacity,
Animated,
Dimensions,
Easing
} from 'react-native';
import { AnimatedCircularProgress } from 'react-native-circular-progress';
import Icon from 'react-native-vector-icons/FontAwesome';
import styles from './main.style';
const { width, height } = Dimensions.get('window');
const AnimatingCicleBackgroundColor = '#bbbbbb';
class SubmitButton extends Component {
constructor(props, context) {
super(props, context);
this.state = {
isLoading: false,
animatedValue: new Animated.Value(0),
fill: 0,
canShowAnimatedCircle: false,
isReady: false
};
}
static defaultProps = {
width: 180,
height: 54,
primaryColor: 'rgb(30, 205, 151)',
secondaryColor: 'white',
buttonText: 'Submit',
iconName: 'check',
onSubmit: () => {},
onSuccess: () => {},
onError: () => {},
buttonState: 'normal',
animationDuration: 200,
errorColor: '#ff6666'
};
componentWillReceiveProps(nextProps) {
const buttonState = nextProps.buttonState;
if ((buttonState === 'success' || buttonState === 'error') && this.props.buttonState !== buttonState) {
this.setState(
{ isLoading: false, isReady: true, canShowAnimatedCircle: false },
this.animateBackToOriginal
);
}
}
render() {
const {
width,
height,
primaryColor,
secondaryColor,
buttonStyle,
errorColor,
buttonState
} = this.props;
const {
animatedValue,
isLoading,
isReady,
canShowAnimatedCircle,
fill
} = this.state;
const buttonWidth = animatedValue.interpolate({
inputRange: [0, 1, 2],
outputRange: [width, height, width]
});
const buttonHeight = height;
const readyStateBorderColor =
buttonState === 'success'
? primaryColor
: buttonState === 'error' ? errorColor : primaryColor;
let borderColor = primaryColor;
borderColor = isLoading
? AnimatingCicleBackgroundColor
: readyStateBorderColor;
const readyStateBgColor =
buttonState === 'success' ? primaryColor : errorColor;
const backgroundColor = isLoading
? 'transparent'
: !isReady ? secondaryColor : readyStateBgColor;
const buttonOpacity = canShowAnimatedCircle ? 0 : 1;
return (
<Animated.View style={[styles.buttonContainer, { width: buttonWidth }]}>
<TouchableOpacity
style={[
styles.button,
buttonStyle,
{
height: buttonHeight,
borderColor: borderColor,
backgroundColor: backgroundColor,
opacity: buttonOpacity
}
]}
onPress={this.onSubmitButtonClick}
>
{this.renderBody()}
</TouchableOpacity>
{this.renderAnimatedCircle(animatedValue, fill, height, primaryColor)}
</Animated.View>
);
}
renderBody = () => {
const {
buttonTextWhenReady,
iconName,
textStyle,
buttonText,
secondaryColor,
buttonState,
primaryColor
} = this.props;
const { animatedValue, isReady } = this.state;
const successIconName = buttonState === 'success' ? iconName : 'times';
const textColor = isReady ? secondaryColor : primaryColor;
const textOpacity = animatedValue.interpolate({
inputRange: [0, 0.5, 1, 1.5, 2],
outputRange: [1, 0, 0, 0, 1]
});
if (!isReady) {
return (
<Animated.Text
style={[
styles.text,
textStyle,
{ opacity: textOpacity, color: textColor }
]}
>
{buttonText}
</Animated.Text>
);
}
if (buttonTextWhenReady) {
return (
<Animated.Text
style={[
styles.text,
textStyle,
{ opacity: textOpacity, color: textColor }
]}
>
{buttonTextWhenReady}
</Animated.Text>
);
}
return <Icon name={successIconName} size={26} color={secondaryColor} />;
};
renderAnimatedCircle = (animatedValue, fill, height, primaryColor) => {
const opacity = animatedValue.interpolate({
inputRange: [0, 0.99, 1, 1.1, 2],
outputRange: [0, 0, 1, 0, 0],
extrapolate: 'clamp'
});
return (
<Animated.View
style={{
opacity: opacity,
backgroundColor: 'transparent',
position: 'absolute',
top: 0,
left: 0
}}
>
<AnimatedCircularProgress
ref="circularProgress"
size={height}
width={4}
fill={fill}
tintColor={primaryColor}
backgroundColor={AnimatingCicleBackgroundColor}
rotation={0}
tension={20}
style={{
backgroundColor: 'transparent',
position: 'absolute',
top: 0,
left: 0
}}
/>
</Animated.View>
);
};
onSubmitButtonClick = () => {
const { isLoading, isReady, animatedValue } = this.state;
const { onSubmit, animationDuration } = this.props;
if (isLoading || isReady) return;
onSubmit();
this.setState({ isLoading: true }, () =>
setTimeout(() => {
this.setState({ fill: 100, canShowAnimatedCircle: true });
}, animationDuration)
);
Animated.timing(animatedValue, {
toValue: 1,
duration: animationDuration,
easing: Easing.linear
}).start();
};
animateBackToOriginal = () => {
const { animatedValue } = this.state;
const { buttonState, onSuccess, onError, animationDuration } = this.props;
const cb = buttonState === 'success' ? onSuccess : onError;
Animated.timing(animatedValue, {
toValue: 2,
duration: animationDuration,
easing: Easing.linear
}).start(cb());
};
}
SubmitButton.propTypes = {
primaryColor: PropTypes.string,
secondaryColor: PropTypes.string,
buttonState: PropTypes.oneOf(['normal', 'success', 'error']),
width: PropTypes.number, // button width
height: PropTypes.number, // button height
buttonText: PropTypes.string, // button text. eg: Submit
buttonTextWhenReady: PropTypes.string, // to show when success.Either pass this or pass icon name (any name from fontawesome lib)
iconName: PropTypes.string, // any name from font awesome lib
textStyle: PropTypes.oneOfType([PropTypes.number, PropTypes.object]), // button text style
buttonStyle: PropTypes.oneOfType([PropTypes.number, PropTypes.object]), // button style
animationDuration: PropTypes.number,
errorColor: PropTypes.string,
onSubmit: PropTypes.func, // function to be executed on button press.
onSuccess: PropTypes.func, // on success callback
onError: PropTypes.func // on error callback
};
module.exports = SubmitButton;