node-details-table-headers.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import React from 'react';
  2. import { defaultSortDesc, getTableColumnsStyles } from '../../utils/node-details-utils';
  3. import { NODE_DETAILS_TABLE_CW, NODE_DETAILS_TABLE_XS_LABEL } from '../../constants/styles';
  4. export default class NodeDetailsTableHeaders extends React.Component {
  5. handleClick(ev, headerId, currentSortedBy, currentSortedDesc) {
  6. ev.preventDefault();
  7. const header = this.props.headers.find(h => h.id === headerId);
  8. const sortedBy = header.id;
  9. const sortedDesc = sortedBy === currentSortedBy
  10. ? !currentSortedDesc : defaultSortDesc(header);
  11. this.props.onClick(sortedBy, sortedDesc);
  12. }
  13. render() {
  14. const { headers, sortedBy, sortedDesc } = this.props;
  15. const colStyles = getTableColumnsStyles(headers);
  16. return (
  17. <tr>
  18. {headers.map((header, index) => {
  19. const headerClasses = ['node-details-table-header', 'truncate'];
  20. const onClick = (ev) => {
  21. this.handleClick(ev, header.id, sortedBy, sortedDesc);
  22. };
  23. // sort by first metric by default
  24. const isSorted = header.id === sortedBy;
  25. const isSortedDesc = isSorted && sortedDesc;
  26. const isSortedAsc = isSorted && !isSortedDesc;
  27. if (isSorted) {
  28. headerClasses.push('node-details-table-header-sorted');
  29. }
  30. const style = colStyles[index];
  31. const label = (
  32. style.width === NODE_DETAILS_TABLE_CW.XS && NODE_DETAILS_TABLE_XS_LABEL[header.id]
  33. ) ? NODE_DETAILS_TABLE_XS_LABEL[header.id] : header.label;
  34. return (
  35. <td className={headerClasses.join(' ')} style={style} title={header.label} key={header.id}>
  36. <div className="node-details-table-header-sortable" onClick={onClick}>
  37. {isSortedAsc
  38. && <i className="node-details-table-header-sorter fa fa-caret-up" />}
  39. {isSortedDesc
  40. && <i className="node-details-table-header-sorter fa fa-caret-down" />}
  41. {label}
  42. </div>
  43. </td>
  44. );
  45. })}
  46. </tr>
  47. );
  48. }
  49. }