src/components/molecules/Table/index.tsx   A
last analyzed

Complexity

Total Complexity 3
Complexity/F 0

Size

Lines of Code 87
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
eloc 46
mnd 3
bc 3
fnc 0
dl 0
loc 87
ccs 18
cts 18
cp 1
rs 10
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
1 1
import "./styles.css";
2
import { TableComponent } from "./types";
3
4 1
import React from "react";
5
6 1
import classNames from "classnames";
7 1
import { buildBoxComponent } from "../../../utils";
8
9
/**
10
 * A standard Table component. It follows a CSV-like format for its content.
11
 *
12
 * @since 0.9.0
13
 *
14
 * @param {(string | JSX.Element | Element)[][]} rows Table rows
15
 * @param {boolean} headers if true, the first row will be used as headers (and not generic data)
16
 * @param {string} className `common modular-ui prop` - custom className (to better customize it)
17
 * @param {boolean} unstyled `common modular-ui prop` - Style/unstyle component (to better customize it)
18
 * @param {string} id `common modular-ui prop` - `data-id` parameter (for testing purpose, to easily find the component into the DOM)
19
 * @param {boolean} dark `common modular-ui prop` - Enable/disable dark mode
20
 * @param {boolean} hide `common modular-ui prop` - Hide/show component
21
 * @param {boolean} shadow `common modular-ui prop` - Enable/disable shadow behind component (to better customize it)
22
 *
23
 * @example <caption>Example Table usage</caption>
24
 *
25
 * import { render } from "react-dom";
26
 * import { Table } from '@cianciarusocataldo/modular-ui';
27
 *
28
 * render(<Table rows={[["Header 1", "Header 2"],["Cell 1", "Cell 2"]]} headers />, document.getElementById("root"));
29
 *
30
 * @see https://cianciarusocataldo.github.io/modular-ui/components/molecules/Table
31
 *
32
 * @author Cataldo Cianciaruso <https://github.com/CianciarusoCataldo>
33
 *
34
 * @copyright 2022 Cataldo Cianciaruso
35
 */
36 3
const Table: TableComponent = ({ label, headers, rows, ...commonProps }) => {
37 3
  let gridTemplateRows = "";
38 3
  let gridTemplateColumns = "";
39
40 3
  let tableRows = rows || [];
41
42 3
  let elements: (JSX.Element | string)[][] = tableRows.map((row, rowIndex) =>
43 8
    row.map((element, index) => (
44
      <div
45
        key={`element_${rowIndex}_${index}`}
46
        className={classNames({
47
          header: headers && rowIndex === 0,
48
          element: !headers || rowIndex > 0,
49
        })}
50
      >
51
        {element}
52
      </div>
53
    ))
54
  );
55
56 3
  if (tableRows.length > 0) {
57 2
    for (let i = 0; i < rows.length; i++) {
58 4
      gridTemplateRows += " auto";
59
    }
60
61 2
    for (let i = 0; i < rows[0].length; i++) {
62 4
      gridTemplateColumns += " auto";
63
    }
64
  }
65
66 3
  return buildBoxComponent({
67 3
    callBack: () => ({
68
      name: "modular-table",
69
      Component: (
70
        <div
71
          className="rows"
72
          style={{
73
            gridTemplateRows,
74
            gridTemplateColumns,
75
          }}
76
        >
77
          {elements}
78
        </div>
79
      ),
80
      commonProps,
81
    }),
82
    label,
83
  });
84
};
85
86
export default Table;
87