StockStats.js 4.82 KB
'use strict';

import React, {Component} from 'react';
import PlainTextSection from './PlainTextSection';
import Placeholder from './Placeholder';
import LoadMoreIndicator from './indicator/LoadMoreIndicator';
import LoadingIndicator from './indicator/LoadingIndicator';
import moment from 'moment';

import {
    StyleSheet,
    View,
    Text,
    ListView,
    Image,
    Dimensions,
    TouchableHighlight,
} from 'react-native';

export default class StockStats extends Component {

    constructor(props) {
        super (props);

        this.ds = new ListView.DataSource({
            rowHasChanged: (r1, r2) => !Immutable.is(r1, r2),
            sectionHeaderHasChanged: (s1, s2) => !Immutable.is(s1, s2),
        });

        this._renderRow = this._renderRow.bind(this);
    }

	_renderRow(rowData, sectionID, rowID) {
        switch (sectionID) {
            case 'SECTION_HEADER':
                return (
                    <View style={styles.container}>
                        <PlainTextSection content={rowData.section2} />
                        <Placeholder containerStyle={{borderBottomWidth: 0}}/>
                    </View>
                );


            case 'SECTION_TIME':
                return (
                    <View style={styles.container}>
                        <View style={styles.dateContainer}>
                            <Image source={require('../images/date.png')}/>
                            <Text style={styles.date}>{moment().subtract(1, 'days').format('YYYY年M月D日')}</Text>
                        </View>
                        <Placeholder containerStyle={{borderBottomWidth: 0}}/>
                    </View>
                );

            case 'SECTION_CONTENT':
                return (
                    <TouchableHighlight underlayColor={'white'} onPress={() => {}}>
                        <View style={styles.row}>
                            <Image source={{uri: rowData.imageUrl}} style={styles.thumb}/>
                            <View style={styles.detail}>
                                <Text style={styles.brand}>{rowData.brandName}</Text>
                                <Text style={styles.product}>{rowData.goodsName}</Text>
                                <Text style={styles.brand}>{'厂家编号:' + rowData.factoryCode + '  sku:' + rowData.productSku}</Text>
                                <Text style={styles.product}>{'进货价:' + rowData.purchasePrice + '元       库存:' + rowData.storeNums}</Text>
                            </View>
                        </View>
                    </TouchableHighlight>
                );
            default:
                return (
                    <Text>Error data source</Text>
                );
        }
	}

	render() {

        let isFetching = this.props.isFetching;

        let loadText = '暂无更多数据';
        if (!this.props.reachEnd) {
            loadText = isFetching ? '正在加载...' : '上拉加载更多';
        }

		return (
            <View style={styles.container}>
                <ListView
                    dataSource={this.ds.cloneWithRowsAndSections(this.props.section.toJS())}
                    renderRow={this._renderRow}
                    renderSeparator={(sectionID, rowID) => <View key={`${sectionID}-${rowID}`} style={styles.separator}/>}
                    onEndReached={this.props.requestData}
                    enableEmptySections={true}
                    removeClippedSubviews={false}
                    renderFooter={()=>{
                        return <LoadMoreIndicator
                                hidden={this.props.hideLoadingFooter}
                                loadingText={loadText}
                                animating={isFetching}
                            />
                    }}
                />
                {!this.props.hideLoadingFooter ? null : <LoadingIndicator isVisible={isFetching}/>}
            </View>
		);
	}

}

const styles = StyleSheet.create({
    container: {
        flex: 1,
    },
    dateContainer: {
        flexDirection: 'row',
        justifyContent: 'center',
        alignItems: 'center',
        height: 45,
        backgroundColor: 'white',
    },
    date: {
        fontSize: 14,
        color: '#444444',
        textAlign: 'center',
        marginLeft: 10,
    },
    separator: {
        height: 1,

    },
    row: {
        flexDirection: 'row',
        paddingLeft: 10,
        paddingRight: 10,
        paddingTop: 10,
        paddingBottom: 10,
        backgroundColor: 'white',
        alignItems: 'center',
    },
    detail: {
        flexDirection: 'column',
        justifyContent: 'center',
        flex: 1,
        paddingLeft: 20,

    },
    thumb: {
        width: 60,
        height: 80,
    },
    brand: {
        color: '#B0B0B0',
        fontSize: 12,
    },
    product: {
        color: '#444444',
        fontSize: 12,
    },
});