react-data-matrix 0.1.9 → 0.3.1

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
@@ -1,17 +1,18 @@
1
1
  # React Matrix   [![npm version](https://badge.fury.io/js/react-data-matrix.svg)](https://badge.fury.io/js/react-data-matrix)
2
2
 
3
- [React Matrix Demo](https://react-matrix.herokuapp.com/)
3
+ [React Matrix Demo](https://bronz3beard.github.io/react-matrix)
4
4
 
5
5
  React Matrix table shows relationships between two or more variables in a data set in grid format.
6
6
 
7
- The most common usage for a table like React Matrix, is to display the __likelihood__ and __consequence__ "scores" of __risks/hazards__, this can be for anything from Corporate, Work Health Safety and Environment risks, and more.
7
+ The most common usage for a table like React Matrix, is to display the **likelihood** and **consequence** "scores" of **risks/hazards**, this can be for anything from Corporate, Work Health Safety and Environment risks, and more.
8
8
 
9
9
  # PR's
10
+
10
11
  - Have a look at the [PR template doc](https://github.com/bronz3beard/react-matrix/blob/main/docs) for best approach to getting your pr merged.
11
12
 
12
13
  # Usage
13
14
 
14
- ```js
15
+ ```js
15
16
  import { ReactMatrix } from "react-data-matrix";
16
17
 
17
18
  const App = () => {
@@ -24,7 +25,8 @@ const App = () => {
24
25
  ```
25
26
 
26
27
  # Expected Data Structure
27
- - 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,
28
+
29
+ - 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,
28
30
  or an example of how to construct/deconstruct your data objects from your api.
29
31
 
30
32
  ```js
@@ -83,192 +85,255 @@ const App = () => {
83
85
 
84
86
  # PropTypes
85
87
 
86
- ```js
87
- propTypes = {
88
- data: PropTypes.shape({
89
- id: PropTypes.number,
90
- matrix_name: PropTypes.string,
91
- matrix_description: PropTypes.string,
92
- matrix_size: PropTypes.number.isRequired,
93
- matrix_details: PropTypes.arrayOf(
94
- PropTypes.shape({
95
- id: PropTypes.number,
96
- likelihood: PropTypes.string,
97
- consequence: PropTypes.number,
98
- header_title: PropTypes.string,
99
- header_sub_title: PropTypes.string,
100
- row_header_title: PropTypes.string,
101
- row_header_sub_title: PropTypes.string,
102
- })
103
- ).isRequired,
104
-
105
- matrix_values: PropTypes.arrayOf(
106
- PropTypes.shape({
107
- id: PropTypes.number.isRequired,
108
- colour: PropTypes.string,
109
- position: PropTypes.number.isRequired,
110
- matrix_id: PropTypes.number,
111
- score_value: PropTypes.number,
112
- description: PropTypes.string,
113
- response: PropTypes.string,
114
- likelihood_descriptor: PropTypes.string.isRequired,
115
- consequence_descriptor: PropTypes.number,
116
- })
117
- ).isRequired,
118
- }).isRequired,
119
-
120
- hasTableBorder: PropTypes.bool,
121
- hasInlineStyles: PropTypes.bool,
122
- hasContainerStyles: PropTypes.bool,
123
- reverseMatrixValues: PropTypes.bool,
124
- matrixSizeSelected: PropTypes.number,
125
-
126
- rowPrimaryUpper: PropTypes.bool,
127
- headerPrimaryUpper: PropTypes.bool,
128
-
129
- thRowStyles: PropTypes.shape({}),
130
- thTitleStyles: PropTypes.shape({}),
131
- thSubTitleStyles: PropTypes.shape({}),
132
- thPrimaryTitleStyles: PropTypes.shape({}),
133
-
134
- trRowStyles: PropTypes.shape({}),
135
- trTitleStyles: PropTypes.shape({}),
136
- trSubTitleStyles: PropTypes.shape({}),
137
- trPrimaryTitleStyles: PropTypes.shape({}),
138
-
139
- tdStyles: PropTypes.shape({}),
140
-
141
- customHeaderRowIdValue: PropTypes.string,
142
- customDynamicHeaderTitleIdValue: PropTypes.string,
143
- customDynamicSubHeaderTitleIdValue: PropTypes.string,
144
-
145
- customRowDynamicIdValue: PropTypes.string,
146
- customRowHeaderDynamicIdValue: PropTypes.string,
147
-
148
- customTableDataDynamicIdValue: PropTypes.string,
88
+ ```ts
89
+ export interface MatrixDetail {
90
+ id: number;
91
+ position: number;
92
+ matrix_type: string;
93
+ likelihood: string;
94
+ consequence: number;
95
+ header_title: string;
96
+ header_sub_title: string;
97
+ row_header_title: string;
98
+ row_header_sub_title: string;
99
+ }
100
+
101
+ export interface MatrixValue {
102
+ id: number;
103
+ matrix_id: number;
104
+ description: string;
105
+ score_value: number;
106
+ colour: string;
107
+ position: number;
108
+ likelihood_descriptor: string;
109
+ consequence_descriptor: number;
110
+ response: string;
111
+ }
112
+
113
+ export interface MatrixData {
114
+ id: number;
115
+ matrix_size: number;
116
+ matrix_name: string;
117
+ primary_header_title: string;
118
+ primary_row_header_title: string;
119
+ matrix_description: string;
120
+ matrix_details: MatrixDetail[];
121
+ matrix_values: MatrixValue[];
122
+ }
123
+
124
+ export interface ReactMatrixProps {
125
+ data?: {
126
+ id: number;
127
+ matrix_name: string;
128
+ matrix_description: string;
129
+ matrix_size: number;
130
+ primary_header_title: string;
131
+ primary_row_header_title: string;
132
+ matrix_details: {
133
+ id: number;
134
+ likelihood: string;
135
+ consequence: number;
136
+ header_title: string;
137
+ header_sub_title: string;
138
+ row_header_title: string;
139
+ row_header_sub_title: string;
140
+ }[];
141
+ matrix_values: {
142
+ id: number;
143
+ colour: string;
144
+ position: number;
145
+ matrix_id: number;
146
+ score_value: number;
147
+ description: string;
148
+ response: string;
149
+ likelihood_descriptor: string;
150
+ consequence_descriptor: number;
151
+ }[];
149
152
  };
153
+ hasTableBorder?: boolean;
154
+ hasInlineStyles?: boolean;
155
+ hasContainerStyles?: boolean;
156
+ reverseMatrixValues?: boolean;
157
+ matrixSizeSelected?: number;
158
+ rowPrimaryUpper?: boolean;
159
+ headerPrimaryUpper?: boolean;
160
+ tableContainerStyles?: React.CSSProperties;
161
+ tableStyles?: React.CSSProperties;
162
+ thRowStyles?: React.CSSProperties;
163
+ thTitleStyles?: React.CSSProperties;
164
+ thSubTitleStyles?: React.CSSProperties;
165
+ thPrimaryTitleStyles?: React.CSSProperties;
166
+ trRowStyles?: React.CSSProperties;
167
+ trTitleStyles?: React.CSSProperties;
168
+ trSubTitleStyles?: React.CSSProperties;
169
+ trPrimaryTitleStyles?: React.CSSProperties;
170
+ tdStyles?: React.CSSProperties;
171
+ customHeaderRowIdValue?: string;
172
+ customDynamicHeaderTitleIdValue?: string;
173
+ customDynamicSubHeaderTitleIdValue?: string;
174
+ customRowDynamicIdValue?: string;
175
+ customRowHeaderDynamicIdValue?: string;
176
+ customTableDataDynamicIdValue?: string;
177
+ }
150
178
 
151
- defaultProps = {
152
- hasTableBorder: true,
153
- rowPrimaryUpper: true,
154
- hasInlineStyles: true,
155
- matrixSizeSelected: 5,
156
- headerPrimaryUpper: true,
157
- hasContainerStyles: true,
158
- reverseMatrixValues: true,
159
-
160
- thRowStyles: {},
161
- thTitleStyles: {},
162
- thSubTitleStyles: {},
163
- thPrimaryTitleStyles: {},
164
-
165
- trRowStyles: {},
166
- trTitleStyles: {},
167
- trSubTitleStyles: {},
168
- trPrimaryTitleStyles: {},
169
-
170
- tdStyles: {},
171
-
172
- matrixName: "",
173
- matrixDescription: "",
174
- customHeaderRowIdValue: "",
175
- customRowDynamicIdValue: "",
176
- customTableDataDynamicIdValue: "",
177
- customRowHeaderDynamicIdValue: "",
178
- customDynamicHeaderTitleIdValue: "",
179
- customDynamicSubHeaderTitleIdValue: "",
179
+ export interface TableDataProps {
180
+ data: {
181
+ id: number;
182
+ colour: string;
183
+ position: number;
184
+ matrix_id: number;
185
+ score_value: number;
186
+ description: string;
187
+ response: string;
188
+ likelihood_descriptor: string;
189
+ consequence_descriptor: number;
180
190
  };
191
+ tdStyles?: React.CSSProperties;
192
+ hasInlineStyles?: boolean;
193
+ customTableDataDynamicIdValue?: string;
194
+ }
195
+ ```
196
+
197
+ ```js
198
+ defaultProps = {
199
+ hasTableBorder: true,
200
+ rowPrimaryUpper: true,
201
+ hasInlineStyles: true,
202
+ matrixSizeSelected: 5,
203
+ headerPrimaryUpper: true,
204
+ hasContainerStyles: true,
205
+ reverseMatrixValues: true,
206
+
207
+ thRowStyles: {},
208
+ thTitleStyles: {},
209
+ thSubTitleStyles: {},
210
+ thPrimaryTitleStyles: {},
211
+
212
+ trRowStyles: {},
213
+ trTitleStyles: {},
214
+ trSubTitleStyles: {},
215
+ trPrimaryTitleStyles: {},
216
+
217
+ tdStyles: {},
218
+
219
+ matrixName: "",
220
+ matrixDescription: "",
221
+ customHeaderRowIdValue: "",
222
+ customRowDynamicIdValue: "",
223
+ customTableDataDynamicIdValue: "",
224
+ customRowHeaderDynamicIdValue: "",
225
+ customDynamicHeaderTitleIdValue: "",
226
+ customDynamicSubHeaderTitleIdValue: "",
227
+ };
181
228
  ```
182
229
 
183
230
  # Available Properties
184
231
 
185
- - __hasInlineStyles__
186
- * Default is true.
187
- * If false all inline styles are removed.
188
-
189
- ----
190
- - __hasTableBorder__
191
- * Default is true.
192
- * If false the most outer border is removed.
193
-
194
- ----
195
- - __hasContainerStyles__
196
- * Default is true.
197
- * This will center the matrix in the middle of the page.
198
-
199
- ----
200
- - __reverseMatrixValues__
201
- * Default is true.
202
- * When true the lowest value will be in the bottom left and the highest value will be in the top right, with all sequential values in-between.
203
- * When false the values will be reversed, lowest value top left highest value bottom right.
204
-
205
- ----
206
- - __rowPrimaryUpper__
207
- * Default is true.
208
- * If false string formatting will be removed.
209
-
210
- ----
211
- - __headerPrimaryUpper__
212
- * Default is true.
213
- * If false string formatting will be removed.
214
-
215
- ----
216
- - __customHeaderRowIdValue__
217
- * Default is empty string.
218
- * can be used to target element through id prop.
219
-
220
- ----
221
- - __customDynamicHeaderTitleIdValue__
222
- * Default is empty string.
223
- * can be used to target element through id prop.
224
-
225
- ----
226
- - __customDynamicSubHeaderTitleIdValue__
227
- * Default is empty string.
228
- * can be used to target element through id prop.
229
-
230
- ----
231
- - __customRowDynamicIdValue__
232
- * Default is empty string.
233
- * can be used to target element through id prop.
234
-
235
- ----
236
- - __customRowHeaderDynamicIdValue__
237
- * Default is empty string.
238
- * can be used to target element through id prop.
239
-
240
- ----
241
- - __customTableDataDynamicIdValue__
242
- * Default is empty string.
243
- * can be used to target element through id prop.
244
-
245
- ----
232
+ - **hasInlineStyles**
233
+ - Default is true.
234
+ - If false all inline styles are removed.
235
+
236
+ ---
237
+
238
+ - **hasTableBorder**
239
+ - Default is true.
240
+ - If false the most outer border is removed.
241
+
242
+ ---
243
+
244
+ - **hasContainerStyles**
245
+ - Default is true.
246
+ - This will center the matrix in the middle of the page.
247
+
248
+ ---
249
+
250
+ - **reverseMatrixValues**
251
+ - Default is true.
252
+ - When true the lowest value will be in the bottom left and the highest value will be in the top right, with all sequential values in-between.
253
+ - When false the values will be reversed, lowest value top left highest value bottom right.
254
+
255
+ ---
256
+
257
+ - **rowPrimaryUpper**
258
+ - Default is true.
259
+ - If false string formatting will be removed.
260
+
261
+ ---
262
+
263
+ - **headerPrimaryUpper**
264
+ - Default is true.
265
+ - If false string formatting will be removed.
266
+
267
+ ---
268
+
269
+ - **customHeaderRowIdValue**
270
+ - Default is empty string.
271
+ - can be used to target element through id prop.
272
+
273
+ ---
274
+
275
+ - **customDynamicHeaderTitleIdValue**
276
+ - Default is empty string.
277
+ - can be used to target element through id prop.
278
+
279
+ ---
280
+
281
+ - **customDynamicSubHeaderTitleIdValue**
282
+ - Default is empty string.
283
+ - can be used to target element through id prop.
284
+
285
+ ---
286
+
287
+ - **customRowDynamicIdValue**
288
+ - Default is empty string.
289
+ - can be used to target element through id prop.
290
+
291
+ ---
292
+
293
+ - **customRowHeaderDynamicIdValue**
294
+ - Default is empty string.
295
+ - can be used to target element through id prop.
296
+
297
+ ---
298
+
299
+ - **customTableDataDynamicIdValue**
300
+ - Default is empty string.
301
+ - can be used to target element through id prop.
302
+
303
+ ---
246
304
 
247
305
  # Styling Properties
248
306
 
249
307
  ### Inline
250
- __These styles can be added to the current styles or,
251
- if you make ```hasInlineStyles = false```, then add these values with your own styles, you can have full control over styling inline.__
252
308
 
253
- __ReactMatrix__
309
+ **These styles can be added to the current styles or,
310
+ if you make `hasInlineStyles = false`, then add these values with your own styles, you can have full control over styling inline.**
311
+
312
+ **ReactMatrix**
313
+
254
314
  - tableContainerStyles={{}}
255
315
  - tableStyles={{}}
256
316
 
257
- __MatrixHeaders__
317
+ **MatrixHeaders**
318
+
258
319
  - thRowStyles={{}}
259
320
  - thTitleStyles={{}}
260
321
  - thSubTitleStyles={{}}
261
322
  - thPrimaryTitleStyles={{}}
262
323
 
263
- __MatrixRows__
324
+ **MatrixRows**
325
+
264
326
  - trRowStyles={{}},
265
327
  - trTitleStyles={{}},
266
328
  - trSubTitleStyles={{}},
267
329
  - trPrimaryTitleStyles={{}},
268
330
 
269
- __TableData__
331
+ **TableData**
332
+
270
333
  - tdStyles={{}},
271
334
 
272
- ----
335
+ ---
336
+
273
337
  # Create React App
338
+
274
339
  - This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
@@ -0,0 +1,92 @@
1
+ import React, { FC, useContext } from "react";
2
+ import {
3
+ getHeaderRowStyles,
4
+ getHeaderTitleStyles,
5
+ getHeaderSubTitleStyles,
6
+ getHeaderPrimaryTitleStyles,
7
+ } from "../helpers/getStyles";
8
+ import { capitaliseString } from "../utils/functions";
9
+ import Context from "../context";
10
+ // import "./styles/riskMatrix.scss";
11
+
12
+ const MatrixHeaders: FC = () => {
13
+ const {
14
+ data,
15
+ hasInlineStyles = true,
16
+ headerPrimaryUpper = true,
17
+ thRowStyles = {},
18
+ thTitleStyles = {},
19
+ thSubTitleStyles = {},
20
+ thPrimaryTitleStyles = {},
21
+ customHeaderRowIdValue = "",
22
+ customDynamicHeaderTitleIdValue = "",
23
+ customDynamicSubHeaderTitleIdValue = "",
24
+ } = useContext(Context);
25
+
26
+ const headerRowStyles = getHeaderRowStyles(hasInlineStyles, thRowStyles);
27
+ const headerTitleStyles = getHeaderTitleStyles(
28
+ hasInlineStyles,
29
+ thTitleStyles
30
+ );
31
+ const headerSubTitleStyles = getHeaderSubTitleStyles(
32
+ hasInlineStyles,
33
+ thSubTitleStyles
34
+ );
35
+ const headerPrimaryTitleStyles = getHeaderPrimaryTitleStyles(
36
+ hasInlineStyles,
37
+ thPrimaryTitleStyles
38
+ );
39
+
40
+ const headerPrimaryTitle = !headerPrimaryUpper
41
+ ? capitaliseString(data?.primary_header_title)
42
+ : data?.primary_header_title;
43
+
44
+ return (
45
+ <thead>
46
+ <tr id="react-matrix-blank-headers-primary-title-row">
47
+ <th headers="blank" colSpan={4}></th>
48
+ <th
49
+ style={headerPrimaryTitleStyles}
50
+ id="react-matrix-header-primary-title"
51
+ >
52
+ {headerPrimaryTitle}
53
+ </th>
54
+ <th headers="blank" colSpan={2}></th>
55
+ </tr>
56
+ <tr
57
+ style={headerRowStyles}
58
+ id={`react-matrix-dynamic-headers-row-${customHeaderRowIdValue}`}
59
+ >
60
+ <th headers="blank" colSpan={2}></th>
61
+ {data.matrix_details.slice(0, data.matrix_size).map((column, index) => {
62
+ return (
63
+ <th
64
+ scope="col"
65
+ style={headerTitleStyles}
66
+ id={`react-matrix-dynamic-column-header-title${
67
+ !customDynamicHeaderTitleIdValue
68
+ ? ""
69
+ : `-${customDynamicHeaderTitleIdValue}`
70
+ }`}
71
+ key={`${column.id}-${index}`}
72
+ >
73
+ {column.header_title}
74
+ <div
75
+ style={headerSubTitleStyles}
76
+ id={`react-matrix-dynamic-column-header-sub-title${
77
+ !customDynamicSubHeaderTitleIdValue
78
+ ? ""
79
+ : `-${customDynamicSubHeaderTitleIdValue}`
80
+ }`}
81
+ >
82
+ {column.header_sub_title}
83
+ </div>
84
+ </th>
85
+ );
86
+ })}
87
+ </tr>
88
+ </thead>
89
+ );
90
+ };
91
+
92
+ export default MatrixHeaders;
@@ -0,0 +1,135 @@
1
+ import React, { FC, useContext } from "react";
2
+ import {
3
+ getHeaderRowStyles,
4
+ getHeaderTitleStyles,
5
+ getHeaderSubTitleStyles,
6
+ getHeaderPrimaryTitleStyles,
7
+ } from "../helpers/getStyles";
8
+ import { groupObjectsByProp, capitaliseString } from "../utils/functions";
9
+ import TableData from "./TableData";
10
+ import Context from "../context";
11
+ import { MatrixValue } from "../types";
12
+
13
+ const MatrixRows: FC = () => {
14
+ const {
15
+ data,
16
+ rowPrimaryUpper = true,
17
+ hasInlineStyles = true,
18
+ reverseMatrixValues = true,
19
+ trRowStyles = {},
20
+ trTitleStyles = {},
21
+ trSubTitleStyles = {},
22
+ trPrimaryTitleStyles = {},
23
+ tdStyles = {},
24
+ customRowDynamicIdValue = "",
25
+ customRowHeaderDynamicIdValue = "",
26
+ customTableDataDynamicIdValue = "",
27
+ } = useContext(Context);
28
+
29
+ const valuesArray = reverseMatrixValues
30
+ ? groupObjectsByProp(data.matrix_values, "likelihood_descriptor")
31
+ .reverse()
32
+ .slice(0, data.matrix_size)
33
+ : groupObjectsByProp(data.matrix_values, "likelihood_descriptor").slice(
34
+ 0,
35
+ data.matrix_size
36
+ );
37
+
38
+ const tableRowStyles = getHeaderRowStyles(hasInlineStyles, trRowStyles);
39
+ const tableRowHeaderTitleStyles = getHeaderTitleStyles(
40
+ hasInlineStyles,
41
+ trTitleStyles
42
+ );
43
+ const tableRowHeaderSubTitleStyles = getHeaderSubTitleStyles(
44
+ hasInlineStyles,
45
+ trSubTitleStyles
46
+ );
47
+ const rowHeaderPrimaryTitleStyles = getHeaderPrimaryTitleStyles(
48
+ hasInlineStyles,
49
+ trPrimaryTitleStyles
50
+ );
51
+
52
+ const rowPrimaryTitle = rowPrimaryUpper
53
+ ? capitaliseString(data.primary_row_header_title)
54
+ : data.primary_row_header_title;
55
+
56
+ return (
57
+ <tbody>
58
+ <tr id="react-matrix-row-primary-title">
59
+ <th scope="row" rowSpan={6} style={rowHeaderPrimaryTitleStyles}>
60
+ {rowPrimaryTitle}
61
+ </th>
62
+ </tr>
63
+ {valuesArray.map((row, index) => {
64
+ const rowHeaderOrder = reverseMatrixValues
65
+ ? data.matrix_size - (index + 1)
66
+ : index;
67
+
68
+ return (
69
+ <tr
70
+ style={tableRowStyles}
71
+ key={`row-${index}`}
72
+ id={`react-matrix-dynamic-rows-${
73
+ data.matrix_details[rowHeaderOrder]?.row_header_title
74
+ }-${data.matrix_details[rowHeaderOrder]?.row_header_sub_title}${
75
+ !customRowDynamicIdValue ? "" : `-${customRowDynamicIdValue}`
76
+ }`}
77
+ >
78
+ <th
79
+ scope="row"
80
+ style={tableRowHeaderTitleStyles}
81
+ id={`react-matrix-dynamic-rows-${
82
+ data.matrix_details[rowHeaderOrder]?.row_header_title
83
+ }-${data.matrix_details[rowHeaderOrder]?.row_header_sub_title}${
84
+ !customRowHeaderDynamicIdValue
85
+ ? ""
86
+ : `-${customRowHeaderDynamicIdValue}`
87
+ }`}
88
+ >
89
+ {data.matrix_details[rowHeaderOrder]?.row_header_title}
90
+ <div style={tableRowHeaderSubTitleStyles}>
91
+ {data.matrix_details[rowHeaderOrder]?.row_header_sub_title}
92
+ </div>
93
+ </th>
94
+ {row
95
+ .slice(0, data.matrix_size)
96
+ .map((item: MatrixValue, index: number) => {
97
+ return (
98
+ <TableData
99
+ data={item}
100
+ tdStyles={tdStyles}
101
+ key={`${item.id}-${index}`}
102
+ hasInlineStyles={hasInlineStyles}
103
+ customTableDataDynamicIdValue={
104
+ customTableDataDynamicIdValue
105
+ }
106
+ />
107
+ );
108
+ })}
109
+ </tr>
110
+ );
111
+ })}
112
+ </tbody>
113
+ );
114
+ };
115
+
116
+ MatrixRows.defaultProps = {
117
+ rowPrimaryUpper: true,
118
+ hasInlineStyles: true,
119
+ reverseMatrixValues: true,
120
+
121
+ trRowStyles: {},
122
+ trTitleStyles: {},
123
+ trSubTitleStyles: {},
124
+ trPrimaryTitleStyles: {},
125
+
126
+ tdStyles: {},
127
+
128
+ matrixSizeSelected: 5,
129
+
130
+ customRowDynamicIdValue: "",
131
+ customRowHeaderDynamicIdValue: "",
132
+ customTableDataDynamicIdValue: "",
133
+ };
134
+
135
+ export default MatrixRows;