Avatar refactored
This commit is contained in:
parent
47991da302
commit
dae4b7ea69
|
@ -2,6 +2,7 @@ import React from 'react';
|
||||||
import PropTypes from 'prop-types';
|
import PropTypes from 'prop-types';
|
||||||
import { StyleSheet, Text, View } from 'react-native';
|
import { StyleSheet, Text, View } from 'react-native';
|
||||||
import { CachedImage } from 'react-native-img-cache';
|
import { CachedImage } from 'react-native-img-cache';
|
||||||
|
import MaterialCommunityIcons from 'react-native-vector-icons/MaterialCommunityIcons';
|
||||||
import avatarInitialsAndColor from '../utils/avatarInitialsAndColor';
|
import avatarInitialsAndColor from '../utils/avatarInitialsAndColor';
|
||||||
|
|
||||||
const styles = StyleSheet.create({
|
const styles = StyleSheet.create({
|
||||||
|
@ -21,7 +22,7 @@ const styles = StyleSheet.create({
|
||||||
class Avatar extends React.PureComponent {
|
class Avatar extends React.PureComponent {
|
||||||
render() {
|
render() {
|
||||||
const {
|
const {
|
||||||
text = '', size = 25, baseUrl, borderRadius = 4, style, avatar
|
text = '', size = 25, baseUrl, borderRadius = 4, style, avatar, type = 'd'
|
||||||
} = this.props;
|
} = this.props;
|
||||||
const { initials, color } = avatarInitialsAndColor(`${ text }`);
|
const { initials, color } = avatarInitialsAndColor(`${ text }`);
|
||||||
|
|
||||||
|
@ -42,6 +43,7 @@ class Avatar extends React.PureComponent {
|
||||||
borderRadius
|
borderRadius
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (type === 'd') {
|
||||||
const uri = avatar || `${ baseUrl }/avatar/${ text }`;
|
const uri = avatar || `${ baseUrl }/avatar/${ text }`;
|
||||||
const image = (avatar || baseUrl) && (
|
const image = (avatar || baseUrl) && (
|
||||||
<CachedImage
|
<CachedImage
|
||||||
|
@ -49,13 +51,25 @@ class Avatar extends React.PureComponent {
|
||||||
source={{ uri }}
|
source={{ uri }}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<View style={[styles.iconContainer, iconContainerStyle, style]}>
|
<View style={[styles.iconContainer, iconContainerStyle, style]}>
|
||||||
<Text style={[styles.avatarInitials, avatarInitialsStyle]}>{initials}</Text>
|
<Text style={[styles.avatarInitials, avatarInitialsStyle]}>{initials}</Text>
|
||||||
{image}
|
{image}
|
||||||
</View>);
|
</View>);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const icon = {
|
||||||
|
c: 'pound',
|
||||||
|
p: 'lock',
|
||||||
|
l: 'account'
|
||||||
|
}[type];
|
||||||
|
|
||||||
|
return (
|
||||||
|
<View style={[styles.iconContainer, iconContainerStyle, style]}>
|
||||||
|
<MaterialCommunityIcons name={icon} style={[styles.avatarInitials, avatarInitialsStyle]} />
|
||||||
|
</View>
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Avatar.propTypes = {
|
Avatar.propTypes = {
|
||||||
|
@ -64,6 +78,7 @@ Avatar.propTypes = {
|
||||||
text: PropTypes.string.isRequired,
|
text: PropTypes.string.isRequired,
|
||||||
avatar: PropTypes.string,
|
avatar: PropTypes.string,
|
||||||
size: PropTypes.number,
|
size: PropTypes.number,
|
||||||
borderRadius: PropTypes.number
|
borderRadius: PropTypes.number,
|
||||||
|
type: PropTypes.string
|
||||||
};
|
};
|
||||||
export default Avatar;
|
export default Avatar;
|
||||||
|
|
|
@ -1,10 +1,8 @@
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
import moment from 'moment';
|
import moment from 'moment';
|
||||||
import MaterialCommunityIcons from 'react-native-vector-icons/MaterialCommunityIcons';
|
|
||||||
import PropTypes from 'prop-types';
|
import PropTypes from 'prop-types';
|
||||||
import { View, Text, StyleSheet, TouchableOpacity } from 'react-native';
|
import { View, Text, StyleSheet, TouchableOpacity } from 'react-native';
|
||||||
import Avatar from '../containers/Avatar';
|
import Avatar from '../containers/Avatar';
|
||||||
import avatarInitialsAndColor from '../utils/avatarInitialsAndColor';
|
|
||||||
|
|
||||||
const styles = StyleSheet.create({
|
const styles = StyleSheet.create({
|
||||||
container: {
|
container: {
|
||||||
|
@ -49,22 +47,6 @@ const styles = StyleSheet.create({
|
||||||
fontSize: 10,
|
fontSize: 10,
|
||||||
height: 10,
|
height: 10,
|
||||||
color: '#888'
|
color: '#888'
|
||||||
},
|
|
||||||
iconContainer: {
|
|
||||||
height: 40,
|
|
||||||
width: 40,
|
|
||||||
borderRadius: 4,
|
|
||||||
overflow: 'hidden',
|
|
||||||
justifyContent: 'center',
|
|
||||||
alignItems: 'center'
|
|
||||||
},
|
|
||||||
icon: {
|
|
||||||
fontSize: 20,
|
|
||||||
color: '#fff'
|
|
||||||
},
|
|
||||||
avatarInitials: {
|
|
||||||
fontSize: 20,
|
|
||||||
color: '#ffffff'
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -84,30 +66,7 @@ export default class RoomItem extends React.PureComponent {
|
||||||
|
|
||||||
get icon() {
|
get icon() {
|
||||||
const { type, name, baseUrl } = this.props;
|
const { type, name, baseUrl } = this.props;
|
||||||
|
return <Avatar text={name} baseUrl={baseUrl} size={40} type={type} />;
|
||||||
const icon = {
|
|
||||||
d: 'at',
|
|
||||||
c: 'pound',
|
|
||||||
p: 'lock',
|
|
||||||
l: 'account'
|
|
||||||
}[type];
|
|
||||||
|
|
||||||
if (!icon) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (type === 'd') {
|
|
||||||
return (
|
|
||||||
<Avatar text={name} baseUrl={baseUrl} size={40} />
|
|
||||||
);
|
|
||||||
}
|
|
||||||
const { color } = avatarInitialsAndColor(name);
|
|
||||||
|
|
||||||
return (
|
|
||||||
<View style={[styles.iconContainer, { backgroundColor: color }]}>
|
|
||||||
<MaterialCommunityIcons name={icon} style={styles.icon} />
|
|
||||||
</View>
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
renderNumber = (unread, userMentions) => {
|
renderNumber = (unread, userMentions) => {
|
||||||
|
|
|
@ -57,11 +57,7 @@ export default class extends React.Component {
|
||||||
|
|
||||||
isDirect = () => this.state.room && this.state.room.t === 'd';
|
isDirect = () => this.state.room && this.state.room.t === 'd';
|
||||||
|
|
||||||
renderLeft() {
|
renderLeft = () => <HeaderBackButton onPress={() => this.props.navigation.goBack(null)} tintColor='#292E35' />;
|
||||||
return (
|
|
||||||
<HeaderBackButton onPress={() => this.props.navigation.goBack(null)} tintColor='#292E35' />
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
renderTitle() {
|
renderTitle() {
|
||||||
if (!this.state.roomName) {
|
if (!this.state.roomName) {
|
||||||
|
@ -78,6 +74,7 @@ export default class extends React.Component {
|
||||||
size={24}
|
size={24}
|
||||||
style={{ marginRight: 5 }}
|
style={{ marginRight: 5 }}
|
||||||
baseUrl={this.props.baseUrl}
|
baseUrl={this.props.baseUrl}
|
||||||
|
type={this.state.room.t}
|
||||||
/>
|
/>
|
||||||
<View style={{ flexDirection: 'column' }}>
|
<View style={{ flexDirection: 'column' }}>
|
||||||
<Text style={styles.title}>{this.state.roomName}</Text>
|
<Text style={styles.title}>{this.state.roomName}</Text>
|
||||||
|
|
Loading…
Reference in New Issue