summaryTableHeader.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. const React = require('react');
  2. function getSortDetails(sortKey, activeSort) {
  3. let newSort = { sortKey, order: 'desc' };
  4. let sortClass = '';
  5. if (activeSort && activeSort.sortKey === sortKey) {
  6. sortClass = 'sorted';
  7. if (activeSort.order === 'desc') {
  8. sortClass += '-desc';
  9. newSort.order = 'asc';
  10. } else {
  11. if (sortKey !== 'file') {
  12. newSort = { sortKey: 'file', order: 'desc' };
  13. }
  14. }
  15. }
  16. return {
  17. newSort,
  18. sortClass
  19. };
  20. }
  21. function SummaryTableHeaderCell({ name, onSort, sortKey, activeSort }) {
  22. const { newSort, sortClass } = getSortDetails(sortKey, activeSort);
  23. return (
  24. <th
  25. className={'sortable headercell ' + sortClass}
  26. onClick={() => onSort(newSort)}
  27. >
  28. {name}
  29. <span className="sorter" />
  30. </th>
  31. );
  32. }
  33. function FileHeaderCell({ onSort, activeSort }) {
  34. const { newSort, sortClass } = getSortDetails('file', activeSort);
  35. return (
  36. <th
  37. className={'sortable file ' + sortClass}
  38. onClick={() => onSort(newSort)}
  39. >
  40. File
  41. <span className="sorter" />
  42. </th>
  43. );
  44. }
  45. function SubHeadings({ sortKeyPrefix, onSort, activeSort }) {
  46. return (
  47. <>
  48. <SummaryTableHeaderCell
  49. name="%"
  50. onSort={onSort}
  51. sortKey={sortKeyPrefix + '.pct'}
  52. activeSort={activeSort}
  53. />
  54. <th className="headercell"></th>
  55. <SummaryTableHeaderCell
  56. name="Covered"
  57. onSort={onSort}
  58. sortKey={sortKeyPrefix + '.covered'}
  59. activeSort={activeSort}
  60. />
  61. <SummaryTableHeaderCell
  62. name="Missed"
  63. onSort={onSort}
  64. sortKey={sortKeyPrefix + '.missed'}
  65. activeSort={activeSort}
  66. />
  67. <SummaryTableHeaderCell
  68. name="Total"
  69. onSort={onSort}
  70. sortKey={sortKeyPrefix + '.total'}
  71. activeSort={activeSort}
  72. />
  73. </>
  74. );
  75. }
  76. module.exports = function SummaryTableHeader({
  77. onSort,
  78. activeSort,
  79. metricsToShow
  80. }) {
  81. return (
  82. <thead>
  83. <tr className="topheading">
  84. <th></th>
  85. {metricsToShow.statements && <th colSpan={4}>Statements</th>}
  86. {metricsToShow.branches && <th colSpan={4}>Branches</th>}
  87. {metricsToShow.functions && <th colSpan={4}>Functions</th>}
  88. {metricsToShow.lines && <th colSpan={4}>Lines</th>}
  89. </tr>
  90. <tr className="subheading">
  91. <FileHeaderCell onSort={onSort} activeSort={activeSort} />
  92. {metricsToShow.statements && (
  93. <SubHeadings
  94. sortKeyPrefix="statements"
  95. onSort={onSort}
  96. activeSort={activeSort}
  97. />
  98. )}
  99. {metricsToShow.branches && (
  100. <SubHeadings
  101. sortKeyPrefix="branches"
  102. onSort={onSort}
  103. activeSort={activeSort}
  104. />
  105. )}
  106. {metricsToShow.functions && (
  107. <SubHeadings
  108. sortKeyPrefix="functions"
  109. onSort={onSort}
  110. activeSort={activeSort}
  111. />
  112. )}
  113. {metricsToShow.lines && (
  114. <SubHeadings
  115. sortKeyPrefix="lines"
  116. onSort={onSort}
  117. activeSort={activeSort}
  118. />
  119. )}
  120. </tr>
  121. </thead>
  122. );
  123. };