GoodsCell.js 6.54 KB
'use strict';

import React from 'react';
import ReactNative from 'react-native';
import Immutable, {Map} from 'immutable';

const {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  Image,
  Dimensions,
  TouchableOpacity,
  Platform,
} = ReactNative;


export default class GoodsCell extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            moreProduct: true,
        };
    }

    shouldComponentUpdate(nextProps,nextState){
        if (Immutable.is(nextProps.resource, this.props.resource) &&  nextState.moreProduct == this.state.moreProduct) {
            return false;
        } else {
            return true;
        }
    }

    render() {
		let {resource} = this.props;
		let originList = resource.get('productList');
        let listSize = originList?originList.size:0;
		if (!originList || listSize == 0) {
			return null;
		}
        let list = originList;
        if (this.state.moreProduct && listSize > 2) {
            list = originList.slice(0,2);
        }
        let surplusSize = listSize-2;
        let moreStr = '展开剩余'+ surplusSize + '个商品';
        let backgroundWidth =  width;
        let showMoreViewHeigth = (this.state.moreProduct && listSize > 2)?60:0;
		let backgroundHeight = Math.ceil(list.size) * (rowHeight+1) + showMoreViewHeigth;

		return(
			<View style={{width: backgroundWidth, height:backgroundHeight, backgroundColor:'white'}}>
                {list.map((value, i) => {
                    let product = value?value.toJS():null;
                    if (!product) {
                        return null;
                    }
                    let default_images = product.default_images;
                    let product_name = product.product_name;
                    let originPrice = parseFloat(product.sales_price);
                    let originPriceStr = '¥ ' + originPrice.toFixed(2); // 拼接的原价
                    let product_skn = product.product_skn;
                    let prd_id = product.product_id;
                    return (
                        <TouchableOpacity
                            key={product_skn + '_key_' + i}
                            style={styles.content_View}
                            activeOpacity={0.5}
                            onPress={() => {
                                let pos_id = 103;
                                this.props.onPressProduct && this.props.onPressProduct(value,i,pos_id);
                            }}
                        >
                            <View style={styles.content_View}>
                                <View style={styles.line}/>
                                <View style={styles.single_View}>
                                    <Image source={{uri: default_images}} style={styles.icon} resizeMode={'contain'}></Image>
                                    <View style={styles.nameView}>
                                        <Text style={styles.name} numberOfLines={2}>{product_name}</Text>
                                        <Text style={styles.nowPrice} numberOfLines={1}>{originPriceStr}</Text>
                                    </View>
                                    <TouchableOpacity
                                        style={styles.touchableOpacityButton}
                                        activeOpacity={0.5}
                                        onPress={() => {
                                            let pos_id = 106;
                                            this.props.onPressShopCar && this.props.onPressShopCar(product.product_skn,prd_id,pos_id);
                                        }}
                                    >
                                        <Image source={require('../../image/jgwc_bt.png')} style={styles.button} resizeMode={'contain'}></Image>
                                    </TouchableOpacity>
                                </View>
                            </View>
                        </TouchableOpacity>
                    );
                })}
                {(this.state.moreProduct && listSize > 2) ? <TouchableOpacity style={styles.more} activeOpacity={0.5} onPress={() => {
                    this.setState({moreProduct: !this.state.moreProduct});
                }}>
                    <View style={styles.more}>
                        <Text style={styles.moreText} numberOfLines={1}>{moreStr}</Text>
                        <View style={styles.arrowView}>
                            <Image source={require('../../image/arrow_ic.png')} style={styles.arrow_icon} resizeMode={'contain'}></Image>
                        </View>
                    </View>
                </TouchableOpacity>:null}
			</View>
		);
    }
};

let {width, height} = Dimensions.get('window');
const DEVICE_WIDTH_RATIO = width / 750;
let rowHeight = Math.ceil(254 * DEVICE_WIDTH_RATIO);
let imageWidth =  Math.ceil((rowHeight - 20) * (235/314));

let styles = StyleSheet.create({
    content_View: {
        height: rowHeight + 1,//+1 分隔线
        width:width,
        backgroundColor: 'white',
    },
    single_View: {
        height: rowHeight,
        width:width,
        backgroundColor: 'white',
        flexDirection: 'row',
    },
    icon: {
        marginTop: 10,
        marginLeft: 20,
        height: rowHeight - 20,
		width: imageWidth,
    },
    nameView: {
        height: rowHeight,
        width: width - imageWidth - 20,
    },
    name: {
        marginTop: 30,
        marginLeft:15,
        fontSize: 13,
        width: width - imageWidth - 35,
    },
    nowPrice: {
        fontSize: 15,
        marginTop: 20,
        marginLeft:15,
        color: '#d0021b',
    },
    touchableOpacityButton: {
        height: 23,
        width: 40,
        right: 15,
        bottom: 10,
        position: 'absolute',
    },
    button: {
        height: 23,
        width: 40,
    },
    more: {
		height: 60,
		width:width,
        alignItems: 'center',
        justifyContent: 'center',
        flexDirection: 'row',
        backgroundColor: 'white',
    },
    moreText: {
        height: Platform.OS === 'ios' ? 22 : 24,
        paddingLeft: 10,
        paddingRight: 2,
        paddingTop: 2,
        paddingBottom: 2,
        fontSize: 15,
        color: 'white',
        backgroundColor: 'rgb(68, 68, 68)',
    },
    line: {
        width: width-20,
        height: 1,
        marginLeft: 20,
        backgroundColor: '#e5e5e5',
    },
    arrowView: {
        width: 25,
        height: Platform.OS === 'ios' ? 22 : 24,
        justifyContent: 'center',
        backgroundColor: 'rgb(68, 68, 68)',
    },
    arrow_icon: {
        width: 15,
        height: 7,
    }
});