import React, { Children } from 'react' import { StyleSheet, RefreshControl, View, Text, Dimensions, FlatList, Image, TouchableOpacity, PixelRatio, ActivityIndicator, } from 'react-native' import PropTypes from 'prop-types' import Icon from './Iconfont/Iconfont' import { ScaleUtil } from '../utils/utils' import { ImageBaseUrl } from '../utils/fetch/Fetchx' import { createAction } from '../utils' const { width, height } = Dimensions.get('window') export default class Grid extends React.Component { constructor(props) { super(props) this.state = {} } static propTypes = { titleName: PropTypes.string, titleColor: PropTypes.string, titleIcon: PropTypes.string, nameKey: PropTypes.string, pictureKey: PropTypes.string, priceKey: PropTypes.string, keyext: PropTypes.string, data: PropTypes.array, shoppingCart: PropTypes.func, imageTouch: PropTypes.func, moreTouch: PropTypes.func, titleBool: PropTypes.bool, listRowBool: PropTypes.bool, homeLIstBool: PropTypes.bool, keyextstr: PropTypes.string, //暂时 SearchVal: PropTypes.object, //查询条件 shopListIds: PropTypes.array, } static defaultProps = { titleName: '热卖促销', titleColor: '#333', titleIcon: 'icon-icon-jianjinzhishiqi', data: [], nameKey: 'name', pictureKey: 'pictureUrl', priceKey: 'salePrice', keyext: 'code', titleBool: true, listRowBool: false, homeLIstBool: false, viewWid: { width: width / 2 - 4, // height: width / 2 + 81 }, imgWid: { width: width / 2 - 4, height: width / 2 - 4, }, shoppingCart: () => {}, imageTouch: () => {}, moreTouch: () => {}, SearchVal: {}, shopListIds: [], } _renderItemCol(item) { let gouwucheColor = '#666' if (this.props.shopListIds.indexOf(item.id) !== -1) { gouwucheColor = '#E70013' } // 首页显示 if (this.props.homeLIstBool) { return ( { this.props.imageTouch(item) }} > { this.props.imageTouch(item) }} > { item.goodsAttrVals&&item.goodsAttrVals.length?():"" } {item[this.props.nameKey]} {'型号:'+(item.model?(item.model.length>10?item.model.substr(0,10):item.model):"")} {'规格:'+(item.specification?(item.specification.split("/")[0]+"/"+item.basicUnitName):"")} {CURRENCY.currencySign} {ScaleUtil(item[this.props.priceKey] || item.salePrice)} { this.props.shoppingCart(item) }} > ) } else { // 非首页 --- 宫格 return ( { this.props.imageTouch(item) }} > {CURRENCY.currencySign} {ScaleUtil(item[this.props.priceKey] || item.salePrice)} {item[this.props.nameKey]} 编码:{item[this.props.codeKey]} {/* 库存:{ScaleUtil(item[this.props.invenKey])}(台) */} { this.props.shoppingCart(item) }} > ) } } _renderItemRow(item) { let gouwucheColor = '#666' if (this.props.shopListIds.indexOf(item.id) !== -1) { gouwucheColor = '#E70013' } // 列表形式 return ( { this.props.imageTouch(item) }} > { item.goodsAttrVals&&item.goodsAttrVals.length?():"" } {item[this.props.nameKey]} 编码:{item[this.props.codeKey]} {/* 库存:{ScaleUtil(item[this.props.invenKey])}(件) */} {'型号:'+(item.model?(item.model.length>10?item.model.substr(0,10):item.model):"")} {'规格:'+(item.specification?(item.specification.split("/")[0]+"/"+item.basicUnitName):"")} {/* {'型号:'+(item.model?(item.model.length>10?item.model.substr(0,10):item.model):"")} {'规格:'+(item.specification?(item.specification.split("/")[0]+"/"+item.basicUnitName):"")} */} {CURRENCY.currencySign} {ScaleUtil(item[this.props.priceKey] || item.salePrice)} { this.props.shoppingCart(item) }} > ) } // 底部 footerCom() { if (this.props.Parentprops.showFoot == 0) { return } else if (this.props.Parentprops.showFoot == 1) { return ( 没有更多数据 ) } else if (this.props.Parentprops.showFoot == 2) { return ( 正在加载更多数据... ) } } render() { return ( {this.props.titleBool ? ( { this.props.moreTouch() }} style={{ backgroundColor: '#FFF', flexDirection: 'row', justifyContent: 'space-between', padding: 10, paddingLeft: 20, }} > {this.props.imageSource ? ( ) : null} {this.props.titleName} {this.props.titleIcon ? ( ) : null} ) : null} {this.props.listRowBool ? ( // 列表形式 item[this.props.keyext]} keyExtractor={(item, index) => this.props.keyext ? item[this.props.keyext] + index : index } extraData={this.props} data={this.props.data} ListFooterComponent={this.footerCom.bind(this)} columnWrapperStyle={{ flexWrap: 'wrap' }} numColumns={2} renderItem={({ item }) => this._renderItemRow(item)} onEndReachedThreshold={0.1} onEndReached={e => this.props.onEndReached()} refreshControl={ this.onRefresh)或者通过bind来绑定this引用来调用方法 tintColor="red" title={ this.props.Parentprops.isRefreshing ? '刷新中....' : '下拉刷新' } /> } /> ) : this.props.homeLIstBool ? ( item[this.props.keyext]} keyExtractor={(item, index) => this.props.keyext ? item[this.props.keyext] + index : index } extraData={this.props.extraData} columnWrapperStyle={{ flexWrap: 'wrap', justifyContent: 'space-between', }} ListFooterComponent={ this.props.homeLIstBool ? null : this.footerCom.bind(this) } numColumns={2} data={this.props.data} renderItem={({ item }) => this._renderItemCol(item)} /> ) : ( item[this.props.keyext]} keyExtractor={(item, index) => this.props.keyext ? item[this.props.keyext] + index : index } extraData={this.props.extraData} columnWrapperStyle={{ flexWrap: 'wrap', justifyContent: 'space-between', }} ListFooterComponent={ this.props.homeLIstBool ? null : this.footerCom.bind(this) } numColumns={2} data={this.props.data} renderItem={({ item }) => this._renderItemCol(item)} onEndReachedThreshold={0.1} onEndReached={e => this.props.onEndReached()} refreshControl={ this.onRefresh)或者通过bind来绑定this引用来调用方法 tintColor="red" title={ this.props.Parentprops.isRefreshing ? '刷新中....' : '下拉刷新' } /> } /> )} ) } } const styles = StyleSheet.create({ footer: { flexDirection: 'row', height: 24, justifyContent: 'center', alignItems: 'center', marginBottom: 10, marginTop: 10, }, })