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 2.6KB

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