FiveStarView.js
2.73 KB
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
import React, {Component} from 'react';
import {
View,
Text,
} from 'react-native';
import StarRating from "react-native-star-rating";
const titleViewHeight = (24);
const HPading = (16);
const VPading = (6);
export default class FiveStarView extends Component {
constructor(props) {
super(props);
this.state = {
rating: 0,
};
this.attrData = this.props.attrData;
this.extendData = (this.attrData.data && this.attrData.data.length > 0)?JSON.parse(this.attrData.data):{};// allowHalf 固定:true|false
this.starRating = 1;// 星级比例
}
componentWillMount() {
this.maxValue = this.extendData.maxValue? parseInt(this.extendData.maxValue):5;
this.starRating = 5 / this.maxValue;// 计算比例
this.starValue = this.maxValue / 5;// 每课的数值
let defaultValue = this.extendData.default? parseInt(this.extendData.default): 5;
this.setState({
rating: defaultValue * this.starRating
});
this.attrData.value = defaultValue;
}
render() {
let {name, code, type, description, isRequired, isPreview} = this.attrData;
let {allowHalf} = this.extendData;
return(
<View style={{backgroundColor: "#fff", paddingTop: VPading,paddingLeft: 10, paddingRight: 15}}>
<View style={{flexDirection: 'row',height:titleViewHeight,justifyContent:'flex-start',alignItems:'center'}}>
{isRequired&&<Text style={{ color: "#FF3030" }}>* </Text>}
{!isRequired&&<Text style={{ color: "#fff" }}>* </Text>}
<Text style={{fontSize: 16, color: 'rgba(0, 0, 0, 1)'}}>{name||""}</Text>
</View>
<StarRating
disabled={false}
activeOpacity={0.8}
maxStars={5}
starSize={20}
starStyle={{marginRight: 15}}
containerStyle={{width: '60%',backgroundColor: 'white', marginLeft: 10, marginVertical: 10}}
selectedStar={rating=>{
this.setState({
rating: rating
});
// TODO 为了与pc端保持一致,取半分时,不计算整数值
//this.attrData.value = rating * this.starValue;
this.attrData.value = rating;
}}
rating={this.state.rating}
emptyStarColor="#cbcbcb" // 默认 gray
fullStarColor="#ffc300"
halfStarColor="#ffc300"
halfStarEnabled={this.maxValue === 10}
/>
</View>
)
}
}