@@ -28,6 +28,8 @@ const StatePropsPanel = ({ isThemeLight, data }): JSX.Element => {
28
28
const [ inputValue , setInputValue ] = useState ( '' ) ;
29
29
const [ inputType , setInputType ] = useState ( '' ) ;
30
30
const [ errorStatus , setErrorStatus ] = useState ( false ) ;
31
+ const [ inputTypeError , setInputTypeError ] = useState ( '' ) ;
32
+ const [ newVal , setNewVal ] = useState ( 'test' ) ;
31
33
const [ errorMsg , setErrorMsg ] = useState ( '' ) ;
32
34
const currentId = state . canvasFocus . componentId ;
33
35
const currentComponent = state . components [ currentId - 1 ] ;
@@ -42,27 +44,79 @@ const StatePropsPanel = ({ isThemeLight, data }): JSX.Element => {
42
44
const typeConversion = ( value : string , type : string ) => {
43
45
switch ( type ) {
44
46
case 'string' :
45
- return String ( value ) ;
47
+ {
48
+ setInputTypeError ( '' ) ;
49
+ return String ( value ) ;
50
+ }
46
51
case 'number' :
47
- return Number ( value ) ;
52
+ {
53
+ setInputTypeError ( '' ) ;
54
+ return Number ( value ) ;
55
+ }
48
56
case 'boolean' :
49
- return value === 'true' ;
57
+ {
58
+ setInputTypeError ( '' ) ;
59
+ return value === 'true' ;
60
+ }
50
61
case 'array' :
51
- return JSON . parse ( value ) ;
52
- case 'object' :
53
- return JSON . parse ( value ) ;
62
+ try {
63
+ let retVal = JSON . parse ( value ) ;
64
+ if ( Array . isArray ( retVal ) ) {
65
+ console . log ( 'is this an array still' , retVal )
66
+ setInputTypeError ( '' ) ;
67
+ return retVal
68
+ } else {
69
+
70
+ throw new Error ( 'Input was not an array!' ) ;
71
+
72
+ }
73
+ }
74
+ catch {
75
+
76
+ setInputTypeError ( type )
77
+ return null ;
78
+ }
79
+ case 'object' :{
80
+ try {
81
+ let retVal = JSON . parse ( value ) ;
82
+
83
+ if ( typeof retVal === 'object' && ! Array . isArray ( retVal ) ) {
84
+ setInputTypeError ( '' ) ;
85
+ return retVal
86
+ } else {
87
+ throw new Error ( 'Input was not an object (excluding Arrays)!' ) ;
88
+ }
89
+ }
90
+ catch {
91
+
92
+ setInputTypeError ( type )
93
+ return null ;
94
+ }
95
+ }
54
96
default :
55
- return value ;
97
+ {
98
+ setInputTypeError ( '' ) ;
99
+ return value ;
100
+ }
56
101
}
57
102
} ;
58
103
104
+
105
+
59
106
// clears the input key, value, and type on Form
60
107
const clearForm = ( ) => {
61
108
setInputKey ( '' ) ;
62
109
setInputValue ( '' ) ;
63
110
setInputType ( '' ) ;
64
111
} ;
65
112
113
+
114
+ useEffect ( ( ) => {
115
+ console . log ( "inputValue changed" , inputValue )
116
+ setNewVal ( typeConversion ( inputValue , inputType ) ) ;
117
+ console . log ( "current newVal" , newVal )
118
+ } , [ inputType , inputValue ] )
119
+ console . log ( "current outside newVal" , newVal )
66
120
// submit new stateProps entries to state context
67
121
const submitNewState = ( e ) => {
68
122
e . preventDefault ( ) ;
@@ -91,17 +145,21 @@ const StatePropsPanel = ({ isThemeLight, data }): JSX.Element => {
91
145
setErrorMsg ( 'Key name already in use.' ) ;
92
146
return ;
93
147
}
148
+ else {
149
+ setErrorStatus ( false ) ;
150
+ setErrorMsg ( '' ) ;
151
+ }
94
152
}
95
153
}
96
- setPropNum ( ( prev ) => prev + 1 ) ;
154
+ setPropNum ( ( prev ) => prev + 1 ) ;
97
155
const newState = {
98
156
// id name of state will be the parent component name concated with propNum. it will start at 1 and increase by 1 for each new state added
99
157
id : `${ currentComponent . name } -${ inputKey } ` ,
100
158
key : inputKey ,
101
- value : typeConversion ( inputValue , inputType ) ,
159
+ value : newVal ,
102
160
type : inputType
103
161
} ;
104
-
162
+
105
163
const setNewState = {
106
164
// id name of state will be the parent component name concated with propNum. it will start at 1 and increase by 1 for each new state added
107
165
id : `${ currentComponent . name } -set${ inputKey
@@ -111,16 +169,19 @@ const StatePropsPanel = ({ isThemeLight, data }): JSX.Element => {
111
169
value : '' ,
112
170
type : 'func'
113
171
} ;
114
- dispatch (
115
- addState ( {
116
- newState : newState ,
117
- setNewState : setNewState ,
118
- contextParam : contextParam
119
- } )
120
- ) ;
121
- setRows1 ( [ ...rows1 , newState ] ) ;
122
- setErrorStatus ( false ) ;
123
- clearForm ( ) ;
172
+ if ( ! inputTypeError ) {
173
+ dispatch (
174
+ addState ( {
175
+ newState : newState ,
176
+ setNewState : setNewState ,
177
+ contextParam : contextParam
178
+ } )
179
+ ) ;
180
+ setRows1 ( [ ...rows1 , newState ] ) ;
181
+ setErrorStatus ( false ) ;
182
+ clearForm ( ) ;
183
+
184
+ }
124
185
} ;
125
186
126
187
// find table row using its id and if it exists, populate form with its details
@@ -134,7 +195,8 @@ const StatePropsPanel = ({ isThemeLight, data }): JSX.Element => {
134
195
if ( exists ) {
135
196
setInputKey ( table . row . key ) ;
136
197
setInputType ( table . row . type ) ;
137
- setInputValue ( table . row . value ) ;
198
+ console . log ( "tablerowvalue" , table . row . value ) ;
199
+ setInputValue ( table . row . value ? JSON . stringify ( table . row . value ) : '' ) ;
138
200
} else clearForm ( ) ;
139
201
} ;
140
202
//use effect to populate parent props table on load and every time canvas focus changes
@@ -216,7 +278,7 @@ const StatePropsPanel = ({ isThemeLight, data }): JSX.Element => {
216
278
? `${ classes . formControl } ${ classes . lightThemeFontColor } `
217
279
: `${ classes . formControl } ${ classes . darkThemeFontColor } `
218
280
}
219
- error = { errorStatus }
281
+ error = { inputTypeError != '' || errorStatus }
220
282
>
221
283
< InputLabel
222
284
id = "select-required-label"
@@ -233,7 +295,7 @@ const StatePropsPanel = ({ isThemeLight, data }): JSX.Element => {
233
295
? `${ classes . selectEmpty } ${ classes . rootUnderlineLight } ${ classes . inputTextLight } `
234
296
: `${ classes . selectEmpty } ${ classes . rootUnderlineDark } ${ classes . inputTextDark } `
235
297
}
236
- value = { inputType }
298
+ value = { inputType === 'func' ? '' : inputType }
237
299
onChange = { ( event ) => setInputType ( event . target . value ) }
238
300
MenuProps = { { disablePortal : true } }
239
301
style = {
@@ -321,7 +383,10 @@ const StatePropsPanel = ({ isThemeLight, data }): JSX.Element => {
321
383
: classes . darkThemeFontColor
322
384
}
323
385
>
324
- Required
386
+ {
387
+ inputTypeError === 'object' ? 'JSON object form: {"key": value}' :
388
+ inputTypeError === 'array' ? 'Array form: [value]' : 'Required'
389
+ }
325
390
</ FormHelperText >
326
391
</ FormControl >
327
392
< br />
0 commit comments