Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import React, { Component } from 'react';
  2. import { Image } from 'react-native';
  3. /**
  4. * Display a participant avatar.
  5. */
  6. export default class Avatar extends Component {
  7. /**
  8. * Avatar component's property types.
  9. *
  10. * @static
  11. */
  12. static propTypes = {
  13. /**
  14. * The optional style to add to an Avatar in order to customize its base
  15. * look (and feel).
  16. */
  17. style: React.PropTypes.object,
  18. uri: React.PropTypes.string
  19. };
  20. /**
  21. * Initializes a new Avatar instance.
  22. *
  23. * @param {Object} props - The read-only React Component props with which
  24. * the new instance is to be initialized.
  25. */
  26. constructor(props) {
  27. super(props);
  28. // Fork (in Facebook/React speak) the prop uri because Image will
  29. // receive it through a source object. Additionally, other props may be
  30. // forked as well.
  31. this.componentWillReceiveProps(props);
  32. }
  33. /**
  34. * Notifies this mounted React Component that it will receive new props.
  35. * Forks (in Facebook/React speak) the prop {@code uri} because
  36. * {@link Image} will receive it through a {@code source} object.
  37. * Additionally, other props may be forked as well.
  38. *
  39. * @inheritdoc
  40. * @param {Object} nextProps - The read-only React Component props that this
  41. * instance will receive.
  42. * @returns {void}
  43. */
  44. componentWillReceiveProps(nextProps) {
  45. // uri
  46. const prevURI = this.props && this.props.uri;
  47. const nextURI = nextProps && nextProps.uri;
  48. let nextState;
  49. if (prevURI !== nextURI || !this.state) {
  50. nextState = {
  51. ...nextState,
  52. /**
  53. * The source of the {@link Image} which is the actual
  54. * representation of this {@link Avatar}. As {@code Avatar}
  55. * accepts a single URI and {@code Image} deals with a set of
  56. * possibly multiple URIs, the state {@code source} was
  57. * explicitly introduced in order to reduce unnecessary renders.
  58. *
  59. * @type {{
  60. * uri: string
  61. * }}
  62. */
  63. source: {
  64. uri: nextURI
  65. }
  66. };
  67. }
  68. if (nextState) {
  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. return (
  83. <Image
  84. // XXX Avatar is expected to display the whole image.
  85. resizeMode = 'contain'
  86. source = { this.state.source }
  87. style = { this.props.style } />
  88. );
  89. }
  90. }