vn-verdnaturachat/app/components/RoomItem.js

122 lines
2.5 KiB
JavaScript
Raw Normal View History

2017-08-09 01:40:55 +00:00
import React from 'react';
2017-08-13 03:19:14 +00:00
import { CachedImage } from 'react-native-img-cache';
import MaterialCommunityIcons from 'react-native-vector-icons/MaterialCommunityIcons';
2017-08-09 01:40:55 +00:00
import PropTypes from 'prop-types';
2017-08-09 18:01:54 +00:00
import { View, Text, StyleSheet } from 'react-native';
2017-08-09 01:40:55 +00:00
2017-08-13 03:19:14 +00:00
import avatarInitialsAndColor from '../utils/avatarInitialsAndColor';
2017-08-09 01:40:55 +00:00
const styles = StyleSheet.create({
2017-08-09 18:01:54 +00:00
container: {
flex: 1,
flexDirection: 'row',
paddingLeft: 16,
paddingRight: 56,
height: 56,
2017-08-09 18:01:54 +00:00
alignItems: 'center'
},
number: {
2017-08-09 20:08:50 +00:00
minWidth: 20,
2017-08-09 18:01:54 +00:00
borderRadius: 5,
backgroundColor: '#1d74f5',
2017-08-09 18:01:54 +00:00
color: '#fff',
textAlign: 'center',
overflow: 'hidden',
fontSize: 14,
right: 16,
marginLeft: 16,
position: 'absolute'
2017-08-09 18:01:54 +00:00
},
roomName: {
flexGrow: 1,
fontSize: 16,
color: '#444',
marginLeft: 16
},
2017-08-13 03:19:14 +00:00
iconContainer: {
height: 40,
width: 40,
borderRadius: 20,
overflow: 'hidden',
justifyContent: 'center',
alignItems: 'center'
},
icon: {
fontSize: 20,
2017-08-13 23:19:27 +00:00
color: '#fff'
2017-08-13 03:19:14 +00:00
},
avatar: {
width: 40,
height: 40,
position: 'absolute',
borderRadius: 20
2017-08-13 03:19:14 +00:00
},
avatarInitials: {
fontSize: 22,
color: '#ffffff'
2017-08-09 01:40:55 +00:00
}
});
export default class RoomItem extends React.PureComponent {
static propTypes = {
2017-08-13 03:19:14 +00:00
item: PropTypes.object.isRequired,
2017-08-13 23:45:47 +00:00
baseUrl: PropTypes.string
}
get icon() {
const icon = {
d: 'at',
c: 'pound',
p: 'lock',
l: 'account'
}[this.props.item.t];
2017-08-13 03:19:14 +00:00
if (!icon) {
return null;
}
2017-08-13 03:19:14 +00:00
const { name } = this.props.item;
2017-08-13 03:19:14 +00:00
if (this.props.item.t === 'd') {
const { initials, color } = avatarInitialsAndColor(name);
return (
<View style={[styles.iconContainer, { backgroundColor: color }]}>
<Text style={styles.avatarInitials}>{initials}</Text>
<CachedImage style={styles.avatar} source={{ uri: `${ this.props.baseUrl }/avatar/${ name }` }} />
</View>
);
}
const { color } = avatarInitialsAndColor(name);
2017-08-13 03:19:14 +00:00
return (
2017-08-13 23:19:27 +00:00
<View style={[styles.iconContainer, { backgroundColor: color }]}>
<MaterialCommunityIcons name={icon} style={styles.icon} />
2017-08-13 03:19:14 +00:00
</View>
);
2017-08-09 01:40:55 +00:00
}
2017-08-13 03:19:14 +00:00
2017-08-09 18:01:54 +00:00
renderNumber = (item) => {
if (item.unread) {
return (
<Text style={styles.number}>
{ item.unread }
</Text>
);
}
}
2017-08-09 01:40:55 +00:00
render() {
let extraSpace = {};
if (this.props.item.unread) {
extraSpace = { paddingRight: 92 };
}
2017-08-09 01:40:55 +00:00
return (
<View style={[styles.container, extraSpace]}>
{this.icon}
<Text style={styles.roomName} ellipsizeMode='tail' numberOfLines={1}>{ this.props.item.name }</Text>
2017-08-09 18:01:54 +00:00
{this.renderNumber(this.props.item)}
</View>
2017-08-09 01:40:55 +00:00
);
}
}