forked from MechanicKim/react-native-tag-cloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TagCloud.js
111 lines (96 loc) · 2.44 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
import React, { Component } 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],
fontFamily: this.props.fontFamily,
};
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.defaultProps = {
tagList: [],
colorList: ['#cdd2d2', '#757b7c', '#2e3031'],
minFontSize: 12,
tagPaddingLeft: 30,
tagPaddingTop: 30,
tagPaddingRight: 30,
tagPaddingBottom: 30,
fontFamily: 'Arial',
};