Skip to content

Commit 2ed65bb

Browse files
committed
added formatting fro the user input on the props tab-app stopped crashing
1 parent c731709 commit 2ed65bb

File tree

1 file changed

+69
-73
lines changed

1 file changed

+69
-73
lines changed

src/components/Props.tsx

Lines changed: 69 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,60 @@
1-
import React, { Component, Fragment } from "react";
2-
import { connect } from "react-redux";
3-
import { withStyles } from "@material-ui/core/styles";
4-
import FormControl from "@material-ui/core/FormControl";
5-
import Grid from "@material-ui/core/Grid";
6-
import TextField from "@material-ui/core/TextField";
7-
import Button from "@material-ui/core/Button";
8-
import Select from "@material-ui/core/Select";
9-
import Switch from "@material-ui/core/Switch";
10-
import InputLabel from "@material-ui/core/InputLabel";
11-
import { addProp, deleteProp } from "../actions/components.ts";
12-
import DataTable from "./DataTable.tsx";
13-
import { ComponentInt } from "../utils/Interfaces.ts";
1+
import React, { Component, Fragment } from 'react';
2+
import { connect } from 'react-redux';
3+
import { withStyles } from '@material-ui/core/styles';
4+
import FormControl from '@material-ui/core/FormControl';
5+
import Grid from '@material-ui/core/Grid';
6+
import TextField from '@material-ui/core/TextField';
7+
import Button from '@material-ui/core/Button';
8+
import Select from '@material-ui/core/Select';
9+
import Switch from '@material-ui/core/Switch';
10+
import InputLabel from '@material-ui/core/InputLabel';
11+
import { addProp, deleteProp } from '../actions/components.ts';
12+
import DataTable from './DataTable.tsx';
13+
import { ComponentInt } from '../utils/Interfaces.ts';
1414

