FiveStarView.js 3.25 KB
import React, {Component} from 'react';
import {
    View,
    Text,
    Image,
    TouchableOpacity,
    TouchableWithoutFeedback
} from 'react-native';
import {observer} from "mobx-react/native";
import {observable} from "mobx";
import {width} from "../utils/getSize";
import {NoDoublePress} from "../utils/Common";
const defaultHeight = (100);
const HPading = (16);
const VPading = (6);
@observer
export default class FiveStarView extends Component {

    @observable
    star = !!this.data.star?this.data.star:0

    constructor(props) {
        super(props);
        this.state = {
            data: {star: 2}
        }
        this.attrData = this.props.attrData;
        this.extendData = JSON.parse(this.attrData.data);// allowHalf 固定:true|false
        this.starMap = [];
    }

    componentWillMount() {
        let defaultValue = this.extendData.default? parseInt(this.extendData.default): 0;
        this.setState({
            data: {star: defaultValue}
        });
        this.attrData.value = defaultValue;

        let maxValue = this.extendData.maxValue? parseInt(this.extendData.maxValue):5;
        for (let i=1; i <= maxValue;i++) {
            this.starMap.push(i)
        }
    }

    render() {
        let {name, code, type, description, isRequired, isPreview} = this.attrData;
        let {allowHalf} = this.extendData;
        let starSize = 40;
        if(this.starMap.length == 10){
            starSize = 20;
        }

        return(
            <View style={{backgroundColor: "#fff", paddingTop: VPading,paddingHorizontal: HPading,height:defaultHeight}}>
                <View style={{flexDirection: 'row',height:'40%',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>
                <View style={[{flex:1,paddingHorizontal:HPading, flexDirection:'row', justifyContent:'space-between',alignItems:'center'}]}>
                    {this.starMap.map((item,loop)=>{
                        return (
                            <TouchableWithoutFeedback key={loop} onPress={()=>NoDoublePress.onPress(()=>{
                                //this.state.data.star = loop+1;
                                let _temp = this.state.data;
                                _temp.star = loop+1;
                                this.setState({
                                    data: _temp
                                })
                                this.attrData.value = _temp.star;
                            })}>
                                <Image source={this.state.data.star > loop?require('../img/star_on.png'):
                                    require('../img/star_off.png')} style={{height: starSize,width: starSize,marginRight: 16}} resizeMode={"contain"}/>
                            </TouchableWithoutFeedback>
                        )
                    })}
                    {/*<Text style={{fontSize:setSpText2(12),color:'#999999'}}>
                        {this.logic.getStarStr(this.data)}
                    </Text>*/}
                </View>
            </View>
        )
    }
}