forked from lwansbrough/react-native-progress-bar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProgressBar.js
87 lines (78 loc) · 1.82 KB
/
ProgressBar.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
import PropTypes from "prop-types";
import React, { PureComponent } from "react";
import {
View,
Easing,
Animated,
StyleSheet,
Dimensions,
ViewPropTypes
} from "react-native";
const styles = StyleSheet.create({
background: {
backgroundColor: "#bbbbbb",
height: 5,
overflow: "hidden"
},
fill: {
backgroundColor: "#3b5998",
height: 5
}
});
export default class ProgressBar extends PureComponent {
static propTypes = {
width: PropTypes.number,
easing: PropTypes.any,
style: PropTypes.object,
progress: PropTypes.number,
fillStyle: ViewPropTypes.style,
easingDuration: PropTypes.number,
};
static defaultProps = {
progress: 0,
style: styles,
easingDuration: 500,
fillStyle: undefined,
easing: Easing.inOut(Easing.ease),
width: Dimensions.get("window").width
};
state = {
progress: new Animated.Value(this.props.progress)
};
componentWillReceiveProps(prevProps) {
if (prevProps.progress <= 1 && this.props.progress !== prevProps.progress) {
this.update(prevProps.progress);
}
}
update(progress) {
Animated.timing(this.state.progress, {
easing: this.props.easing,
duration: this.props.easingDuration,
toValue: progress > 1 ? 1 : progress
}).start();
}
value() {
return this.state.progress._value;
}
render() {
const fillWidth = this.state.progress.interpolate({
inputRange: [0, 1],
outputRange: [
0 * (this.props.style.width || this.props.width),
1 * (this.props.style.width || this.props.width)
]
});
return (
<View
style={[
styles.background,
this.props.style
]}
>
<Animated.View
style={[styles.fill, this.props.fillStyle, { width: fillWidth }]}
/>
</View>
);
}
}