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

StatelessAvatar.js 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. alt = 'avatar'
  57. className = { this._getAvatarClassName() }
  58. data-testid = { this.props.testId }
  59. id = { this.props.id }
  60. onError = { this.props.onAvatarLoadError }
  61. src = { url }
  62. style = { this._getAvatarStyle() } />
  63. </div>
  64. );
  65. }
  66. if (initials) {
  67. return (
  68. <div
  69. className = { `${this._getAvatarClassName()} ${this._getBadgeClassName()}` }
  70. data-testid = { this.props.testId }
  71. id = { this.props.id }
  72. style = { this._getAvatarStyle(this.props.color) }>
  73. <svg
  74. className = 'avatar-svg'
  75. viewBox = '0 0 100 100'
  76. xmlns = 'http://www.w3.org/2000/svg'
  77. xmlnsXlink = 'http://www.w3.org/1999/xlink'>
  78. <text
  79. dominantBaseline = 'central'
  80. fill = 'rgba(255,255,255,1)'
  81. fontSize = '40pt'
  82. textAnchor = 'middle'
  83. x = '50'
  84. y = '50'>
  85. { initials }
  86. </text>
  87. </svg>
  88. </div>
  89. );
  90. }
  91. // default avatar
  92. return (
  93. <div className = { this._getBadgeClassName() }>
  94. <img
  95. alt = 'avatar'
  96. className = { this._getAvatarClassName('defaultAvatar') }
  97. data-testid = { this.props.testId }
  98. id = { this.props.id }
  99. src = { this.props.defaultAvatar || 'images/avatar.png' }
  100. style = { this._getAvatarStyle() } />
  101. </div>
  102. );
  103. }
  104. /**
  105. * Constructs a style object to be used on the avatars.
  106. *
  107. * @param {string?} color - The desired background color.
  108. * @returns {Object}
  109. */
  110. _getAvatarStyle(color) {
  111. const { size } = this.props;
  112. return {
  113. background: color || undefined,
  114. fontSize: size ? size * 0.5 : '180%',
  115. height: size || '100%',
  116. width: size || '100%'
  117. };
  118. }
  119. /**
  120. * Constructs a list of class names required for the avatar component.
  121. *
  122. * @param {string} additional - Any additional class to add.
  123. * @returns {string}
  124. */
  125. _getAvatarClassName(additional) {
  126. return `avatar ${additional || ''} ${this.props.className || ''}`;
  127. }
  128. /**
  129. * Generates a class name to render a badge on the avatar, if necessary.
  130. *
  131. * @returns {string}
  132. */
  133. _getBadgeClassName() {
  134. const { status } = this.props;
  135. if (status) {
  136. return `avatar-badge avatar-badge-${status}`;
  137. }
  138. return '';
  139. }
  140. _isIcon: (?string | ?Object) => boolean
  141. }