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.native.js 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import React, { Component } from 'react';
  2. import { CustomCachedImage } from 'react-native-img-cache';
  3. import AvatarImage from './AvatarImage';
  4. /**
  5. * Implements an avatar as a React Native/mobile {@link Component}.
  6. */
  7. export default class Avatar extends Component {
  8. /**
  9. * Avatar component's property types.
  10. *
  11. * @static
  12. */
  13. static propTypes = {
  14. /**
  15. * The optional style to add to the {@link Avatar} in order to customize
  16. * its base look (and feel).
  17. */
  18. style: React.PropTypes.object,
  19. /**
  20. * The URI of the {@link Avatar}.
  21. *
  22. * @type {string}
  23. */
  24. uri: React.PropTypes.string
  25. };
  26. /**
  27. * Initializes a new Avatar instance.
  28. *
  29. * @param {Object} props - The read-only React Component props with which
  30. * the new instance is to be initialized.
  31. */
  32. constructor(props) {
  33. super(props);
  34. // Fork (in Facebook/React speak) the prop uri because Image will
  35. // receive it through a source object. Additionally, other props may be
  36. // forked as well.
  37. this.componentWillReceiveProps(props);
  38. }
  39. /**
  40. * Notifies this mounted React Component that it will receive new props.
  41. * Forks (in Facebook/React speak) the prop {@code uri} because
  42. * {@link Image} will receive it through a {@code source} object.
  43. * Additionally, other props may be forked as well.
  44. *
  45. * @inheritdoc
  46. * @param {Object} nextProps - The read-only React Component props that this
  47. * instance will receive.
  48. * @returns {void}
  49. */
  50. componentWillReceiveProps(nextProps) {
  51. const prevURI = this.props && this.props.uri;
  52. const nextURI = nextProps && nextProps.uri;
  53. if (prevURI !== nextURI || !this.state) {
  54. const nextState = {
  55. /**
  56. * The source of the {@link Image} which is the actual
  57. * representation of this {@link Avatar}. The state
  58. * {@code source} was explicitly introduced in order to reduce
  59. * unnecessary renders.
  60. *
  61. * @type {{
  62. * uri: string
  63. * }}
  64. */
  65. source: {
  66. uri: nextURI
  67. }
  68. };
  69. if (this.state) {
  70. this.setState(nextState);
  71. } else {
  72. this.state = nextState;
  73. }
  74. }
  75. }
  76. /**
  77. * Implements React's {@link Component#render()}.
  78. *
  79. * @inheritdoc
  80. */
  81. render() {
  82. // Propagate all props of this Avatar but the ones consumed by this
  83. // Avatar to the Image it renders.
  84. // eslint-disable-next-line no-unused-vars
  85. const { uri, ...props } = this.props;
  86. return (
  87. <CustomCachedImage
  88. { ...props }
  89. component = { AvatarImage }
  90. resizeMode = 'contain'
  91. source = { this.state.source } />
  92. );
  93. }
  94. }