BrandFliter.js 3.29 KB
'use strict';

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

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


export default class BrandFliter 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,
        });
		this.state = {
            selectedIndex: this.props.selectID,
        };
    }

    shouldComponentUpdate(nextProps){

        if (Immutable.is(nextProps.resource, this.props.resource) && Immutable.is(nextProps.selectID, this.props.selectID)) {
            return false;
        } else {
            return true;
        }
    }

    _renderRow(rowData, sectionID, rowID) {

        let colorStyle = rowID == this.state.selectedIndex ? {color: '#444444'} : {color: '#b0b0b0'};

        return (
            <TouchableOpacity
                activeOpacity={1}
                onPress={() => {

                if (this.state.selectedIndex == rowID) {
                    return;
                }
                if (rowID != 3) {
                    this.setState({
                        selectedIndex: rowID,
                    });
                }
                this.props.onPressFilter && this.props.onPressFilter(rowID);
            }}>
                <View key={'row' + rowID} style={styles.rowContainer}>
                    <Text style={[styles.name, colorStyle]}>{rowData.name}</Text>
                </View>
            </TouchableOpacity>
        );
    }

    _renderSeparator(sectionID, rowID, adjacentRowHighlighted) {
        return (
            <View key={'sep' + rowID} style={styles.separator}>
            </View>
        );
    }

    render() {

        let {resource} = this.props;

        let brandsText = resource.get('brandsText');

        let res = brandsText.toJS();

        return (
            <View style={styles.container}>
                <ListView
                    contentContainerStyle={styles.contentContainer}
                    enableEmptySections={true}
                    dataSource={this.dataSource.cloneWithRows(res)}
                    renderRow={this._renderRow}
                    renderSeparator={this._renderSeparator}
                    scrollEnabled={false}
                    scrollsToTop={false}
                />
            </View>
        );
    }
}

let {width, height} = Dimensions.get('window');

let styles = StyleSheet.create({
    container: {
        marginLeft: -1,
        width: width + 2,
        height: 37,
        borderTopColor: 'transparent',
        borderBottomColor: '#e5e5e5',
        borderWidth: 0.5,
		backgroundColor:'white',
    },
    contentContainer: {
        flexDirection: 'row',
    },
    rowContainer: {
        flexDirection: 'row',
        justifyContent: 'center',
        alignItems: 'center',
        width: Math.ceil(width / 3),
        height: 37,
		backgroundColor:'white',
    },
    name: {
        color: '#b0b0b0',
    },
    separator: {
        width: 0.5,
        top: 37 / 4,
        height: 37 / 2,
        backgroundColor: '#e5e5e5',
    },
});