@momo-kits/avatar 0.0.1 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Avatar.js +71 -15
- package/Avatar.web.js +3 -1
- package/package.json +1 -1
package/Avatar.js
CHANGED
|
@@ -35,6 +35,29 @@ export const AvatarSize = {
|
|
|
35
35
|
size96: 'size96'
|
|
36
36
|
};
|
|
37
37
|
|
|
38
|
+
const COLORS = [
|
|
39
|
+
'#3683f3',
|
|
40
|
+
'#ea63af',
|
|
41
|
+
'#f94889',
|
|
42
|
+
'#82b6da',
|
|
43
|
+
'#2fc093',
|
|
44
|
+
'#7b4a41',
|
|
45
|
+
'#ff97bc',
|
|
46
|
+
'#4130d7',
|
|
47
|
+
'#c3c3c3',
|
|
48
|
+
'#f7ae2f',
|
|
49
|
+
'#b04595',
|
|
50
|
+
'#57b748',
|
|
51
|
+
'#ff8900',
|
|
52
|
+
'#7841bf',
|
|
53
|
+
'#be3f3e',
|
|
54
|
+
'#c51515',
|
|
55
|
+
'#8e6472',
|
|
56
|
+
'#fed27d',
|
|
57
|
+
'#ff6f5d',
|
|
58
|
+
'#05b8ae',
|
|
59
|
+
];
|
|
60
|
+
|
|
38
61
|
export const SubIconType = {
|
|
39
62
|
momo: 'momo',
|
|
40
63
|
online: 'online',
|
|
@@ -280,6 +303,9 @@ const shortName = (width) => ({
|
|
|
280
303
|
medium: {
|
|
281
304
|
fontSize: ScaleSize(15, width)
|
|
282
305
|
},
|
|
306
|
+
middle: {
|
|
307
|
+
fontSize: ScaleSize(16, width)
|
|
308
|
+
},
|
|
283
309
|
large: {
|
|
284
310
|
fontSize: ScaleSize(20, width)
|
|
285
311
|
},
|
|
@@ -291,6 +317,7 @@ const shortName = (width) => ({
|
|
|
291
317
|
export default class Avatar extends Component {
|
|
292
318
|
constructor(props) {
|
|
293
319
|
super(props);
|
|
320
|
+
this.color = this.getAvatarColor();
|
|
294
321
|
this.state = {
|
|
295
322
|
hideSource: false,
|
|
296
323
|
ownUpdate: false,
|
|
@@ -306,7 +333,9 @@ export default class Avatar extends Component {
|
|
|
306
333
|
this.subscription = Dimensions.addEventListener(
|
|
307
334
|
'change',
|
|
308
335
|
({ window }) => {
|
|
309
|
-
|
|
336
|
+
if(window?.height > 0 && window?.width > 0) {
|
|
337
|
+
this.setState({ dimensions: window });
|
|
338
|
+
}
|
|
310
339
|
}
|
|
311
340
|
);
|
|
312
341
|
}
|
|
@@ -342,12 +371,24 @@ export default class Avatar extends Component {
|
|
|
342
371
|
return alphabet.join('');
|
|
343
372
|
}
|
|
344
373
|
|
|
345
|
-
|
|
374
|
+
hashUrl(url) {
|
|
375
|
+
let hash = 0;
|
|
376
|
+
let char;
|
|
377
|
+
if (url?.length === 0) return hash;
|
|
378
|
+
for (let i = 0; i < url?.length; i++) {
|
|
379
|
+
char = url?.charCodeAt(i);
|
|
380
|
+
hash = ((hash << 5) - hash) + char;
|
|
381
|
+
hash &= hash; // Convert to 32bit integer
|
|
382
|
+
}
|
|
383
|
+
return hash;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
renderShortName = (name, avatarStyle, size, color,) => {
|
|
346
387
|
if (name) {
|
|
347
388
|
const shortedName = this.getContactShortName(name);
|
|
348
389
|
const shortNameStyle = this.mapShortNameStyle(size);
|
|
349
390
|
return (
|
|
350
|
-
<View style={[styles.shortNameView, avatarStyle]}>
|
|
391
|
+
<View style={[styles.shortNameView, avatarStyle, { backgroundColor: color }]}>
|
|
351
392
|
<Text.H4 style={[styles.shortNameText, shortNameStyle]}>{shortedName}</Text.H4>
|
|
352
393
|
</View>
|
|
353
394
|
);
|
|
@@ -381,9 +422,9 @@ export default class Avatar extends Component {
|
|
|
381
422
|
|
|
382
423
|
mapStyleFromSize = (size) => {
|
|
383
424
|
const { dimensions } = this.state;
|
|
384
|
-
const avatarStyle = avatarSize(dimensions?.width
|
|
425
|
+
const avatarStyle = avatarSize(dimensions?.width > 0 ? dimensions?.width : null).small;
|
|
385
426
|
if (typeof size === 'object') return size;
|
|
386
|
-
if (avatarSize(dimensions?.width
|
|
427
|
+
if (avatarSize(dimensions?.width > 0 ? dimensions?.width : null)[size]) return avatarSize(dimensions?.width > 0 ? dimensions?.width : null)[size];
|
|
387
428
|
return avatarStyle;
|
|
388
429
|
}
|
|
389
430
|
|
|
@@ -392,35 +433,39 @@ export default class Avatar extends Component {
|
|
|
392
433
|
const { dimensions } = this.state;
|
|
393
434
|
switch (size) {
|
|
394
435
|
case AvatarSize.tiny: {
|
|
395
|
-
shortNameStyle = shortName(dimensions?.width
|
|
436
|
+
shortNameStyle = shortName(dimensions?.width > 0 ? dimensions?.width : null).tiny;
|
|
396
437
|
break;
|
|
397
438
|
}
|
|
398
439
|
case AvatarSize.small: {
|
|
399
|
-
shortNameStyle = shortName(dimensions?.width
|
|
440
|
+
shortNameStyle = shortName(dimensions?.width > 0 ? dimensions?.width : null).small;
|
|
400
441
|
break;
|
|
401
442
|
}
|
|
402
443
|
case AvatarSize.medium: {
|
|
403
|
-
shortNameStyle = shortName(dimensions?.width
|
|
444
|
+
shortNameStyle = shortName(dimensions?.width > 0 ? dimensions?.width : null).medium;
|
|
445
|
+
break;
|
|
446
|
+
}
|
|
447
|
+
case AvatarSize.size48: {
|
|
448
|
+
shortNameStyle = shortName(dimensions?.width || null).middle;
|
|
404
449
|
break;
|
|
405
450
|
}
|
|
406
451
|
case AvatarSize.large: {
|
|
407
|
-
shortNameStyle = shortName(dimensions?.width
|
|
452
|
+
shortNameStyle = shortName(dimensions?.width > 0 ? dimensions?.width : null).large;
|
|
408
453
|
break;
|
|
409
454
|
}
|
|
410
455
|
case AvatarSize.giant: {
|
|
411
|
-
shortNameStyle = shortName(dimensions?.width
|
|
456
|
+
shortNameStyle = shortName(dimensions?.width > 0 ? dimensions?.width : null).giant;
|
|
412
457
|
break;
|
|
413
458
|
}
|
|
414
459
|
default:
|
|
415
|
-
shortNameStyle = shortName(dimensions?.width
|
|
460
|
+
shortNameStyle = shortName(dimensions?.width > 0 ? dimensions?.width : null).small;
|
|
416
461
|
}
|
|
417
462
|
return shortNameStyle;
|
|
418
463
|
}
|
|
419
464
|
|
|
420
465
|
mapSubIconSize = (size) => {
|
|
421
466
|
const { dimensions } = this.state;
|
|
422
|
-
const subIconStyle = subIconSize(dimensions?.width
|
|
423
|
-
if (subIconSize(dimensions?.width
|
|
467
|
+
const subIconStyle = subIconSize(dimensions?.width > 0 ? dimensions?.width : null).small;
|
|
468
|
+
if (subIconSize(dimensions?.width > 0 ? dimensions?.width : null)[size]) return subIconSize(dimensions?.width > 0 ? dimensions?.width : null)[size];
|
|
424
469
|
return subIconStyle;
|
|
425
470
|
}
|
|
426
471
|
|
|
@@ -473,6 +518,16 @@ export default class Avatar extends Component {
|
|
|
473
518
|
);
|
|
474
519
|
}
|
|
475
520
|
|
|
521
|
+
getAvatarColor() {
|
|
522
|
+
const { source } = this.props;
|
|
523
|
+
const number = Math.abs(this.hashUrl(source));
|
|
524
|
+
if (number && !isNaN(Number(number))) {
|
|
525
|
+
const colorIndex = Number(number) % COLORS.length;
|
|
526
|
+
return COLORS[colorIndex];
|
|
527
|
+
}
|
|
528
|
+
return '#d5d6d6';
|
|
529
|
+
}
|
|
530
|
+
|
|
476
531
|
onPress = () => {
|
|
477
532
|
const { onPress } = this.props;
|
|
478
533
|
if (onPress && typeof onPress === 'function') onPress();
|
|
@@ -500,7 +555,7 @@ export default class Avatar extends Component {
|
|
|
500
555
|
|
|
501
556
|
const inner = (
|
|
502
557
|
<View style={[styles.container, containerSize, style]}>
|
|
503
|
-
{(showName || hideSource) ? this.renderShortName(name, avatarStyle, size) : <View />}
|
|
558
|
+
{(showName || hideSource) ? this.renderShortName(name, avatarStyle, size, this.color) : <View />}
|
|
504
559
|
{this.isValidImageUrl(imageSource) && !hideSource
|
|
505
560
|
? (
|
|
506
561
|
<Image
|
|
@@ -543,7 +598,8 @@ Avatar.propTypes = {
|
|
|
543
598
|
loading: PropTypes.bool,
|
|
544
599
|
cached: PropTypes.bool,
|
|
545
600
|
scaleSize: PropTypes.bool,
|
|
546
|
-
extraPropsImage: PropTypes.object
|
|
601
|
+
extraPropsImage: PropTypes.object,
|
|
602
|
+
number: PropTypes.string
|
|
547
603
|
};
|
|
548
604
|
|
|
549
605
|
Avatar.defaultProps = {
|
package/Avatar.web.js
CHANGED
|
@@ -307,7 +307,9 @@ export default class Avatar extends Component {
|
|
|
307
307
|
this.subscription = Dimensions.addEventListener(
|
|
308
308
|
'change',
|
|
309
309
|
({ window }) => {
|
|
310
|
-
|
|
310
|
+
if(window?.height > 0 && window?.width > 0) {
|
|
311
|
+
this.setState({ dimensions: window });
|
|
312
|
+
}
|
|
311
313
|
}
|
|
312
314
|
);
|
|
313
315
|
}
|