Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
106 lines (85 loc) · 2.13 KB
/
Copy pathindex.js
File metadata and controls
106 lines (85 loc) · 2.13 KB
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
98
99
100
101
102
103
104
105
106
import React, { Component } from 'react';
import { View, Image, Text } from 'react-native';
import initials from 'initials';
import contrast from 'contrast';
// from https://flatuicolors.com/
const defaultColors = [
'#2ecc71', // emerald
'#3498db', // peter river
'#8e44ad', // wisteria
'#e67e22', // carrot
'#e74c3c', // alizarin
'#1abc9c', // turquoise
'#2c3e50', // midnight blue
];
function sumChars(str) {
let sum = 0;
for (let i = 0; i < str.length; i++) {
sum += str.charCodeAt(i);
}
return sum;
}
class UserAvatar extends Component {
render() {
let {
src,
name,
color,
textColor = '#fff',
colors = defaultColors,
fontDecrease,
size,
style,
defaultName,
radius = 0.5
} = this.props;
if (!fontDecrease) fontDecrease = 2.5;
if (!name) throw new Error('Avatar requires a name');
if(typeof size !== 'number') size = parseInt(size);
let abbr = initials(name);
if(!abbr) abbr = defaultName;
if(isNaN(radius)) radius = 0.5
const borderRadius = size * radius;
const imageStyle = {
borderRadius
};
const innerStyle = {
borderRadius,
borderWidth: 1,
borderColor: 'transparent',
justifyContent: 'center',
alignItems: 'center'
};
if (size) {
imageStyle.width = innerStyle.width = size;
imageStyle.height = innerStyle.height = size;
}
let inner, classes;
if (src) {
const props = {
style: imageStyle,
source: {uri: src}
}
inner = React.createElement( this.props.component || Image, props )
} else {
let background;
if (color) {
background = color;
} else {
// pick a deterministic color from the list
let i = sumChars(name) % colors.length;
background = colors[i];
}
innerStyle.backgroundColor = background;
inner = <Text style={{ fontSize: size / fontDecrease, color: textColor }}>{abbr}</Text>
}
return (
<View>
<View style={[innerStyle, style]}>
{inner}
</View>
</View>
)
}
}
module.exports = UserAvatar;