avatar_composite.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import ImmutablePropTypes from 'react-immutable-proptypes';
  4. import { autoPlayGif } from '../initial_state';
  5. import Avatar from './avatar';
  6. export default class AvatarComposite extends React.PureComponent {
  7. static propTypes = {
  8. accounts: ImmutablePropTypes.list.isRequired,
  9. animate: PropTypes.bool,
  10. size: PropTypes.number.isRequired,
  11. };
  12. static defaultProps = {
  13. animate: autoPlayGif,
  14. };
  15. renderItem (account, size, index) {
  16. const { animate } = this.props;
  17. let width = 50;
  18. let height = 100;
  19. let top = 'auto';
  20. let left = 'auto';
  21. let bottom = 'auto';
  22. let right = 'auto';
  23. if (size === 1) {
  24. width = 100;
  25. }
  26. if (size === 4 || (size === 3 && index > 0)) {
  27. height = 50;
  28. }
  29. if (size === 2) {
  30. if (index === 0) {
  31. right = '1px';
  32. } else {
  33. left = '1px';
  34. }
  35. } else if (size === 3) {
  36. if (index === 0) {
  37. right = '1px';
  38. } else if (index > 0) {
  39. left = '1px';
  40. }
  41. if (index === 1) {
  42. bottom = '1px';
  43. } else if (index > 1) {
  44. top = '1px';
  45. }
  46. } else if (size === 4) {
  47. if (index === 0 || index === 2) {
  48. right = '1px';
  49. }
  50. if (index === 1 || index === 3) {
  51. left = '1px';
  52. }
  53. if (index < 2) {
  54. bottom = '1px';
  55. } else {
  56. top = '1px';
  57. }
  58. }
  59. const style = {
  60. left: left,
  61. top: top,
  62. right: right,
  63. bottom: bottom,
  64. width: `${width}%`,
  65. height: `${height}%`,
  66. };
  67. return (
  68. <div key={account.get('id')} style={style}>
  69. <Avatar account={account} animate={animate} />
  70. </div>
  71. );
  72. }
  73. render() {
  74. const { accounts, size } = this.props;
  75. return (
  76. <div className='account__avatar-composite' style={{ width: `${size}px`, height: `${size}px` }}>
  77. {accounts.take(4).map((account, i) => this.renderItem(account, Math.min(accounts.size, 4), i))}
  78. {accounts.size > 4 && (
  79. <span className='account__avatar-composite__label'>
  80. +{accounts.size - 4}
  81. </span>
  82. )}
  83. </div>
  84. );
  85. }
  86. }