|
1 | 1 | import * as React from 'react';
|
2 |
| -import { Image, ImageProps } from 'react-native'; |
| 2 | +import { Image, ImageProps, View } from 'react-native'; |
3 | 3 |
|
| 4 | +import styles from './styles'; |
4 | 5 | import {
|
5 | 6 | CommonSingleAvatarProps,
|
6 | 7 | SingleAvatarOnlySpecificProps,
|
@@ -28,13 +29,10 @@ const useAvatarStyle = (props: Pick<SingleAvatarProps, 'size' | 'style'>) => {
|
28 | 29 | () => [
|
29 | 30 | props.style,
|
30 | 31 | {
|
| 32 | + aspectRatio: 1, |
31 | 33 | backgroundColor: theme.ui.background.card,
|
32 |
| - borderColor: theme.palette.grey.white, |
33 |
| - borderRadius: AvatarSizes[props.size] / 2 + UI_SIZES.border.small * 2, |
34 |
| - borderWidth: UI_SIZES.border.small, |
35 |
| - height: AvatarSizes[props.size] + UI_SIZES.border.small * 2, |
36 |
| - margin: -UI_SIZES.border.small * 2, |
37 |
| - width: AvatarSizes[props.size] + UI_SIZES.border.small * 2, |
| 34 | + borderRadius: AvatarSizes[props.size] / 2, |
| 35 | + width: AvatarSizes[props.size], |
38 | 36 | },
|
39 | 37 | ],
|
40 | 38 | [props.size, props.style],
|
@@ -125,8 +123,39 @@ export function SingleAvatar(props: SingleAvatarProps) {
|
125 | 123 | setError(true);
|
126 | 124 | }, []);
|
127 | 125 |
|
| 126 | + const sizeValue = AvatarSizes[props.size]; |
| 127 | + const sizeContainer = React.useMemo( |
| 128 | + () => (props.border ? sizeValue + UI_SIZES.border.small * 2 : sizeValue), |
| 129 | + [props.border, sizeValue], |
| 130 | + ); |
128 | 131 | const computedStyle = useAvatarStyle(props);
|
129 | 132 | const imageSource = useAvatarImage(props as SingleAvatarOnlySpecificProps, error);
|
130 | 133 |
|
131 |
| - return <Image style={computedStyle} source={imageSource} onError={onError} {...otherProps} />; |
| 134 | + const sizeBorderStyles: any = React.useMemo( |
| 135 | + () => ({ |
| 136 | + borderRadius: sizeContainer / 2, |
| 137 | + width: sizeContainer, |
| 138 | + }), |
| 139 | + [sizeContainer], |
| 140 | + ); |
| 141 | + |
| 142 | + const sizeContainerStyles: any = React.useMemo( |
| 143 | + () => ({ |
| 144 | + height: sizeContainer, |
| 145 | + width: sizeContainer, |
| 146 | + }), |
| 147 | + [sizeContainer], |
| 148 | + ); |
| 149 | + |
| 150 | + const renderBorder = React.useCallback(() => { |
| 151 | + if (!props.border) return null; |
| 152 | + return <View style={[styles.border, sizeBorderStyles]} />; |
| 153 | + }, [props.border, sizeBorderStyles]); |
| 154 | + |
| 155 | + return ( |
| 156 | + <View style={[styles.container, sizeContainerStyles]}> |
| 157 | + {renderBorder()} |
| 158 | + <Image style={computedStyle} source={imageSource} onError={onError} {...otherProps} /> |
| 159 | + </View> |
| 160 | + ); |
132 | 161 | }
|
0 commit comments