Skip to content

Commit 9ae5014

Browse files
ArnonZethanshar
authored andcommitted
Fix/Avatar - Refactor to enable dynamic prop change (#494)
* Refactored Avatar to enable dynamic prop change * Small fix
1 parent 98fb02c commit 9ae5014

File tree

1 file changed

+47
-54
lines changed

1 file changed

+47
-54
lines changed

src/components/avatar/index.js

Lines changed: 47 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import _ from 'lodash';
22
import PropTypes from 'prop-types';
33
import React from 'react';
44
import {StyleSheet, ViewPropTypes, TouchableOpacity} from 'react-native';
5-
import {Colors, BorderRadiuses} from '../../style';
5+
import {Colors} from '../../style';
66
import {PureBaseComponent} from '../../commons';
77
import Badge, {BADGE_SIZES} from '../badge';
88
import View from '../view';
@@ -155,6 +155,39 @@ export default class Avatar extends PureBaseComponent {
155155
this.styles = createStyles(this.props);
156156
}
157157

158+
getContainerStyle() {
159+
const {size} = this.props;
160+
161+
return {
162+
width: size,
163+
height: size,
164+
alignItems: 'center',
165+
justifyContent: 'center',
166+
borderRadius: size / 2,
167+
};
168+
}
169+
170+
getInitialsContainer() {
171+
const {size} = this.props;
172+
return {
173+
...StyleSheet.absoluteFillObject,
174+
alignItems: 'center',
175+
justifyContent: 'center',
176+
borderRadius: size / 2,
177+
};
178+
}
179+
180+
getRibbonStyle() {
181+
const {size} = this.props;
182+
183+
return {
184+
position: 'absolute',
185+
top: '10%',
186+
left: size / 1.7,
187+
borderRadius: size / 2,
188+
};
189+
}
190+
158191
getStatusBadgeColor(status) {
159192
switch (status) {
160193
case Avatar.modes.AWAY:
@@ -222,9 +255,9 @@ export default class Avatar extends PureBaseComponent {
222255
const {ribbonLabel, ribbonStyle, ribbonLabelStyle, customRibbon} = this.props;
223256
if (ribbonLabel) {
224257
return customRibbon ? (
225-
<View style={this.styles.customRibbon}>{customRibbon}</View>
258+
<View style={this.getRibbonStyle()}>{customRibbon}</View>
226259
) : (
227-
<View style={[this.styles.ribbon, ribbonStyle]}>
260+
<View style={[this.getRibbonStyle(), this.styles.ribbon, ribbonStyle]}>
228261
<Text numberOfLines={1} text100 white style={[ribbonLabelStyle]}>
229262
{ribbonLabel}
230263
</Text>
@@ -246,17 +279,18 @@ export default class Avatar extends PureBaseComponent {
246279
} = this.props;
247280
const hasImage = !_.isUndefined(imageSource);
248281
const ImageContainer = animate ? AnimatedImage : Image;
282+
249283
if (hasImage) {
250284
return (
251285
<ImageContainer
252286
animate={animate}
253-
style={[this.styles.image, imageStyle]}
287+
style={[this.getContainerStyle(), StyleSheet.absoluteFillObject, imageStyle]}
254288
source={imageSource}
255289
onLoadStart={onImageLoadStart}
256290
onLoadEnd={onImageLoadEnd}
257291
onError={onImageLoadError}
258292
testID={`${testID}.image`}
259-
containerStyle={this.styles.container}
293+
containerStyle={this.getContainerStyle()}
260294
{...imageProps}
261295
/>
262296
);
@@ -273,16 +307,20 @@ export default class Avatar extends PureBaseComponent {
273307
onPress,
274308
containerStyle,
275309
children,
310+
size,
276311
testID,
277312
} = this.props;
278313
const Container = onPress ? TouchableOpacity : View;
279314
const hasImage = !_.isUndefined(imageSource);
315+
const fontSizeToImageSizeRatio = 0.32;
316+
const fontSize = size * fontSizeToImageSizeRatio;
317+
280318
return (
281-
<Container style={[this.styles.container, containerStyle]} testID={testID} onPress={onPress}>
319+
<Container style={[this.getContainerStyle(), containerStyle]} testID={testID} onPress={onPress}>
282320
<View
283-
style={[this.styles.initialsContainer, {backgroundColor}, hasImage && this.styles.initialsContainerWithInset]}
321+
style={[this.getInitialsContainer(), {backgroundColor}, hasImage && this.styles.initialsContainerWithInset]}
284322
>
285-
<Text numberOfLines={1} style={[this.styles.initials, {color}]}>
323+
<Text numberOfLines={1} style={[{fontSize}, this.styles.initials, {color}]}>
286324
{label}
287325
</Text>
288326
</View>
@@ -295,68 +333,23 @@ export default class Avatar extends PureBaseComponent {
295333
}
296334
}
297335

298-
function createStyles({size, labelColor}) {
299-
const borderRadius = size / 2;
300-
const fontSizeToImageSizeRatio = 0.32;
301-
const ribbonPosition = {
302-
position: 'absolute',
303-
top: '10%',
304-
left: size / 1.7,
305-
};
336+
function createStyles({labelColor}) {
306337
const styles = StyleSheet.create({
307-
container: {
308-
alignItems: 'center',
309-
justifyContent: 'center',
310-
width: size,
311-
height: size,
312-
borderRadius,
313-
},
314-
initialsContainer: {
315-
...StyleSheet.absoluteFillObject,
316-
alignItems: 'center',
317-
justifyContent: 'center',
318-
borderRadius,
319-
},
320338
initialsContainerWithInset: {
321339
top: 1,
322340
right: 1,
323341
bottom: 1,
324342
left: 1,
325343
},
326-
/*eslint-disable*/
327344
initials: {
328-
fontSize: size * fontSizeToImageSizeRatio,
329345
color: labelColor,
330346
backgroundColor: 'transparent',
331347
},
332-
/*eslint-enable*/
333-
defaultImage: {
334-
width: size,
335-
height: size,
336-
borderRadius,
337-
},
338-
image: {
339-
...StyleSheet.absoluteFillObject,
340-
position: 'absolute',
341-
width: size,
342-
height: size,
343-
borderRadius,
344-
},
345-
fixAbsolutePosition: {
346-
position: undefined,
347-
left: undefined,
348-
bottom: undefined,
349-
},
350348
ribbon: {
351-
...ribbonPosition,
352349
backgroundColor: Colors.blue30,
353-
borderRadius: BorderRadiuses.br100,
354350
paddingHorizontal: 6,
355351
paddingVertical: 3,
356352
},
357-
customRibbon: {
358-
...ribbonPosition,
359-
},
360353
});
361354

362355
return styles;

0 commit comments

Comments
 (0)