選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

StatelessAvatar.js 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. import { Icon } from '../../../font-icons';
  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. const icon = this._parseIconUrl(url);
  29. if (icon) {
  30. avatar = this._renderIconAvatar(icon);
  31. } else if (url) {
  32. avatar = this._renderURLAvatar();
  33. } else if (initials) {
  34. avatar = this._renderInitialsAvatar();
  35. } else {
  36. avatar = this._renderDefaultAvatar();
  37. }
  38. return (
  39. <View
  40. style = { [
  41. styles.avatarContainer(size),
  42. style
  43. ] }>
  44. { avatar }
  45. </View>
  46. );
  47. }
  48. _parseIconUrl: ?string => ?string
  49. /**
  50. * Renders the default avatar.
  51. *
  52. * @returns {React$Element<*>}
  53. */
  54. _renderDefaultAvatar() {
  55. const { size } = this.props;
  56. return (
  57. <Image
  58. source = { DEFAULT_AVATAR }
  59. style = { [
  60. styles.avatarContent(size),
  61. styles.staticAvatar
  62. ] } />
  63. );
  64. }
  65. /**
  66. * Renders the initials-based avatar.
  67. *
  68. * @param {string} icon - The icon name to render.
  69. * @returns {React$Element<*>}
  70. */
  71. _renderIconAvatar(icon) {
  72. const { color, size } = this.props;
  73. return (
  74. <View
  75. style = { [
  76. styles.initialsContainer,
  77. {
  78. backgroundColor: color
  79. }
  80. ] }>
  81. <Icon
  82. name = { icon }
  83. style = { styles.initialsText(size) } />
  84. </View>
  85. );
  86. }
  87. /**
  88. * Renders the initials-based avatar.
  89. *
  90. * @returns {React$Element<*>}
  91. */
  92. _renderInitialsAvatar() {
  93. const { color, initials, size } = this.props;
  94. return (
  95. <View
  96. style = { [
  97. styles.initialsContainer,
  98. {
  99. backgroundColor: color
  100. }
  101. ] }>
  102. <Text style = { styles.initialsText(size) }> { initials } </Text>
  103. </View>
  104. );
  105. }
  106. /**
  107. * Renders the url-based avatar.
  108. *
  109. * @returns {React$Element<*>}
  110. */
  111. _renderURLAvatar() {
  112. const { onAvatarLoadError, size, url } = this.props;
  113. return (
  114. <Image
  115. defaultSource = { DEFAULT_AVATAR }
  116. onError = { onAvatarLoadError }
  117. resizeMode = 'cover'
  118. source = {{ uri: url }}
  119. style = { styles.avatarContent(size) } />
  120. );
  121. }
  122. }