node-details-relatives.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import React from 'react';
  2. import { Map as makeMap } from 'immutable';
  3. import { NODE_DETAILS_DATA_ROWS_DEFAULT_LIMIT } from '../../constants/limits';
  4. import NodeDetailsRelativesLink from './node-details-relatives-link';
  5. export default class NodeDetailsRelatives extends React.Component {
  6. constructor(props, context) {
  7. super(props, context);
  8. this.state = {
  9. limit: NODE_DETAILS_DATA_ROWS_DEFAULT_LIMIT
  10. };
  11. this.handleLimitClick = this.handleLimitClick.bind(this);
  12. }
  13. handleLimitClick(ev) {
  14. ev.preventDefault();
  15. this.setState(prevState => ({
  16. limit: prevState.limit ? 0 : NODE_DETAILS_DATA_ROWS_DEFAULT_LIMIT
  17. }));
  18. }
  19. render() {
  20. let { relatives } = this.props;
  21. const { matches = makeMap() } = this.props;
  22. const limited = this.state.limit > 0 && relatives.length > this.state.limit;
  23. const showLimitAction = limited || (this.state.limit === 0
  24. && relatives.length > NODE_DETAILS_DATA_ROWS_DEFAULT_LIMIT);
  25. const limitActionText = limited ? 'Show more' : 'Show less';
  26. if (limited) {
  27. relatives = relatives.slice(0, this.state.limit);
  28. }
  29. return (
  30. <div className="node-details-relatives">
  31. {relatives.map(relative => (
  32. <NodeDetailsRelativesLink
  33. key={relative.id}
  34. match={matches.get(relative.id)}
  35. {...relative} />
  36. ))}
  37. {showLimitAction
  38. && (
  39. <span
  40. className="node-details-relatives-more"
  41. onClick={this.handleLimitClick}>
  42. {limitActionText}
  43. </span>
  44. )
  45. }
  46. </div>
  47. );
  48. }
  49. }