node-details-property-list.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import React from 'react';
  2. import { Map as makeMap } from 'immutable';
  3. import sortBy from 'lodash/sortBy';
  4. import { NODE_DETAILS_DATA_ROWS_DEFAULT_LIMIT } from '../../constants/limits';
  5. import NodeDetailsControlButton from './node-details-control-button';
  6. import MatchedText from '../matched-text';
  7. import ShowMore from '../show-more';
  8. const Controls = controls => (
  9. <div className="node-details-property-list-controls">
  10. {sortBy(controls, 'rank').map(control => (
  11. <NodeDetailsControlButton
  12. nodeId={control.nodeId}
  13. control={control}
  14. key={control.id} />
  15. ))}
  16. </div>
  17. );
  18. export default class NodeDetailsPropertyList extends React.Component {
  19. constructor(props, context) {
  20. super(props, context);
  21. this.state = {
  22. limit: NODE_DETAILS_DATA_ROWS_DEFAULT_LIMIT,
  23. };
  24. this.handleLimitClick = this.handleLimitClick.bind(this);
  25. }
  26. handleLimitClick() {
  27. this.setState(prevState => ({
  28. limit: prevState.limit ? 0 : NODE_DETAILS_DATA_ROWS_DEFAULT_LIMIT
  29. }));
  30. }
  31. render() {
  32. const { controls, matches = makeMap() } = this.props;
  33. let { rows } = this.props;
  34. let notShown = 0;
  35. const limited = rows && this.state.limit > 0 && rows.length > this.state.limit;
  36. const expanded = this.state.limit === 0;
  37. if (rows && limited) {
  38. const hasNotShownMatch = rows.filter((row, index) => index >= this.state.limit
  39. && matches.has(row.id)).length > 0;
  40. if (!hasNotShownMatch) {
  41. notShown = rows.length - NODE_DETAILS_DATA_ROWS_DEFAULT_LIMIT;
  42. rows = rows.slice(0, this.state.limit);
  43. }
  44. }
  45. return (
  46. <div className="node-details-property-list">
  47. {controls && Controls(controls)}
  48. {rows.map(field => (
  49. <div className="node-details-property-list-field" key={field.id}>
  50. <div
  51. className="node-details-property-list-field-label truncate"
  52. title={field.entries.label}
  53. key={field.id}>
  54. {field.entries.label}
  55. </div>
  56. <div
  57. className="node-details-property-list-field-value truncate"
  58. title={field.entries.value}>
  59. <MatchedText text={field.entries.value} match={matches.get(field.id)} />
  60. </div>
  61. </div>
  62. ))}
  63. <ShowMore
  64. handleClick={this.handleLimitClick}
  65. collection={this.props.rows}
  66. expanded={expanded}
  67. notShown={notShown} />
  68. </div>
  69. );
  70. }
  71. }