-
Notifications
You must be signed in to change notification settings - Fork 3
/
TagCloud.js
120 lines (104 loc) · 2.71 KB
/
TagCloud.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
import React, { Component, propTypes } from 'react';
import {
StyleSheet,
Text,
View
} from 'react-native';
const styles = StyleSheet.create({
container: {
alignItems: 'center',
justifyContent: 'center'
},
cloudTagContainer: {
flexDirection: 'row',
flexWrap: 'wrap',
alignItems: 'center',
justifyContent: 'center',
},
});
export default class TagCloud extends Component {
constructor(props) {
super(props);
this.TagCloud = this.orderData().map((item, key) => {
const tagContainerStyle = {
paddingLeft: this.getRandomPaddingLeft(),
paddingTop: this.getRandomPaddingTop(),
paddingRight: this.getRandomPaddingRight(),
paddingBottom: this.getRandomPaddingBottom(),
};
const tagStyle = {
fontSize: this.props.minFontSize + (item.point * 4),
color: this.props.colorList[item.point],
};
return (
<View key={key} style={tagContainerStyle}>
<Text style={tagStyle}>{item.title}</Text>
</View>
);
});
}
orderData() {
let result = [];
let tagList = this.props.tagList;
tagList.sort((a, b) => {
if (a.point === b.point) return 0;
else return a.point > b.point ? -1 : 1;
});
const maxPoint = tagList[0].point;
let switchFlag = true;
tagList.map((item, key) => {
if (maxPoint === item.point) {
result.push(item);
return;
}
if (switchFlag) {
result.unshift(item);
switchFlag = false;
} else {
result.push(item);
switchFlag = true;
}
});
return result;
}
getRandomPaddingLeft() {
return Math.floor(Math.random() * this.props.tagPaddingLeft);
}
getRandomPaddingTop() {
return Math.floor(Math.random() * this.props.tagPaddingTop);
}
getRandomPaddingRight() {
return Math.floor(Math.random() * this.props.tagPaddingRight);
}
getRandomPaddingBottom() {
return Math.floor(Math.random() * this.props.tagPaddingBottom);
}
render() {
return (
<View style={[styles.container, this.props.style]}>
<View style={styles.cloudTagContainer}>
{this.TagCloud}
</View>
</View>
);
}
}
TagCloud.propTypes = {
tagList: React.PropTypes.array,
colorList: React.PropTypes.array,
minFontSize: React.PropTypes.number,
style: View.propTypes.style,
tagPaddingLeft: React.PropTypes.number,
tagPaddingTop: React.PropTypes.number,
tagPaddingRight: React.PropTypes.number,
tagPaddingBottom: React.PropTypes.number,
};
TagCloud.defaultProps = {
tagList: [],
colorList: ['#cdd2d2', '#757b7c', '#2e3031'],
minFontSize: 12,
tagPaddingLeft: 30,
tagPaddingTop: 30,
tagPaddingRight: 30,
tagPaddingBottom: 30,
};