@momo-kits/avatar 0.0.62-beta.1 → 0.0.62-beta.4
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 +547 -255
- package/package.json +2 -2
package/Avatar.js
CHANGED
|
@@ -1,15 +1,22 @@
|
|
|
1
1
|
/* eslint-disable indent */
|
|
2
|
+
import PropTypes from 'prop-types';
|
|
3
|
+
import React, { Component } from 'react';
|
|
4
|
+
import {
|
|
5
|
+
View,
|
|
6
|
+
StyleSheet,
|
|
7
|
+
TouchableOpacity,
|
|
8
|
+
Dimensions,
|
|
9
|
+
Platform,
|
|
10
|
+
} from 'react-native';
|
|
11
|
+
import { get, isEqual } from 'lodash';
|
|
2
12
|
import {
|
|
13
|
+
ValueUtil,
|
|
3
14
|
Colors,
|
|
15
|
+
Text,
|
|
16
|
+
IconSource,
|
|
4
17
|
Image,
|
|
5
|
-
Radius,
|
|
6
18
|
ScaleSize,
|
|
7
|
-
|
|
8
|
-
ValueUtil,
|
|
9
|
-
} from '@momo-kits/core-v2';
|
|
10
|
-
import PropTypes from 'prop-types';
|
|
11
|
-
import React from 'react';
|
|
12
|
-
import { StyleSheet, TouchableOpacity, View } from 'react-native';
|
|
19
|
+
} from '@momo-kits/core';
|
|
13
20
|
|
|
14
21
|
export const AvatarQuality = {
|
|
15
22
|
low: 'low',
|
|
@@ -23,8 +30,43 @@ export const AvatarSize = {
|
|
|
23
30
|
medium: 'medium',
|
|
24
31
|
large: 'large',
|
|
25
32
|
giant: 'giant',
|
|
33
|
+
size8: 'size8',
|
|
34
|
+
size24: 'size24',
|
|
35
|
+
size36: 'size36',
|
|
36
|
+
size44: 'size44',
|
|
37
|
+
size48: 'size48',
|
|
38
|
+
size52: 'size52',
|
|
39
|
+
size56: 'size56',
|
|
40
|
+
size64: 'size64',
|
|
41
|
+
size72: 'size72',
|
|
42
|
+
size80: 'size80',
|
|
43
|
+
size88: 'size88',
|
|
44
|
+
size96: 'size96',
|
|
26
45
|
};
|
|
27
46
|
|
|
47
|
+
const COLORS = [
|
|
48
|
+
'#3683f3',
|
|
49
|
+
'#ea63af',
|
|
50
|
+
'#f94889',
|
|
51
|
+
'#82b6da',
|
|
52
|
+
'#2fc093',
|
|
53
|
+
'#7b4a41',
|
|
54
|
+
'#ff97bc',
|
|
55
|
+
'#4130d7',
|
|
56
|
+
'#c3c3c3',
|
|
57
|
+
'#f7ae2f',
|
|
58
|
+
'#b04595',
|
|
59
|
+
'#57b748',
|
|
60
|
+
'#ff8900',
|
|
61
|
+
'#7841bf',
|
|
62
|
+
'#be3f3e',
|
|
63
|
+
'#c51515',
|
|
64
|
+
'#8e6472',
|
|
65
|
+
'#fed27d',
|
|
66
|
+
'#ff6f5d',
|
|
67
|
+
'#05b8ae',
|
|
68
|
+
];
|
|
69
|
+
|
|
28
70
|
export const SubIconType = {
|
|
29
71
|
momo: 'momo',
|
|
30
72
|
online: 'online',
|
|
@@ -38,11 +80,12 @@ const styles = StyleSheet.create({
|
|
|
38
80
|
alignItems: 'center',
|
|
39
81
|
},
|
|
40
82
|
shortNameView: {
|
|
41
|
-
backgroundColor: Colors.pink_09,
|
|
42
83
|
justifyContent: 'center',
|
|
43
84
|
alignItems: 'center',
|
|
44
|
-
|
|
45
|
-
|
|
85
|
+
|
|
86
|
+
backgroundColor: Colors.very_light_pink_four,
|
|
87
|
+
width: '100%',
|
|
88
|
+
height: '100%',
|
|
46
89
|
},
|
|
47
90
|
shortNameText: {
|
|
48
91
|
// fontSize: 17,
|
|
@@ -63,11 +106,6 @@ const styles = StyleSheet.create({
|
|
|
63
106
|
bottom: 0,
|
|
64
107
|
right: 0,
|
|
65
108
|
},
|
|
66
|
-
topIcon: {
|
|
67
|
-
position: 'absolute',
|
|
68
|
-
top: 0,
|
|
69
|
-
right: 0,
|
|
70
|
-
},
|
|
71
109
|
subIconImage: {
|
|
72
110
|
width: 22,
|
|
73
111
|
height: 22,
|
|
@@ -79,135 +117,268 @@ const styles = StyleSheet.create({
|
|
|
79
117
|
},
|
|
80
118
|
});
|
|
81
119
|
|
|
82
|
-
const subIconSize = (
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
120
|
+
const subIconSize = (width) => ({
|
|
121
|
+
tiny: {
|
|
122
|
+
width: 8,
|
|
123
|
+
height: 8,
|
|
124
|
+
borderRadius: 4,
|
|
125
|
+
},
|
|
126
|
+
small: {
|
|
127
|
+
width: 10,
|
|
128
|
+
height: 10,
|
|
129
|
+
borderRadius: 5,
|
|
130
|
+
},
|
|
131
|
+
medium: {
|
|
132
|
+
width: 12,
|
|
133
|
+
height: 12,
|
|
134
|
+
borderRadius: 6,
|
|
135
|
+
},
|
|
136
|
+
large: {
|
|
137
|
+
width: 16,
|
|
138
|
+
height: 16,
|
|
139
|
+
borderRadius: 8,
|
|
140
|
+
},
|
|
141
|
+
size8: {
|
|
142
|
+
width: 8,
|
|
143
|
+
height: 8,
|
|
144
|
+
borderRadius: 4,
|
|
145
|
+
},
|
|
146
|
+
size24: {
|
|
147
|
+
width: 10,
|
|
148
|
+
height: 10,
|
|
149
|
+
borderRadius: 5,
|
|
150
|
+
},
|
|
151
|
+
size36: {
|
|
152
|
+
width: 10,
|
|
153
|
+
height: 10,
|
|
154
|
+
borderRadius: 5,
|
|
155
|
+
},
|
|
156
|
+
size44: {
|
|
157
|
+
width: 14,
|
|
158
|
+
height: 14,
|
|
159
|
+
borderRadius: 7,
|
|
160
|
+
},
|
|
161
|
+
size48: {
|
|
162
|
+
width: 14,
|
|
163
|
+
height: 14,
|
|
164
|
+
borderRadius: 7,
|
|
165
|
+
},
|
|
166
|
+
size52: {
|
|
167
|
+
width: 16,
|
|
168
|
+
height: 16,
|
|
169
|
+
borderRadius: 8,
|
|
170
|
+
},
|
|
171
|
+
size56: {
|
|
172
|
+
width: 16,
|
|
173
|
+
height: 16,
|
|
174
|
+
borderRadius: 8,
|
|
175
|
+
},
|
|
176
|
+
size64: {
|
|
177
|
+
width: 16,
|
|
178
|
+
height: 16,
|
|
179
|
+
borderRadius: 8,
|
|
180
|
+
top: -2,
|
|
181
|
+
left: -2,
|
|
182
|
+
},
|
|
183
|
+
size72: {
|
|
184
|
+
width: 18,
|
|
185
|
+
height: 18,
|
|
186
|
+
borderRadius: 9,
|
|
187
|
+
top: -3,
|
|
188
|
+
left: -3,
|
|
189
|
+
},
|
|
190
|
+
size80: {
|
|
191
|
+
width: 18,
|
|
192
|
+
height: 18,
|
|
193
|
+
borderRadius: 9,
|
|
194
|
+
top: -3,
|
|
195
|
+
left: -3,
|
|
196
|
+
},
|
|
197
|
+
size88: {
|
|
198
|
+
width: 18,
|
|
199
|
+
height: 18,
|
|
200
|
+
borderRadius: 9,
|
|
201
|
+
top: -4,
|
|
202
|
+
left: -4,
|
|
203
|
+
},
|
|
204
|
+
size96: {
|
|
205
|
+
width: 20,
|
|
206
|
+
height: 20,
|
|
207
|
+
borderRadius: 10,
|
|
208
|
+
top: -4,
|
|
209
|
+
left: -4,
|
|
210
|
+
},
|
|
211
|
+
giant: {
|
|
212
|
+
top: -5,
|
|
213
|
+
left: -5,
|
|
214
|
+
width: 24,
|
|
215
|
+
height: 24,
|
|
216
|
+
borderRadius: 12,
|
|
217
|
+
},
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
const avatarSize = (width) => ({
|
|
221
|
+
tiny: {
|
|
222
|
+
width: 16,
|
|
223
|
+
height: 16,
|
|
224
|
+
borderRadius: 8,
|
|
225
|
+
},
|
|
226
|
+
small: {
|
|
227
|
+
width: 32,
|
|
228
|
+
height: 32,
|
|
229
|
+
borderRadius: 16,
|
|
230
|
+
},
|
|
231
|
+
medium: {
|
|
232
|
+
width: 40,
|
|
233
|
+
height: 40,
|
|
234
|
+
borderRadius: 20,
|
|
235
|
+
},
|
|
236
|
+
large: {
|
|
237
|
+
width: 60,
|
|
238
|
+
height: 60,
|
|
239
|
+
borderRadius: 30,
|
|
240
|
+
},
|
|
241
|
+
giant: {
|
|
242
|
+
width: 120,
|
|
243
|
+
height: 120,
|
|
244
|
+
borderRadius: 60,
|
|
245
|
+
},
|
|
246
|
+
size8: {
|
|
247
|
+
width: 8,
|
|
248
|
+
height: 8,
|
|
249
|
+
borderRadius: 4,
|
|
250
|
+
},
|
|
251
|
+
size24: {
|
|
252
|
+
width: 24,
|
|
253
|
+
height: 24,
|
|
254
|
+
borderRadius: 12,
|
|
255
|
+
},
|
|
256
|
+
size36: {
|
|
257
|
+
width: 36,
|
|
258
|
+
height: 36,
|
|
259
|
+
borderRadius: 18,
|
|
260
|
+
},
|
|
261
|
+
size44: {
|
|
262
|
+
width: 44,
|
|
263
|
+
height: 44,
|
|
264
|
+
borderRadius: 22,
|
|
265
|
+
},
|
|
266
|
+
size48: {
|
|
267
|
+
width: 48,
|
|
268
|
+
height: 48,
|
|
269
|
+
borderRadius: 24,
|
|
270
|
+
},
|
|
271
|
+
size52: {
|
|
272
|
+
width: 52,
|
|
273
|
+
height: 52,
|
|
274
|
+
borderRadius: 26,
|
|
275
|
+
},
|
|
276
|
+
size56: {
|
|
277
|
+
width: 56,
|
|
278
|
+
height: 56,
|
|
279
|
+
borderRadius: 28,
|
|
280
|
+
},
|
|
281
|
+
size64: {
|
|
282
|
+
width: 64,
|
|
283
|
+
height: 64,
|
|
284
|
+
borderRadius: 32,
|
|
285
|
+
},
|
|
286
|
+
size72: {
|
|
287
|
+
width: 72,
|
|
288
|
+
height: 72,
|
|
289
|
+
borderRadius: 36,
|
|
290
|
+
},
|
|
291
|
+
size80: {
|
|
292
|
+
width: 80,
|
|
293
|
+
height: 80,
|
|
294
|
+
borderRadius: 40,
|
|
295
|
+
},
|
|
296
|
+
size88: {
|
|
297
|
+
width: 88,
|
|
298
|
+
height: 88,
|
|
299
|
+
borderRadius: 44,
|
|
300
|
+
},
|
|
301
|
+
size96: {
|
|
302
|
+
width: 96,
|
|
303
|
+
height: 96,
|
|
304
|
+
borderRadius: 48,
|
|
305
|
+
},
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
const shortName = (width) => ({
|
|
309
|
+
tiny: {
|
|
310
|
+
fontSize: ScaleSize(5, width),
|
|
311
|
+
},
|
|
312
|
+
small: {
|
|
313
|
+
fontSize: ScaleSize(11, width),
|
|
314
|
+
},
|
|
315
|
+
medium: {
|
|
316
|
+
fontSize: ScaleSize(15, width),
|
|
317
|
+
},
|
|
318
|
+
middle: {
|
|
319
|
+
fontSize: ScaleSize(16, width),
|
|
320
|
+
},
|
|
321
|
+
large: {
|
|
322
|
+
fontSize: ScaleSize(20, width),
|
|
323
|
+
},
|
|
324
|
+
giant: {
|
|
325
|
+
fontSize: ScaleSize(25, width),
|
|
326
|
+
},
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
export default class Avatar extends Component {
|
|
330
|
+
constructor(props) {
|
|
331
|
+
super(props);
|
|
332
|
+
this.color = props.custom
|
|
333
|
+
? this.getAvatarColor(props.name)
|
|
334
|
+
: Colors.pink_10;
|
|
335
|
+
this.state = {
|
|
336
|
+
hideSource: false,
|
|
337
|
+
ownUpdate: false,
|
|
338
|
+
source: props.source,
|
|
339
|
+
dimensions: {},
|
|
340
|
+
};
|
|
341
|
+
this.imageSource = '';
|
|
114
342
|
}
|
|
115
|
-
};
|
|
116
343
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
344
|
+
componentDidMount() {
|
|
345
|
+
const { scaleSize } = this.props;
|
|
346
|
+
if (scaleSize) {
|
|
347
|
+
this.subscription = Dimensions.addEventListener(
|
|
348
|
+
'change',
|
|
349
|
+
({ window }) => {
|
|
350
|
+
if (
|
|
351
|
+
window?.height > 0 &&
|
|
352
|
+
window?.width > 0 &&
|
|
353
|
+
Platform.OS === 'android'
|
|
354
|
+
) {
|
|
355
|
+
this.setState({ dimensions: window });
|
|
356
|
+
}
|
|
357
|
+
},
|
|
358
|
+
);
|
|
129
359
|
}
|
|
130
|
-
case 'medium':
|
|
131
|
-
return {
|
|
132
|
-
width: 40,
|
|
133
|
-
height: 40,
|
|
134
|
-
};
|
|
135
|
-
case 'large':
|
|
136
|
-
return {
|
|
137
|
-
width: 56,
|
|
138
|
-
height: 56,
|
|
139
|
-
};
|
|
140
|
-
case 'giant':
|
|
141
|
-
return {
|
|
142
|
-
width: 72,
|
|
143
|
-
height: 72,
|
|
144
|
-
};
|
|
145
|
-
default:
|
|
146
|
-
return {
|
|
147
|
-
width: 40,
|
|
148
|
-
height: 40,
|
|
149
|
-
};
|
|
150
360
|
}
|
|
151
|
-
};
|
|
152
361
|
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
fontSize: ScaleSize(14),
|
|
163
|
-
lineHeight: 20,
|
|
164
|
-
};
|
|
165
|
-
case 'medium':
|
|
166
|
-
return {
|
|
167
|
-
fontSize: ScaleSize(18),
|
|
168
|
-
lineHeight: 22,
|
|
169
|
-
};
|
|
170
|
-
case 'large':
|
|
171
|
-
return {
|
|
172
|
-
fontSize: ScaleSize(25),
|
|
173
|
-
};
|
|
174
|
-
case 'giant':
|
|
175
|
-
return {
|
|
176
|
-
fontSize: ScaleSize(32),
|
|
177
|
-
};
|
|
178
|
-
default:
|
|
362
|
+
componentWillUnmount() {
|
|
363
|
+
const { scaleSize } = this.props;
|
|
364
|
+
if (scaleSize) {
|
|
365
|
+
this.subscription?.remove?.();
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
static getDerivedStateFromProps(nextProps, prevState) {
|
|
370
|
+
if (prevState.ownUpdate) {
|
|
179
371
|
return {
|
|
180
|
-
|
|
181
|
-
lineHeight: 22,
|
|
372
|
+
ownUpdate: false,
|
|
182
373
|
};
|
|
374
|
+
}
|
|
375
|
+
if (!isEqual(nextProps.source, prevState.source)) {
|
|
376
|
+
return { source: nextProps.source, hideSource: false };
|
|
377
|
+
}
|
|
378
|
+
return null;
|
|
183
379
|
}
|
|
184
|
-
};
|
|
185
380
|
|
|
186
|
-
|
|
187
|
-
resizeMode,
|
|
188
|
-
cached,
|
|
189
|
-
quality,
|
|
190
|
-
size,
|
|
191
|
-
shape,
|
|
192
|
-
name,
|
|
193
|
-
source,
|
|
194
|
-
onPress,
|
|
195
|
-
style,
|
|
196
|
-
subIcon,
|
|
197
|
-
topIcon,
|
|
198
|
-
loading,
|
|
199
|
-
}) => {
|
|
200
|
-
const { width, height } = avatarSize(size);
|
|
201
|
-
const { fontSize, lineHeight } = shortName(size);
|
|
202
|
-
const {
|
|
203
|
-
width: iconWidth,
|
|
204
|
-
height: iconHeight,
|
|
205
|
-
borderRadius: iconRadius,
|
|
206
|
-
} = subIconSize(size);
|
|
207
|
-
|
|
208
|
-
const imageSource = ValueUtil.getImageSource(source);
|
|
209
|
-
|
|
210
|
-
const getContactShortName = (name) => {
|
|
381
|
+
getContactShortName = (name) => {
|
|
211
382
|
if (!name) return '';
|
|
212
383
|
const shortNameList = name.split(' ', 2);
|
|
213
384
|
const alphabet = [];
|
|
@@ -218,18 +389,57 @@ const Avatar = ({
|
|
|
218
389
|
});
|
|
219
390
|
return alphabet.join('');
|
|
220
391
|
};
|
|
221
|
-
|
|
392
|
+
|
|
393
|
+
hashUrl(url) {
|
|
394
|
+
let hash = 0;
|
|
395
|
+
let char;
|
|
396
|
+
if (url?.length === 0) return hash;
|
|
397
|
+
for (let i = 0; i < url?.length; i++) {
|
|
398
|
+
char = url?.charCodeAt(i);
|
|
399
|
+
hash = (hash << 5) - hash + char;
|
|
400
|
+
hash &= hash; // Convert to 32bit integer
|
|
401
|
+
}
|
|
402
|
+
return hash;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
renderShortName = (name, avatarStyle, size, color) => {
|
|
406
|
+
if (name) {
|
|
407
|
+
const shortedName = this.getContactShortName(name);
|
|
408
|
+
const shortNameStyle = this.mapShortNameStyle(size);
|
|
409
|
+
return (
|
|
410
|
+
<View
|
|
411
|
+
style={[
|
|
412
|
+
styles.shortNameView,
|
|
413
|
+
avatarStyle,
|
|
414
|
+
{ backgroundColor: color },
|
|
415
|
+
]}>
|
|
416
|
+
<Text.H4
|
|
417
|
+
style={[
|
|
418
|
+
this.props.custom
|
|
419
|
+
? styles.shortNameTextCustom
|
|
420
|
+
: styles.shortNameText,
|
|
421
|
+
shortNameStyle,
|
|
422
|
+
]}>
|
|
423
|
+
{shortedName}
|
|
424
|
+
</Text.H4>
|
|
425
|
+
</View>
|
|
426
|
+
);
|
|
427
|
+
}
|
|
428
|
+
return <View />;
|
|
429
|
+
};
|
|
430
|
+
|
|
431
|
+
isUrl(url) {
|
|
222
432
|
const expression =
|
|
223
433
|
/(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-/]))?/gi;
|
|
224
434
|
return expression.test(url);
|
|
225
|
-
}
|
|
435
|
+
}
|
|
226
436
|
|
|
227
|
-
|
|
228
|
-
const validSource = source && source.uri && isUrl(source.uri);
|
|
437
|
+
isValidImageUrl = (source) => {
|
|
438
|
+
const validSource = source && source.uri && this.isUrl(source.uri);
|
|
229
439
|
return validSource;
|
|
230
440
|
};
|
|
231
441
|
|
|
232
|
-
|
|
442
|
+
mapAvatarQuality = (url, quality) => {
|
|
233
443
|
if (!quality || quality === AvatarQuality.medium) {
|
|
234
444
|
return url;
|
|
235
445
|
}
|
|
@@ -243,145 +453,227 @@ const Avatar = ({
|
|
|
243
453
|
return url;
|
|
244
454
|
};
|
|
245
455
|
|
|
246
|
-
|
|
456
|
+
mapStyleFromSize = (size) => {
|
|
457
|
+
const { dimensions } = this.state;
|
|
458
|
+
const avatarStyle = avatarSize(
|
|
459
|
+
dimensions?.width > 0 ? dimensions?.width : null,
|
|
460
|
+
).small;
|
|
461
|
+
if (typeof size === 'object') return size;
|
|
462
|
+
if (avatarSize(dimensions?.width > 0 ? dimensions?.width : null)[size])
|
|
463
|
+
return avatarSize(dimensions?.width > 0 ? dimensions?.width : null)[
|
|
464
|
+
size
|
|
465
|
+
];
|
|
466
|
+
return avatarStyle;
|
|
467
|
+
};
|
|
468
|
+
|
|
469
|
+
mapShortNameStyle = (size) => {
|
|
470
|
+
let shortNameStyle = {};
|
|
471
|
+
const { dimensions } = this.state;
|
|
472
|
+
switch (size) {
|
|
473
|
+
case AvatarSize.tiny: {
|
|
474
|
+
shortNameStyle = shortName(
|
|
475
|
+
dimensions?.width > 0 ? dimensions?.width : null,
|
|
476
|
+
).tiny;
|
|
477
|
+
break;
|
|
478
|
+
}
|
|
479
|
+
case AvatarSize.small: {
|
|
480
|
+
shortNameStyle = shortName(
|
|
481
|
+
dimensions?.width > 0 ? dimensions?.width : null,
|
|
482
|
+
).small;
|
|
483
|
+
break;
|
|
484
|
+
}
|
|
485
|
+
case AvatarSize.medium: {
|
|
486
|
+
shortNameStyle = shortName(
|
|
487
|
+
dimensions?.width > 0 ? dimensions?.width : null,
|
|
488
|
+
).medium;
|
|
489
|
+
break;
|
|
490
|
+
}
|
|
491
|
+
case AvatarSize.size48: {
|
|
492
|
+
shortNameStyle = shortName(dimensions?.width || null).middle;
|
|
493
|
+
break;
|
|
494
|
+
}
|
|
495
|
+
case AvatarSize.large: {
|
|
496
|
+
shortNameStyle = shortName(
|
|
497
|
+
dimensions?.width > 0 ? dimensions?.width : null,
|
|
498
|
+
).large;
|
|
499
|
+
break;
|
|
500
|
+
}
|
|
501
|
+
case AvatarSize.giant: {
|
|
502
|
+
shortNameStyle = shortName(
|
|
503
|
+
dimensions?.width > 0 ? dimensions?.width : null,
|
|
504
|
+
).giant;
|
|
505
|
+
break;
|
|
506
|
+
}
|
|
507
|
+
default:
|
|
508
|
+
shortNameStyle = shortName(
|
|
509
|
+
dimensions?.width > 0 ? dimensions?.width : null,
|
|
510
|
+
).small;
|
|
511
|
+
}
|
|
512
|
+
return shortNameStyle;
|
|
513
|
+
};
|
|
514
|
+
|
|
515
|
+
mapSubIconSize = (size) => {
|
|
516
|
+
const { dimensions } = this.state;
|
|
517
|
+
const subIconStyle = subIconSize(
|
|
518
|
+
dimensions?.width > 0 ? dimensions?.width : null,
|
|
519
|
+
).small;
|
|
520
|
+
if (subIconSize(dimensions?.width > 0 ? dimensions?.width : null)[size])
|
|
521
|
+
return subIconSize(
|
|
522
|
+
dimensions?.width > 0 ? dimensions?.width : null,
|
|
523
|
+
)[size];
|
|
524
|
+
return subIconStyle;
|
|
525
|
+
};
|
|
526
|
+
|
|
527
|
+
extractSize = (avatarStyle) => {
|
|
528
|
+
const { style } = this.props;
|
|
529
|
+
const borderWidth = get(style, 'borderWidth', 1);
|
|
530
|
+
const containerWidth = Math.round(avatarStyle.width + 2 + borderWidth);
|
|
531
|
+
return {
|
|
532
|
+
width: containerWidth,
|
|
533
|
+
height: containerWidth,
|
|
534
|
+
borderRadius: containerWidth / 2,
|
|
535
|
+
borderWidth,
|
|
536
|
+
borderColor: Colors.black_01,
|
|
537
|
+
};
|
|
538
|
+
};
|
|
539
|
+
|
|
540
|
+
onLoadSourceError = (imageSource) => {
|
|
247
541
|
if (this.imageSource?.uri !== imageSource?.uri) {
|
|
248
542
|
this.imageSource = imageSource;
|
|
249
543
|
this.setState({ hideSource: true, ownUpdate: true });
|
|
250
544
|
}
|
|
251
545
|
};
|
|
252
546
|
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
if (subIcon
|
|
256
|
-
return
|
|
257
|
-
|
|
547
|
+
renderSubIcon = () => {
|
|
548
|
+
const { subIcon, size, subIconSource } = this.props;
|
|
549
|
+
if ((!subIcon || subIcon === SubIconType.none) && !subIconSource)
|
|
550
|
+
return <View />;
|
|
551
|
+
const subIconSizeStyle = this.mapSubIconSize(size);
|
|
552
|
+
const iconSource = ValueUtil.getImageSource(subIconSource);
|
|
553
|
+
return (
|
|
554
|
+
<View style={styles.subIcon}>
|
|
555
|
+
{subIcon === SubIconType.momo ? (
|
|
258
556
|
<Image
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
{
|
|
262
|
-
width: iconWidth,
|
|
263
|
-
height: iconHeight,
|
|
264
|
-
borderRadius: iconRadius,
|
|
265
|
-
bottom: iconWidth / 2 - 2,
|
|
266
|
-
right: iconWidth / 2 - 2,
|
|
267
|
-
},
|
|
268
|
-
]}
|
|
557
|
+
style={[styles.subIconImage, subIconSizeStyle]}
|
|
558
|
+
source={IconSource.ic_round_momo_tiny}
|
|
269
559
|
/>
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
560
|
+
) : (
|
|
561
|
+
<View />
|
|
562
|
+
)}
|
|
563
|
+
{subIcon === SubIconType.online ? (
|
|
564
|
+
<View
|
|
565
|
+
style={[styles.subIconImageOnline, subIconSizeStyle]}
|
|
566
|
+
/>
|
|
567
|
+
) : (
|
|
568
|
+
<View />
|
|
569
|
+
)}
|
|
274
570
|
|
|
275
|
-
|
|
276
|
-
if (topIcon && shape === 'circle' && size !== 'tiny') {
|
|
277
|
-
return (
|
|
278
|
-
<View style={styles.topIcon}>
|
|
571
|
+
{iconSource && subIconSource && !subIcon ? (
|
|
279
572
|
<Image
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
{
|
|
283
|
-
width: iconWidth,
|
|
284
|
-
height: iconHeight,
|
|
285
|
-
borderRadius: iconRadius,
|
|
286
|
-
top: iconWidth / 2 - 2,
|
|
287
|
-
right: iconWidth / 2 - 2,
|
|
288
|
-
},
|
|
289
|
-
]}
|
|
573
|
+
style={[styles.subIconImage, subIconSizeStyle]}
|
|
574
|
+
source={iconSource}
|
|
290
575
|
/>
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
576
|
+
) : (
|
|
577
|
+
<View />
|
|
578
|
+
)}
|
|
579
|
+
</View>
|
|
580
|
+
);
|
|
294
581
|
};
|
|
295
582
|
|
|
296
|
-
|
|
583
|
+
getAvatarColor(name) {
|
|
584
|
+
const shortName = this.getContactShortName(name);
|
|
585
|
+
if (!shortName) return '#d5d6d6';
|
|
586
|
+
const firstLetter = shortName[0].charCodeAt(0);
|
|
587
|
+
let colorIndex = Number(firstLetter) % 26;
|
|
588
|
+
if (colorIndex > 19) colorIndex = 25 - colorIndex;
|
|
589
|
+
return COLORS[colorIndex];
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
onPress = () => {
|
|
593
|
+
const { onPress } = this.props;
|
|
297
594
|
if (onPress && typeof onPress === 'function') onPress();
|
|
298
595
|
};
|
|
299
596
|
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
)
|
|
597
|
+
render() {
|
|
598
|
+
const {
|
|
599
|
+
size,
|
|
600
|
+
source,
|
|
601
|
+
name,
|
|
602
|
+
resizeMode,
|
|
603
|
+
style,
|
|
604
|
+
onPress,
|
|
605
|
+
isShowLoading,
|
|
606
|
+
loading,
|
|
607
|
+
cached,
|
|
608
|
+
extraPropsImage,
|
|
609
|
+
quality,
|
|
610
|
+
} = this.props;
|
|
611
|
+
const { hideSource } = this.state;
|
|
612
|
+
const avatarStyle = this.mapStyleFromSize(size);
|
|
613
|
+
const containerSize = this.extractSize(avatarStyle);
|
|
614
|
+
const imageSource = ValueUtil.getImageSource(source);
|
|
615
|
+
const showName =
|
|
616
|
+
typeof imageSource === 'object' &&
|
|
617
|
+
Object.keys(imageSource).length === 0 &&
|
|
618
|
+
typeof imageSource !== 'number';
|
|
619
|
+
// add priority high for avatar
|
|
620
|
+
if (imageSource?.uri) {
|
|
621
|
+
imageSource.priority = 'high';
|
|
622
|
+
imageSource.uri = this.mapAvatarQuality(imageSource.uri, quality);
|
|
623
|
+
// if doest not have time query, set default cache time to 1 day
|
|
624
|
+
if (!imageSource.uri?.includes('?') && cached) {
|
|
625
|
+
const midnight = new Date().setHours(0, 0, 0, 0);
|
|
626
|
+
imageSource.uri = `${imageSource.uri}?time=${midnight}`;
|
|
627
|
+
}
|
|
321
628
|
}
|
|
322
|
-
return <View />;
|
|
323
|
-
};
|
|
324
629
|
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
630
|
+
const inner = (
|
|
631
|
+
<View style={[styles.container, containerSize, style]}>
|
|
632
|
+
{showName || hideSource ? (
|
|
633
|
+
this.renderShortName(name, avatarStyle, size, this.color)
|
|
634
|
+
) : (
|
|
635
|
+
<View />
|
|
636
|
+
)}
|
|
637
|
+
{this.isValidImageUrl(imageSource) && !hideSource ? (
|
|
638
|
+
<Image
|
|
639
|
+
cached={cached}
|
|
640
|
+
loading={loading || isShowLoading}
|
|
641
|
+
source={imageSource}
|
|
642
|
+
onError={() => this.onLoadSourceError(imageSource)}
|
|
643
|
+
style={avatarStyle}
|
|
644
|
+
resizeMode={resizeMode}
|
|
645
|
+
{...extraPropsImage}
|
|
646
|
+
/>
|
|
647
|
+
) : (
|
|
648
|
+
<View />
|
|
649
|
+
)}
|
|
650
|
+
{this.renderSubIcon()}
|
|
651
|
+
</View>
|
|
652
|
+
);
|
|
653
|
+
if (onPress) {
|
|
654
|
+
return (
|
|
655
|
+
<TouchableOpacity
|
|
656
|
+
activeOpacity={onPress ? 0.5 : 1}
|
|
657
|
+
onPress={this.onPress}>
|
|
658
|
+
{inner}
|
|
659
|
+
</TouchableOpacity>
|
|
660
|
+
);
|
|
332
661
|
}
|
|
662
|
+
return inner;
|
|
333
663
|
}
|
|
334
|
-
|
|
335
|
-
const renderImage = () => {
|
|
336
|
-
return (
|
|
337
|
-
<Image
|
|
338
|
-
cached={cached}
|
|
339
|
-
resizeMode={resizeMode}
|
|
340
|
-
onLoadSourceError={() => onLoadSourceError(source)}
|
|
341
|
-
source={source}
|
|
342
|
-
loading={loading}
|
|
343
|
-
style={[
|
|
344
|
-
{
|
|
345
|
-
width,
|
|
346
|
-
height,
|
|
347
|
-
borderRadius:
|
|
348
|
-
shape === 'circle' ? width / 2 : Radius.XS,
|
|
349
|
-
},
|
|
350
|
-
]}
|
|
351
|
-
/>
|
|
352
|
-
);
|
|
353
|
-
};
|
|
354
|
-
|
|
355
|
-
return (
|
|
356
|
-
<TouchableOpacity
|
|
357
|
-
activeOpacity={!!onPress ? 0.5 : 1}
|
|
358
|
-
style={[style]}
|
|
359
|
-
onPress={onPressIcon}>
|
|
360
|
-
<View
|
|
361
|
-
style={{
|
|
362
|
-
alignSelf: 'baseline',
|
|
363
|
-
}}>
|
|
364
|
-
{!!imageSource && isValidImageUrl(imageSource)
|
|
365
|
-
? renderImage()
|
|
366
|
-
: renderShortName()}
|
|
367
|
-
{renderTopIcon()}
|
|
368
|
-
{renderSubIcon()}
|
|
369
|
-
</View>
|
|
370
|
-
</TouchableOpacity>
|
|
371
|
-
);
|
|
372
|
-
};
|
|
664
|
+
}
|
|
373
665
|
|
|
374
666
|
Avatar.propTypes = {
|
|
375
667
|
name: PropTypes.string,
|
|
668
|
+
resizeMode: PropTypes.string,
|
|
376
669
|
quality: PropTypes.oneOf(['low', 'medium', 'high']),
|
|
377
|
-
size: PropTypes.
|
|
670
|
+
size: PropTypes.any,
|
|
378
671
|
source: PropTypes.oneOfType([
|
|
379
672
|
PropTypes.shape({ uri: PropTypes.string }),
|
|
380
673
|
PropTypes.number,
|
|
381
674
|
PropTypes.string,
|
|
382
675
|
]),
|
|
383
|
-
subIcon: PropTypes.
|
|
384
|
-
topIcon: PropTypes.string,
|
|
676
|
+
subIcon: PropTypes.oneOf(['momo', 'online', 'key', 'none']),
|
|
385
677
|
subIconSource: PropTypes.oneOfType([
|
|
386
678
|
PropTypes.shape({ uri: PropTypes.string }),
|
|
387
679
|
PropTypes.number,
|
|
@@ -390,8 +682,11 @@ Avatar.propTypes = {
|
|
|
390
682
|
style: PropTypes.oneOfType([PropTypes.array, PropTypes.object]),
|
|
391
683
|
onPress: PropTypes.func,
|
|
392
684
|
loading: PropTypes.bool,
|
|
685
|
+
cached: PropTypes.bool,
|
|
686
|
+
scaleSize: PropTypes.bool,
|
|
393
687
|
extraPropsImage: PropTypes.object,
|
|
394
|
-
|
|
688
|
+
number: PropTypes.string,
|
|
689
|
+
custom: PropTypes.bool,
|
|
395
690
|
};
|
|
396
691
|
|
|
397
692
|
Avatar.defaultProps = {
|
|
@@ -400,8 +695,5 @@ Avatar.defaultProps = {
|
|
|
400
695
|
scaleSize: false,
|
|
401
696
|
quality: 'medium',
|
|
402
697
|
size: 'small',
|
|
403
|
-
|
|
404
|
-
name: '',
|
|
698
|
+
custom: false,
|
|
405
699
|
};
|
|
406
|
-
|
|
407
|
-
export default Avatar;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@momo-kits/avatar",
|
|
3
|
-
"version": "0.0.62-beta.
|
|
3
|
+
"version": "0.0.62-beta.4",
|
|
4
4
|
"private": false,
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"dependencies": {},
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
"react-native": ">=0.55",
|
|
9
9
|
"prop-types": "^15.7.2",
|
|
10
10
|
"react": "16.9.0",
|
|
11
|
-
"@momo-kits/core
|
|
11
|
+
"@momo-kits/core": ">=0.0.5-beta",
|
|
12
12
|
"lodash": "^4.17.15"
|
|
13
13
|
},
|
|
14
14
|
"devDependencies": {},
|