You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

StatelessAvatar.js 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // @flow
  2. import React from 'react';
  3. import { Image, Text, View } from 'react-native';
  4. import { Icon } from '../../../icons';
  5. import { type StyleType } from '../../../styles';
  6. import AbstractStatelessAvatar, { type Props as AbstractProps } from '../AbstractStatelessAvatar';
  7. import styles from './styles';
  8. type Props = AbstractProps & {
  9. /**
  10. * External style passed to the componant.
  11. */
  12. style?: StyleType
  13. };
  14. const DEFAULT_AVATAR = require('../../../../../../images/avatar.png');
  15. /**
  16. * Implements a stateless avatar component that renders an avatar purely from what gets passed through
  17. * props.
  18. */
  19. export default class StatelessAvatar extends AbstractStatelessAvatar<Props> {
  20. /**
  21. * Implements {@code Component#render}.
  22. *
  23. * @inheritdoc
  24. */
  25. render() {
  26. const { initials, size, style, url } = this.props;
  27. let avatar;
  28. if (this._isIcon(url)) {
  29. avatar = this._renderIconAvatar(url);
  30. } else if (url) {
  31. avatar = this._renderURLAvatar();
  32. } else if (initials) {
  33. avatar = this._renderInitialsAvatar();
  34. } else {
  35. avatar = this._renderDefaultAvatar();
  36. }
  37. return (
  38. <View
  39. style = { [
  40. styles.avatarContainer(size),
  41. style
  42. ] }>
  43. { avatar }
  44. </View>
  45. );
  46. }
  47. _isIcon: (?string | ?Object) => boolean
  48. /**
  49. * Renders the default avatar.
  50. *
  51. * @returns {React$Element<*>}
  52. */
  53. _renderDefaultAvatar() {
  54. const { size } = this.props;
  55. return (
  56. <Image
  57. source = { DEFAULT_AVATAR }
  58. style = { [
  59. styles.avatarContent(size),
  60. styles.staticAvatar
  61. ] } />
  62. );
  63. }
  64. /**
  65. * Renders the icon avatar.
  66. *
  67. * @param {Object} icon - The icon component to render.
  68. * @returns {React$Element<*>}
  69. */
  70. _renderIconAvatar(icon) {
  71. const { color, size } = this.props;
  72. return (
  73. <View
  74. style = { [
  75. styles.initialsContainer,
  76. {
  77. backgroundColor: color
  78. }
  79. ] }>
  80. <Icon
  81. src = { icon }
  82. style = { styles.initialsText(size) } />
  83. </View>
  84. );
  85. }
  86. /**
  87. * Renders the initials-based avatar.
  88. *
  89. * @returns {React$Element<*>}
  90. */
  91. _renderInitialsAvatar() {
  92. const { color, initials, size } = this.props;
  93. return (
  94. <View
  95. style = { [
  96. styles.initialsContainer,
  97. {
  98. backgroundColor: color
  99. }
  100. ] }>
  101. <Text style = { styles.initialsText(size) }> { initials } </Text>
  102. </View>
  103. );
  104. }
  105. /**
  106. * Renders the url-based avatar.
  107. *
  108. * @returns {React$Element<*>}
  109. */
  110. _renderURLAvatar() {
  111. const { onAvatarLoadError, size, url } = this.props;
  112. return (
  113. <Image
  114. defaultSource = { DEFAULT_AVATAR }
  115. onError = { onAvatarLoadError }
  116. resizeMode = 'cover'
  117. source = {{ uri: url }}
  118. style = { styles.avatarContent(size) } />
  119. );
  120. }
  121. }