-
Notifications
You must be signed in to change notification settings - Fork 0
/
Avatar.js
97 lines (86 loc) · 2.42 KB
/
Avatar.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/* This is an Access Contact List example from https://aboutreact.com/ */
/* https://aboutreact.com/access-contact-list-react-native/ */
import React from "react";
import { Image, View, Text, StyleSheet } from "react-native";
import PropTypes from "prop-types";
const Avatar = (props) => {
renderImage = () => {
const { img, width, height, roundedImage } = props;
const { imageContainer, image } = styles;
const viewStyle = [imageContainer];
if (roundedImage)
viewStyle.push({ borderRadius: Math.round(width + height) / 2 });
return (
<View style={viewStyle}>
<Image style={image} source={img} />
</View>
);
};
renderPlaceholder = () => {
const { placeholder, width, height, roundedPlaceholder } = props;
const { placeholderContainer, placeholderText } = styles;
const viewStyle = [placeholderContainer];
if (roundedPlaceholder)
viewStyle.push({ borderRadius: Math.round(width + height) / 2 });
return (
<View style={viewStyle}>
<View style={viewStyle}>
<Text
adjustsFontSizeToFit
numberOfLines={1}
minimumFontScale={0.01}
style={[{ fontSize: Math.round(width) / 2 }, placeholderText]}
>
{placeholder}
</Text>
</View>
</View>
);
};
const { img, width, height } = props;
const { container } = styles;
return (
<View style={[container, props.style, { width, height }]}>
{img ? renderImage() : renderPlaceholder()}
</View>
);
}
const styles = StyleSheet.create({
container: {
width: "100%"
},
imageContainer: {
overflow: "hidden",
justifyContent: "center",
height: "100%"
},
image: {
flex: 1,
alignSelf: "stretch",
width: undefined,
height: undefined
},
placeholderContainer: {
alignItems: "center",
justifyContent: "center",
backgroundColor: "#dddddd",
height: "100%"
},
placeholderText: {
fontWeight: "700",
color: "#ffffff"
}
});
Avatar.propTypes = {
img: Image.propTypes.source,
placeholder: PropTypes.string,
width: PropTypes.number.isRequired,
height: PropTypes.number.isRequired,
roundedImage: PropTypes.bool,
roundedPlaceholder: PropTypes.bool
};
Avatar.defaultProps = {
roundedImage: true,
roundedPlaceholder: true
};
export default Avatar;