BLKBrandCell.js 3.83 KB
'use strict';

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

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


export default class BLKBrandCell extends React.Component {
    constructor(props) {
        super(props);
        this._renderRow = this._renderRow.bind(this);
        this._renderSeparator = this._renderSeparator.bind(this);
        this.dataSource = new ListView.DataSource({
            rowHasChanged: (r1, r2) => r1.key != r2.key,
        });
    }

    shouldComponentUpdate(nextProps){
        if (Immutable.is(nextProps.resource, this.props.resource)) {
            return false;
        } else {
            return true;
        }
    }
    _renderRow(rowData, sectionID, rowID) {
        let img = rowData.get('brand_ico');
        img = img.replace('{width}', cellWidth*2).replace('{height}', (cellWidth-10)*2).replace('{mode}',2);
        let brandName = rowData.get('brand_name');
        return (
            <TouchableOpacity style={styles.cell} onPress={()=>{this.props.onPressBrandItem && this.props.onPressBrandItem(rowData.toJS());}}>
                <Image style={styles.cellImg} source={{uri:img}} resizeMode={'contain'}/>
                <Text style={styles.cellTitle}>{brandName}</Text>
            </TouchableOpacity>
        );
    }
    _renderSeparator(sectionID, rowID, adjacentRowHighlighted) {
        return (
            <View key={'sep' + rowID} style={styles.separatorB}>
            </View>
        );
    }
    render() {
        let data = this.props.rowData;
        if (!data.length) {
            return null;
        }
        let title = this.props.title;
		return(
			<View style={styles.container}>
                <View style={styles.separator}/>
                <Text style={styles.title}>{title}</Text>
                <ListView
                    contentContainerStyle={styles.brandContainer}
                    enableEmptySections={true}
                    initialListSize={data.length}
                    dataSource={this.dataSource.cloneWithRows(data)}
                    renderRow={this._renderRow}
                    renderSeparator={this._renderSeparator}
                    scrollEnabled={false}
                    scrollsToTop={false}
                />
            </View>
		);
		return null;
    }
};


let {width, height} = Dimensions.get('window');
let cellWidth = (width-30)/4.0;

let styles = StyleSheet.create({
    container: {
        backgroundColor: '#f0f0f0',
    },
    separator: {
        width: width,
        height: 10,
        borderColor: '#e0e0e0',
        borderTopWidth: 0.5,
        borderBottomWidth: 0.5,
        backgroundColor: '#f0f0f0',
    },
    title: {
        color: 'black',
        fontSize: 16,
        paddingLeft: 15,
        width: width,
        height: 30,
        paddingTop: 7,
        backgroundColor: 'white'
    },
    brandContainer: {
        backgroundColor: 'white',
        borderColor: 'rgb(215, 215, 215)',
        borderBottomWidth: 0.5,
        flexDirection: 'row',
        flexWrap: 'wrap',
        width: width,
        paddingBottom: 5,
        paddingLeft: 15,
    },
    cell: {
        width: cellWidth,
        height: cellWidth,
        marginTop: 0,
        marginBottom: 10,
        backgroundColor: 'white',
        flexDirection: 'column',
    },
    separatorB: {
        width: 0.5,
        top: 0,
        height: cellWidth,
        backgroundColor: '#e0e0e0',
    },
    cellImg: {
        backgroundColor: 'white',
        marginLeft: 5,
        marginTop: 0,
        width: cellWidth-10,
        height: cellWidth - 10,
    },
    cellTitle: {
        backgroundColor: 'white',
        fontSize: 9,
        color: '#b0b0b0',
        textAlign: 'center',
        marginTop: 0,
        marginLeft: 0,
        marginRight: 0,
    }

});