Skip to content

Commit a430f21

Browse files
ArnonZethanshar
authored andcommitted
Feat/Avatar - support multiple Badge positions (#511)
* Added 2 remaning badge positionings * Refactored badge positioning logic to cater multiple positions * Added example for all Badge positions
1 parent bd19394 commit a430f21

File tree

2 files changed

+12
-15
lines changed

2 files changed

+12
-15
lines changed

demo/src/screens/componentScreens/AvatarsScreen.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,18 @@ const examples = [
2626
'https://lh3.googleusercontent.com/-cw77lUnOvmI/AAAAAAAAAAI/AAAAAAAAAAA/WMNck32dKbc/s181-c/104220521160525129167.jpg',
2727
},
2828
badgeProps: {size: 'pimpleBig', backgroundColor: Colors.yellow30},
29+
badgePosition: 'BOTTOM_RIGHT',
2930
},
3031

3132
{
32-
title: 'Smaller size',
33+
title: 'Smaller size, Badge ("offline")',
3334
size: 40,
3435
imageSource: {
3536
uri:
3637
'https://lh3.googleusercontent.com/-CMM0GmT5tiI/AAAAAAAAAAI/AAAAAAAAAAA/-o9gKbC6FVo/s181-c/111308920004613908895.jpg',
3738
},
39+
badgeProps: {size: 'pimpleBig', backgroundColor: Colors.dark50},
40+
badgePosition: 'BOTTOM_LEFT',
3841
},
3942
{
4043
title: 'Image with fade in animation',
@@ -44,14 +47,14 @@ const examples = [
4447
imageSource: {uri: 'https://static.pexels.com/photos/60628/flower-garden-blue-sky-hokkaido-japan-60628.jpeg'},
4548
},
4649
{
47-
title: 'Big pimple, bottom right',
50+
title: 'Big pimple',
4851
size: 70,
4952
backgroundColor: 'red',
5053
imageSource: {
5154
uri: 'https://randomuser.me/api/portraits/women/24.jpg',
5255
},
5356
badgeProps: {size: 'pimpleHuge', borderWidth: 0, backgroundColor: onlineColor},
54-
badgePosition: 'BOTTOM_RIGHT',
57+
badgePosition: 'TOP_LEFT',
5558
},
5659
{
5760
title: 'Icon badge',

src/components/avatar/index.js

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ export const STATUS_MODES = {
2424

2525
export const BADGE_POSITIONS = {
2626
TOP_RIGHT: 'TOP_RIGHT',
27+
TOP_LEFT: 'TOP_LEFT',
2728
BOTTOM_RIGHT: 'BOTTOM_RIGHT',
29+
BOTTOM_LEFT: 'BOTTOM_LEFT'
2830
};
2931

3032
const DEFAULT_BADGE_SIZE = 'pimpleBig';
@@ -59,7 +61,7 @@ export default class Avatar extends PureBaseComponent {
5961
animate: PropTypes.bool,
6062
/**
6163
* Background color for Avatar
62-
*/
64+
*/
6365
backgroundColor: PropTypes.string,
6466
/**
6567
* Badge location on Avatar
@@ -220,18 +222,10 @@ export default class Avatar extends PureBaseComponent {
220222
const x = Math.sqrt(radius ** 2 * 2);
221223
const y = x - radius;
222224
const shift = Math.sqrt(y ** 2 / 2) - (BADGE_SIZES[this.getBadgeSize()] + this.getBadgeBorderWidth() * 2) / 2;
225+
const badgeLocation = _.split(_.toLower(badgePosition), '_', 2)
226+
const badgeAlignment = {position: 'absolute', [badgeLocation[0]]: shift, [badgeLocation[1]]: shift}
223227

224-
let badgeAlignment = {};
225-
switch (badgePosition) {
226-
case BADGE_POSITIONS.BOTTOM_RIGHT:
227-
badgeAlignment = {bottom: shift, right: shift};
228-
break;
229-
case BADGE_POSITIONS.TOP_RIGHT:
230-
default:
231-
badgeAlignment = {top: shift, right: shift};
232-
break;
233-
}
234-
return {position: 'absolute', ...badgeAlignment};
228+
return badgeAlignment;
235229
}
236230

237231
renderBadge() {

0 commit comments

Comments
 (0)