Skip to content

Commit dd89856

Browse files
authored
Merge pull request #13 from williamdyoon/feat/table-state-props
typecasted values, useState code snippets, tabularized table state props
2 parents 7bbdd47 + ba2ec38 commit dd89856

File tree

6 files changed

+139
-39
lines changed

6 files changed

+139
-39
lines changed

app/src/components/right/StatePropsPanel.tsx

Lines changed: 61 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
// CARET
2-
import React, {
3-
Component,
4-
useState,
5-
useContext,
6-
useEffect,
7-
useCallback
8-
} from 'react';
2+
import React, { useState, useContext } from 'react';
93
import {
104
createStyles,
115
makeStyles,
@@ -31,53 +25,79 @@ import StateContext from '../../context/context';
3125
import ComponentPanelItem from './ComponentPanelItem';
3226
import ComponentPanelRoutingItem from './ComponentPanelRoutingItem';
3327

28+
import TableStateProps from './TableStateProps';
29+
3430
const StatePropsPanel = ({ isThemeLight }): JSX.Element => {
3531
const classes = useStyles();
3632
const [state, dispatch] = useContext(StateContext);
37-
const [compName, setCompName] = useState('');
3833
const [newStateProp, setNewStateProp] = useState({});
39-
const [isRoot, setIsRoot] = useState(false);
4034

4135
const debug = () => {
42-
console.log('state:', state);
43-
console.log('state.canvasFocus:', state.canvasFocus);
4436
const currentId = state.canvasFocus.componentId;
37+
const currentComponent = state.components[currentId - 1];
38+
console.log('currentComponent:', currentComponent);
39+
console.log('currentComponent.stateProps:', currentComponent.stateProps);
4540
console.log(
46-
'state.canvasFocus.components[currentId-1]:',
47-
state.components[currentId - 1]
41+
'currentComponent.useStateCodes:',
42+
currentComponent.useStateCodes
4843
);
49-
console.log('key', document.getElementById('textfield-key').value);
50-
console.log('value', document.getElementById('textfield-value').value);
51-
console.log(document.getElementById('type-input').innerHTML);
52-
console.log('newStateProp:', newStateProp);
44+
};
45+
46+
const typeConversion = (value, type) => {
47+
// based on user input for value, convert value to correct type
48+
switch (type) {
49+
case 'String':
50+
return String(value);
51+
case 'Number':
52+
return Number(value);
53+
case 'Boolean':
54+
return Boolean(value);
55+
default:
56+
return value;
57+
}
5358
};
5459

5560
const submitNewState = () => {
5661
// currently focused component's id
5762
const currentId = state.canvasFocus.componentId;
58-
5963
// current component
6064
const currentComponent = state.components[currentId - 1];
6165
// grab user inputs for key, value, type
6266
const key = document.getElementById('textfield-key').value;
6367
const value = document.getElementById('textfield-value').value;
6468
const type = document.getElementById('type-input').innerHTML;
6569

66-
// FOR CONSIDERING USER INPUT DATA VISUALIZATION:
67-
// case 1: [{ name: 'John Doe'}, {age: 99}, {alive: true}]
68-
// case 2: [{ key: 'name', value: 'John Doe', type: 'string'}, {key: 'age', value: 99, type: 'number'}, {key: 'alive', value: true, type: 'boolean'}]
69-
70-
// store key, value, type in newStateProp obj
70+
// store key, value, type in newStateProp object
7171
newStateProp.key = key;
72-
newStateProp.value = value;
73-
newStateProp.type = type;
72+
newStateProp.type = type.charAt(0).toLowerCase() + type.slice(1);
73+
newStateProp.value = typeConversion(value, type);
7474

75+
// update newStateProp after storing key, value, type
7576
setNewStateProp(newStateProp);
7677
// store this newStateProp obj to our Component's stateProps array
7778
currentComponent.stateProps.push(newStateProp);
78-
// set newStateProp to empty for any new state prop entries
79+
// reset newStateProp to empty for future new state prop entries
7980
setNewStateProp({});
80-
console.log('currentComponent.stateProps:', currentComponent.stateProps);
81+
updateUseStateCodes();
82+
};
83+
84+
const updateUseStateCodes = () => {
85+
// currently focused component's id
86+
const currentId = state.canvasFocus.componentId;
87+
// current component
88+
const currentComponent = state.components[currentId - 1];
89+
90+
const localStateCode = [];
91+
// iterate thru current component's state props to generate code expression for each new state prop entry
92+
currentComponent.stateProps.forEach(el => {
93+
const useStateCode = `const [${el.key}, set${el.key
94+
.charAt(0)
95+
.toUpperCase() + el.key.slice(1)}] = useState<${
96+
el.type
97+
} | undefined>(${JSON.stringify(el.value)})`;
98+
localStateCode.push(useStateCode);
99+
});
100+
currentComponent.useStateCodes = localStateCode;
81101
};
82102

83103
return (
@@ -99,22 +119,22 @@ const StatePropsPanel = ({ isThemeLight }): JSX.Element => {
99119
<MenuItem value="">
100120
<em>Types</em>
101121
</MenuItem>
102-
<MenuItem id="type-selector" value={`string`}>
122+
<MenuItem id="type-selector" value={'string'}>
103123
String
104124
</MenuItem>
105-
<MenuItem id="type-selector" value={`number`}>
125+
<MenuItem id="type-selector" value={'number'}>
106126
Number
107127
</MenuItem>
108-
<MenuItem id="type-selector" value={`boolean`}>
128+
<MenuItem id="type-selector" value={'boolean'}>
109129
Boolean
110130
</MenuItem>
111-
<MenuItem id="type-selector" value={`array`}>
131+
<MenuItem id="type-selector" value={'array'}>
112132
Array
113133
</MenuItem>
114-
<MenuItem id="type-selector" value={`undefined`}>
134+
<MenuItem id="type-selector" value={'undefined'}>
115135
Undefined
116136
</MenuItem>
117-
<MenuItem id="type-selector" value={`any`}>
137+
<MenuItem id="type-selector" value={'any'}>
118138
Any
119139
</MenuItem>
120140
</Select>
@@ -141,13 +161,17 @@ const StatePropsPanel = ({ isThemeLight }): JSX.Element => {
141161
<label>
142162
Name: {state.components[state.canvasFocus.componentId - 1].name}
143163
</label>
164+
{/* CARET - HANGING TABLE STATE PROPS */}
165+
<div style={{ border: `${3}px solid green` }}>
166+
<TableStateProps />
167+
</div>
144168
</div>
145169
</div>
146170
);
147171
};
148172

149-
const useStyles = makeStyles((theme: Theme) => {
150-
return createStyles({
173+
const useStyles = makeStyles((theme: Theme) =>
174+
createStyles({
151175
inputField: {
152176
marginTop: '10px',
153177
borderRadius: '5px',
@@ -254,8 +278,8 @@ const useStyles = makeStyles((theme: Theme) => {
254278
selectEmpty: {
255279
marginTop: theme.spacing(2)
256280
}
257-
});
258-
});
281+
})
282+
);
259283

260284
const MyButton = styled(Button)({
261285
background: 'linear-gradient(45deg, #FE6B8B 30%, #FF8E53 90%)',
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// CARET
2+
import React, { useState, useContext } from 'react';
3+
import {
4+
DataGrid,
5+
GridColumns,
6+
GridEditRowsModel,
7+
} from '@material-ui/data-grid';
8+
9+
import StateContext from '../../context/context';
10+
11+
const columns: GridColumns = [
12+
{
13+
field: 'id',
14+
headerName: 'ID',
15+
width: 70,
16+
editable: false,
17+
},
18+
{
19+
field: 'key',
20+
headerName: 'Key',
21+
width: 100,
22+
editable: true,
23+
},
24+
{
25+
field: 'value',
26+
headerName: 'Value',
27+
width: 100,
28+
editable: true,
29+
},
30+
{
31+
field: 'type',
32+
headerName: 'Type',
33+
width: 100,
34+
editable: false,
35+
},
36+
];
37+
38+
// This function iterates thru current component's state props array to build rows to display in grid below
39+
const buildRows = () => {
40+
const [state] = useContext(StateContext);
41+
const currentId = state.canvasFocus.componentId;
42+
const currentComponent = state.components[currentId - 1];
43+
const rows = [];
44+
45+
for (let i = 0; i < currentComponent.stateProps.length; i += 1) {
46+
const newObj = {};
47+
newObj.id = i + 1; // id starts from 1
48+
newObj.key = currentComponent.stateProps[i].key;
49+
newObj.value = currentComponent.stateProps[i].value;
50+
newObj.type = currentComponent.stateProps[i].type;
51+
52+
rows.push(newObj);
53+
}
54+
return rows;
55+
};
56+
57+
export default function DataTable() {
58+
const [editRowsModel, setEditRowsModel] = useState < GridEditRowsModel > ({});
59+
60+
const rows = buildRows();
61+
62+
return (
63+
<div style={{ height: 400, width: '100%' }}>
64+
<DataGrid
65+
rows={rows}
66+
columns={columns}
67+
pageSize={5}
68+
editRowsModel={editRowsModel}
69+
/>
70+
</div>
71+
);
72+
}

app/src/context/initialState.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ const initialState: State = {
1616
past: [],
1717
future: [],
1818
// CARET
19-
stateProps: []
19+
stateProps: [],
20+
useStateCodes: [], // array of strings for each useState codes
2021
// END CARET
2122
}
2223
],

app/src/interfaces/Interfaces.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export interface Component {
3636
past: any[];
3737
future: any[];
3838
stateProps: StateProp[]; // state: [ { key: value, type }, {key: value, type}, {key: value, type} ]
39+
useStateCodes: string[];
3940
}
4041

4142
export interface StateProp {

app/src/reducers/componentReducer.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,8 @@ const reducer = (state: State, action: Action) => {
220220
isPage: action.payload.root,
221221
past: [],
222222
future: [],
223-
stateProps: []
223+
stateProps: [],
224+
useStateCodes: [],
224225
};
225226
components.push(newComponent);
226227

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
"@babel/cli": "^7.10.4",
105105
"@babel/register": "^7.10.4",
106106
"@material-ui/core": "^4.11.0",
107+
"@material-ui/data-grid": "^4.0.0-alpha.24",
107108
"@material-ui/icons": "^4.0.1",
108109
"@material-ui/styles": "^4.9.6",
109110
"@material-ui/system": "^4.11.2",

0 commit comments

Comments
 (0)