@@ -2,7 +2,7 @@ import _ from 'lodash';
2
2
import PropTypes from 'prop-types' ;
3
3
import React from 'react' ;
4
4
import { StyleSheet , ViewPropTypes , TouchableOpacity } from 'react-native' ;
5
- import { Colors , BorderRadiuses } from '../../style' ;
5
+ import { Colors } from '../../style' ;
6
6
import { PureBaseComponent } from '../../commons' ;
7
7
import Badge , { BADGE_SIZES } from '../badge' ;
8
8
import View from '../view' ;
@@ -155,6 +155,39 @@ export default class Avatar extends PureBaseComponent {
155
155
this . styles = createStyles ( this . props ) ;
156
156
}
157
157
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
+
158
191
getStatusBadgeColor ( status ) {
159
192
switch ( status ) {
160
193
case Avatar . modes . AWAY :
@@ -222,9 +255,9 @@ export default class Avatar extends PureBaseComponent {
222
255
const { ribbonLabel, ribbonStyle, ribbonLabelStyle, customRibbon} = this . props ;
223
256
if ( ribbonLabel ) {
224
257
return customRibbon ? (
225
- < View style = { this . styles . customRibbon } > { customRibbon } </ View >
258
+ < View style = { this . getRibbonStyle ( ) } > { customRibbon } </ View >
226
259
) : (
227
- < View style = { [ this . styles . ribbon , ribbonStyle ] } >
260
+ < View style = { [ this . getRibbonStyle ( ) , this . styles . ribbon , ribbonStyle ] } >
228
261
< Text numberOfLines = { 1 } text100 white style = { [ ribbonLabelStyle ] } >
229
262
{ ribbonLabel }
230
263
</ Text >
@@ -246,17 +279,18 @@ export default class Avatar extends PureBaseComponent {
246
279
} = this . props ;
247
280
const hasImage = ! _ . isUndefined ( imageSource ) ;
248
281
const ImageContainer = animate ? AnimatedImage : Image ;
282
+
249
283
if ( hasImage ) {
250
284
return (
251
285
< ImageContainer
252
286
animate = { animate }
253
- style = { [ this . styles . image , imageStyle ] }
287
+ style = { [ this . getContainerStyle ( ) , StyleSheet . absoluteFillObject , imageStyle ] }
254
288
source = { imageSource }
255
289
onLoadStart = { onImageLoadStart }
256
290
onLoadEnd = { onImageLoadEnd }
257
291
onError = { onImageLoadError }
258
292
testID = { `${ testID } .image` }
259
- containerStyle = { this . styles . container }
293
+ containerStyle = { this . getContainerStyle ( ) }
260
294
{ ...imageProps }
261
295
/>
262
296
) ;
@@ -273,16 +307,20 @@ export default class Avatar extends PureBaseComponent {
273
307
onPress,
274
308
containerStyle,
275
309
children,
310
+ size,
276
311
testID,
277
312
} = this . props ;
278
313
const Container = onPress ? TouchableOpacity : View ;
279
314
const hasImage = ! _ . isUndefined ( imageSource ) ;
315
+ const fontSizeToImageSizeRatio = 0.32 ;
316
+ const fontSize = size * fontSizeToImageSizeRatio ;
317
+
280
318
return (
281
- < Container style = { [ this . styles . container , containerStyle ] } testID = { testID } onPress = { onPress } >
319
+ < Container style = { [ this . getContainerStyle ( ) , containerStyle ] } testID = { testID } onPress = { onPress } >
282
320
< View
283
- style = { [ this . styles . initialsContainer , { backgroundColor} , hasImage && this . styles . initialsContainerWithInset ] }
321
+ style = { [ this . getInitialsContainer ( ) , { backgroundColor} , hasImage && this . styles . initialsContainerWithInset ] }
284
322
>
285
- < Text numberOfLines = { 1 } style = { [ this . styles . initials , { color} ] } >
323
+ < Text numberOfLines = { 1 } style = { [ { fontSize } , this . styles . initials , { color} ] } >
286
324
{ label }
287
325
</ Text >
288
326
</ View >
@@ -295,68 +333,23 @@ export default class Avatar extends PureBaseComponent {
295
333
}
296
334
}
297
335
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} ) {
306
337
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
- } ,
320
338
initialsContainerWithInset : {
321
339
top : 1 ,
322
340
right : 1 ,
323
341
bottom : 1 ,
324
342
left : 1 ,
325
343
} ,
326
- /*eslint-disable*/
327
344
initials : {
328
- fontSize : size * fontSizeToImageSizeRatio ,
329
345
color : labelColor ,
330
346
backgroundColor : 'transparent' ,
331
347
} ,
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
- } ,
350
348
ribbon : {
351
- ...ribbonPosition ,
352
349
backgroundColor : Colors . blue30 ,
353
- borderRadius : BorderRadiuses . br100 ,
354
350
paddingHorizontal : 6 ,
355
351
paddingVertical : 3 ,
356
352
} ,
357
- customRibbon : {
358
- ...ribbonPosition ,
359
- } ,
360
353
} ) ;
361
354
362
355
return styles ;
0 commit comments