node-details-generic-table.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import React from 'react';
  2. import sortBy from 'lodash/sortBy';
  3. import { Map as makeMap } from 'immutable';
  4. import { NODE_DETAILS_DATA_ROWS_DEFAULT_LIMIT } from '../../constants/limits';
  5. import {
  6. isNumeric,
  7. getTableColumnsStyles,
  8. genericTableEntryKey
  9. } from '../../utils/node-details-utils';
  10. import NodeDetailsTableHeaders from './node-details-table-headers';
  11. import MatchedText from '../matched-text';
  12. import ShowMore from '../show-more';
  13. function sortedRows(rows, columns, sortedBy, sortedDesc) {
  14. const column = columns.find(c => c.id === sortedBy);
  15. const sorted = sortBy(rows, (row) => {
  16. let value = row.entries[sortedBy];
  17. if (isNumeric(column)) {
  18. value = parseFloat(value);
  19. }
  20. return value;
  21. });
  22. if (sortedDesc) {
  23. sorted.reverse();
  24. }
  25. return sorted;
  26. }
  27. export default class NodeDetailsGenericTable extends React.Component {
  28. constructor(props, context) {
  29. super(props, context);
  30. this.state = {
  31. limit: NODE_DETAILS_DATA_ROWS_DEFAULT_LIMIT,
  32. sortedBy: props.columns && props.columns[0].id,
  33. sortedDesc: true
  34. };
  35. this.handleLimitClick = this.handleLimitClick.bind(this);
  36. this.updateSorted = this.updateSorted.bind(this);
  37. }
  38. updateSorted(sortedBy, sortedDesc) {
  39. this.setState({ sortedBy, sortedDesc });
  40. }
  41. handleLimitClick() {
  42. this.setState(prevState => ({
  43. limit: prevState.limit ? 0 : NODE_DETAILS_DATA_ROWS_DEFAULT_LIMIT
  44. }));
  45. }
  46. render() {
  47. const { sortedBy, sortedDesc } = this.state;
  48. const { columns, matches = makeMap() } = this.props;
  49. const expanded = this.state.limit === 0;
  50. let rows = this.props.rows || [];
  51. let notShown = 0;
  52. // If there are rows that would be hidden behind 'show more', keep them
  53. // expanded if any of them match the search query; otherwise hide them.
  54. if (this.state.limit > 0 && rows.length > this.state.limit) {
  55. const hasHiddenMatch = rows
  56. .slice(this.state.limit)
  57. .some(
  58. row => columns.some(column => matches.has(genericTableEntryKey(row, column)))
  59. );
  60. if (!hasHiddenMatch) {
  61. notShown = rows.length - NODE_DETAILS_DATA_ROWS_DEFAULT_LIMIT;
  62. rows = rows.slice(0, this.state.limit);
  63. }
  64. }
  65. const styles = getTableColumnsStyles(columns);
  66. return (
  67. <div className="node-details-generic-table">
  68. <table>
  69. <thead>
  70. <NodeDetailsTableHeaders
  71. headers={columns}
  72. sortedBy={sortedBy}
  73. sortedDesc={sortedDesc}
  74. onClick={this.updateSorted}
  75. />
  76. </thead>
  77. <tbody>
  78. {sortedRows(rows, columns, sortedBy, sortedDesc).map(row => (
  79. <tr className="node-details-generic-table-row" key={row.id}>
  80. {columns.map((column, index) => {
  81. const match = matches.get(genericTableEntryKey(row, column));
  82. const value = row.entries[column.id];
  83. return (
  84. <td
  85. className="node-details-generic-table-value truncate"
  86. title={value}
  87. key={column.id}
  88. style={styles[index]}>
  89. {column.dataType === 'link'
  90. ? (
  91. <a
  92. rel="noopener noreferrer"
  93. target="_blank"
  94. className="node-details-table-node-link"
  95. href={value}>
  96. {value}
  97. </a>
  98. )
  99. : <MatchedText text={value} match={match} />
  100. }
  101. </td>
  102. );
  103. })}
  104. </tr>
  105. ))}
  106. </tbody>
  107. </table>
  108. <ShowMore
  109. handleClick={this.handleLimitClick}
  110. collection={this.props.rows}
  111. expanded={expanded}
  112. notShown={notShown}
  113. />
  114. </div>
  115. );
  116. }
  117. }