Debian Codegraph @

jsx/src/components/ReactObjectTableViewer/ReactObjectTableViewer.jsx

raw
1 // Originally copied from
2 // https://github.com/jinkwon/react-object-table-viewer/blob/f29827028fad547a0a17e044567cf1486849fb7a/src/ReactObjectTableViewer.tsx
3 import React from "react";
4 import PropTypes from "prop-types";
5
6 const ReactObjectTableViewer = (props) => {
7 const opt = props;
8
9 const data = opt.data;
10 const keys = Object.keys(data || {}) || [];
11
12 return (
13 <table
14 className={opt.className}
15 style={{
16 ...opt.style,
17 }}
18 >
19 <tbody>
20 {keys.map((k, key) => {
21 const val = data[k];
22 const isObject = typeof val === "object";
23 const isElement = React.isValidElement(val);
24
25 return (
26 <tr key={key}>
27 <th
28 style={{
29 ...opt.keyStyle,
30 }}
31 >
32 {k}
33 </th>
34 {isObject && (
35 <td>
36 {isElement && val}
37 {!isElement && <ReactObjectTableViewer {...opt} data={val} />}
38 </td>
39 )}
40 {!isObject && (
41 <td
42 style={{
43 whiteSpace: "nowrap",
44 ...opt.valueStyle,
45 }}
46 >{`${val}`}</td>
47 )}
48 </tr>
49 );
50 })}
51 </tbody>
52 </table>
53 );
54 };
55
56 ReactObjectTableViewer.propTypes = {
57 data: PropTypes.object,
58 style: PropTypes.objectOf(PropTypes.string),
59 keyStyle: PropTypes.objectOf(PropTypes.string),
60 valueStyle: PropTypes.objectOf(PropTypes.string),
61 className: PropTypes.string,
62 layout: PropTypes.string,
63 };
64
65 export default ReactObjectTableViewer;