@@ -10,67 +10,129 @@ import StateContext from '../../context/context';
10
10
import { makeStyles } from '@material-ui/core/styles' ;
11
11
import { StatePropsPanelProps } from '../../interfaces/Interfaces' ;
12
12
13
- const TableStateProps = ( props ) => {
13
+ const TableStateProps = props => {
14
14
const [ state , dispatch ] = useContext ( StateContext ) ;
15
15
const classes = useStyles ( ) ;
16
- const [ editRowsModel ] = useState < GridEditRowsModel > ( { } ) ;
16
+ const [ editRowsModel ] = useState < GridEditRowsModel > ( { } ) ;
17
17
const [ gridColumns , setGridColumns ] = useState ( [ ] ) ;
18
18
19
- const [ rows , setRows ] = useState ( [ ] ) ;
20
-
19
+ // const [rows, setRows] = useState([]);
20
+
21
21
const columnTabs = [
22
- {
23
- field : 'id' ,
24
- headerName : 'ID' ,
25
- width : 70 ,
26
- editable : false ,
27
- } ,
28
- {
29
- field : 'key' ,
30
- headerName : 'Key' ,
31
- width : 90 ,
32
- editable : true ,
33
- } ,
34
- {
35
- field : 'value' ,
36
- headerName : 'Value' ,
37
- width : 90 ,
38
- editable : true ,
39
- } ,
40
- {
41
- field : 'type' ,
42
- headerName : 'Type' ,
43
- width : 90 ,
44
- editable : false ,
45
- } ,
46
- {
47
- field : 'delete' ,
48
- headerName : 'X' ,
49
- width : 70 ,
50
- editable : false ,
51
- renderCell : function renderCell ( params :any ) {
52
- return (
53
- < Button style = { { width :`${ 3 } px` } } onClick = { ( ) => {
54
- deleteState ( params . id )
55
- } } >
56
- < ClearIcon style = { { width :`${ 15 } px` } } />
57
- </ Button >
58
- ) ;
59
- } ,
60
- } ,
22
+ {
23
+ field : 'id' ,
24
+ headerName : 'ID' ,
25
+ width : 70 ,
26
+ editable : false
27
+ } ,
28
+ {
29
+ field : 'key' ,
30
+ headerName : 'Key' ,
31
+ width : 90 ,
32
+ editable : true
33
+ } ,
34
+ {
35
+ field : 'value' ,
36
+ headerName : 'Value' ,
37
+ width : 90 ,
38
+ editable : true
39
+ } ,
40
+ {
41
+ field : 'type' ,
42
+ headerName : 'Type' ,
43
+ width : 90 ,
44
+ editable : false
45
+ } ,
46
+ {
47
+ field : 'delete' ,
48
+ headerName : 'X' ,
49
+ width : 70 ,
50
+ editable : false ,
51
+ renderCell : function renderCell ( params : any ) {
52
+ return (
53
+ < Button
54
+ style = { { width : `${ 3 } px` } }
55
+ onClick = { ( ) => {
56
+ deleteState ( params . id ) ;
57
+ } }
58
+ >
59
+ < ClearIcon style = { { width : `${ 15 } px` } } />
60
+ </ Button >
61
+ ) ;
62
+ }
63
+ }
61
64
] ;
62
65
66
+ const deleteState = selectedId => {
67
+ // get the current focused component
68
+ // remove the state that the button is clicked
69
+ // send a dispatch to rerender the table
70
+ const currentId = state . canvasFocus . componentId ;
71
+ const currentComponent = state . components [ currentId - 1 ] ;
72
+
73
+ const filtered = currentComponent . stateProps . filter (
74
+ element => element . id !== selectedId
75
+ ) ;
76
+ dispatch ( {
77
+ type : 'DELETE STATE' ,
78
+ payload : { stateProps : filtered , rowId : selectedId }
79
+ } ) ;
80
+ } ;
81
+
82
+ useEffect ( ( ) => {
83
+ setGridColumns ( columnTabs ) ;
84
+ } , [ props . isThemeLight ] ) ;
85
+
86
+ const { selectHandler } : StatePropsPanelProps = props ;
87
+
88
+ // the delete button needs to be updated to remove
89
+ // the states from the current focused component
90
+ useEffect ( ( ) => {
91
+ if ( props . canDeleteState ) {
92
+ setGridColumns ( columnTabs ) ;
93
+ } else {
94
+ setGridColumns ( columnTabs . slice ( 0 , gridColumns . length - 1 ) ) ;
95
+ }
96
+ } , [ state . canvasFocus . componentId ] ) ;
63
97
64
- const deleteState = ( selectedId ) => {
65
- // get the current focused component
66
- // remove the state that the button is clicked
67
- // send a dispatch to rerender the table
68
- const currentId = state . canvasFocus . componentId ;
69
- const currentComponent = state . components [ currentId - 1 ] ;
98
+ // when we switch between tabs in our modal or focus of our current
99
+ // component, we need to update the states displayed in our table
100
+ // we also need to update the table when the state is changed by
101
+ // deleting and adding component state
102
+ // useEffect(() => {
103
+ // // rows to show are either from current component or from a given provider
104
+ // let rows = [];
105
+ // if (!props.providerId) {
106
+ // const currentId = state.canvasFocus.componentId;
107
+ // const currentComponent = state.components[currentId - 1];
108
+ // rows = currentComponent.stateProps.slice();
109
+ // } else {
110
+ // const providerComponent = state.components[props.providerId - 1];
111
+ // // changed to get whole object
112
+ // if (props.displayObject) {
113
+ // const displayObject = props.displayObject;
114
+ // // format for DataGrid
115
+ // let id = 1;
116
+ // for (const key in displayObject) {
117
+ // // if key is a number make it a string with brackets aroung number
118
+ // rows.push({
119
+ // id: id++,
120
+ // key: isNaN(key) ? key : '[' + key + ']',
121
+ // value: displayObject[key],
122
+ // type: typeof displayObject[key]
123
+ // });
124
+ // }
125
+ // } else {
126
+ // rows = providerComponent.stateProps.slice();
127
+ // }
128
+ // }
129
+ // }, [props.providerId, state]);
70
130
71
131
// rows to show are either from current component or from a given provider
72
132
let rows = [ ] ;
73
133
if ( ! props . providerId ) {
134
+ const currentId = state . canvasFocus . componentId ;
135
+ const currentComponent = state . components [ currentId - 1 ] ;
74
136
rows = currentComponent . stateProps . slice ( ) ;
75
137
} else {
76
138
const providerComponent = state . components [ props . providerId - 1 ] ;
@@ -84,74 +146,32 @@ const deleteState = (selectedId) => {
84
146
rows . push ( { id : id ++ , key : ( isNaN ( key ) ? key : '[' + key + ']' ) , value : displayObject [ key ] , type : typeof ( displayObject [ key ] ) } ) ;
85
147
}
86
148
} else {
87
- rows = providerComponent . stateProps . slice ( ) ;
149
+ rows = providerComponent . stateProps . slice ( ) ;
88
150
}
89
151
}
90
152
91
- const filtered = currentComponent . stateProps . filter ( element => element . id !== selectedId ) ;
92
- dispatch ( {
93
- type : 'DELETE STATE' ,
94
- payload : { stateProps : filtered , rowId : selectedId }
95
- } ) ;
96
- }
97
153
98
154
99
- useEffect ( ( ) => {
100
- setGridColumns ( columnTabs ) ;
101
- } , [ props . isThemeLight ] )
102
-
103
-
104
- const { selectHandler } : StatePropsPanelProps = props ;
105
-
106
-
107
- // the delete button needs to be updated to remove
108
- // the states from the current focused component
109
- useEffect ( ( ) => {
110
- if ( props . canDeleteState ) {
111
- setGridColumns ( columnTabs ) ;
112
- }
113
- else {
114
- setGridColumns ( columnTabs . slice ( 0 , gridColumns . length - 1 ) ) ;
115
- }
116
- } , [ state . canvasFocus . componentId ] ) ;
117
-
118
- // when we switch between tabs in our modal or focus of our current
119
- // component, we need to update the states displayed in our table
120
- // we also need to update the table when the state is changed by
121
- // deleting and adding component state
122
- useEffect ( ( ) => {
123
- if ( ! props . providerId ) {
124
- const currentId = state . canvasFocus . componentId ;
125
- const currentComponent = state . components [ currentId - 1 ] ;
126
- setRows ( currentComponent . stateProps . slice ( ) ) ;
127
- } else {
128
- const providerComponent = state . components [ props . providerId - 1 ] ;
129
- setRows ( providerComponent . stateProps . slice ( ) ) ;
130
- }
131
- } , [ props . providerId , state ] ) ;
132
-
133
155
return (
134
156
< div className = { 'state-prop-grid' } >
135
157
< DataGrid
136
158
rows = { rows }
137
159
columns = { gridColumns }
138
160
pageSize = { 5 }
139
161
editRowsModel = { editRowsModel }
140
- onRowClick = { selectHandler }
162
+ onRowClick = { selectHandler }
141
163
className = { props . isThemeLight ? classes . themeLight : classes . themeDark }
142
164
/>
143
165
</ div >
144
166
) ;
145
167
} ;
146
168
147
-
148
-
149
169
const useStyles = makeStyles ( {
150
170
themeLight : {
151
171
color : 'rgba(0,0,0,0.54)' ,
152
172
'& .MuiTablePagination-root' : {
153
173
color : 'rbga(0,0,0,0.54)'
154
- } ,
174
+ }
155
175
} ,
156
176
themeDark : {
157
177
color : 'white' ,
0 commit comments