SPLikeCell.js 3.75 KB
'use strict';

import React from 'react';
import ReactNative from 'react-native';
import ImmutablePropTypes from 'react-immutable-proptypes';
import SlicedImage from '../../../common/components/SlicedImage';
import PropTypes from 'prop-types';

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

export default class SPLikeCell extends React.Component {

    static propTypes = {
        users: PropTypes.arrayOf(
            PropTypes.shape({
                nickName: PropTypes.string,
                headIcon: PropTypes.string,
                uid: PropTypes.number,
        })),
        like: PropTypes.string,
        browse: PropTypes.string,
        onPressLikeCell: PropTypes.func,
    };

    constructor(props) {
        super (props);

    }

    _renderLikeAvatar(avatars) {
        let users = this.props.users;
        if (users.length) {
            let space = 10;
            let left = (users.length - 1) * space;
            return (
                <View style={[styles.avatarPannel, {left}]}>
                    {users.map((item, i)=> {
                        let headStr = item && item.headIcon ? item.headIcon : '';
                        return (
                            <SlicedImage
                                key={i}
                                style={[styles.likeAvatar, {right: space * i}]}
                                source={{uri: headStr}}
                                defaultSource={require('../../images/avatar/avatar_default.png')}
                            />
                        );
                    })}
                </View>
            );
        }
    }

    render() {
        let {users, like, browse} = this.props;
        let likeTextMarginLeft = users.length > 0 ?10 - (users.length - 1) * 10 : 0;
        return (
            <TouchableOpacity onPress={() => {
                this.props.onPressLikeCell && this.props.onPressLikeCell()
            }}>
                <View style={styles.container}>
                    <View style={styles.leftLikeContainer}>
                        {this._renderLikeAvatar()}
                        <Text style={[styles.likeText, {left: likeTextMarginLeft}]}>{like + '人点赞'}</Text>
                    </View>
                    <View style={styles.rightLikeContainer}>
                        <Text style={styles.browseText}>{browse + '人看过'}</Text>
                    </View>
                    <View style={styles.topLine}/>
                    <View style={styles.bottomLine}/>
                </View>
            </TouchableOpacity>
        );
    }
}

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

let styles = StyleSheet.create({
    container: {
        backgroundColor: 'white',
        height: 51 ,
        flexDirection: 'row',
        justifyContent: 'space-between',
        flex: 1,
    },
    topLine: {
        position: 'absolute',
        left: 0,
        top: 0,
        width: width,
        height: 0.5,
        backgroundColor: '#e0e0e0',
    },
    bottomLine: {
        position: 'absolute',
        left:0,
        bottom: 0,
        width: width,
        height: 0.5,
        backgroundColor: '#e0e0e0',
    },
    leftLikeContainer: {
        flex: 1,
        flexDirection: 'row',
        alignItems: 'center',
        left: 15,
    },
    avatarPannel: {
        flexDirection: 'row-reverse',
        justifyContent: 'flex-start',
    },
    rightLikeContainer: {
        flex: 1,
        flexDirection: 'row',
        alignItems: 'center',
        justifyContent: 'flex-end',
        right: 15,
    },
    likeAvatar: {
        width: 30,
        height: 30,
        borderRadius: 15,
    },
    likeText: {
        fontSize: 14,
    },
    browseText: {
        fontSize: 14,
    },
});