-
Notifications
You must be signed in to change notification settings - Fork 18
/
StarRatingBar.js
162 lines (148 loc) · 5.65 KB
/
StarRatingBar.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/**
* @author GJS <[email protected]>
*
* GJS reserves all rights not expressly granted.
*
* The MIT License (MIT)
*
* Copyright (c) 2017 GJS
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL
* FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* @providesModule StarRatingBar
* @flow
*/
/**
* 功能描述:评分条(支持显示小数、滑动打分)
* 2017-3-22
* 作者:GJS
*/
import React, {
Component
} from 'react'
import {
View,
Text,
StyleSheet,
TouchableHighlight,
ViewPropTypes
} from 'react-native'
import PropTypes from 'prop-types'
import StarRatingView, { StarLog } from './StarRatingView'
const RNViewPropTypes = ViewPropTypes || View.propTypes;
const RNPropTypes = PropTypes || React.PropTypes;
const propTypes = {
style: RNViewPropTypes.style,
starStyle: RNViewPropTypes.style, // 自定义星星样式
readOnly: RNPropTypes.bool, // 是否只读
continuous: RNPropTypes.bool, // 是否允许滑动打分
maximumValue: RNPropTypes.number, // 最大值
minimumValue: RNPropTypes.number, // 最小值
value: RNPropTypes.number, // 具体数值
valueToFix: RNPropTypes.number, // 保留几位小数
spacing: RNPropTypes.number, // 分数
allowsHalfStars: RNPropTypes.bool, // 是否允许半颗星
accurateHalfStars: RNPropTypes.bool, // 是否允许精确值
/* todo: 绘制星星图片
starBorderColor: RNPropTypes.string, // 星星边线颜色
starBorderWidth: RNPropTypes.number, // 星星边线宽度
*/
emptyStarColor: RNPropTypes.string, // 空星填充色
tintColor: RNPropTypes.string, // 着色(填充色)
emptyStarImage: RNPropTypes.element, // 空星图片
halfStarImage: RNPropTypes.element, // 半星图片
filledStarImage: RNPropTypes.element, // 实星图片
onStarValueChanged: RNPropTypes.func, // 数值改变时的回调函数
// 额外的属性
scoreTextStyle: RNPropTypes.object, // 自定义分数文本样式
scoreText: RNPropTypes.string, // 分数文本
dontShowScore: RNPropTypes.bool, // 不显示分数
};
const defaultProps = {
readOnly: true,
maximumValue: 5,
minimumValue: 0,
value: 0,
valueToFix: 1,
spacing: 10,
allowsHalfStars: false,
accurateHalfStars: false,
scoreTextStyle: {marginLeft: 10, color: '#ffb819'},
scoreText: '分',
};
export default class StarRatingBar extends Component {
static propTypes = propTypes;
static defaultProps = defaultProps;
constructor(props) {
super(props);
this.state = {
value: this.props.score || this.props.value,
}
StarLog("hello", "star", "log");
}
componentWillReceiveProps(nextProps) {
let value = nextProps.score || nextProps.value;
if (value !== this.state.value) {
this.setState({value});
}
}
render() {
const {starStyle, spacing, maximumValue, scoreTextStyle, scoreText, dontShowScore} = this.props;
let starWidth = 20;
if (starStyle && starStyle.width) {
starWidth = starStyle.width;
}
let starViewWidth = (starWidth + spacing) * maximumValue - spacing;
return <View style={styles.startList}>
<View style={{width: starViewWidth, overflow: 'hidden'}}>
<StarRatingView
starStyle={this.props.starStyle}
readOnly={this.props.readOnly}
allowsHalfStars={this.props.allowsHalfStars}
accurateHalfStars={this.props.accurateHalfStars}
continuous={this.props.continuous}
maximumValue={this.props.maximumValue}
minimumValue={this.props.minimumValue}
spacing={this.props.spacing}
valueToFix={this.props.valueToFix}
emptyStarColor={this.props.emptyStarColor}
tintColor={this.props.tintColor}
emptyStarImage={this.props.emptyStarImage}
halfStarImage={this.props.halfStarImage}
filledStarImage={this.props.filledStarImage}
value={this.state.value}
onStarValueChanged={(changedValue) => {
this.setState({value: changedValue});
this.props.onStarValueChanged && this.props.onStarValueChanged(changedValue);
}}
>
</StarRatingView>
</View>
{dontShowScore ? null : <Text style={scoreTextStyle}>{this.state.value}{scoreText}</Text>}
</View>;
}
}
const styles = StyleSheet.create({
startList: {
flexDirection: 'row',
alignItems: 'center'
},
star: {
marginRight: 10
},
});