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

StatelessAvatar.js 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // @flow
  2. import React from 'react';
  3. import AbstractStatelessAvatar, { type Props as AbstractProps } from '../AbstractStatelessAvatar';
  4. type Props = AbstractProps & {
  5. /**
  6. * External class name passed through props.
  7. */
  8. className?: string,
  9. /**
  10. * The default avatar URL if we want to override the app bundled one (e.g. AlwaysOnTop)
  11. */
  12. defaultAvatar?: string,
  13. /**
  14. * ID of the component to be rendered.
  15. */
  16. id?: string
  17. };
  18. /**
  19. * Implements a stateless avatar component that renders an avatar purely from what gets passed through
  20. * props.
  21. */
  22. export default class StatelessAvatar extends AbstractStatelessAvatar<Props> {
  23. /**
  24. * Implements {@code Component#render}.
  25. *
  26. * @inheritdoc
  27. */
  28. render() {
  29. const { initials, url } = this.props;
  30. if (url) {
  31. return (
  32. <img
  33. className = { this._getAvatarClassName() }
  34. id = { this.props.id }
  35. onError = { this.props.onAvatarLoadError }
  36. src = { url }
  37. style = { this._getAvatarStyle() } />
  38. );
  39. }
  40. if (initials) {
  41. return (
  42. <div
  43. className = { this._getAvatarClassName() }
  44. id = { this.props.id }
  45. style = { this._getAvatarStyle(this.props.color) }>
  46. <svg
  47. className = 'avatar-svg'
  48. viewBox = '0 0 100 100'
  49. xmlns = 'http://www.w3.org/2000/svg'
  50. xmlnsXlink = 'http://www.w3.org/1999/xlink'>
  51. <foreignObject
  52. height = '100%'
  53. width = '100%'>
  54. <span
  55. className = 'avatar-foreign'>
  56. { initials }
  57. </span>
  58. </foreignObject>
  59. </svg>
  60. </div>
  61. );
  62. }
  63. // default avatar
  64. return (
  65. <img
  66. className = { this._getAvatarClassName('defaultAvatar') }
  67. id = { this.props.id }
  68. src = { this.props.defaultAvatar || 'images/avatar.png' }
  69. style = { this._getAvatarStyle() } />
  70. );
  71. }
  72. /**
  73. * Constructs a style object to be used on the avatars.
  74. *
  75. * @param {string?} color - The desired background color.
  76. * @returns {Object}
  77. */
  78. _getAvatarStyle(color) {
  79. const { size } = this.props;
  80. return {
  81. backgroundColor: color || undefined,
  82. fontSize: size ? size * 0.5 : '180%',
  83. height: size || '100%',
  84. width: size || '100%'
  85. };
  86. }
  87. /**
  88. * Constructs a list of class names required for the avatar component.
  89. *
  90. * @param {string} additional - Any additional class to add.
  91. * @returns {string}
  92. */
  93. _getAvatarClassName(additional) {
  94. return `avatar ${additional || ''} ${this.props.className || ''}`;
  95. }
  96. }