@momo-kits/avatar 0.0.38-beta → 0.0.42-beta

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.
Files changed (3) hide show
  1. package/Avatar.js +47 -22
  2. package/Avatar.web.js +58 -63
  3. package/package.json +1 -1
package/Avatar.js CHANGED
@@ -2,11 +2,11 @@
2
2
  import PropTypes from 'prop-types';
3
3
  import React, { Component } from 'react';
4
4
  import {
5
- View, StyleSheet, TouchableOpacity
5
+ View, StyleSheet, TouchableOpacity, Dimensions
6
6
  } from 'react-native';
7
7
  import { get, isEqual } from 'lodash';
8
8
  import {
9
- ValueUtil, Colors, Text, IconSource, Image
9
+ ValueUtil, Colors, Text, IconSource, Image, ScaleSize
10
10
  } from '@momo-kits/core';
11
11
 
12
12
  export const AvatarSize = {
@@ -76,7 +76,7 @@ const styles = StyleSheet.create({
76
76
  }
77
77
  });
78
78
 
79
- const subIconSize = StyleSheet.create({
79
+ const subIconSize = (width) => ({
80
80
  tiny: {
81
81
  width: 8,
82
82
  height: 8,
@@ -176,7 +176,7 @@ const subIconSize = StyleSheet.create({
176
176
  }
177
177
  });
178
178
 
179
- const avatarSize = StyleSheet.create({
179
+ const avatarSize = (width) => ({
180
180
  tiny: {
181
181
  width: 16,
182
182
  height: 16,
@@ -264,21 +264,21 @@ const avatarSize = StyleSheet.create({
264
264
  }
265
265
  });
266
266
 
267
- const shortName = StyleSheet.create({
267
+ const shortName = (width) => ({
268
268
  tiny: {
269
- fontSize: 5
269
+ fontSize: ScaleSize(5, width)
270
270
  },
271
271
  small: {
272
- fontSize: 11
272
+ fontSize: ScaleSize(11, width)
273
273
  },
274
274
  medium: {
275
- fontSize: 15
275
+ fontSize: ScaleSize(15, width)
276
276
  },
277
277
  large: {
278
- fontSize: 20
278
+ fontSize: ScaleSize(20, width)
279
279
  },
280
280
  giant: {
281
- fontSize: 25
281
+ fontSize: ScaleSize(25, width)
282
282
  }
283
283
  });
284
284
 
@@ -288,10 +288,30 @@ export default class Avatar extends Component {
288
288
  this.state = {
289
289
  hideSource: false,
290
290
  ownUpdate: false,
291
- source: props.source
291
+ source: props.source,
292
+ dimensions: {}
292
293
  };
293
294
  }
294
295
 
296
+ componentDidMount() {
297
+ const { scaleSize } = this.props;
298
+ if (scaleSize) {
299
+ this.subscription = Dimensions.addEventListener(
300
+ 'change',
301
+ ({ window }) => {
302
+ this.setState({ dimensions: window });
303
+ }
304
+ );
305
+ }
306
+ }
307
+
308
+ componentWillUnmount() {
309
+ const { scaleSize } = this.props;
310
+ if (scaleSize) {
311
+ this.subscription?.remove?.();
312
+ }
313
+ }
314
+
295
315
  static getDerivedStateFromProps(nextProps, prevState) {
296
316
  if (prevState.ownUpdate) {
297
317
  return {
@@ -339,44 +359,47 @@ export default class Avatar extends Component {
339
359
  }
340
360
 
341
361
  mapStyleFromSize = (size) => {
342
- const avatarStyle = avatarSize.small;
362
+ const { dimensions } = this.state;
363
+ const avatarStyle = avatarSize(dimensions?.width || null).small;
343
364
  if (typeof size === 'object') return size;
344
- if (avatarSize[size]) return avatarSize[size];
365
+ if (avatarSize(dimensions?.width || null)[size]) return avatarSize(dimensions?.width || null)[size];
345
366
  return avatarStyle;
346
367
  }
347
368
 
348
369
  mapShortNameStyle = (size) => {
349
370
  let shortNameStyle = {};
371
+ const { dimensions } = this.state;
350
372
  switch (size) {
351
373
  case AvatarSize.tiny: {
352
- shortNameStyle = shortName.tiny;
374
+ shortNameStyle = shortName(dimensions?.width || null).tiny;
353
375
  break;
354
376
  }
355
377
  case AvatarSize.small: {
356
- shortNameStyle = shortName.small;
378
+ shortNameStyle = shortName(dimensions?.width || null).small;
357
379
  break;
358
380
  }
359
381
  case AvatarSize.medium: {
360
- shortNameStyle = shortName.medium;
382
+ shortNameStyle = shortName(dimensions?.width || null).medium;
361
383
  break;
362
384
  }
363
385
  case AvatarSize.large: {
364
- shortNameStyle = shortName.large;
386
+ shortNameStyle = shortName(dimensions?.width || null).large;
365
387
  break;
366
388
  }
367
389
  case AvatarSize.giant: {
368
- shortNameStyle = shortName.giant;
390
+ shortNameStyle = shortName(dimensions?.width || null).giant;
369
391
  break;
370
392
  }
371
393
  default:
372
- shortNameStyle = shortName.small;
394
+ shortNameStyle = shortName(dimensions?.width || null).small;
373
395
  }
374
396
  return shortNameStyle;
375
397
  }
376
398
 
377
399
  mapSubIconSize = (size) => {
378
- const subIconStyle = subIconSize.small;
379
- if (subIconSize[size]) return subIconSize[size];
400
+ const { dimensions } = this.state;
401
+ const subIconStyle = subIconSize(dimensions?.width || null).small;
402
+ if (subIconSize(dimensions?.width || null)[size]) return subIconSize(dimensions?.width || null)[size];
380
403
  return subIconStyle;
381
404
  }
382
405
 
@@ -483,10 +506,12 @@ Avatar.propTypes = {
483
506
  onPress: PropTypes.func,
484
507
  loading: PropTypes.bool,
485
508
  cached: PropTypes.bool,
509
+ scaleSize: PropTypes.bool,
486
510
  extraPropsImage: PropTypes.object
487
511
  };
488
512
 
489
513
  Avatar.defaultProps = {
490
514
  resizeMode: 'cover',
491
- cached: true
515
+ cached: true,
516
+ scaleSize: false
492
517
  };
package/Avatar.web.js CHANGED
@@ -2,13 +2,14 @@
2
2
  import PropTypes from 'prop-types';
3
3
  import React, { Component } from 'react';
4
4
  import {
5
- View, StyleSheet, TouchableOpacity, Image
5
+ View, StyleSheet, TouchableOpacity, Image, Dimensions
6
6
  } from 'react-native';
7
7
  import { get, isEqual } from 'lodash';
8
- import ValueUtils from '../../core/utils/ValueUtil';
8
+ import ValueUtil from '../../core/utils/ValueUtil';
9
9
  import Colors from '../../core/colors';
10
- import { Icons } from '../../core/icons';
10
+ import { Icons as IconSource } from '../../core/icons';
11
11
  import Text from '../../core/components/typography';
12
+ import { RFValueHorizontal as ScaleSize } from '../../core/components/typography/reponsiveSize';
12
13
 
13
14
  export const AvatarSize = {
14
15
  tiny: 'tiny',
@@ -77,7 +78,7 @@ const styles = StyleSheet.create({
77
78
  }
78
79
  });
79
80
 
80
- const subIconSize = StyleSheet.create({
81
+ const subIconSize = (width) => ({
81
82
  tiny: {
82
83
  width: 8,
83
84
  height: 8,
@@ -177,7 +178,7 @@ const subIconSize = StyleSheet.create({
177
178
  }
178
179
  });
179
180
 
180
- const avatarSize = StyleSheet.create({
181
+ const avatarSize = (width) => ({
181
182
  tiny: {
182
183
  width: 16,
183
184
  height: 16,
@@ -265,21 +266,21 @@ const avatarSize = StyleSheet.create({
265
266
  }
266
267
  });
267
268
 
268
- const shortName = StyleSheet.create({
269
+ const shortName = (width) => ({
269
270
  tiny: {
270
- fontSize: 5
271
+ fontSize: ScaleSize(5, width)
271
272
  },
272
273
  small: {
273
- fontSize: 11
274
+ fontSize: ScaleSize(11, width)
274
275
  },
275
276
  medium: {
276
- fontSize: 15
277
+ fontSize: ScaleSize(15, width)
277
278
  },
278
279
  large: {
279
- fontSize: 20
280
+ fontSize: ScaleSize(20, width)
280
281
  },
281
282
  giant: {
282
- fontSize: 25
283
+ fontSize: ScaleSize(25, width)
283
284
  }
284
285
  });
285
286
 
@@ -289,10 +290,30 @@ export default class Avatar extends Component {
289
290
  this.state = {
290
291
  hideSource: false,
291
292
  ownUpdate: false,
292
- source: props.source
293
+ source: props.source,
294
+ dimensions: {}
293
295
  };
294
296
  }
295
297
 
298
+ componentDidMount() {
299
+ const { scaleSize } = this.props;
300
+ if (scaleSize) {
301
+ this.subscription = Dimensions.addEventListener(
302
+ 'change',
303
+ ({ window }) => {
304
+ this.setState({ dimensions: window });
305
+ }
306
+ );
307
+ }
308
+ }
309
+
310
+ componentWillUnmount() {
311
+ const { scaleSize } = this.props;
312
+ if (scaleSize) {
313
+ this.subscription?.remove?.();
314
+ }
315
+ }
316
+
296
317
  static getDerivedStateFromProps(nextProps, prevState) {
297
318
  if (prevState.ownUpdate) {
298
319
  return {
@@ -340,44 +361,47 @@ export default class Avatar extends Component {
340
361
  }
341
362
 
342
363
  mapStyleFromSize = (size) => {
343
- const avatarStyle = avatarSize.small;
364
+ const { dimensions } = this.state;
365
+ const avatarStyle = avatarSize(dimensions?.width || null).small;
344
366
  if (typeof size === 'object') return size;
345
- if (avatarSize[size]) return avatarSize[size];
367
+ if (avatarSize(dimensions?.width || null)[size]) return avatarSize(dimensions?.width || null)[size];
346
368
  return avatarStyle;
347
369
  }
348
370
 
349
371
  mapShortNameStyle = (size) => {
350
372
  let shortNameStyle = {};
373
+ const { dimensions } = this.state;
351
374
  switch (size) {
352
375
  case AvatarSize.tiny: {
353
- shortNameStyle = shortName.tiny;
376
+ shortNameStyle = shortName(dimensions?.width || null).tiny;
354
377
  break;
355
378
  }
356
379
  case AvatarSize.small: {
357
- shortNameStyle = shortName.small;
380
+ shortNameStyle = shortName(dimensions?.width || null).small;
358
381
  break;
359
382
  }
360
383
  case AvatarSize.medium: {
361
- shortNameStyle = shortName.medium;
384
+ shortNameStyle = shortName(dimensions?.width || null).medium;
362
385
  break;
363
386
  }
364
387
  case AvatarSize.large: {
365
- shortNameStyle = shortName.large;
388
+ shortNameStyle = shortName(dimensions?.width || null).large;
366
389
  break;
367
390
  }
368
391
  case AvatarSize.giant: {
369
- shortNameStyle = shortName.giant;
392
+ shortNameStyle = shortName(dimensions?.width || null).giant;
370
393
  break;
371
394
  }
372
395
  default:
373
- shortNameStyle = shortName.small;
396
+ shortNameStyle = shortName(dimensions?.width || null).small;
374
397
  }
375
398
  return shortNameStyle;
376
399
  }
377
400
 
378
401
  mapSubIconSize = (size) => {
379
- const subIconStyle = subIconSize.small;
380
- if (subIconSize[size]) return subIconSize[size];
402
+ const { dimensions } = this.state;
403
+ const subIconStyle = subIconSize(dimensions?.width || null).small;
404
+ if (subIconSize(dimensions?.width || null)[size]) return subIconSize(dimensions?.width || null)[size];
381
405
  return subIconStyle;
382
406
  }
383
407
 
@@ -402,14 +426,14 @@ export default class Avatar extends Component {
402
426
  const { subIcon, size, subIconSource } = this.props;
403
427
  if ((!subIcon || subIcon === SubIconType.none) && !subIconSource) return <View />;
404
428
  const subIconSizeStyle = this.mapSubIconSize(size);
405
- const iconSource = ValueUtils.getImageSource(subIconSource);
429
+ const iconSource = ValueUtil.getImageSource(subIconSource);
406
430
  return (
407
431
  <View style={styles.subIcon}>
408
432
  {subIcon === SubIconType.momo
409
433
  ? (
410
434
  <Image
411
435
  style={[styles.subIconImage, subIconSizeStyle]}
412
- source={Icons.ic_round_momo_tiny}
436
+ source={IconSource.ic_round_momo_tiny}
413
437
  />
414
438
  )
415
439
  : <View />}
@@ -439,11 +463,11 @@ export default class Avatar extends Component {
439
463
  const { hideSource } = this.state;
440
464
  const avatarStyle = this.mapStyleFromSize(size);
441
465
  const containerSize = this.extractSize(avatarStyle);
442
- const imageSource = ValueUtils.getImageSource(source);
466
+ const imageSource = ValueUtil.getImageSource(source);
443
467
  const showName = typeof imageSource === 'object' && Object.keys(imageSource).length === 0 && typeof imageSource !== 'number';
444
468
  const inner = (
445
- <View style={[styles.container, avatarStyle, style]}>
446
- {(showName || hideSource) ? this.renderShortName(name, avatarStyle, size) : null}
469
+ <View style={[styles.container, containerSize, style]}>
470
+ {(showName || hideSource) ? this.renderShortName(name, avatarStyle, size) : <View />}
447
471
  {this.isValidImageUrl(imageSource) && !hideSource
448
472
  ? (
449
473
  <Image
@@ -471,54 +495,25 @@ export default class Avatar extends Component {
471
495
  }
472
496
 
473
497
  Avatar.propTypes = {
474
- /**
475
- * The default name when image load fail.
476
- */
477
498
  name: PropTypes.string,
478
- /**
479
- * How resize should the avatar be?
480
- */
481
499
  resizeMode: PropTypes.string,
482
- /**
483
- * How large should the avatar be?
484
- */
485
- size: PropTypes.oneOf(['tiny', 'small', 'medium', 'large', 'giant', 'size8', 'size24', 'size36', 'size44', 'size48', 'size52', 'size56', 'size64', 'size72', 'size80', 'size88', 'size96']),
486
- /**
487
- * What source Avatar to use?
488
- */
500
+ size: PropTypes.oneOf(['tiny', 'small', 'medium', 'large', 'giant', 'size8', 'size24', 'size36', 'size44', 'size48', 'size52', 'size56', 'size64', 'size72', 'size80', 'size88', 'size96',
501
+ PropTypes.shape({
502
+ width: PropTypes.number, height: PropTypes.number, borderRadius: PropTypes.number
503
+ })]),
489
504
  source: PropTypes.oneOfType([PropTypes.shape({ uri: PropTypes.string }), PropTypes.number, PropTypes.string]),
490
- /**
491
- * What subIcon Avatar to use?
492
- */
493
505
  subIcon: PropTypes.oneOf(['momo', 'online', 'key', 'none']),
494
- /**
495
- * What custom subIcon Avatar to use?
496
- */
497
506
  subIconSource: PropTypes.oneOfType([PropTypes.shape({ uri: PropTypes.string }), PropTypes.number, PropTypes.string]),
498
- /**
499
- * What custom style Avatar to use?
500
- */
501
507
  style: PropTypes.oneOfType([PropTypes.array, PropTypes.object]),
502
- /**
503
- * Optional click handler.
504
- */
505
508
  onPress: PropTypes.func,
506
- /**
507
- * Optional loading handler.
508
- */
509
509
  loading: PropTypes.bool,
510
- /**
511
- * Optional cached handler.
512
- */
513
510
  cached: PropTypes.bool,
514
- /**
515
- * Optional extra prop of Image.
516
- */
511
+ scaleSize: PropTypes.bool,
517
512
  extraPropsImage: PropTypes.object
518
513
  };
519
514
 
520
515
  Avatar.defaultProps = {
521
516
  resizeMode: 'cover',
522
- size: 'medium',
523
- cached: true
517
+ cached: true,
518
+ scaleSize: false
524
519
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@momo-kits/avatar",
3
- "version": "0.0.38-beta",
3
+ "version": "0.0.42-beta",
4
4
  "private": false,
5
5
  "main": "index.js",
6
6
  "dependencies": {},