FiveStarView.js 2.73 KB
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>
        )
    }
}