-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
106 lines (92 loc) · 2.27 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
import React, { Component, PropTypes } from 'react';
import {
View,
StyleSheet,
Dimensions,
Animated,
} from 'react-native';
import invariant from 'invariant';
const RUNNER_WIDTH = 180;
const RUNNER_HEIGHT = 8;
const runnerDuration = 1500;
const styles = StyleSheet.create({
container: {
height: RUNNER_HEIGHT,
backgroundColor: '#CCCCCC',
flexDirection: 'row',
justifyContent: 'flex-start',
},
runner: {
position: 'absolute',
height: RUNNER_HEIGHT,
backgroundColor: '#52B370',
},
});
const deviceWidth = Dimensions.get('window').width;
class InfiniteProgressBar extends Component {
constructor(props) {
super(props);
this.state = {
runnerPos: new Animated.Value(-RUNNER_WIDTH),
};
this._value = -RUNNER_WIDTH;
this.validateDuration(props.duration);
}
componentWillMount() {
this.state.runnerPos.addListener(({value}) => this._value = value);
this.doLoop(this.makeAnimation);
}
validateDuration(duration) {
if(duration) {
invariant(
typeof duration === 'number',
'The duration must be a number'
);
}
}
makeAnimation(currentValue, value, duration) {
const toValue = value === -RUNNER_WIDTH ? deviceWidth : -RUNNER_WIDTH;
return Animated.timing(
currentValue,
{
toValue,
duration: duration || runnerDuration,
}
);
}
doLoop(animation) {
animation(this.state.runnerPos, this._value, this.props.duration).start(() => {
if (this._value === deviceWidth) {
this.state.runnerPos.setValue(-RUNNER_WIDTH);
}
return this.doLoop(animation);
});
}
render() {
const {
containerStyle,
runnerStyle,
} = this.props;
const {
runnerPos,
} = this.state;
const _containerStyle = [styles.container, containerStyle];
const _runnerStyle = [styles.runner, runnerStyle, { left: runnerPos, top: 0, width: RUNNER_WIDTH }];
return (
<View style={_containerStyle}>
<Animated.View
style={_runnerStyle}
/>
</View>
);
}
}
InfiniteProgressBar.defaultProps = {
containerStyle: {},
runnerStyle: {},
};
InfiniteProgressBar.propTypes = {
containerStyle: PropTypes.any,
runnerStyle: PropTypes.any,
};
export default InfiniteProgressBar;