-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
92 lines (78 loc) · 2.01 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
import React, { Component } from "react";
import PropTypes from "prop-types";
import { StyleSheet, View, Easing, Animated, Text } from "react-native";
class Marquee extends Component {
constructor(props) {
super(props);
this.state = {
height: 0
};
this.animatedTransformY = new Animated.Value(0);
}
componentDidUpdate(prevProps, prevState) {
if (prevState.height !== this.state.height) {
this.runAnimation();
}
}
runAnimation() {
this.animatedTransformY.setValue(0);
Animated.timing(this.animatedTransformY, {
duration: this.props.duration,
toValue: -this.state.height / 2,
easing: Easing.linear
}).start(() => this.runAnimation());
}
wrapperOnLayout(e) {
this.setState({
height: Math.round(e.nativeEvent.layout.height)
});
}
render() {
const { children, data } = this.props;
let contentCom = [];
if (children) {
contentCom = children;
} else {
contentCom = data.map((item, index) => (
<View key={`marqueeList${index}`}>
<Text>{item}</Text>
</View>
));
}
const cloneChildren = React.Children.map(contentCom, contentCom =>
React.cloneElement(contentCom)
);
const reactElementArr = [contentCom, cloneChildren];
return (
<View style={[styles.container, { height: this.props.height }]}>
<Animated.View
style={{
transform: [{ translateY: this.animatedTransformY }]
}}
>
<View onLayout={event => this.wrapperOnLayout(event)}>
{reactElementArr.map(reactEle => reactEle)}
</View>
</Animated.View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
overflow: "hidden"
}
});
Marquee.propTypes = {
duration: PropTypes.number,
height: PropTypes.number,
children: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.node),
PropTypes.node
])
};
Marquee.defaultProps = {
duration: 10000,
height: 100
};
export default Marquee;