mirror of
https://github.com/zhigang1992/react-native-gifted-chat.git
synced 2026-05-28 23:51:12 +08:00
142 lines
3.2 KiB
JavaScript
142 lines
3.2 KiB
JavaScript
import React, { Component } from 'react';
|
|
import {
|
|
Image,
|
|
Text,
|
|
TouchableOpacity,
|
|
View,
|
|
} from 'react-native';
|
|
|
|
// TODO
|
|
// 3 words name initials
|
|
// handle only alpha numeric chars
|
|
|
|
class GiftedAvatar extends Component {
|
|
setAvatarColor() {
|
|
const userName = this.props.user.name || '';
|
|
const name = userName.toUpperCase().split(' ');
|
|
if (name.length === 1) {
|
|
this.avatarName = `${name[0].charAt(0)}`;
|
|
} else if (name.length > 1) {
|
|
this.avatarName = `${name[0].charAt(0)}${name[1].charAt(0)}`;
|
|
} else {
|
|
this.avatarName = '';
|
|
}
|
|
|
|
let sumChars = 0;
|
|
for(let i = 0; i < userName.length; i++) {
|
|
sumChars += userName.charCodeAt(i);
|
|
}
|
|
|
|
// inspired by https://github.com/wbinnssmith/react-user-avatar
|
|
// colors from https://flatuicolors.com/
|
|
const colors = [
|
|
'#2ecc71', // emerald
|
|
'#3498db', // peter river
|
|
'#8e44ad', // wisteria
|
|
'#e67e22', // carrot
|
|
'#e74c3c', // alizarin
|
|
'#1abc9c', // turquoise
|
|
'#2c3e50', // midnight blue
|
|
];
|
|
|
|
this.avatarColor = colors[sumChars % colors.length];
|
|
}
|
|
|
|
renderAvatar() {
|
|
if (typeof this.props.user.avatar === 'function') {
|
|
return this.props.user.avatar();
|
|
} else if (typeof this.props.user.avatar === 'string') {
|
|
return (
|
|
<Image
|
|
source={{uri: this.props.user.avatar}}
|
|
style={[defaultStyles.avatarStyle, this.props.avatarStyle]}
|
|
/>
|
|
);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
renderInitials() {
|
|
return (
|
|
<Text style={[defaultStyles.textStyle, this.props.textStyle]}>
|
|
{this.avatarName}
|
|
</Text>
|
|
);
|
|
}
|
|
|
|
render() {
|
|
if (!this.props.user.name && !this.props.user.avatar) {
|
|
// render placeholder
|
|
return (
|
|
<View style={[
|
|
defaultStyles.avatarStyle,
|
|
{backgroundColor: 'transparent'},
|
|
this.props.avatarStyle,
|
|
]}/>
|
|
)
|
|
}
|
|
if (this.props.user.avatar) {
|
|
// disabled={this.props.onPress ? false : true}
|
|
return (
|
|
<TouchableOpacity
|
|
onPress={() => {
|
|
const {onPress, ...other} = this.props;
|
|
this.props.onPress && this.props.onPress(other);
|
|
}}
|
|
>
|
|
{this.renderAvatar()}
|
|
</TouchableOpacity>
|
|
);
|
|
}
|
|
|
|
if (!this.avatarColor) {
|
|
this.setAvatarColor();
|
|
}
|
|
|
|
// disabled={this.props.onPress ? false : true}
|
|
return (
|
|
<TouchableOpacity
|
|
onPress={() => {
|
|
const {onPress, ...other} = this.props;
|
|
this.props.onPress && this.props.onPress(other);
|
|
}}
|
|
style={[
|
|
defaultStyles.avatarStyle,
|
|
{backgroundColor: this.avatarColor},
|
|
this.props.avatarStyle,
|
|
]}
|
|
>
|
|
{this.renderInitials()}
|
|
</TouchableOpacity>
|
|
);
|
|
}
|
|
}
|
|
|
|
GiftedAvatar.defaultProps = {
|
|
user: {
|
|
name: null,
|
|
avatar: null,
|
|
},
|
|
onPress: null,
|
|
avatarStyle: {},
|
|
textStyle: {},
|
|
};
|
|
|
|
const defaultStyles = {
|
|
avatarStyle: {
|
|
justifyContent: 'center',
|
|
alignItems: 'center',
|
|
width: 40,
|
|
height: 40,
|
|
borderRadius: 20,
|
|
},
|
|
textStyle: {
|
|
color: '#fff',
|
|
fontSize: 16,
|
|
backgroundColor: 'transparent',
|
|
fontWeight: '100',
|
|
},
|
|
};
|
|
|
|
export default GiftedAvatar;
|