1515
const styles = theme => ({
1616
root: {
17-
display: "flex",
18-
justifyContent: "center",
19-
flexWrap: "wrap"
17+
display: 'flex',
18+
justifyContent: 'center',
19+
flexWrap: 'wrap'
2020
},
2121
chip: {
22-
color: "#eee",
23-
backgroundColor: "#333333"
22+
color: '#eee',
23+
backgroundColor: '#333333'
2424
},
2525
column: {
26-
display: "inline-flex",
27-
alignItems: "baseline"
26+
display: 'inline-flex',
27+
alignItems: 'baseline'
2828
},
2929
icon: {
30-
fontSize: "20px",
31-
color: "#eee",
32-
opacity: "0.7",
33-
transition: "all .2s ease",
30+
fontSize: '20px',
31+
color: '#eee',
32+
opacity: '0.7',
33+
transition: 'all .2s ease',
3434

35-
"&:hover": {
36-
color: "red"
35+
'&:hover': {
36+
color: 'red'
3737
}
3838
},
3939
cssLabel: {
40-
color: "white",
40+
color: 'white',
4141

42-
"&$cssFocused": {
43-
color: "green"
42+
'&$cssFocused': {
43+
color: 'green'
4444
}
4545
},
4646
cssFocused: {},
4747
input: {
48-
color: "#eee",
49-
marginBottom: "10px",
50-
width: "60%"
48+
color: '#eee',
49+
marginBottom: '10px',
50+
width: '60%'
5151
},
5252
light: {
53-
color: "#eee"
53+
color: '#eee'
5454
},
5555
avatar: {
56-
color: "#eee",
57-
fontSize: "10px"
56+
color: '#eee',
57+
fontSize: '10px'
5858
}
5959
});
6060

@@ -86,39 +86,39 @@ const mapStateToProps = (store: any) => ({
8686
});
8787

8888
const availablePropTypes = {
89-
string: "STR",
90-
number: "NUM",
91-
object: "OBJ",
92-
array: "ARR",
93-
boolean: "BOOL",
94-
function: "FUNC",
89+
string: 'STR',
90+
number: 'NUM',
91+
object: 'OBJ',
92+
array: 'ARR',
93+
boolean: 'BOOL',
94+
function: 'FUNC',
9595
// symbol: 'SYM',
96-
node: "NODE",
97-
element: "ELEM",
98-
any: "ANY",
99-
tuple: "TUP",
100-
enum: "ENUM"
96+
node: 'NODE',
97+
element: 'ELEM',
98+
any: 'ANY',
99+
tuple: 'TUP',
100+
enum: 'ENUM'
101101
};
102102

103103
const typeOptions = [
104104
<option value="" key="" />,
105105
...Object.keys(availablePropTypes).map(type => (
106-
<option value={type} key={type} style={{ color: "#000" }}>
106+
<option value={type} key={type} style={{ color: '#000' }}>
107107
{type}
108108
</option>
109109
))
110110
];
111111

112112
class Props extends Component {
113113
state = {
114-
propKey: "",
115-
propValue: "",
114+
propKey: '',
115+
propValue: '',
116116
propRequired: true,
117-
propType: ""
117+
propType: ''
118118
};
119119

120120
handleChange = event => {
121-
if (event.target.id === "propKey") {
121+
if (event.target.id === 'propKey') {
122122
this.setState({
123123
[event.target.id]: event.target.value.trim()
124124
});
@@ -135,10 +135,15 @@ class Props extends Component {
135135
});
136136
};
137137

138+
// function that handles the addition of props to a given componnent
139+
// added regex to strip usr input from non alpha numeric properties
140+
// presence of these characters crashes the app and should not be a valid input anyways
138141
handleAddProp = event => {
139142
event.preventDefault();
140143

141-
const { propKey, propValue, propRequired, propType } = this.state;
144+
let { propKey, propValue, propRequired, propType } = this.state;
145+
propKey = propKey.replace(/\W+/gi, '');
146+
propValue = propKey.replace(/\W+/gi, '');
142147

143148
// check if prop exists with same key. CANNOT have duplicates
144149
const savedPropKeys = this.props.focusComponent.props.map(p => p.key);
@@ -149,7 +154,7 @@ class Props extends Component {
149154

150155
// check if prop starts with digits. Digits at string start breaks indexedDB
151156
if (/^\d/.test(propKey)) {
152-
window.alert("Props are not allowed to begin with digits");
157+
window.alert('Props are not allowed to begin with digits');
153158
return;
154159
}
155160

@@ -161,17 +166,17 @@ class Props extends Component {
161166
});
162167

163168
this.setState({
164-
propKey: "",
165-
propValue: "",
169+
propKey: '',
170+
propValue: '',
166171
propRequired: true,
167-
propType: ""
172+
propType: ''
168173
});
169174
};
170175

171176
render() {
172177
const { focusComponent, classes, deleteProp, addProp } = this.props;
173178

174-
const rowHeader = ["_Key", "Value", "Type", "Required"];
179+
const rowHeader = ['_Key', 'Value', 'Type', 'Required'];
175180
// prepare the saved Props in a nice way, so you can sent them to TableData
176181
const propsRows = focusComponent.props.map(prop => ({
177182
_Key: prop.key,
@@ -182,17 +187,17 @@ class Props extends Component {
182187
}));
183188

184189
return (
185-
<div className={"htmlattr"}>
186-
{" "}
190+
<div className={'htmlattr'}>
191+
{' '}
187192
{Object.keys(focusComponent).length < 1 ? (
188-
<div style={{ marginTop: "20px", width: "90%" }}>
193+
<div style={{ marginTop: '20px', width: '90%' }}>
189194
Click a component to view its props.
190195
</div>
191196
) : (
192197
<Fragment>
193198
<div
194199
className="props-container"
195-
style={{ marginTop: "20px", width: "90%", height: "80%" }}
200+
style={{ marginTop: '20px', width: '90%', height: '80%' }}
196201
>
197202
<Grid container spacing={8}>
198203
<Grid item xs={3}>
@@ -232,10 +237,7 @@ class Props extends Component {
232237
</Grid>
233238
<Grid item xs={6}>
234239
<FormControl required>
235-
<InputLabel
236-
className={classes.light}
237-
htmlFor="propType"
238-
>
240+
<InputLabel className={classes.light} htmlFor="propType">
239241
Type
240242
</InputLabel>
241243
<Select
@@ -253,10 +255,7 @@ class Props extends Component {
253255
</Grid>
254256
<Grid item xs={6}>
255257
<div className={classes.column}>
256-
<InputLabel
257-
className={classes.light}
258-
htmlFor="propRequired"
259-
>
258+
<InputLabel className={classes.light} htmlFor="propRequired">
260259
Required?
261260
</InputLabel>
262261
<Switch
@@ -300,7 +299,4 @@ class Props extends Component {
300299
}
301300
}
302301

303-
export default connect(
304-
mapStateToProps,
305-
mapDispatchToProps
306-
)(withStyles(styles)(Props));
302+
export default connect(mapStateToProps, mapDispatchToProps)(withStyles(styles)(Props));

0 commit comments

Comments
 (0)