forked from iamshadmirza/react-native-design-system
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAvatar.js
128 lines (120 loc) · 3.34 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import React from 'react';
import { TouchableOpacity, TouchableNativeFeedback, Platform, View, Text, Image, StyleSheet } from 'react-native';
import PropTypes from 'prop-types';
import Feather from 'react-native-vector-icons/Feather';
import { useThemeContext } from '../util/ThemeProvider';
const getContainerStyle = ({ theme, source, square, rounded, size }) => {
const avatarStyle = [styles.container];
avatarStyle.push({
backgroundColor: '#f4f4f4',
padding: theme.size[size],
width: theme.avatarSize[size],
height: theme.avatarSize[size],
borderRadius: theme.avatarSize[size] * 2,
});
if (source) {
avatarStyle.push({
padding: 0,
});
}
if (square) {
avatarStyle.push({
borderRadius: 0,
});
}
if (rounded) {
avatarStyle.push({
borderRadius: 10,
});
}
return avatarStyle;
};
const getEditIconStyle = ({ theme, size }) => {
const iconStyle = [styles.editView, {
width: theme.avatarSize[size] / 4,
height: theme.avatarSize[size] / 4,
borderRadius: theme.avatarSize[size] / 8,
backgroundColor: theme.brandColor.disabled,
}];
return iconStyle;
};
const getTitleStyle = ({ theme, size }) => {
return {
fontWeight: '600',
fontSize: theme.avatarSize[size] / 4,
color: theme.textColor.disabled,
};
};
const Avatar = (props) => {
const theme = useThemeContext();
const TouchableElement =
Platform.OS === 'android' ? TouchableNativeFeedback : TouchableOpacity;
return (
<View style={StyleSheet.flatten([styles.propView, { width: theme.avatarSize[props.size] }])}>
<TouchableElement disabled={!props.editable} {...props}>
<View style={StyleSheet.flatten([getContainerStyle({ ...props, theme }), props.style])}>
{props.source ?
<Image
source={props.source}
resizeMode="cover"
style={styles.image}
/> :
<Text numberOfLines={1} style={StyleSheet.flatten([getTitleStyle({ ...props, theme }), props.textStyle])}>
{props.title}
</Text>
}
</View>
</TouchableElement>
{props.editable &&
<View style={StyleSheet.flatten([getEditIconStyle({ ...props, theme }), props.editIconStyle])}>
<Feather
name="edit-2"
size={theme.avatarSize[props.size] / 8}
color={props.editIconColor || theme.textColor.disabled}
/>
</View>}
</View>
);
};
Avatar.propTypes = {
style: PropTypes.object,
textStyle: PropTypes.object,
title: PropTypes.string,
source: PropTypes.object,
editable: PropTypes.bool,
onPress: PropTypes.func,
size: PropTypes.oneOf(['xxsmall', 'xsmall', 'small', 'medium', 'large', 'xlarge', 'xxlarge']),
square: PropTypes.bool,
rounded: PropTypes.bool,
editIconStyle: PropTypes.object,
editIconColor: PropTypes.string,
};
Avatar.defaultProps = {
title: 'MD',
editable: false,
size: 'medium',
};
const styles = StyleSheet.create({
container: {
overflow: 'hidden',
justifyContent: 'center',
alignItems: 'center',
elevation: 1,
},
propView: {
backgroundColor: 'transparent',
},
image: {
width: '100%',
height: '100%',
},
editView: {
position: 'absolute',
right: 0,
bottom: 0,
justifyContent: 'center',
alignItems: 'center',
elevation: 1,
},
});
export default Avatar;