Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

Avatar.native.js 3.2KB

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