vn-verdnaturachat/app/containers/Avatar.js

76 lines
1.4 KiB
JavaScript
Raw Normal View History

2019-04-18 20:57:35 +00:00
import React from 'react';
import PropTypes from 'prop-types';
import { View } from 'react-native';
import FastImage from 'react-native-fast-image';
import Touchable from 'react-native-platform-touchable';
import { settings as RocketChatSettings } from '@rocket.chat/sdk';
import { avatarURL } from '../utils/avatar';
2019-04-18 20:57:35 +00:00
const Avatar = React.memo(({
text, size, baseUrl, borderRadius, style, avatar, type, children, userId, token, onPress
2019-04-18 20:57:35 +00:00
}) => {
const avatarStyle = {
width: size,
height: size,
borderRadius
};
2019-04-18 20:57:35 +00:00
if (!text && !avatar) {
return null;
}
const uri = avatarURL({
type, text, size, userId, token, avatar, baseUrl
});
2019-11-25 20:01:17 +00:00
let image = (
2019-04-18 20:57:35 +00:00
<FastImage
style={avatarStyle}
source={{
uri,
headers: RocketChatSettings.customHeaders,
2019-04-18 20:57:35 +00:00
priority: FastImage.priority.high
}}
/>
);
2019-11-25 20:01:17 +00:00
if (onPress) {
image = (
<Touchable onPress={onPress}>
2019-11-25 20:01:17 +00:00
{image}
</Touchable>
2019-11-25 20:01:17 +00:00
);
}
2019-04-18 20:57:35 +00:00
return (
<View style={[avatarStyle, style]}>
{image}
{children}
</View>
);
});
2019-04-18 20:57:35 +00:00
Avatar.propTypes = {
baseUrl: PropTypes.string.isRequired,
style: PropTypes.any,
2019-04-18 20:57:35 +00:00
text: PropTypes.string,
avatar: PropTypes.string,
size: PropTypes.number,
borderRadius: PropTypes.number,
type: PropTypes.string,
children: PropTypes.object,
userId: PropTypes.string,
2019-11-25 20:01:17 +00:00
token: PropTypes.string,
onPress: PropTypes.func
2019-04-18 20:57:35 +00:00
};
2018-12-05 20:52:08 +00:00
2019-04-18 20:57:35 +00:00
Avatar.defaultProps = {
text: '',
size: 25,
type: 'd',
borderRadius: 4
};
2019-04-18 20:57:35 +00:00
export default Avatar;