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.

Avatar.js 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // @flow
  2. import React from 'react';
  3. import { connect } from '../../../redux';
  4. import AbstractAvatar, {
  5. _mapStateToProps,
  6. type Props as AbstractProps
  7. } from '../AbstractAvatar';
  8. type Props = AbstractProps & {
  9. className?: string,
  10. id: string
  11. };
  12. /**
  13. * Implements an avatar as a React/Web {@link Component}.
  14. */
  15. class Avatar extends AbstractAvatar<Props> {
  16. /**
  17. * Constructs a style object to be used on the avatars.
  18. *
  19. * @param {string?} color - The desired background color.
  20. * @returns {Object}
  21. */
  22. _getAvatarStyle(color) {
  23. const { size } = this.props;
  24. return {
  25. backgroundColor: color || undefined,
  26. fontSize: size ? size * 0.5 : '180%',
  27. height: size || '100%',
  28. width: size || '100%'
  29. };
  30. }
  31. /**
  32. * Constructs a list of class names required for the avatar component.
  33. *
  34. * @param {string} additional - Any additional class to add.
  35. * @returns {string}
  36. */
  37. _getAvatarClassName(additional) {
  38. return `avatar ${additional || ''} ${this.props.className || ''}`;
  39. }
  40. _onAvatarLoadError: () => void;
  41. /**
  42. * Implements {@code AbstractAvatar#_renderDefaultAvatar}.
  43. *
  44. * @inheritdoc
  45. */
  46. _renderDefaultAvatar() {
  47. return (
  48. <img
  49. className = { this._getAvatarClassName('defaultAvatar') }
  50. id = { this.props.id }
  51. src = '/images/avatar.png'
  52. style = { this._getAvatarStyle() } />
  53. );
  54. }
  55. /**
  56. * Implements {@code AbstractAvatar#_renderGravatar}.
  57. *
  58. * @inheritdoc
  59. */
  60. _renderInitialsAvatar(initials, color) {
  61. return (
  62. <div
  63. className = { this._getAvatarClassName() }
  64. id = { this.props.id }
  65. style = { this._getAvatarStyle(color) }>
  66. { initials }
  67. </div>
  68. );
  69. }
  70. /**
  71. * Implements {@code AbstractAvatar#_renderGravatar}.
  72. *
  73. * @inheritdoc
  74. */
  75. _renderURLAvatar(uri) {
  76. return (
  77. <img
  78. className = { this._getAvatarClassName() }
  79. id = { this.props.id }
  80. onError = { this._onAvatarLoadError }
  81. src = { uri }
  82. style = { this._getAvatarStyle() } />
  83. );
  84. }
  85. }
  86. export default connect(_mapStateToProps)(Avatar);