1+ import React from 'react' ;
2+ import Table from '@mui/material/Table' ;
3+ import TableBody from '@mui/material/TableBody' ;
4+ import TableContainer from '@mui/material/TableContainer' ;
5+ import TableHead from '@mui/material/TableHead' ;
6+ import TableRow from '@mui/material/TableRow' ;
7+ import Paper from '@mui/material/Paper' ;
8+ import { styled } from '@mui/material/styles' ;
9+ import TableCell , { tableCellClasses } from '@mui/material/TableCell' ;
10+ import { useSelector } from 'react-redux' ;
11+ import { RootState } from '../../../redux/store'
12+
13+ const StyledTableCell = styled ( TableCell ) ( ( { theme } ) => ( {
14+ [ `&.${ tableCellClasses . head } ` ] : {
15+ backgroundColor : theme . palette . common . black ,
16+ color : theme . palette . common . white ,
17+ } ,
18+ [ `&.${ tableCellClasses . body } ` ] : {
19+ color : theme . palette . common . black ,
20+ fontSize : 14 ,
21+ } ,
22+ } ) ) ;
23+
24+ const StyledTableRow = styled ( TableRow ) ( ( { theme } ) => ( {
25+ '&:nth-of-type(odd)' : {
26+ backgroundColor : theme . palette . action . hover
27+ } ,
28+ // hide last border
29+ '&:last-child td, &:last-child th' : {
30+ border : 0 ,
31+ } ,
32+ } ) ) ;
33+
34+ export default function DataTable ( props ) {
35+ const {
36+ currComponentState, parentProps, clickedComp, data,
37+ } = props ;
38+ const state = useSelector ( ( store :RootState ) => store . appState )
39+
40+ // determine if the current component is a root component
41+ let isRoot = false ;
42+
43+ for ( let i = 0 ; i < data . length ; i ++ ) {
44+ if ( data [ i ] . name === clickedComp ) {
45+ if ( state . rootComponents . includes ( data [ i ] . id ) ) isRoot = true ;
46+ }
47+ }
48+
49+ return (
50+ < TableContainer component = { Paper } sx = { { maxHeight : '350px' } } >
51+ < Table
52+ sx = { { width : '510px' } }
53+ aria-label = "customized table"
54+ stickyHeader
55+ >
56+
57+ { /* we are checking if the clicked component is a root component-- if yes, it doesn't have any parents so don't need passed-in props table */ }
58+ { ( ! isRoot
59+ && (
60+ < >
61+ < TableHead >
62+ < TableRow >
63+ < StyledTableCell align = "center" colSpan = { 3 } >
64+ Props Passed in from Parent:
65+ </ StyledTableCell >
66+ </ TableRow >
67+ </ TableHead >
68+ < TableBody >
69+ < StyledTableRow >
70+ < StyledTableCell component = "th" scope = "row" > < b > Key</ b > </ StyledTableCell >
71+ < StyledTableCell align = "right" > < b > Type</ b > </ StyledTableCell >
72+ < StyledTableCell align = "right" > < b > Initial Value</ b > </ StyledTableCell >
73+ </ StyledTableRow >
74+ { parentProps ? parentProps . map ( ( data , index ) => (
75+ < StyledTableRow key = { index } >
76+ < StyledTableCell component = "th" scope = "row" > { data . key } </ StyledTableCell >
77+ < StyledTableCell align = "right" > { data . type } </ StyledTableCell >
78+ < StyledTableCell align = "right" > { data . value } </ StyledTableCell >
79+ </ StyledTableRow >
80+ ) ) : '' }
81+ </ TableBody >
82+ </ >
83+ )
84+ ) }
85+
86+ { /* The below table will contain the state initialized within the clicked component */ }
87+ < TableHead >
88+ < TableRow >
89+ < StyledTableCell align = "center" colSpan = { 3 } >
90+ State Initialized in Current Component:
91+ </ StyledTableCell >
92+ </ TableRow >
93+ </ TableHead >
94+ < TableBody >
95+ < StyledTableRow >
96+ < StyledTableCell component = "th" scope = "row" > < b > Key</ b > </ StyledTableCell >
97+ < StyledTableCell align = "right" > < b > Type</ b > </ StyledTableCell >
98+ < StyledTableCell align = "right" > < b > Initial Value</ b > </ StyledTableCell >
99+ </ StyledTableRow >
100+ { currComponentState ? currComponentState . map ( ( data , index ) => (
101+ < StyledTableRow key = { index } >
102+ < StyledTableCell component = "th" scope = "row" > { data . key } </ StyledTableCell >
103+ < StyledTableCell align = "right" > { data . type } </ StyledTableCell >
104+ < StyledTableCell align = "right" > { data . value } </ StyledTableCell >
105+ </ StyledTableRow >
106+ ) ) : '' }
107+ </ TableBody >
108+ </ Table >
109+ </ TableContainer >
110+ ) ;
111+ }
0 commit comments