您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

StatelessAvatar.js 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // @flow
  2. import React from 'react';
  3. import { Icon } from '../../../icons';
  4. import AbstractStatelessAvatar, { type Props as AbstractProps } from '../AbstractStatelessAvatar';
  5. type Props = AbstractProps & {
  6. /**
  7. * External class name passed through props.
  8. */
  9. className?: string,
  10. /**
  11. * The default avatar URL if we want to override the app bundled one (e.g. AlwaysOnTop)
  12. */
  13. defaultAvatar?: string,
  14. /**
  15. * ID of the component to be rendered.
  16. */
  17. id?: string,
  18. /**
  19. * One of the expected status strings (e.g. 'available') to render a badge on the avatar, if necessary.
  20. */
  21. status?: ?string,
  22. /**
  23. * TestId of the element, if any.
  24. */
  25. testId?: string
  26. };
  27. /**
  28. * Implements a stateless avatar component that renders an avatar purely from what gets passed through
  29. * props.
  30. */
  31. export default class StatelessAvatar extends AbstractStatelessAvatar<Props> {
  32. /**
  33. * Implements {@code Component#render}.
  34. *
  35. * @inheritdoc
  36. */
  37. render() {
  38. const { initials, url } = this.props;
  39. if (this._isIcon(url)) {
  40. return (
  41. <div
  42. className = { `${this._getAvatarClassName()} ${this._getBadgeClassName()}` }
  43. data-testid = { this.props.testId }
  44. id = { this.props.id }
  45. style = { this._getAvatarStyle(this.props.color) }>
  46. <Icon
  47. size = '50%'
  48. src = { url } />
  49. </div>
  50. );
  51. }
  52. if (url) {
  53. return (
  54. <div className = { this._getBadgeClassName() }>
  55. <img
  56. className = { this._getAvatarClassName() }
  57. data-testid = { this.props.testId }
  58. id = { this.props.id }
  59. onError = { this.props.onAvatarLoadError }
  60. src = { url }
  61. style = { this._getAvatarStyle() } />
  62. </div>
  63. );
  64. }
  65. if (initials) {
  66. return (
  67. <div
  68. className = { `${this._getAvatarClassName()} ${this._getBadgeClassName()}` }
  69. data-testid = { this.props.testId }
  70. id = { this.props.id }
  71. style = { this._getAvatarStyle(this.props.color) }>
  72. <svg
  73. className = 'avatar-svg'
  74. viewBox = '0 0 100 100'
  75. xmlns = 'http://www.w3.org/2000/svg'
  76. xmlnsXlink = 'http://www.w3.org/1999/xlink'>
  77. <text
  78. dominantBaseline = 'central'
  79. fill = 'rgba(255,255,255,.6)'
  80. fontSize = '40pt'
  81. textAnchor = 'middle'
  82. x = '50'
  83. y = '50'>
  84. { initials }
  85. </text>
  86. </svg>
  87. </div>
  88. );
  89. }
  90. // default avatar
  91. return (
  92. <div className = { this._getBadgeClassName() }>
  93. <img
  94. className = { this._getAvatarClassName('defaultAvatar') }
  95. data-testid = { this.props.testId }
  96. id = { this.props.id }
  97. src = { this.props.defaultAvatar || 'images/avatar.png' }
  98. style = { this._getAvatarStyle() } />
  99. </div>
  100. );
  101. }
  102. /**
  103. * Constructs a style object to be used on the avatars.
  104. *
  105. * @param {string?} color - The desired background color.
  106. * @returns {Object}
  107. */
  108. _getAvatarStyle(color) {
  109. const { size } = this.props;
  110. return {
  111. backgroundColor: color || undefined,
  112. fontSize: size ? size * 0.5 : '180%',
  113. height: size || '100%',
  114. width: size || '100%'
  115. };
  116. }
  117. /**
  118. * Constructs a list of class names required for the avatar component.
  119. *
  120. * @param {string} additional - Any additional class to add.
  121. * @returns {string}
  122. */
  123. _getAvatarClassName(additional) {
  124. return `avatar ${additional || ''} ${this.props.className || ''}`;
  125. }
  126. /**
  127. * Generates a class name to render a badge on the avatar, if necessary.
  128. *
  129. * @returns {string}
  130. */
  131. _getBadgeClassName() {
  132. const { status } = this.props;
  133. if (status) {
  134. return `avatar-badge avatar-badge-${status}`;
  135. }
  136. return '';
  137. }
  138. _isIcon: (?string | ?Object) => boolean
  139. }