2017-09-01 19:17:53 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2017-09-01 19:42:50 +00:00
|
|
|
import { StyleSheet, Text, View } from 'react-native';
|
2017-09-01 19:17:53 +00:00
|
|
|
import { CachedImage } from 'react-native-img-cache';
|
|
|
|
import avatarInitialsAndColor from '../../utils/avatarInitialsAndColor';
|
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
2017-09-01 19:42:50 +00:00
|
|
|
iconContainer: {
|
|
|
|
overflow: 'hidden',
|
|
|
|
justifyContent: 'center',
|
|
|
|
alignItems: 'center'
|
2017-09-01 19:17:53 +00:00
|
|
|
},
|
|
|
|
avatar: {
|
|
|
|
position: 'absolute'
|
|
|
|
},
|
|
|
|
avatarInitials: {
|
|
|
|
color: '#ffffff'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-09-01 19:42:50 +00:00
|
|
|
class Avatar extends React.PureComponent {
|
2017-09-01 19:17:53 +00:00
|
|
|
render() {
|
2017-09-01 19:42:50 +00:00
|
|
|
const { text = '', size = 25, baseUrl = this.props.baseUrl,
|
|
|
|
borderRadius = 5, style, avatar } = this.props;
|
|
|
|
const { initials, color } = avatarInitialsAndColor(`${ text }`);
|
2017-09-01 19:17:53 +00:00
|
|
|
return (
|
2017-09-01 19:42:50 +00:00
|
|
|
<View style={[styles.iconContainer, {
|
|
|
|
backgroundColor: color,
|
|
|
|
width: size,
|
|
|
|
height: size,
|
|
|
|
borderRadius
|
|
|
|
}, style]}
|
|
|
|
>
|
|
|
|
<Text style={[styles.avatarInitials, { fontSize: size / 2 }]}>{initials}</Text>
|
2017-09-01 20:00:31 +00:00
|
|
|
{(avatar || baseUrl) && <CachedImage
|
|
|
|
style={[styles.avatar, {
|
|
|
|
width: size,
|
|
|
|
height: size
|
|
|
|
}]}
|
2017-09-01 19:42:50 +00:00
|
|
|
source={{ uri: avatar || `${ baseUrl }/avatar/${ text }` }}
|
|
|
|
/>}
|
|
|
|
</View>);
|
2017-09-01 19:17:53 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-01 19:42:50 +00:00
|
|
|
|
|
|
|
Avatar.propTypes = {
|
|
|
|
style: PropTypes.object,
|
|
|
|
baseUrl: PropTypes.string,
|
|
|
|
text: PropTypes.string.isRequired,
|
|
|
|
avatar: PropTypes.string,
|
|
|
|
size: PropTypes.number,
|
|
|
|
borderRadius: PropTypes.number
|
|
|
|
};
|
|
|
|
export default Avatar;
|