react-data-matrix 0.3.4 → 0.3.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -61,7 +61,7 @@ const App: FC = () => {
61
61
 
62
62
  # Expected Data Structure
63
63
 
64
- - See [`./src/lib/utils/data.js`](https://github.com/bronz3beard/react-matrix/blob/main/src/lib/utils/data.js) for an example of the data, this can be used for testing if needed,
64
+ - See [`./src/lib/utils/data.ts`](https://github.com/bronz3beard/react-matrix/blob/main/src/lib/utils/data.ts) for an example of the data, this can be used for testing if needed,
65
65
  or an example of how to construct/deconstruct your data objects from your api.
66
66
 
67
67
  ```ts
@@ -0,0 +1,4 @@
1
+ import { FC } from "react";
2
+ import { MatrixHeaderProps } from "../types";
3
+ declare const MatrixHeaders: FC<MatrixHeaderProps>;
4
+ export default MatrixHeaders;
@@ -0,0 +1,38 @@
1
+ import React from "react";
2
+ import { getHeaderRowStyles, getHeaderTitleStyles, getHeaderSubTitleStyles, getHeaderPrimaryTitleStyles, } from "../helpers/getStyles";
3
+ import { capitaliseString } from "../utils/functions";
4
+ // import "./styles/riskMatrix.scss";
5
+ const MatrixHeaders = ({ data, hasInlineStyles = true, headerPrimaryUpper = true, thRowStyles = {}, thTitleStyles = {}, thSubTitleStyles = {}, thPrimaryTitleStyles = {}, customHeaderRowIdValue = "", customDynamicHeaderTitleIdValue = "", customDynamicSubHeaderTitleIdValue = "", }) => {
6
+ const headerRowStyles = getHeaderRowStyles(hasInlineStyles, thRowStyles);
7
+ const headerTitleStyles = getHeaderTitleStyles(hasInlineStyles, thTitleStyles);
8
+ const headerSubTitleStyles = getHeaderSubTitleStyles(hasInlineStyles, thSubTitleStyles);
9
+ const headerPrimaryTitleStyles = getHeaderPrimaryTitleStyles(hasInlineStyles, thPrimaryTitleStyles);
10
+ const headerPrimaryTitle = !headerPrimaryUpper
11
+ ? capitaliseString(data === null || data === void 0 ? void 0 : data.primary_header_title)
12
+ : data === null || data === void 0 ? void 0 : data.primary_header_title;
13
+ return (<thead>
14
+ <tr id="react-matrix-blank-headers-primary-title-row">
15
+ <th headers="blank" colSpan={4}></th>
16
+ <th style={headerPrimaryTitleStyles} id="react-matrix-header-primary-title">
17
+ {headerPrimaryTitle}
18
+ </th>
19
+ <th headers="blank" colSpan={2}></th>
20
+ </tr>
21
+ <tr style={headerRowStyles} id={`react-matrix-dynamic-headers-row-${customHeaderRowIdValue}`}>
22
+ <th headers="blank" colSpan={2}></th>
23
+ {data.matrix_details.slice(0, data.matrix_size).map((column, index) => {
24
+ return (<th scope="col" style={headerTitleStyles} id={`react-matrix-dynamic-column-header-title${!customDynamicHeaderTitleIdValue
25
+ ? ""
26
+ : `-${customDynamicHeaderTitleIdValue}`}`} key={`${column.id}-${index}`}>
27
+ {column.header_title}
28
+ <div style={headerSubTitleStyles} id={`react-matrix-dynamic-column-header-sub-title${!customDynamicSubHeaderTitleIdValue
29
+ ? ""
30
+ : `-${customDynamicSubHeaderTitleIdValue}`}`}>
31
+ {column.header_sub_title}
32
+ </div>
33
+ </th>);
34
+ })}
35
+ </tr>
36
+ </thead>);
37
+ };
38
+ export default MatrixHeaders;
@@ -0,0 +1,4 @@
1
+ import { FC } from "react";
2
+ import { MatrixRowsProps } from "../types";
3
+ declare const MatrixRows: FC<MatrixRowsProps>;
4
+ export default MatrixRows;
@@ -0,0 +1,48 @@
1
+ import React from "react";
2
+ import { getHeaderRowStyles, getHeaderTitleStyles, getHeaderSubTitleStyles, getHeaderPrimaryTitleStyles, } from "../helpers/getStyles";
3
+ import { groupObjectsByProp, capitaliseString } from "../utils/functions";
4
+ import TableData from "./TableData";
5
+ const MatrixRows = ({ data, rowPrimaryUpper = true, hasInlineStyles = true, reverseMatrixValues = true, trRowStyles = {}, trTitleStyles = {}, trSubTitleStyles = {}, trPrimaryTitleStyles = {}, tdStyles = {}, customRowDynamicIdValue = "", customRowHeaderDynamicIdValue = "", customTableDataDynamicIdValue = "", }) => {
6
+ const valuesArray = reverseMatrixValues
7
+ ? groupObjectsByProp(data.matrix_values, "likelihood_descriptor")
8
+ .reverse()
9
+ .slice(0, data.matrix_size)
10
+ : groupObjectsByProp(data.matrix_values, "likelihood_descriptor").slice(0, data.matrix_size);
11
+ const tableRowStyles = getHeaderRowStyles(hasInlineStyles, trRowStyles);
12
+ const tableRowHeaderTitleStyles = getHeaderTitleStyles(hasInlineStyles, trTitleStyles);
13
+ const tableRowHeaderSubTitleStyles = getHeaderSubTitleStyles(hasInlineStyles, trSubTitleStyles);
14
+ const rowHeaderPrimaryTitleStyles = getHeaderPrimaryTitleStyles(hasInlineStyles, trPrimaryTitleStyles);
15
+ const rowPrimaryTitle = rowPrimaryUpper
16
+ ? capitaliseString(data.primary_row_header_title)
17
+ : data.primary_row_header_title;
18
+ return (<tbody>
19
+ <tr id="react-matrix-row-primary-title">
20
+ <th scope="row" rowSpan={6} style={rowHeaderPrimaryTitleStyles}>
21
+ {rowPrimaryTitle}
22
+ </th>
23
+ </tr>
24
+ {valuesArray.map((row, index) => {
25
+ var _a, _b, _c, _d, _e, _f;
26
+ const rowHeaderOrder = reverseMatrixValues
27
+ ? data.matrix_size - (index + 1)
28
+ : index;
29
+ return (<tr style={tableRowStyles} key={`row-${index}`} id={`react-matrix-dynamic-rows-${(_a = data.matrix_details[rowHeaderOrder]) === null || _a === void 0 ? void 0 : _a.row_header_title}-${(_b = data.matrix_details[rowHeaderOrder]) === null || _b === void 0 ? void 0 : _b.row_header_sub_title}${!customRowDynamicIdValue ? "" : `-${customRowDynamicIdValue}`}`}>
30
+ <th scope="row" style={tableRowHeaderTitleStyles} id={`react-matrix-dynamic-rows-${(_c = data.matrix_details[rowHeaderOrder]) === null || _c === void 0 ? void 0 : _c.row_header_title}-${(_d = data.matrix_details[rowHeaderOrder]) === null || _d === void 0 ? void 0 : _d.row_header_sub_title}${!customRowHeaderDynamicIdValue
31
+ ? ""
32
+ : `-${customRowHeaderDynamicIdValue}`}`}>
33
+ {(_e = data.matrix_details[rowHeaderOrder]) === null || _e === void 0 ? void 0 : _e.row_header_title}
34
+ <div style={tableRowHeaderSubTitleStyles}>
35
+ {(_f = data.matrix_details[rowHeaderOrder]) === null || _f === void 0 ? void 0 : _f.row_header_sub_title}
36
+ </div>
37
+ </th>
38
+ {row
39
+ .slice(0, data.matrix_size)
40
+ .map((item, index) => {
41
+ return (<TableData data={item} tdStyles={tdStyles} key={`${item.id}-${index}`} hasInlineStyles={hasInlineStyles} customTableDataDynamicIdValue={customTableDataDynamicIdValue}/>);
42
+ })}
43
+ </tr>);
44
+ })}
45
+ </tbody>);
46
+ };
47
+ MatrixRows.defaultProps = {};
48
+ export default MatrixRows;
@@ -0,0 +1,4 @@
1
+ import { FC } from "react";
2
+ import { ReactMatrixProps } from "../types";
3
+ declare const ReactMatrix: FC<ReactMatrixProps>;
4
+ export default ReactMatrix;
@@ -0,0 +1,42 @@
1
+ import React from "react";
2
+ import { getTableStyles, getTableBoarder, getContainerStyles, } from "../helpers/getStyles";
3
+ import MatrixHeaders from "./MatrixHeaders";
4
+ import MatrixRows from "./MatrixRows";
5
+ const ReactMatrix = ({ data, matrixName, tableStyles: contextTableStyles, hasTableBorder: contextHasTableBorder, hasInlineStyles: contextHasInlineStyles, matrixDescription, hasContainerStyles: contextHasContainerStyles, tableContainerStyles: contextTableContainerStyles, tableStyles = {}, }) => {
6
+ const tableBorder = getTableBoarder(!!(contextHasInlineStyles && contextHasTableBorder));
7
+ const tableElmStyles = getTableStyles(!!(contextHasInlineStyles && contextTableStyles), tableStyles);
8
+ const containerStyles = getContainerStyles(!!(contextHasInlineStyles && contextHasContainerStyles), contextTableContainerStyles !== null && contextTableContainerStyles !== void 0 ? contextTableContainerStyles : {});
9
+ return (<div style={containerStyles}>
10
+ <h1>{matrixName}</h1>
11
+ <h4>{matrixDescription}</h4>
12
+ <table id="matrix-table" style={Object.assign(Object.assign({}, tableElmStyles), tableBorder)}>
13
+ <MatrixHeaders {...{
14
+ data,
15
+ hasInlineStyles: true,
16
+ headerPrimaryUpper: true,
17
+ thRowStyles: {},
18
+ thTitleStyles: {},
19
+ thSubTitleStyles: {},
20
+ thPrimaryTitleStyles: {},
21
+ customHeaderRowIdValue: "",
22
+ customDynamicHeaderTitleIdValue: "",
23
+ customDynamicSubHeaderTitleIdValue: "",
24
+ }}/>
25
+ <MatrixRows {...{
26
+ data,
27
+ rowPrimaryUpper: true,
28
+ hasInlineStyles: true,
29
+ reverseMatrixValues: true,
30
+ trRowStyles: {},
31
+ trTitleStyles: {},
32
+ trSubTitleStyles: {},
33
+ trPrimaryTitleStyles: {},
34
+ tdStyles: {},
35
+ customRowDynamicIdValue: "",
36
+ customRowHeaderDynamicIdValue: "",
37
+ customTableDataDynamicIdValue: "",
38
+ }}/>
39
+ </table>
40
+ </div>);
41
+ };
42
+ export default ReactMatrix;
@@ -0,0 +1,4 @@
1
+ import { FC } from "react";
2
+ import { TableDataProps } from "../types";
3
+ declare const TableData: FC<TableDataProps>;
4
+ export default TableData;
@@ -0,0 +1,19 @@
1
+ import React from "react";
2
+ const TableData = ({ data, tdStyles = {}, hasInlineStyles = true, customTableDataDynamicIdValue = "", }) => {
3
+ const handleCellClick = (tdSelected) => {
4
+ alert(`${tdSelected === null || tdSelected === void 0 ? void 0 : tdSelected.description}, ${tdSelected === null || tdSelected === void 0 ? void 0 : tdSelected.score_value} \n${tdSelected === null || tdSelected === void 0 ? void 0 : tdSelected.response}`);
5
+ };
6
+ const customTdStyles = !tdStyles ? {} : tdStyles;
7
+ const tableDataStyles = hasInlineStyles
8
+ ? Object.assign({ cursor: "pointer", textAlign: "center", borderColor: "black", borderStyle: "solid", borderWidth: `${1}px`, backgroundColor: data === null || data === void 0 ? void 0 : data.colour }, tdStyles) : Object.assign({}, customTdStyles);
9
+ return (<>
10
+ <td style={tableDataStyles} onClick={() => handleCellClick(data)} id={`react-matrix-dynamic-table-data${!customTableDataDynamicIdValue
11
+ ? ""
12
+ : `-${customTableDataDynamicIdValue}`}`}>
13
+ {`${data === null || data === void 0 ? void 0 : data.description}`}
14
+ <br />
15
+ {`(${data === null || data === void 0 ? void 0 : data.score_value})`}
16
+ </td>
17
+ </>);
18
+ };
19
+ export default TableData;
@@ -0,0 +1,12 @@
1
+ export declare const getCustomStyles: (hasStyle: boolean) => {};
2
+ export declare const getContainerStyles: (hasStyle: boolean, styles: React.CSSProperties) => React.CSSProperties;
3
+ export declare const getTableBoarder: (hasStyle: boolean) => {
4
+ border?: undefined;
5
+ } | {
6
+ border: string;
7
+ };
8
+ export declare const getTableStyles: (hasStyle: boolean, styles: React.CSSProperties) => React.CSSProperties;
9
+ export declare const getHeaderPrimaryTitleStyles: (hasStyle: boolean, styles: React.CSSProperties) => React.CSSProperties;
10
+ export declare const getHeaderRowStyles: (hasStyle: boolean, styles: React.CSSProperties) => React.CSSProperties;
11
+ export declare const getHeaderTitleStyles: (hasStyle: boolean, styles: React.CSSProperties) => React.CSSProperties;
12
+ export declare const getHeaderSubTitleStyles: (hasStyle: boolean, styles: React.CSSProperties) => React.CSSProperties;
@@ -0,0 +1,16 @@
1
+ export const getCustomStyles = (hasStyle) => !hasStyle ? {} : hasStyle;
2
+ export const getContainerStyles = (hasStyle, styles) =>
3
+ // if custom styles are passed remove default styles by returning a spread empty object
4
+ !hasStyle
5
+ ? Object.assign({}, getCustomStyles(hasStyle)) : Object.assign({ top: `${50}%`, left: `${50}%`, height: `${100}%`, textAlign: "center", position: "absolute", transform: `translate(${-50}%, ${-50}%)` }, styles);
6
+ export const getTableBoarder = (hasStyle) => !hasStyle ? {} : { border: `${1}px solid black` };
7
+ export const getTableStyles = (hasStyle, styles) => !hasStyle
8
+ ? Object.assign({}, getCustomStyles(hasStyle)) : Object.assign({ width: `${70}rem` }, styles);
9
+ export const getHeaderPrimaryTitleStyles = (hasStyle, styles) => !hasStyle
10
+ ? Object.assign({}, getCustomStyles(hasStyle)) : Object.assign({ borderWidth: `${0}`, padding: `${1}rem ${0} ${1}rem ${0}` }, styles);
11
+ export const getHeaderRowStyles = (hasStyle, styles) => !hasStyle
12
+ ? Object.assign({}, getCustomStyles(hasStyle)) : Object.assign({ borderColor: "black", borderStyle: "solid", padding: `${10}rem ${0} ${0} ${0}`, borderWidth: `${0} ${0} ${0} ${10}px` }, styles);
13
+ export const getHeaderTitleStyles = (hasStyle, styles) => !hasStyle
14
+ ? Object.assign({}, getCustomStyles(hasStyle)) : Object.assign({ padding: `${0} ${0.5}rem ${0} ${0.5}rem`, backgroundColor: "light-grey", border: `${1}px solid black` }, styles);
15
+ export const getHeaderSubTitleStyles = (hasStyle, styles) => !hasStyle
16
+ ? Object.assign({}, getCustomStyles(hasStyle)) : Object.assign({ fontSize: `${0.9}em`, fontWeight: "normal" }, styles);
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ export { default as ReactMatrix } from "./components/ReactMatrix";
@@ -1,109 +1,104 @@
1
+ /// <reference types="react" />
1
2
  export interface MatrixDetail {
2
- id: number;
3
- position: number;
4
- matrix_type: string;
5
- likelihood: string;
6
- consequence: number;
7
- header_title: string;
8
- header_sub_title: string;
9
- row_header_title: string;
10
- row_header_sub_title: string;
3
+ id: number;
4
+ position: number;
5
+ matrix_type: string;
6
+ likelihood: string;
7
+ consequence: number;
8
+ header_title: string;
9
+ header_sub_title: string;
10
+ row_header_title: string;
11
+ row_header_sub_title: string;
11
12
  }
12
-
13
13
  export interface MatrixValue {
14
- id: number;
15
- matrix_id: number;
16
- description: string;
17
- score_value: number;
18
- colour: string;
19
- position: number;
20
- likelihood_descriptor: string;
21
- consequence_descriptor: number;
22
- response: string;
14
+ id: number;
15
+ matrix_id: number;
16
+ description: string;
17
+ score_value: number;
18
+ colour: string;
19
+ position: number;
20
+ likelihood_descriptor: string;
21
+ consequence_descriptor: number;
22
+ response: string;
23
23
  }
24
-
25
24
  export interface MatrixData {
26
- id: number;
27
- matrix_size: number;
28
- matrix_name: string;
29
- primary_header_title: string;
30
- primary_row_header_title: string;
31
- matrix_description: string;
32
- matrix_details: MatrixDetail[];
33
- matrix_values: MatrixValue[];
25
+ id: number;
26
+ matrix_size: number;
27
+ matrix_name: string;
28
+ primary_header_title: string;
29
+ primary_row_header_title: string;
30
+ matrix_description: string;
31
+ matrix_details: MatrixDetail[];
32
+ matrix_values: MatrixValue[];
34
33
  }
35
-
36
34
  export interface ReactMatrixProps {
37
- data: MatrixData;
38
- matrixName: string;
39
- matrixDescription: string;
40
- hasTableBorder?: boolean;
41
- hasInlineStyles?: boolean | undefined;
42
- hasContainerStyles?: boolean | undefined;
43
- reverseMatrixValues?: boolean;
44
- matrixSizeSelected?: number;
45
- rowPrimaryUpper?: boolean;
46
- headerPrimaryUpper?: boolean;
47
- tableContainerStyles?: React.CSSProperties;
48
- tableStyles?: React.CSSProperties;
49
- thRowStyles?: React.CSSProperties;
50
- thTitleStyles?: React.CSSProperties;
51
- thSubTitleStyles?: React.CSSProperties;
52
- thPrimaryTitleStyles?: React.CSSProperties;
53
- trRowStyles?: React.CSSProperties;
54
- trTitleStyles?: React.CSSProperties;
55
- trSubTitleStyles?: React.CSSProperties;
56
- trPrimaryTitleStyles?: React.CSSProperties;
57
- tdStyles?: React.CSSProperties;
58
- customHeaderRowIdValue?: string;
59
- customDynamicHeaderTitleIdValue?: string;
60
- customDynamicSubHeaderTitleIdValue?: string;
61
- customRowDynamicIdValue?: string;
62
- customRowHeaderDynamicIdValue?: string;
63
- customTableDataDynamicIdValue?: string;
35
+ data: MatrixData;
36
+ matrixName: string;
37
+ matrixDescription: string;
38
+ hasTableBorder?: boolean;
39
+ hasInlineStyles?: boolean | undefined;
40
+ hasContainerStyles?: boolean | undefined;
41
+ reverseMatrixValues?: boolean;
42
+ matrixSizeSelected?: number;
43
+ rowPrimaryUpper?: boolean;
44
+ headerPrimaryUpper?: boolean;
45
+ tableContainerStyles?: React.CSSProperties;
46
+ tableStyles?: React.CSSProperties;
47
+ thRowStyles?: React.CSSProperties;
48
+ thTitleStyles?: React.CSSProperties;
49
+ thSubTitleStyles?: React.CSSProperties;
50
+ thPrimaryTitleStyles?: React.CSSProperties;
51
+ trRowStyles?: React.CSSProperties;
52
+ trTitleStyles?: React.CSSProperties;
53
+ trSubTitleStyles?: React.CSSProperties;
54
+ trPrimaryTitleStyles?: React.CSSProperties;
55
+ tdStyles?: React.CSSProperties;
56
+ customHeaderRowIdValue?: string;
57
+ customDynamicHeaderTitleIdValue?: string;
58
+ customDynamicSubHeaderTitleIdValue?: string;
59
+ customRowDynamicIdValue?: string;
60
+ customRowHeaderDynamicIdValue?: string;
61
+ customTableDataDynamicIdValue?: string;
64
62
  }
65
-
66
63
  export interface MatrixHeaderProps {
67
- data: MatrixData;
68
- hasInlineStyles?: boolean | undefined;
69
- headerPrimaryUpper?: boolean;
70
- thRowStyles?: React.CSSProperties;
71
- thTitleStyles?: React.CSSProperties;
72
- thSubTitleStyles?: React.CSSProperties;
73
- thPrimaryTitleStyles?: React.CSSProperties;
74
- customHeaderRowIdValue?: string;
75
- customDynamicHeaderTitleIdValue?: string;
76
- customDynamicSubHeaderTitleIdValue?: string;
64
+ data: MatrixData;
65
+ hasInlineStyles?: boolean | undefined;
66
+ headerPrimaryUpper?: boolean;
67
+ thRowStyles?: React.CSSProperties;
68
+ thTitleStyles?: React.CSSProperties;
69
+ thSubTitleStyles?: React.CSSProperties;
70
+ thPrimaryTitleStyles?: React.CSSProperties;
71
+ customHeaderRowIdValue?: string;
72
+ customDynamicHeaderTitleIdValue?: string;
73
+ customDynamicSubHeaderTitleIdValue?: string;
77
74
  }
78
-
79
75
  export interface MatrixRowsProps {
80
- data: MatrixData;
81
- rowPrimaryUpper?: boolean;
82
- hasInlineStyles?: boolean | undefined;
83
- reverseMatrixValues?: boolean;
84
- trRowStyles?: React.CSSProperties;
85
- trTitleStyles?: React.CSSProperties;
86
- trSubTitleStyles?: React.CSSProperties;
87
- trPrimaryTitleStyles?: React.CSSProperties;
88
- tdStyles?: React.CSSProperties;
89
- customRowDynamicIdValue?: string;
90
- customRowHeaderDynamicIdValue?: string;
91
- customTableDataDynamicIdValue?: string;
76
+ data: MatrixData;
77
+ rowPrimaryUpper?: boolean;
78
+ hasInlineStyles?: boolean | undefined;
79
+ reverseMatrixValues?: boolean;
80
+ trRowStyles?: React.CSSProperties;
81
+ trTitleStyles?: React.CSSProperties;
82
+ trSubTitleStyles?: React.CSSProperties;
83
+ trPrimaryTitleStyles?: React.CSSProperties;
84
+ tdStyles?: React.CSSProperties;
85
+ customRowDynamicIdValue?: string;
86
+ customRowHeaderDynamicIdValue?: string;
87
+ customTableDataDynamicIdValue?: string;
92
88
  }
93
-
94
89
  export interface TableDataProps {
95
- data: {
96
- id: number;
97
- colour: string;
98
- position: number;
99
- matrix_id: number;
100
- score_value: number;
101
- description: string;
102
- response: string;
103
- likelihood_descriptor: string;
104
- consequence_descriptor: number;
105
- };
106
- tdStyles?: React.CSSProperties;
107
- hasInlineStyles?: boolean;
108
- customTableDataDynamicIdValue?: string;
90
+ data: {
91
+ id: number;
92
+ colour: string;
93
+ position: number;
94
+ matrix_id: number;
95
+ score_value: number;
96
+ description: string;
97
+ response: string;
98
+ likelihood_descriptor: string;
99
+ consequence_descriptor: number;
100
+ };
101
+ tdStyles?: React.CSSProperties;
102
+ hasInlineStyles?: boolean;
103
+ customTableDataDynamicIdValue?: string;
109
104
  }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,2 @@
1
+ import { MatrixData } from "../types";
2
+ export declare const data: MatrixData;