@@ -18,31 +18,87 @@ const TableStateProps = (props) => {
18
18
const [ gridColumns , setGridColumns ] = useState ( [ ] ) ;
19
19
const [ rows , setRows ] = useState ( [ ] ) ;
20
20
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
+ } ,
61
+ ] ;
62
+
63
+
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 ] ;
70
+
71
+ const filtered = currentComponent . stateProps . filter ( element => element . id !== selectedId ) ;
72
+ dispatch ( {
73
+ type : 'DELETE STATE' ,
74
+ payload : { stateProps : filtered }
75
+ } ) ;
76
+ }
77
+
21
78
22
79
useEffect ( ( ) => {
23
- setGridColumns ( getColumns ( props , state , dispatch ) ) ;
80
+ setGridColumns ( columnTabs ) ;
24
81
} , [ props . isThemeLight ] )
25
- // get currentComponent by using currently focused component's id
26
-
27
- // rows to show are either from current component or from a given provider
82
+
28
83
29
84
const { selectHandler } : StatePropsPanelProps = props ;
30
85
31
- // when component gets mounted, sets the gridColumn
86
+ // the delete button needs to be updated to remove
87
+ // the states from the current focused component
32
88
useEffect ( ( ) => {
33
-
34
-
35
- // get the columns and remove delete buttons
36
- // only if table is displayed as modal
37
- const columns = getColumns ( props , state , dispatch ) ;
38
89
if ( props . canDeleteState ) {
39
- setGridColumns ( columns ) ;
90
+ setGridColumns ( columnTabs ) ;
40
91
}
41
92
else {
42
- setGridColumns ( columns . slice ( 0 , columns . length - 1 ) ) ;
93
+ setGridColumns ( columnTabs . slice ( 0 , gridColumns . length - 1 ) ) ;
43
94
}
44
-
95
+ } , [ state . canvasFocus . componentId ] ) ;
45
96
97
+ // when we switch between tabs in our modal or focus of our current
98
+ // component, we need to update the states displayed in our table
99
+ // we also need to update the table when the state is changed by
100
+ // deleting and adding component state
101
+ useEffect ( ( ) => {
46
102
if ( ! props . providerId ) {
47
103
const currentId = state . canvasFocus . componentId ;
48
104
const currentComponent = state . components [ currentId - 1 ] ;
@@ -51,9 +107,9 @@ const TableStateProps = (props) => {
51
107
const providerComponent = state . components [ props . providerId - 1 ] ;
52
108
setRows ( providerComponent . stateProps . slice ( ) ) ;
53
109
}
54
-
55
110
} , [ props . providerId , state ] ) ;
56
111
112
+
57
113
return (
58
114
< div className = { 'state-prop-grid' } >
59
115
< DataGrid
@@ -69,60 +125,6 @@ const TableStateProps = (props) => {
69
125
} ;
70
126
71
127
72
- const getColumns = ( props , state , dispatch ) => {
73
- return [
74
- {
75
- field : 'id' ,
76
- headerName : 'ID' ,
77
- width : 70 ,
78
- editable : false ,
79
- } ,
80
- {
81
- field : 'key' ,
82
- headerName : 'Key' ,
83
- width : 90 ,
84
- editable : true ,
85
- } ,
86
- {
87
- field : 'value' ,
88
- headerName : 'Value' ,
89
- width : 90 ,
90
- editable : true ,
91
- } ,
92
- {
93
- field : 'type' ,
94
- headerName : 'Type' ,
95
- width : 90 ,
96
- editable : false ,
97
- } ,
98
- {
99
- field : 'delete' ,
100
- headerName : 'X' ,
101
- width : 70 ,
102
- editable : false ,
103
- renderCell : function renderCell ( params :any ) {
104
- return (
105
- < Button style = { { width :`${ 3 } px` } } onClick = { ( ) => {
106
- // get the current focused component
107
- // remove the state that the button is clicked
108
- // send a dispatch to rerender the table
109
- const currentId = state . canvasFocus . componentId ;
110
- const currentComponent = state . components [ currentId - 1 ] ;
111
-
112
- const filtered = currentComponent . stateProps . filter ( element => element . id !== params . id ) ;
113
- dispatch ( {
114
- type : 'DELETE STATE' ,
115
- payload : { stateProps : filtered }
116
- } ) ;
117
-
118
- } } >
119
- < ClearIcon style = { { width :`${ 15 } px` } } />
120
- </ Button >
121
- ) ;
122
- } ,
123
- } ,
124
- ] ;
125
- } ;
126
128
127
129
const useStyles = makeStyles ( {
128
130
themeLight : {
0 commit comments