|
| 1 | +import React from 'react'; |
| 2 | +import { View, Text, StyleSheet, Image, ViewPropTypes } from 'react-native'; |
| 3 | +import PropTypes from 'prop-types'; |
| 4 | +import { withGalio } from 'theme'; |
| 5 | + |
| 6 | +const Avatar = ({ |
| 7 | + source, |
| 8 | + size, |
| 9 | + label, |
| 10 | + labelColor, |
| 11 | + backgroundColor, |
| 12 | + labelStyle, |
| 13 | + imageProps, |
| 14 | + imageStyle, |
| 15 | + containerStyle, |
| 16 | + styles, |
| 17 | + theme, |
| 18 | +}) => { |
| 19 | + const getContainerStyle = () => { |
| 20 | + return { |
| 21 | + width: size, |
| 22 | + height: size, |
| 23 | + alignItems: 'center', |
| 24 | + justifyContent: 'center', |
| 25 | + borderRadius: size / 2, |
| 26 | + }; |
| 27 | + }; |
| 28 | + |
| 29 | + const getLabelContainerStyle = () => { |
| 30 | + return { |
| 31 | + ...StyleSheet.absoluteFillObject, |
| 32 | + alignItems: 'center', |
| 33 | + justifyContent: 'center', |
| 34 | + borderRadius: size / 2, |
| 35 | + }; |
| 36 | + }; |
| 37 | + |
| 38 | + const renderImage = () => { |
| 39 | + if (source) { |
| 40 | + return ( |
| 41 | + <Image |
| 42 | + style={[getContainerStyle(), StyleSheet.absoluteFillObject, imageStyle]} |
| 43 | + {...{ source }} |
| 44 | + {...imageProps} |
| 45 | + /> |
| 46 | + ); |
| 47 | + } |
| 48 | + return null; |
| 49 | + }; |
| 50 | + |
| 51 | + const labelContainerStyle = [ |
| 52 | + getLabelContainerStyle(), |
| 53 | + source && styles.labelContainerWithInset, |
| 54 | + { backgroundColor: backgroundColor || theme.COLORS.MUTED }, |
| 55 | + ]; |
| 56 | + |
| 57 | + const labelTextStyle = [ |
| 58 | + { fontSize: size * 0.32 }, |
| 59 | + styles.labelText, |
| 60 | + labelColor && { color: labelColor }, |
| 61 | + labelStyle || {}, |
| 62 | + ]; |
| 63 | + |
| 64 | + return ( |
| 65 | + <View style={[getContainerStyle(), containerStyle]}> |
| 66 | + <View style={labelContainerStyle}> |
| 67 | + {label && ( |
| 68 | + <Text numberOfLines={1} style={labelTextStyle}> |
| 69 | + {label} |
| 70 | + </Text> |
| 71 | + )} |
| 72 | + </View> |
| 73 | + {renderImage()} |
| 74 | + </View> |
| 75 | + ); |
| 76 | +}; |
| 77 | + |
| 78 | +Avatar.defaultProps = { |
| 79 | + size: 50, |
| 80 | +}; |
| 81 | + |
| 82 | +Avatar.propTypes = { |
| 83 | + label: PropTypes.string, |
| 84 | + labelColor: PropTypes.string, |
| 85 | + size: PropTypes.number, |
| 86 | + source: PropTypes.oneOfType([PropTypes.object, PropTypes.number]), |
| 87 | + backgroundColor: PropTypes.string, |
| 88 | + imageProps: PropTypes.object, |
| 89 | + imageStyle: PropTypes.oneOfType([PropTypes.object, PropTypes.array, PropTypes.number]), |
| 90 | + containerStyle: ViewPropTypes.style, |
| 91 | + styles: PropTypes.any, |
| 92 | + theme: PropTypes.any, |
| 93 | +}; |
| 94 | + |
| 95 | +const styles = theme => |
| 96 | + StyleSheet.create({ |
| 97 | + labelContainerWithInset: { |
| 98 | + top: 1, |
| 99 | + right: 1, |
| 100 | + bottom: 1, |
| 101 | + left: 1, |
| 102 | + }, |
| 103 | + labelText: { |
| 104 | + color: theme.COLORS.BLACK, |
| 105 | + backgroundColor: 'transparent', |
| 106 | + }, |
| 107 | + }); |
| 108 | + |
| 109 | +export default withGalio(Avatar, styles); |
0 commit comments